sl@0: /* sl@0: * tclFileName.c -- sl@0: * sl@0: * This file contains routines for converting file names betwen sl@0: * native and network form. sl@0: * sl@0: * Copyright (c) 1995-1998 Sun Microsystems, Inc. sl@0: * Copyright (c) 1998-1999 by Scriptics Corporation. sl@0: * Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: tclFileName.c,v 1.40.2.15 2006/10/03 18:20:33 dgp Exp $ sl@0: */ sl@0: sl@0: #include "tclInt.h" sl@0: #include "tclPort.h" sl@0: #include "tclRegexp.h" sl@0: #if defined(__SYMBIAN32__) && defined(__WINSCW__) sl@0: #include "tclSymbianGlobals.h" sl@0: #define dataKey getdataKey(2) sl@0: #endif sl@0: sl@0: /* sl@0: * This define is used to activate Tcl's interpretation of Unix-style sl@0: * paths (containing forward slashes, '.' and '..') on MacOS. A sl@0: * side-effect of this is that some paths become ambiguous. sl@0: */ sl@0: #define MAC_UNDERSTANDS_UNIX_PATHS sl@0: sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: /* sl@0: * The following regular expression matches the root portion of a Macintosh sl@0: * absolute path. It will match degenerate Unix-style paths, tilde paths, sl@0: * Unix-style paths, and Mac paths. The various subexpressions in this sl@0: * can be summarised as follows: ^(/..|~user/unix|~user:mac|/unix|mac:dir). sl@0: * The subexpression indices which match the root portions, are as follows: sl@0: * sl@0: * degenerate unix-style: 2 sl@0: * unix-tilde: 5 sl@0: * mac-tilde: 7 sl@0: * unix-style: 9 (or 10 to cut off the irrelevant header). sl@0: * mac: 12 sl@0: * sl@0: */ sl@0: sl@0: #define MAC_ROOT_PATTERN "^((/+([.][.]?/+)*([.][.]?)?)|(~[^:/]*)(/[^:]*)?|(~[^:]*)(:.*)?|/+([.][.]?/+)*([^:/]+)(/[^:]*)?|([^:]+):.*)$" sl@0: sl@0: /* sl@0: * The following variables are used to hold precompiled regular expressions sl@0: * for use in filename matching. sl@0: */ sl@0: sl@0: typedef struct ThreadSpecificData { sl@0: int initialized; sl@0: Tcl_Obj *macRootPatternPtr; sl@0: } ThreadSpecificData; sl@0: sl@0: static void FileNameCleanup _ANSI_ARGS_((ClientData clientData)); sl@0: static void FileNameInit _ANSI_ARGS_((void)); sl@0: sl@0: #endif sl@0: sl@0: #if !defined(__SYMBIAN32__) || !defined(__WINSCW__) sl@0: static Tcl_ThreadDataKey dataKey; sl@0: sl@0: /* sl@0: * The following variable is set in the TclPlatformInit call to one sl@0: * of: TCL_PLATFORM_UNIX, TCL_PLATFORM_MAC, or TCL_PLATFORM_WINDOWS. sl@0: */ sl@0: sl@0: TclPlatformType tclPlatform = TCL_PLATFORM_UNIX; sl@0: #endif sl@0: /* sl@0: * Prototypes for local procedures defined in this file: sl@0: */ sl@0: sl@0: static CONST char * DoTildeSubst _ANSI_ARGS_((Tcl_Interp *interp, sl@0: CONST char *user, Tcl_DString *resultPtr)); sl@0: static CONST char * ExtractWinRoot _ANSI_ARGS_((CONST char *path, sl@0: Tcl_DString *resultPtr, int offset, sl@0: Tcl_PathType *typePtr)); sl@0: static int SkipToChar _ANSI_ARGS_((char **stringPtr, sl@0: char *match)); sl@0: static Tcl_Obj* SplitMacPath _ANSI_ARGS_((CONST char *path)); sl@0: static Tcl_Obj* SplitWinPath _ANSI_ARGS_((CONST char *path)); sl@0: static Tcl_Obj* SplitUnixPath _ANSI_ARGS_((CONST char *path)); sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * FileNameInit -- sl@0: * sl@0: * This procedure initializes the patterns used by this module. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * Compiles the regular expressions. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static void sl@0: FileNameInit() sl@0: { sl@0: ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); sl@0: if (!tsdPtr->initialized) { sl@0: tsdPtr->initialized = 1; sl@0: tsdPtr->macRootPatternPtr = Tcl_NewStringObj(MAC_ROOT_PATTERN, -1); sl@0: Tcl_CreateThreadExitHandler(FileNameCleanup, NULL); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * FileNameCleanup -- sl@0: * sl@0: * This procedure is a Tcl_ExitProc used to clean up the static sl@0: * data structures used in this file. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * Deallocates storage used by the procedures in this file. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static void sl@0: FileNameCleanup(clientData) sl@0: ClientData clientData; /* Not used. */ sl@0: { sl@0: ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); sl@0: Tcl_DecrRefCount(tsdPtr->macRootPatternPtr); sl@0: tsdPtr->initialized = 0; sl@0: } sl@0: #endif sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * ExtractWinRoot -- sl@0: * sl@0: * Matches the root portion of a Windows path and appends it sl@0: * to the specified Tcl_DString. sl@0: * sl@0: * Results: sl@0: * Returns the position in the path immediately after the root sl@0: * including any trailing slashes. sl@0: * Appends a cleaned up version of the root to the Tcl_DString sl@0: * at the specified offest. sl@0: * sl@0: * Side effects: sl@0: * Modifies the specified Tcl_DString. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static CONST char * sl@0: ExtractWinRoot(path, resultPtr, offset, typePtr) sl@0: CONST char *path; /* Path to parse. */ sl@0: Tcl_DString *resultPtr; /* Buffer to hold result. */ sl@0: int offset; /* Offset in buffer where result should be sl@0: * stored. */ sl@0: Tcl_PathType *typePtr; /* Where to store pathType result */ sl@0: { sl@0: if (path[0] == '/' || path[0] == '\\') { sl@0: /* Might be a UNC or Vol-Relative path */ sl@0: CONST char *host, *share, *tail; sl@0: int hlen, slen; sl@0: if (path[1] != '/' && path[1] != '\\') { sl@0: Tcl_DStringSetLength(resultPtr, offset); sl@0: *typePtr = TCL_PATH_VOLUME_RELATIVE; sl@0: Tcl_DStringAppend(resultPtr, "/", 1); sl@0: return &path[1]; sl@0: } sl@0: host = &path[2]; sl@0: sl@0: /* Skip separators */ sl@0: while (host[0] == '/' || host[0] == '\\') host++; sl@0: sl@0: for (hlen = 0; host[hlen];hlen++) { sl@0: if (host[hlen] == '/' || host[hlen] == '\\') sl@0: break; sl@0: } sl@0: if (host[hlen] == 0 || host[hlen+1] == 0) { sl@0: /* sl@0: * The path given is simply of the form sl@0: * '/foo', '//foo', '/////foo' or the same sl@0: * with backslashes. If there is exactly sl@0: * one leading '/' the path is volume relative sl@0: * (see filename man page). If there are more sl@0: * than one, we are simply assuming they sl@0: * are superfluous and we trim them away. sl@0: * (An alternative interpretation would sl@0: * be that it is a host name, but we have sl@0: * been documented that that is not the case). sl@0: */ sl@0: *typePtr = TCL_PATH_VOLUME_RELATIVE; sl@0: Tcl_DStringAppend(resultPtr, "/", 1); sl@0: return &path[2]; sl@0: } sl@0: Tcl_DStringSetLength(resultPtr, offset); sl@0: share = &host[hlen]; sl@0: sl@0: /* Skip separators */ sl@0: while (share[0] == '/' || share[0] == '\\') share++; sl@0: sl@0: for (slen = 0; share[slen];slen++) { sl@0: if (share[slen] == '/' || share[slen] == '\\') sl@0: break; sl@0: } sl@0: Tcl_DStringAppend(resultPtr, "//", 2); sl@0: Tcl_DStringAppend(resultPtr, host, hlen); sl@0: Tcl_DStringAppend(resultPtr, "/", 1); sl@0: Tcl_DStringAppend(resultPtr, share, slen); sl@0: sl@0: tail = &share[slen]; sl@0: sl@0: /* Skip separators */ sl@0: while (tail[0] == '/' || tail[0] == '\\') tail++; sl@0: sl@0: *typePtr = TCL_PATH_ABSOLUTE; sl@0: return tail; sl@0: } else if (*path && path[1] == ':') { sl@0: /* Might be a drive sep */ sl@0: Tcl_DStringSetLength(resultPtr, offset); sl@0: sl@0: if (path[2] != '/' && path[2] != '\\') { sl@0: *typePtr = TCL_PATH_VOLUME_RELATIVE; sl@0: Tcl_DStringAppend(resultPtr, path, 2); sl@0: return &path[2]; sl@0: } else { sl@0: char *tail = (char*)&path[3]; sl@0: sl@0: /* Skip separators */ sl@0: while (*tail && (tail[0] == '/' || tail[0] == '\\')) tail++; sl@0: sl@0: *typePtr = TCL_PATH_ABSOLUTE; sl@0: Tcl_DStringAppend(resultPtr, path, 2); sl@0: Tcl_DStringAppend(resultPtr, "/", 1); sl@0: sl@0: return tail; sl@0: } sl@0: } else { sl@0: int abs = 0; sl@0: if ((path[0] == 'c' || path[0] == 'C') sl@0: && (path[1] == 'o' || path[1] == 'O')) { sl@0: if ((path[2] == 'm' || path[2] == 'M') sl@0: && path[3] >= '1' && path[3] <= '4') { sl@0: /* May have match for 'com[1-4]:?', which is a serial port */ sl@0: if (path[4] == '\0') { sl@0: abs = 4; sl@0: } else if (path [4] == ':' && path[5] == '\0') { sl@0: abs = 5; sl@0: } sl@0: } else if ((path[2] == 'n' || path[2] == 'N') && path[3] == '\0') { sl@0: /* Have match for 'con' */ sl@0: abs = 3; sl@0: } sl@0: } else if ((path[0] == 'l' || path[0] == 'L') sl@0: && (path[1] == 'p' || path[1] == 'P') sl@0: && (path[2] == 't' || path[2] == 'T')) { sl@0: if (path[3] >= '1' && path[3] <= '3') { sl@0: /* May have match for 'lpt[1-3]:?' */ sl@0: if (path[4] == '\0') { sl@0: abs = 4; sl@0: } else if (path [4] == ':' && path[5] == '\0') { sl@0: abs = 5; sl@0: } sl@0: } sl@0: } else if ((path[0] == 'p' || path[0] == 'P') sl@0: && (path[1] == 'r' || path[1] == 'R') sl@0: && (path[2] == 'n' || path[2] == 'N') sl@0: && path[3] == '\0') { sl@0: /* Have match for 'prn' */ sl@0: abs = 3; sl@0: } else if ((path[0] == 'n' || path[0] == 'N') sl@0: && (path[1] == 'u' || path[1] == 'U') sl@0: && (path[2] == 'l' || path[2] == 'L') sl@0: && path[3] == '\0') { sl@0: /* Have match for 'nul' */ sl@0: abs = 3; sl@0: } else if ((path[0] == 'a' || path[0] == 'A') sl@0: && (path[1] == 'u' || path[1] == 'U') sl@0: && (path[2] == 'x' || path[2] == 'X') sl@0: && path[3] == '\0') { sl@0: /* Have match for 'aux' */ sl@0: abs = 3; sl@0: } sl@0: if (abs != 0) { sl@0: *typePtr = TCL_PATH_ABSOLUTE; sl@0: Tcl_DStringSetLength(resultPtr, offset); sl@0: Tcl_DStringAppend(resultPtr, path, abs); sl@0: return path + abs; sl@0: } sl@0: } sl@0: /* Anything else is treated as relative */ sl@0: *typePtr = TCL_PATH_RELATIVE; sl@0: return path; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_GetPathType -- sl@0: * sl@0: * Determines whether a given path is relative to the current sl@0: * directory, relative to the current volume, or absolute. sl@0: * sl@0: * The objectified Tcl_FSGetPathType should be used in sl@0: * preference to this function (as you can see below, this sl@0: * is just a wrapper around that other function). sl@0: * sl@0: * Results: sl@0: * Returns one of TCL_PATH_ABSOLUTE, TCL_PATH_RELATIVE, or sl@0: * TCL_PATH_VOLUME_RELATIVE. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C Tcl_PathType sl@0: Tcl_GetPathType(path) sl@0: CONST char *path; sl@0: { sl@0: Tcl_PathType type; sl@0: Tcl_Obj *tempObj = Tcl_NewStringObj(path,-1); sl@0: Tcl_IncrRefCount(tempObj); sl@0: type = Tcl_FSGetPathType(tempObj); sl@0: Tcl_DecrRefCount(tempObj); sl@0: return type; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpGetNativePathType -- sl@0: * sl@0: * Determines whether a given path is relative to the current sl@0: * directory, relative to the current volume, or absolute, but sl@0: * ONLY FOR THE NATIVE FILESYSTEM. This function is called from sl@0: * tclIOUtil.c (but needs to be here due to its dependence on sl@0: * static variables/functions in this file). The exported sl@0: * function Tcl_FSGetPathType should be used by extensions. sl@0: * sl@0: * Results: sl@0: * Returns one of TCL_PATH_ABSOLUTE, TCL_PATH_RELATIVE, or sl@0: * TCL_PATH_VOLUME_RELATIVE. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: Tcl_PathType sl@0: TclpGetNativePathType(pathObjPtr, driveNameLengthPtr, driveNameRef) sl@0: Tcl_Obj *pathObjPtr; sl@0: int *driveNameLengthPtr; sl@0: Tcl_Obj **driveNameRef; sl@0: { sl@0: Tcl_PathType type = TCL_PATH_ABSOLUTE; sl@0: int pathLen; sl@0: char *path = Tcl_GetStringFromObj(pathObjPtr, &pathLen); sl@0: sl@0: if (path[0] == '~') { sl@0: /* sl@0: * This case is common to all platforms. sl@0: * Paths that begin with ~ are absolute. sl@0: */ sl@0: if (driveNameLengthPtr != NULL) { sl@0: char *end = path + 1; sl@0: while ((*end != '\0') && (*end != '/')) { sl@0: end++; sl@0: } sl@0: *driveNameLengthPtr = end - path; sl@0: } sl@0: } else { sl@0: switch (tclPlatform) { sl@0: case TCL_PLATFORM_UNIX: { sl@0: char *origPath = path; sl@0: sl@0: /* sl@0: * Paths that begin with / are absolute. sl@0: */ sl@0: sl@0: #ifdef __QNX__ sl@0: /* sl@0: * Check for QNX // prefix sl@0: */ sl@0: if (*path && (pathLen > 3) && (path[0] == '/') sl@0: && (path[1] == '/') && isdigit(UCHAR(path[2]))) { sl@0: path += 3; sl@0: while (isdigit(UCHAR(*path))) { sl@0: ++path; sl@0: } sl@0: } sl@0: #endif sl@0: if (path[0] == '/') { sl@0: if (driveNameLengthPtr != NULL) { sl@0: /* sl@0: * We need this addition in case the QNX code sl@0: * was used sl@0: */ sl@0: *driveNameLengthPtr = (1 + path - origPath); sl@0: } sl@0: } else { sl@0: type = TCL_PATH_RELATIVE; sl@0: } sl@0: break; sl@0: } sl@0: case TCL_PLATFORM_MAC: sl@0: if (path[0] == ':') { sl@0: type = TCL_PATH_RELATIVE; sl@0: } else { sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: ThreadSpecificData *tsdPtr; sl@0: Tcl_RegExp re; sl@0: sl@0: tsdPtr = TCL_TSD_INIT(&dataKey); sl@0: sl@0: /* sl@0: * Since we have eliminated the easy cases, use the sl@0: * root pattern to look for the other types. sl@0: */ sl@0: sl@0: FileNameInit(); sl@0: re = Tcl_GetRegExpFromObj(NULL, tsdPtr->macRootPatternPtr, sl@0: REG_ADVANCED); sl@0: sl@0: if (!Tcl_RegExpExec(NULL, re, path, path)) { sl@0: type = TCL_PATH_RELATIVE; sl@0: } else { sl@0: CONST char *root, *end; sl@0: Tcl_RegExpRange(re, 2, &root, &end); sl@0: if (root != NULL) { sl@0: type = TCL_PATH_RELATIVE; sl@0: } else { sl@0: if (driveNameLengthPtr != NULL) { sl@0: Tcl_RegExpRange(re, 0, &root, &end); sl@0: *driveNameLengthPtr = end - root; sl@0: } sl@0: if (driveNameRef != NULL) { sl@0: if (*root == '/') { sl@0: char *c; sl@0: int gotColon = 0; sl@0: *driveNameRef = Tcl_NewStringObj(root + 1, sl@0: end - root -1); sl@0: c = Tcl_GetString(*driveNameRef); sl@0: while (*c != '\0') { sl@0: if (*c == '/') { sl@0: gotColon++; sl@0: *c = ':'; sl@0: } sl@0: c++; sl@0: } sl@0: /* sl@0: * If there is no colon, we have just a sl@0: * volume name so we must add a colon so sl@0: * it is an absolute path. sl@0: */ sl@0: if (gotColon == 0) { sl@0: Tcl_AppendToObj(*driveNameRef, ":", 1); sl@0: } else if ((gotColon > 1) && sl@0: (*(c-1) == ':')) { sl@0: /* We have an extra colon */ sl@0: Tcl_SetObjLength(*driveNameRef, sl@0: c - Tcl_GetString(*driveNameRef) - 1); sl@0: } sl@0: } sl@0: } sl@0: } sl@0: } sl@0: #else sl@0: if (path[0] == '~') { sl@0: } else if (path[0] == ':') { sl@0: type = TCL_PATH_RELATIVE; sl@0: } else { sl@0: char *colonPos = strchr(path,':'); sl@0: if (colonPos == NULL) { sl@0: type = TCL_PATH_RELATIVE; sl@0: } else { sl@0: } sl@0: } sl@0: if (type == TCL_PATH_ABSOLUTE) { sl@0: if (driveNameLengthPtr != NULL) { sl@0: *driveNameLengthPtr = strlen(path); sl@0: } sl@0: } sl@0: #endif sl@0: } sl@0: break; sl@0: sl@0: case TCL_PLATFORM_WINDOWS: { sl@0: Tcl_DString ds; sl@0: CONST char *rootEnd; sl@0: sl@0: Tcl_DStringInit(&ds); sl@0: rootEnd = ExtractWinRoot(path, &ds, 0, &type); sl@0: if ((rootEnd != path) && (driveNameLengthPtr != NULL)) { sl@0: *driveNameLengthPtr = rootEnd - path; sl@0: if (driveNameRef != NULL) { sl@0: *driveNameRef = Tcl_NewStringObj(Tcl_DStringValue(&ds), sl@0: Tcl_DStringLength(&ds)); sl@0: Tcl_IncrRefCount(*driveNameRef); sl@0: } sl@0: } sl@0: Tcl_DStringFree(&ds); sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: return type; sl@0: } sl@0: sl@0: /* sl@0: *--------------------------------------------------------------------------- sl@0: * sl@0: * TclpNativeSplitPath -- sl@0: * sl@0: * This function takes the given Tcl_Obj, which should be a valid sl@0: * path, and returns a Tcl List object containing each segment sl@0: * of that path as an element. sl@0: * sl@0: * Note this function currently calls the older Split(Plat)Path sl@0: * functions, which require more memory allocation than is sl@0: * desirable. sl@0: * sl@0: * Results: sl@0: * Returns list object with refCount of zero. If the passed in sl@0: * lenPtr is non-NULL, we use it to return the number of elements sl@0: * in the returned list. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *--------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: Tcl_Obj* sl@0: TclpNativeSplitPath(pathPtr, lenPtr) sl@0: Tcl_Obj *pathPtr; /* Path to split. */ sl@0: int *lenPtr; /* int to store number of path elements. */ sl@0: { sl@0: Tcl_Obj *resultPtr = NULL; /* Needed only to prevent gcc warnings. */ sl@0: sl@0: /* sl@0: * Perform platform specific splitting. sl@0: */ sl@0: sl@0: switch (tclPlatform) { sl@0: case TCL_PLATFORM_UNIX: sl@0: resultPtr = SplitUnixPath(Tcl_GetString(pathPtr)); sl@0: break; sl@0: sl@0: case TCL_PLATFORM_WINDOWS: sl@0: resultPtr = SplitWinPath(Tcl_GetString(pathPtr)); sl@0: break; sl@0: sl@0: case TCL_PLATFORM_MAC: sl@0: resultPtr = SplitMacPath(Tcl_GetString(pathPtr)); sl@0: break; sl@0: } sl@0: sl@0: /* sl@0: * Compute the number of elements in the result. sl@0: */ sl@0: sl@0: if (lenPtr != NULL) { sl@0: Tcl_ListObjLength(NULL, resultPtr, lenPtr); sl@0: } sl@0: return resultPtr; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_SplitPath -- sl@0: * sl@0: * Split a path into a list of path components. The first element sl@0: * of the list will have the same path type as the original path. sl@0: * sl@0: * Results: sl@0: * Returns a standard Tcl result. The interpreter result contains sl@0: * a list of path components. sl@0: * *argvPtr will be filled in with the address of an array sl@0: * whose elements point to the elements of path, in order. sl@0: * *argcPtr will get filled in with the number of valid elements sl@0: * in the array. A single block of memory is dynamically allocated sl@0: * to hold both the argv array and a copy of the path elements. sl@0: * The caller must eventually free this memory by calling ckfree() sl@0: * on *argvPtr. Note: *argvPtr and *argcPtr are only modified sl@0: * if the procedure returns normally. sl@0: * sl@0: * Side effects: sl@0: * Allocates memory. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C void sl@0: Tcl_SplitPath(path, argcPtr, argvPtr) sl@0: CONST char *path; /* Pointer to string containing a path. */ sl@0: int *argcPtr; /* Pointer to location to fill in with sl@0: * the number of elements in the path. */ sl@0: CONST char ***argvPtr; /* Pointer to place to store pointer to array sl@0: * of pointers to path elements. */ sl@0: { sl@0: Tcl_Obj *resultPtr = NULL; /* Needed only to prevent gcc warnings. */ sl@0: Tcl_Obj *tmpPtr, *eltPtr; sl@0: int i, size, len; sl@0: char *p, *str; sl@0: sl@0: /* sl@0: * Perform the splitting, using objectified, vfs-aware code. sl@0: */ sl@0: sl@0: tmpPtr = Tcl_NewStringObj(path, -1); sl@0: Tcl_IncrRefCount(tmpPtr); sl@0: resultPtr = Tcl_FSSplitPath(tmpPtr, argcPtr); sl@0: Tcl_DecrRefCount(tmpPtr); sl@0: sl@0: /* Calculate space required for the result */ sl@0: sl@0: size = 1; sl@0: for (i = 0; i < *argcPtr; i++) { sl@0: Tcl_ListObjIndex(NULL, resultPtr, i, &eltPtr); sl@0: Tcl_GetStringFromObj(eltPtr, &len); sl@0: size += len + 1; sl@0: } sl@0: sl@0: /* sl@0: * Allocate a buffer large enough to hold the contents of all of sl@0: * the list plus the argv pointers and the terminating NULL pointer. sl@0: */ sl@0: sl@0: *argvPtr = (CONST char **) ckalloc((unsigned) sl@0: ((((*argcPtr) + 1) * sizeof(char *)) + size)); sl@0: sl@0: /* sl@0: * Position p after the last argv pointer and copy the contents of sl@0: * the list in, piece by piece. sl@0: */ sl@0: sl@0: p = (char *) &(*argvPtr)[(*argcPtr) + 1]; sl@0: for (i = 0; i < *argcPtr; i++) { sl@0: Tcl_ListObjIndex(NULL, resultPtr, i, &eltPtr); sl@0: str = Tcl_GetStringFromObj(eltPtr, &len); sl@0: memcpy((VOID *) p, (VOID *) str, (size_t) len+1); sl@0: p += len+1; sl@0: } sl@0: sl@0: /* sl@0: * Now set up the argv pointers. sl@0: */ sl@0: sl@0: p = (char *) &(*argvPtr)[(*argcPtr) + 1]; sl@0: sl@0: for (i = 0; i < *argcPtr; i++) { sl@0: (*argvPtr)[i] = p; sl@0: while ((*p++) != '\0') {} sl@0: } sl@0: (*argvPtr)[i] = NULL; sl@0: sl@0: /* sl@0: * Free the result ptr given to us by Tcl_FSSplitPath sl@0: */ sl@0: sl@0: Tcl_DecrRefCount(resultPtr); sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * SplitUnixPath -- sl@0: * sl@0: * This routine is used by Tcl_(FS)SplitPath to handle splitting sl@0: * Unix paths. sl@0: * sl@0: * Results: sl@0: * Returns a newly allocated Tcl list object. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static Tcl_Obj* sl@0: SplitUnixPath(path) sl@0: CONST char *path; /* Pointer to string containing a path. */ sl@0: { sl@0: int length; sl@0: CONST char *p, *elementStart; sl@0: Tcl_Obj *result = Tcl_NewObj(); sl@0: sl@0: /* sl@0: * Deal with the root directory as a special case. sl@0: */ sl@0: sl@0: #ifdef __QNX__ sl@0: /* sl@0: * Check for QNX // prefix sl@0: */ sl@0: if ((path[0] == '/') && (path[1] == '/') sl@0: && isdigit(UCHAR(path[2]))) { /* INTL: digit */ sl@0: path += 3; sl@0: while (isdigit(UCHAR(*path))) { /* INTL: digit */ sl@0: ++path; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: if (path[0] == '/') { sl@0: Tcl_ListObjAppendElement(NULL, result, Tcl_NewStringObj("/",1)); sl@0: p = path+1; sl@0: } else { sl@0: p = path; sl@0: } sl@0: sl@0: /* sl@0: * Split on slashes. Embedded elements that start with tilde will be sl@0: * prefixed with "./" so they are not affected by tilde substitution. sl@0: */ sl@0: sl@0: for (;;) { sl@0: elementStart = p; sl@0: while ((*p != '\0') && (*p != '/')) { sl@0: p++; sl@0: } sl@0: length = p - elementStart; sl@0: if (length > 0) { sl@0: Tcl_Obj *nextElt; sl@0: if ((elementStart[0] == '~') && (elementStart != path)) { sl@0: nextElt = Tcl_NewStringObj("./",2); sl@0: Tcl_AppendToObj(nextElt, elementStart, length); sl@0: } else { sl@0: nextElt = Tcl_NewStringObj(elementStart, length); sl@0: } sl@0: Tcl_ListObjAppendElement(NULL, result, nextElt); sl@0: } sl@0: if (*p++ == '\0') { sl@0: break; sl@0: } sl@0: } sl@0: return result; sl@0: } sl@0: sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * SplitWinPath -- sl@0: * sl@0: * This routine is used by Tcl_(FS)SplitPath to handle splitting sl@0: * Windows paths. sl@0: * sl@0: * Results: sl@0: * Returns a newly allocated Tcl list object. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static Tcl_Obj* sl@0: SplitWinPath(path) sl@0: CONST char *path; /* Pointer to string containing a path. */ sl@0: { sl@0: int length; sl@0: CONST char *p, *elementStart; sl@0: Tcl_PathType type = TCL_PATH_ABSOLUTE; sl@0: Tcl_DString buf; sl@0: Tcl_Obj *result = Tcl_NewObj(); sl@0: Tcl_DStringInit(&buf); sl@0: sl@0: p = ExtractWinRoot(path, &buf, 0, &type); sl@0: sl@0: /* sl@0: * Terminate the root portion, if we matched something. sl@0: */ sl@0: sl@0: if (p != path) { sl@0: Tcl_ListObjAppendElement(NULL, result, sl@0: Tcl_NewStringObj(Tcl_DStringValue(&buf), sl@0: Tcl_DStringLength(&buf))); sl@0: } sl@0: Tcl_DStringFree(&buf); sl@0: sl@0: /* sl@0: * Split on slashes. Embedded elements that start with tilde sl@0: * or a drive letter will be prefixed with "./" so they are not sl@0: * affected by tilde substitution. sl@0: */ sl@0: sl@0: do { sl@0: elementStart = p; sl@0: while ((*p != '\0') && (*p != '/') && (*p != '\\')) { sl@0: p++; sl@0: } sl@0: length = p - elementStart; sl@0: if (length > 0) { sl@0: Tcl_Obj *nextElt; sl@0: if ((elementStart != path) sl@0: && ((elementStart[0] == '~') sl@0: || (isalpha(UCHAR(elementStart[0])) sl@0: && elementStart[1] == ':'))) { sl@0: nextElt = Tcl_NewStringObj("./",2); sl@0: Tcl_AppendToObj(nextElt, elementStart, length); sl@0: } else { sl@0: nextElt = Tcl_NewStringObj(elementStart, length); sl@0: } sl@0: Tcl_ListObjAppendElement(NULL, result, nextElt); sl@0: } sl@0: } while (*p++ != '\0'); sl@0: sl@0: return result; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * SplitMacPath -- sl@0: * sl@0: * This routine is used by Tcl_(FS)SplitPath to handle splitting sl@0: * Macintosh paths. sl@0: * sl@0: * Results: sl@0: * Returns a newly allocated Tcl list object. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static Tcl_Obj* sl@0: SplitMacPath(path) sl@0: CONST char *path; /* Pointer to string containing a path. */ sl@0: { sl@0: int isMac = 0; /* 1 if is Mac-style, 0 if Unix-style path. */ sl@0: int length; sl@0: CONST char *p, *elementStart; sl@0: Tcl_Obj *result; sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: Tcl_RegExp re; sl@0: int i; sl@0: ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); sl@0: #endif sl@0: sl@0: result = Tcl_NewObj(); sl@0: sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: /* sl@0: * Initialize the path name parser for Macintosh path names. sl@0: */ sl@0: sl@0: FileNameInit(); sl@0: sl@0: /* sl@0: * Match the root portion of a Mac path name. sl@0: */ sl@0: sl@0: i = 0; /* Needed only to prevent gcc warnings. */ sl@0: sl@0: re = Tcl_GetRegExpFromObj(NULL, tsdPtr->macRootPatternPtr, REG_ADVANCED); sl@0: sl@0: if (Tcl_RegExpExec(NULL, re, path, path) == 1) { sl@0: CONST char *start, *end; sl@0: Tcl_Obj *nextElt; sl@0: sl@0: /* sl@0: * Treat degenerate absolute paths like / and /../.. as sl@0: * Mac relative file names for lack of anything else to do. sl@0: */ sl@0: sl@0: Tcl_RegExpRange(re, 2, &start, &end); sl@0: if (start) { sl@0: Tcl_Obj *elt = Tcl_NewStringObj(":", 1); sl@0: Tcl_RegExpRange(re, 0, &start, &end); sl@0: Tcl_AppendToObj(elt, path, end - start); sl@0: Tcl_ListObjAppendElement(NULL, result, elt); sl@0: return result; sl@0: } sl@0: sl@0: Tcl_RegExpRange(re, 5, &start, &end); sl@0: if (start) { sl@0: /* sl@0: * Unix-style tilde prefixed paths. sl@0: */ sl@0: sl@0: isMac = 0; sl@0: i = 5; sl@0: } else { sl@0: Tcl_RegExpRange(re, 7, &start, &end); sl@0: if (start) { sl@0: /* sl@0: * Mac-style tilde prefixed paths. sl@0: */ sl@0: sl@0: isMac = 1; sl@0: i = 7; sl@0: } else { sl@0: Tcl_RegExpRange(re, 10, &start, &end); sl@0: if (start) { sl@0: /* sl@0: * Normal Unix style paths. sl@0: */ sl@0: sl@0: isMac = 0; sl@0: i = 10; sl@0: } else { sl@0: Tcl_RegExpRange(re, 12, &start, &end); sl@0: if (start) { sl@0: /* sl@0: * Normal Mac style paths. sl@0: */ sl@0: sl@0: isMac = 1; sl@0: i = 12; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: Tcl_RegExpRange(re, i, &start, &end); sl@0: length = end - start; sl@0: sl@0: /* sl@0: * Append the element and terminate it with a : sl@0: */ sl@0: sl@0: nextElt = Tcl_NewStringObj(start, length); sl@0: Tcl_AppendToObj(nextElt, ":", 1); sl@0: Tcl_ListObjAppendElement(NULL, result, nextElt); sl@0: p = end; sl@0: } else { sl@0: isMac = (strchr(path, ':') != NULL); sl@0: p = path; sl@0: } sl@0: #else sl@0: if ((path[0] != ':') && (path[0] == '~' || (strchr(path,':') != NULL))) { sl@0: CONST char *end; sl@0: Tcl_Obj *nextElt; sl@0: sl@0: isMac = 1; sl@0: sl@0: end = strchr(path,':'); sl@0: if (end == NULL) { sl@0: length = strlen(path); sl@0: } else { sl@0: length = end - path; sl@0: } sl@0: sl@0: /* sl@0: * Append the element and terminate it with a : sl@0: */ sl@0: sl@0: nextElt = Tcl_NewStringObj(path, length); sl@0: Tcl_AppendToObj(nextElt, ":", 1); sl@0: Tcl_ListObjAppendElement(NULL, result, nextElt); sl@0: p = path + length; sl@0: } else { sl@0: isMac = (strchr(path, ':') != NULL); sl@0: isMac = 1; sl@0: p = path; sl@0: } sl@0: #endif sl@0: sl@0: if (isMac) { sl@0: sl@0: /* sl@0: * p is pointing at the first colon in the path. There sl@0: * will always be one, since this is a Mac-style path. sl@0: * (This is no longer true if MAC_UNDERSTANDS_UNIX_PATHS sl@0: * is false, so we must check whether 'p' points to the sl@0: * end of the string.) sl@0: */ sl@0: elementStart = p; sl@0: if (*p == ':') { sl@0: p++; sl@0: } sl@0: sl@0: while ((p = strchr(p, ':')) != NULL) { sl@0: length = p - elementStart; sl@0: if (length == 1) { sl@0: while (*p == ':') { sl@0: Tcl_ListObjAppendElement(NULL, result, sl@0: Tcl_NewStringObj("::", 2)); sl@0: elementStart = p++; sl@0: } sl@0: } else { sl@0: /* sl@0: * If this is a simple component, drop the leading colon. sl@0: */ sl@0: sl@0: if ((elementStart[1] != '~') sl@0: && (strchr(elementStart+1, '/') == NULL)) { sl@0: elementStart++; sl@0: length--; sl@0: } sl@0: Tcl_ListObjAppendElement(NULL, result, sl@0: Tcl_NewStringObj(elementStart, length)); sl@0: elementStart = p++; sl@0: } sl@0: } sl@0: if (elementStart[0] != ':') { sl@0: if (elementStart[0] != '\0') { sl@0: Tcl_ListObjAppendElement(NULL, result, sl@0: Tcl_NewStringObj(elementStart, -1)); sl@0: } sl@0: } else { sl@0: if (elementStart[1] != '\0' || elementStart == path) { sl@0: if ((elementStart[1] != '~') && (elementStart[1] != '\0') sl@0: && (strchr(elementStart+1, '/') == NULL)) { sl@0: elementStart++; sl@0: } sl@0: Tcl_ListObjAppendElement(NULL, result, sl@0: Tcl_NewStringObj(elementStart, -1)); sl@0: } sl@0: } sl@0: } else { sl@0: sl@0: /* sl@0: * Split on slashes, suppress extra /'s, and convert .. to ::. sl@0: */ sl@0: sl@0: for (;;) { sl@0: elementStart = p; sl@0: while ((*p != '\0') && (*p != '/')) { sl@0: p++; sl@0: } sl@0: length = p - elementStart; sl@0: if (length > 0) { sl@0: if ((length == 1) && (elementStart[0] == '.')) { sl@0: Tcl_ListObjAppendElement(NULL, result, sl@0: Tcl_NewStringObj(":", 1)); sl@0: } else if ((length == 2) && (elementStart[0] == '.') sl@0: && (elementStart[1] == '.')) { sl@0: Tcl_ListObjAppendElement(NULL, result, sl@0: Tcl_NewStringObj("::", 2)); sl@0: } else { sl@0: Tcl_Obj *nextElt; sl@0: if (*elementStart == '~') { sl@0: nextElt = Tcl_NewStringObj(":",1); sl@0: Tcl_AppendToObj(nextElt, elementStart, length); sl@0: } else { sl@0: nextElt = Tcl_NewStringObj(elementStart, length); sl@0: } sl@0: Tcl_ListObjAppendElement(NULL, result, nextElt); sl@0: } sl@0: } sl@0: if (*p++ == '\0') { sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: return result; sl@0: } sl@0: sl@0: /* sl@0: *--------------------------------------------------------------------------- sl@0: * sl@0: * Tcl_FSJoinToPath -- sl@0: * sl@0: * This function takes the given object, which should usually be a sl@0: * valid path or NULL, and joins onto it the array of paths sl@0: * segments given. sl@0: * sl@0: * Results: sl@0: * Returns object with refCount of zero sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *--------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C Tcl_Obj* sl@0: Tcl_FSJoinToPath(basePtr, objc, objv) sl@0: Tcl_Obj *basePtr; sl@0: int objc; sl@0: Tcl_Obj *CONST objv[]; sl@0: { sl@0: int i; sl@0: Tcl_Obj *lobj, *ret; sl@0: sl@0: if (basePtr == NULL) { sl@0: lobj = Tcl_NewListObj(0, NULL); sl@0: } else { sl@0: lobj = Tcl_NewListObj(1, &basePtr); sl@0: } sl@0: sl@0: for (i = 0; i 0 && (start[length-1] != '/')) { sl@0: Tcl_AppendToObj(prefix, "/", 1); sl@0: length++; sl@0: } sl@0: needsSep = 0; sl@0: sl@0: /* sl@0: * Append the element, eliminating duplicate and trailing sl@0: * slashes. sl@0: */ sl@0: sl@0: Tcl_SetObjLength(prefix, length + (int) strlen(p)); sl@0: sl@0: dest = Tcl_GetString(prefix) + length; sl@0: for (; *p != '\0'; p++) { sl@0: if (*p == '/') { sl@0: while (p[1] == '/') { sl@0: p++; sl@0: } sl@0: if (p[1] != '\0') { sl@0: if (needsSep) { sl@0: *dest++ = '/'; sl@0: } sl@0: } sl@0: } else { sl@0: *dest++ = *p; sl@0: needsSep = 1; sl@0: } sl@0: } sl@0: length = dest - Tcl_GetString(prefix); sl@0: Tcl_SetObjLength(prefix, length); sl@0: break; sl@0: sl@0: case TCL_PLATFORM_WINDOWS: sl@0: /* sl@0: * Check to see if we need to append a separator. sl@0: */ sl@0: sl@0: if ((length > 0) && sl@0: (start[length-1] != '/') && (start[length-1] != ':')) { sl@0: Tcl_AppendToObj(prefix, "/", 1); sl@0: length++; sl@0: } sl@0: needsSep = 0; sl@0: sl@0: /* sl@0: * Append the element, eliminating duplicate and sl@0: * trailing slashes. sl@0: */ sl@0: sl@0: Tcl_SetObjLength(prefix, length + (int) strlen(p)); sl@0: dest = Tcl_GetString(prefix) + length; sl@0: for (; *p != '\0'; p++) { sl@0: if ((*p == '/') || (*p == '\\')) { sl@0: while ((p[1] == '/') || (p[1] == '\\')) { sl@0: p++; sl@0: } sl@0: if ((p[1] != '\0') && needsSep) { sl@0: *dest++ = '/'; sl@0: } sl@0: } else { sl@0: *dest++ = *p; sl@0: needsSep = 1; sl@0: } sl@0: } sl@0: length = dest - Tcl_GetString(prefix); sl@0: Tcl_SetObjLength(prefix, length); sl@0: break; sl@0: sl@0: case TCL_PLATFORM_MAC: { sl@0: int newLength; sl@0: sl@0: /* sl@0: * Sort out separators. We basically add the object we've sl@0: * been given, but we have to make sure that there is sl@0: * exactly one separator inbetween (unless the object we're sl@0: * adding contains multiple contiguous colons, all of which sl@0: * we must add). Also if an object is just ':' we don't sl@0: * bother to add it unless it's the very first element. sl@0: */ sl@0: sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: int adjustedPath = 0; sl@0: if ((strchr(p, ':') == NULL) && (strchr(p, '/') != NULL)) { sl@0: char *start = p; sl@0: adjustedPath = 1; sl@0: while (*start != '\0') { sl@0: if (*start == '/') { sl@0: *start = ':'; sl@0: } sl@0: start++; sl@0: } sl@0: } sl@0: #endif sl@0: if (length > 0) { sl@0: if ((p[0] == ':') && (p[1] == '\0')) { sl@0: return; sl@0: } sl@0: if (start[length-1] != ':') { sl@0: if (*p != '\0' && *p != ':') { sl@0: Tcl_AppendToObj(prefix, ":", 1); sl@0: length++; sl@0: } sl@0: } else if (*p == ':') { sl@0: p++; sl@0: } sl@0: } else { sl@0: if (*p != '\0' && *p != ':') { sl@0: Tcl_AppendToObj(prefix, ":", 1); sl@0: length++; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Append the element sl@0: */ sl@0: sl@0: newLength = strlen(p); sl@0: /* sl@0: * It may not be good to just do 'Tcl_AppendToObj(prefix, sl@0: * p, newLength)' because the object may contain duplicate sl@0: * colons which we want to get rid of. sl@0: */ sl@0: Tcl_AppendToObj(prefix, p, newLength); sl@0: sl@0: /* Remove spurious trailing single ':' */ sl@0: dest = Tcl_GetString(prefix) + length + newLength; sl@0: if (*(dest-1) == ':') { sl@0: if (dest-1 > Tcl_GetString(prefix)) { sl@0: if (*(dest-2) != ':') { sl@0: Tcl_SetObjLength(prefix, length + newLength -1); sl@0: } sl@0: } sl@0: } sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: /* Revert the path to what it was */ sl@0: if (adjustedPath) { sl@0: char *start = joining; sl@0: while (*start != '\0') { sl@0: if (*start == ':') { sl@0: *start = '/'; sl@0: } sl@0: start++; sl@0: } sl@0: } sl@0: #endif sl@0: break; sl@0: } sl@0: } sl@0: return; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_JoinPath -- sl@0: * sl@0: * Combine a list of paths in a platform specific manner. The sl@0: * function 'Tcl_FSJoinPath' should be used in preference where sl@0: * possible. sl@0: * sl@0: * Results: sl@0: * Appends the joined path to the end of the specified sl@0: * Tcl_DString returning a pointer to the resulting string. Note sl@0: * that the Tcl_DString must already be initialized. sl@0: * sl@0: * Side effects: sl@0: * Modifies the Tcl_DString. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C char * sl@0: Tcl_JoinPath(argc, argv, resultPtr) sl@0: int argc; sl@0: CONST char * CONST *argv; sl@0: Tcl_DString *resultPtr; /* Pointer to previously initialized DString */ sl@0: { sl@0: int i, len; sl@0: Tcl_Obj *listObj = Tcl_NewObj(); sl@0: Tcl_Obj *resultObj; sl@0: char *resultStr; sl@0: sl@0: /* Build the list of paths */ sl@0: for (i = 0; i < argc; i++) { sl@0: Tcl_ListObjAppendElement(NULL, listObj, sl@0: Tcl_NewStringObj(argv[i], -1)); sl@0: } sl@0: sl@0: /* Ask the objectified code to join the paths */ sl@0: Tcl_IncrRefCount(listObj); sl@0: resultObj = Tcl_FSJoinPath(listObj, argc); sl@0: Tcl_IncrRefCount(resultObj); sl@0: Tcl_DecrRefCount(listObj); sl@0: sl@0: /* Store the result */ sl@0: resultStr = Tcl_GetStringFromObj(resultObj, &len); sl@0: Tcl_DStringAppend(resultPtr, resultStr, len); sl@0: Tcl_DecrRefCount(resultObj); sl@0: sl@0: /* Return a pointer to the result */ sl@0: return Tcl_DStringValue(resultPtr); sl@0: } sl@0: sl@0: /* sl@0: *--------------------------------------------------------------------------- sl@0: * sl@0: * Tcl_TranslateFileName -- sl@0: * sl@0: * Converts a file name into a form usable by the native system sl@0: * interfaces. If the name starts with a tilde, it will produce a sl@0: * name where the tilde and following characters have been replaced sl@0: * by the home directory location for the named user. sl@0: * sl@0: * Results: sl@0: * The return value is a pointer to a string containing the name sl@0: * after tilde substitution. If there was no tilde substitution, sl@0: * the return value is a pointer to a copy of the original string. sl@0: * If there was an error in processing the name, then an error sl@0: * message is left in the interp's result (if interp was not NULL) sl@0: * and the return value is NULL. Space for the return value is sl@0: * allocated in bufferPtr; the caller must call Tcl_DStringFree() sl@0: * to free the space if the return value was not NULL. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C char * sl@0: Tcl_TranslateFileName(interp, name, bufferPtr) sl@0: Tcl_Interp *interp; /* Interpreter in which to store error sl@0: * message (if necessary). */ sl@0: CONST char *name; /* File name, which may begin with "~" (to sl@0: * indicate current user's home directory) or sl@0: * "~" (to indicate any user's home sl@0: * directory). */ sl@0: Tcl_DString *bufferPtr; /* Uninitialized or free DString filled sl@0: * with name after tilde substitution. */ sl@0: { sl@0: Tcl_Obj *path = Tcl_NewStringObj(name, -1); sl@0: Tcl_Obj *transPtr; sl@0: sl@0: Tcl_IncrRefCount(path); sl@0: transPtr = Tcl_FSGetTranslatedPath(interp, path); sl@0: if (transPtr == NULL) { sl@0: Tcl_DecrRefCount(path); sl@0: return NULL; sl@0: } sl@0: sl@0: Tcl_DStringInit(bufferPtr); sl@0: Tcl_DStringAppend(bufferPtr, Tcl_GetString(transPtr), -1); sl@0: Tcl_DecrRefCount(path); sl@0: Tcl_DecrRefCount(transPtr); sl@0: sl@0: /* sl@0: * Convert forward slashes to backslashes in Windows paths because sl@0: * some system interfaces don't accept forward slashes. sl@0: */ sl@0: sl@0: if (tclPlatform == TCL_PLATFORM_WINDOWS) { sl@0: register char *p; sl@0: for (p = Tcl_DStringValue(bufferPtr); *p != '\0'; p++) { sl@0: if (*p == '/') { sl@0: *p = '\\'; sl@0: } sl@0: } sl@0: } sl@0: return Tcl_DStringValue(bufferPtr); sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclGetExtension -- sl@0: * sl@0: * This function returns a pointer to the beginning of the sl@0: * extension part of a file name. sl@0: * sl@0: * Results: sl@0: * Returns a pointer into name which indicates where the extension sl@0: * starts. If there is no extension, returns NULL. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: char * sl@0: TclGetExtension(name) sl@0: char *name; /* File name to parse. */ sl@0: { sl@0: char *p, *lastSep; sl@0: sl@0: /* sl@0: * First find the last directory separator. sl@0: */ sl@0: sl@0: lastSep = NULL; /* Needed only to prevent gcc warnings. */ sl@0: switch (tclPlatform) { sl@0: case TCL_PLATFORM_UNIX: sl@0: lastSep = strrchr(name, '/'); sl@0: break; sl@0: sl@0: case TCL_PLATFORM_MAC: sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: if (strchr(name, ':') == NULL) { sl@0: lastSep = strrchr(name, '/'); sl@0: } else { sl@0: lastSep = strrchr(name, ':'); sl@0: } sl@0: #else sl@0: lastSep = strrchr(name, ':'); sl@0: #endif sl@0: break; sl@0: sl@0: case TCL_PLATFORM_WINDOWS: sl@0: lastSep = NULL; sl@0: for (p = name; *p != '\0'; p++) { sl@0: if (strchr("/\\:", *p) != NULL) { sl@0: lastSep = p; sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: p = strrchr(name, '.'); sl@0: if ((p != NULL) && (lastSep != NULL) && (lastSep > p)) { sl@0: p = NULL; sl@0: } sl@0: sl@0: /* sl@0: * In earlier versions, we used to back up to the first period in a series sl@0: * so that "foo..o" would be split into "foo" and "..o". This is a sl@0: * confusing and usually incorrect behavior, so now we split at the last sl@0: * period in the name. sl@0: */ sl@0: sl@0: return p; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * DoTildeSubst -- sl@0: * sl@0: * Given a string following a tilde, this routine returns the sl@0: * corresponding home directory. sl@0: * sl@0: * Results: sl@0: * The result is a pointer to a static string containing the home sl@0: * directory in native format. If there was an error in processing sl@0: * the substitution, then an error message is left in the interp's sl@0: * result and the return value is NULL. On success, the results sl@0: * are appended to resultPtr, and the contents of resultPtr are sl@0: * returned. sl@0: * sl@0: * Side effects: sl@0: * Information may be left in resultPtr. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static CONST char * sl@0: DoTildeSubst(interp, user, resultPtr) sl@0: Tcl_Interp *interp; /* Interpreter in which to store error sl@0: * message (if necessary). */ sl@0: CONST char *user; /* Name of user whose home directory should be sl@0: * substituted, or "" for current user. */ sl@0: Tcl_DString *resultPtr; /* Initialized DString filled with name sl@0: * after tilde substitution. */ sl@0: { sl@0: CONST char *dir; sl@0: sl@0: if (*user == '\0') { sl@0: Tcl_DString dirString; sl@0: sl@0: dir = TclGetEnv("HOME", &dirString); sl@0: if (dir == NULL) { sl@0: if (interp) { sl@0: Tcl_ResetResult(interp); sl@0: Tcl_AppendResult(interp, "couldn't find HOME environment ", sl@0: "variable to expand path", (char *) NULL); sl@0: } sl@0: return NULL; sl@0: } sl@0: Tcl_JoinPath(1, &dir, resultPtr); sl@0: Tcl_DStringFree(&dirString); sl@0: } else { sl@0: if (TclpGetUserHome(user, resultPtr) == NULL) { sl@0: if (interp) { sl@0: Tcl_ResetResult(interp); sl@0: Tcl_AppendResult(interp, "user \"", user, "\" doesn't exist", sl@0: (char *) NULL); sl@0: } sl@0: return NULL; sl@0: } sl@0: } sl@0: return Tcl_DStringValue(resultPtr); sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_GlobObjCmd -- sl@0: * sl@0: * This procedure is invoked to process the "glob" Tcl command. sl@0: * See the user documentation for details on what it does. sl@0: * sl@0: * Results: sl@0: * A standard Tcl result. sl@0: * sl@0: * Side effects: sl@0: * See the user documentation. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: /* ARGSUSED */ sl@0: int sl@0: Tcl_GlobObjCmd(dummy, interp, objc, objv) sl@0: ClientData dummy; /* Not used. */ sl@0: Tcl_Interp *interp; /* Current interpreter. */ sl@0: int objc; /* Number of arguments. */ sl@0: Tcl_Obj *CONST objv[]; /* Argument objects. */ sl@0: { sl@0: int index, i, globFlags, length, join, dir, result; sl@0: char *string, *separators; sl@0: Tcl_Obj *typePtr, *resultPtr, *look; sl@0: Tcl_Obj *pathOrDir = NULL; sl@0: Tcl_DString prefix; sl@0: static CONST char *options[] = { sl@0: "-directory", "-join", "-nocomplain", "-path", "-tails", sl@0: "-types", "--", NULL sl@0: }; sl@0: enum options { sl@0: GLOB_DIR, GLOB_JOIN, GLOB_NOCOMPLAIN, GLOB_PATH, GLOB_TAILS, sl@0: GLOB_TYPE, GLOB_LAST sl@0: }; sl@0: enum pathDirOptions {PATH_NONE = -1 , PATH_GENERAL = 0, PATH_DIR = 1}; sl@0: Tcl_GlobTypeData *globTypes = NULL; sl@0: sl@0: globFlags = 0; sl@0: join = 0; sl@0: dir = PATH_NONE; sl@0: typePtr = NULL; sl@0: for (i = 1; i < objc; i++) { sl@0: if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", 0, &index) sl@0: != TCL_OK) { sl@0: string = Tcl_GetStringFromObj(objv[i], &length); sl@0: if (string[0] == '-') { sl@0: /* sl@0: * It looks like the command contains an option so signal sl@0: * an error sl@0: */ sl@0: return TCL_ERROR; sl@0: } else { sl@0: /* sl@0: * This clearly isn't an option; assume it's the first sl@0: * glob pattern. We must clear the error sl@0: */ sl@0: Tcl_ResetResult(interp); sl@0: break; sl@0: } sl@0: } sl@0: switch (index) { sl@0: case GLOB_NOCOMPLAIN: /* -nocomplain */ sl@0: globFlags |= TCL_GLOBMODE_NO_COMPLAIN; sl@0: break; sl@0: case GLOB_DIR: /* -dir */ sl@0: if (i == (objc-1)) { sl@0: Tcl_SetObjResult(interp, Tcl_NewStringObj( sl@0: "missing argument to \"-directory\"", -1)); sl@0: return TCL_ERROR; sl@0: } sl@0: if (dir != PATH_NONE) { sl@0: Tcl_SetObjResult(interp, Tcl_NewStringObj( sl@0: "\"-directory\" cannot be used with \"-path\"", sl@0: -1)); sl@0: return TCL_ERROR; sl@0: } sl@0: dir = PATH_DIR; sl@0: globFlags |= TCL_GLOBMODE_DIR; sl@0: pathOrDir = objv[i+1]; sl@0: i++; sl@0: break; sl@0: case GLOB_JOIN: /* -join */ sl@0: join = 1; sl@0: break; sl@0: case GLOB_TAILS: /* -tails */ sl@0: globFlags |= TCL_GLOBMODE_TAILS; sl@0: break; sl@0: case GLOB_PATH: /* -path */ sl@0: if (i == (objc-1)) { sl@0: Tcl_SetObjResult(interp, Tcl_NewStringObj( sl@0: "missing argument to \"-path\"", -1)); sl@0: return TCL_ERROR; sl@0: } sl@0: if (dir != PATH_NONE) { sl@0: Tcl_SetObjResult(interp, Tcl_NewStringObj( sl@0: "\"-path\" cannot be used with \"-directory\"", sl@0: -1)); sl@0: return TCL_ERROR; sl@0: } sl@0: dir = PATH_GENERAL; sl@0: pathOrDir = objv[i+1]; sl@0: i++; sl@0: break; sl@0: case GLOB_TYPE: /* -types */ sl@0: if (i == (objc-1)) { sl@0: Tcl_SetObjResult(interp, Tcl_NewStringObj( sl@0: "missing argument to \"-types\"", -1)); sl@0: return TCL_ERROR; sl@0: } sl@0: typePtr = objv[i+1]; sl@0: if (Tcl_ListObjLength(interp, typePtr, &length) != TCL_OK) { sl@0: return TCL_ERROR; sl@0: } sl@0: i++; sl@0: break; sl@0: case GLOB_LAST: /* -- */ sl@0: i++; sl@0: goto endOfForLoop; sl@0: } sl@0: } sl@0: endOfForLoop: sl@0: if (objc - i < 1) { sl@0: Tcl_WrongNumArgs(interp, 1, objv, "?switches? name ?name ...?"); sl@0: return TCL_ERROR; sl@0: } sl@0: if ((globFlags & TCL_GLOBMODE_TAILS) && (pathOrDir == NULL)) { sl@0: Tcl_SetObjResult(interp, Tcl_NewStringObj( sl@0: "\"-tails\" must be used with either \"-directory\" or \"-path\"", sl@0: -1)); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: separators = NULL; /* lint. */ sl@0: switch (tclPlatform) { sl@0: case TCL_PLATFORM_UNIX: sl@0: separators = "/"; sl@0: break; sl@0: case TCL_PLATFORM_WINDOWS: sl@0: separators = "/\\:"; sl@0: break; sl@0: case TCL_PLATFORM_MAC: sl@0: separators = ":"; sl@0: break; sl@0: } sl@0: if (dir == PATH_GENERAL) { sl@0: int pathlength; sl@0: char *last; sl@0: char *first = Tcl_GetStringFromObj(pathOrDir,&pathlength); sl@0: sl@0: /* sl@0: * Find the last path separator in the path sl@0: */ sl@0: last = first + pathlength; sl@0: for (; last != first; last--) { sl@0: if (strchr(separators, *(last-1)) != NULL) { sl@0: break; sl@0: } sl@0: } sl@0: if (last == first + pathlength) { sl@0: /* It's really a directory */ sl@0: dir = PATH_DIR; sl@0: } else { sl@0: Tcl_DString pref; sl@0: char *search, *find; sl@0: Tcl_DStringInit(&pref); sl@0: if (last == first) { sl@0: /* The whole thing is a prefix */ sl@0: Tcl_DStringAppend(&pref, first, -1); sl@0: pathOrDir = NULL; sl@0: } else { sl@0: /* Have to split off the end */ sl@0: Tcl_DStringAppend(&pref, last, first+pathlength-last); sl@0: pathOrDir = Tcl_NewStringObj(first, last-first-1); sl@0: /* sl@0: * We must ensure that we haven't cut off too much, sl@0: * and turned a valid path like '/' or 'C:/' into sl@0: * an incorrect path like '' or 'C:'. The way we sl@0: * do this is to add a separator if there are none sl@0: * presently in the prefix. sl@0: */ sl@0: if (strpbrk(Tcl_GetString(pathOrDir), "\\/") == NULL) { sl@0: Tcl_AppendToObj(pathOrDir, last-1, 1); sl@0: } sl@0: } sl@0: /* Need to quote 'prefix' */ sl@0: Tcl_DStringInit(&prefix); sl@0: search = Tcl_DStringValue(&pref); sl@0: while ((find = (strpbrk(search, "\\[]*?{}"))) != NULL) { sl@0: Tcl_DStringAppend(&prefix, search, find-search); sl@0: Tcl_DStringAppend(&prefix, "\\", 1); sl@0: Tcl_DStringAppend(&prefix, find, 1); sl@0: search = find+1; sl@0: if (*search == '\0') { sl@0: break; sl@0: } sl@0: } sl@0: if (*search != '\0') { sl@0: Tcl_DStringAppend(&prefix, search, -1); sl@0: } sl@0: Tcl_DStringFree(&pref); sl@0: } sl@0: } sl@0: sl@0: if (pathOrDir != NULL) { sl@0: Tcl_IncrRefCount(pathOrDir); sl@0: } sl@0: sl@0: if (typePtr != NULL) { sl@0: /* sl@0: * The rest of the possible type arguments (except 'd') are sl@0: * platform specific. We don't complain when they are used sl@0: * on an incompatible platform. sl@0: */ sl@0: Tcl_ListObjLength(interp, typePtr, &length); sl@0: globTypes = (Tcl_GlobTypeData*) ckalloc(sizeof(Tcl_GlobTypeData)); sl@0: globTypes->type = 0; sl@0: globTypes->perm = 0; sl@0: globTypes->macType = NULL; sl@0: globTypes->macCreator = NULL; sl@0: while(--length >= 0) { sl@0: int len; sl@0: char *str; sl@0: Tcl_ListObjIndex(interp, typePtr, length, &look); sl@0: str = Tcl_GetStringFromObj(look, &len); sl@0: if (strcmp("readonly", str) == 0) { sl@0: globTypes->perm |= TCL_GLOB_PERM_RONLY; sl@0: } else if (strcmp("hidden", str) == 0) { sl@0: globTypes->perm |= TCL_GLOB_PERM_HIDDEN; sl@0: } else if (len == 1) { sl@0: switch (str[0]) { sl@0: case 'r': sl@0: globTypes->perm |= TCL_GLOB_PERM_R; sl@0: break; sl@0: case 'w': sl@0: globTypes->perm |= TCL_GLOB_PERM_W; sl@0: break; sl@0: case 'x': sl@0: globTypes->perm |= TCL_GLOB_PERM_X; sl@0: break; sl@0: case 'b': sl@0: globTypes->type |= TCL_GLOB_TYPE_BLOCK; sl@0: break; sl@0: case 'c': sl@0: globTypes->type |= TCL_GLOB_TYPE_CHAR; sl@0: break; sl@0: case 'd': sl@0: globTypes->type |= TCL_GLOB_TYPE_DIR; sl@0: break; sl@0: case 'p': sl@0: globTypes->type |= TCL_GLOB_TYPE_PIPE; sl@0: break; sl@0: case 'f': sl@0: globTypes->type |= TCL_GLOB_TYPE_FILE; sl@0: break; sl@0: case 'l': sl@0: globTypes->type |= TCL_GLOB_TYPE_LINK; sl@0: break; sl@0: case 's': sl@0: globTypes->type |= TCL_GLOB_TYPE_SOCK; sl@0: break; sl@0: default: sl@0: goto badTypesArg; sl@0: } sl@0: } else if (len == 4) { sl@0: /* This is assumed to be a MacOS file type */ sl@0: if (globTypes->macType != NULL) { sl@0: goto badMacTypesArg; sl@0: } sl@0: globTypes->macType = look; sl@0: Tcl_IncrRefCount(look); sl@0: } else { sl@0: Tcl_Obj* item; sl@0: if ((Tcl_ListObjLength(NULL, look, &len) == TCL_OK) && sl@0: (len == 3)) { sl@0: Tcl_ListObjIndex(interp, look, 0, &item); sl@0: if (!strcmp("macintosh", Tcl_GetString(item))) { sl@0: Tcl_ListObjIndex(interp, look, 1, &item); sl@0: if (!strcmp("type", Tcl_GetString(item))) { sl@0: Tcl_ListObjIndex(interp, look, 2, &item); sl@0: if (globTypes->macType != NULL) { sl@0: goto badMacTypesArg; sl@0: } sl@0: globTypes->macType = item; sl@0: Tcl_IncrRefCount(item); sl@0: continue; sl@0: } else if (!strcmp("creator", Tcl_GetString(item))) { sl@0: Tcl_ListObjIndex(interp, look, 2, &item); sl@0: if (globTypes->macCreator != NULL) { sl@0: goto badMacTypesArg; sl@0: } sl@0: globTypes->macCreator = item; sl@0: Tcl_IncrRefCount(item); sl@0: continue; sl@0: } sl@0: } sl@0: } sl@0: /* sl@0: * Error cases. We reset sl@0: * the 'join' flag to zero, since we haven't yet sl@0: * made use of it. sl@0: */ sl@0: badTypesArg: sl@0: resultPtr = Tcl_GetObjResult(interp); sl@0: Tcl_AppendToObj(resultPtr, "bad argument to \"-types\": ", -1); sl@0: Tcl_AppendObjToObj(resultPtr, look); sl@0: result = TCL_ERROR; sl@0: join = 0; sl@0: goto endOfGlob; sl@0: badMacTypesArg: sl@0: Tcl_SetObjResult(interp, Tcl_NewStringObj( sl@0: "only one MacOS type or creator argument" sl@0: " to \"-types\" allowed", -1)); sl@0: result = TCL_ERROR; sl@0: join = 0; sl@0: goto endOfGlob; sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Now we perform the actual glob below. This may involve joining sl@0: * together the pattern arguments, dealing with particular file types sl@0: * etc. We use a 'goto' to ensure we free any memory allocated along sl@0: * the way. sl@0: */ sl@0: objc -= i; sl@0: objv += i; sl@0: result = TCL_OK; sl@0: if (join) { sl@0: if (dir != PATH_GENERAL) { sl@0: Tcl_DStringInit(&prefix); sl@0: } sl@0: for (i = 0; i < objc; i++) { sl@0: string = Tcl_GetStringFromObj(objv[i], &length); sl@0: Tcl_DStringAppend(&prefix, string, length); sl@0: if (i != objc -1) { sl@0: Tcl_DStringAppend(&prefix, separators, 1); sl@0: } sl@0: } sl@0: if (TclGlob(interp, Tcl_DStringValue(&prefix), pathOrDir, sl@0: globFlags, globTypes) != TCL_OK) { sl@0: result = TCL_ERROR; sl@0: goto endOfGlob; sl@0: } sl@0: } else { sl@0: if (dir == PATH_GENERAL) { sl@0: Tcl_DString str; sl@0: for (i = 0; i < objc; i++) { sl@0: Tcl_DStringInit(&str); sl@0: if (dir == PATH_GENERAL) { sl@0: Tcl_DStringAppend(&str, Tcl_DStringValue(&prefix), sl@0: Tcl_DStringLength(&prefix)); sl@0: } sl@0: string = Tcl_GetStringFromObj(objv[i], &length); sl@0: Tcl_DStringAppend(&str, string, length); sl@0: if (TclGlob(interp, Tcl_DStringValue(&str), pathOrDir, sl@0: globFlags, globTypes) != TCL_OK) { sl@0: result = TCL_ERROR; sl@0: Tcl_DStringFree(&str); sl@0: goto endOfGlob; sl@0: } sl@0: } sl@0: Tcl_DStringFree(&str); sl@0: } else { sl@0: for (i = 0; i < objc; i++) { sl@0: string = Tcl_GetString(objv[i]); sl@0: if (TclGlob(interp, string, pathOrDir, sl@0: globFlags, globTypes) != TCL_OK) { sl@0: result = TCL_ERROR; sl@0: goto endOfGlob; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: if ((globFlags & TCL_GLOBMODE_NO_COMPLAIN) == 0) { sl@0: if (Tcl_ListObjLength(interp, Tcl_GetObjResult(interp), sl@0: &length) != TCL_OK) { sl@0: /* This should never happen. Maybe we should be more dramatic */ sl@0: result = TCL_ERROR; sl@0: goto endOfGlob; sl@0: } sl@0: if (length == 0) { sl@0: Tcl_AppendResult(interp, "no files matched glob pattern", sl@0: (join || (objc == 1)) ? " \"" : "s \"", (char *) NULL); sl@0: if (join) { sl@0: Tcl_AppendResult(interp, Tcl_DStringValue(&prefix), sl@0: (char *) NULL); sl@0: } else { sl@0: char *sep = ""; sl@0: for (i = 0; i < objc; i++) { sl@0: string = Tcl_GetString(objv[i]); sl@0: Tcl_AppendResult(interp, sep, string, (char *) NULL); sl@0: sep = " "; sl@0: } sl@0: } sl@0: Tcl_AppendResult(interp, "\"", (char *) NULL); sl@0: result = TCL_ERROR; sl@0: } sl@0: } sl@0: endOfGlob: sl@0: if (join || (dir == PATH_GENERAL)) { sl@0: Tcl_DStringFree(&prefix); sl@0: } sl@0: if (pathOrDir != NULL) { sl@0: Tcl_DecrRefCount(pathOrDir); sl@0: } sl@0: if (globTypes != NULL) { sl@0: if (globTypes->macType != NULL) { sl@0: Tcl_DecrRefCount(globTypes->macType); sl@0: } sl@0: if (globTypes->macCreator != NULL) { sl@0: Tcl_DecrRefCount(globTypes->macCreator); sl@0: } sl@0: ckfree((char *) globTypes); sl@0: } sl@0: return result; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclGlob -- sl@0: * sl@0: * This procedure prepares arguments for the TclDoGlob call. sl@0: * It sets the separator string based on the platform, performs sl@0: * tilde substitution, and calls TclDoGlob. sl@0: * sl@0: * The interpreter's result, on entry to this function, must sl@0: * be a valid Tcl list (e.g. it could be empty), since we will sl@0: * lappend any new results to that list. If it is not a valid sl@0: * list, this function will fail to do anything very meaningful. sl@0: * sl@0: * Results: sl@0: * The return value is a standard Tcl result indicating whether sl@0: * an error occurred in globbing. After a normal return the sl@0: * result in interp (set by TclDoGlob) holds all of the file names sl@0: * given by the pattern and unquotedPrefix arguments. After an sl@0: * error the result in interp will hold an error message, unless sl@0: * the 'TCL_GLOBMODE_NO_COMPLAIN' flag was given, in which case sl@0: * an error results in a TCL_OK return leaving the interpreter's sl@0: * result unmodified. sl@0: * sl@0: * Side effects: sl@0: * The 'pattern' is written to. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: /* ARGSUSED */ sl@0: int sl@0: TclGlob(interp, pattern, unquotedPrefix, globFlags, types) sl@0: Tcl_Interp *interp; /* Interpreter for returning error message sl@0: * or appending list of matching file names. */ sl@0: char *pattern; /* Glob pattern to match. Must not refer sl@0: * to a static string. */ sl@0: Tcl_Obj *unquotedPrefix; /* Prefix to glob pattern, if non-null, which sl@0: * is considered literally. */ sl@0: int globFlags; /* Stores or'ed combination of flags */ sl@0: Tcl_GlobTypeData *types; /* Struct containing acceptable types. sl@0: * May be NULL. */ sl@0: { sl@0: char *separators; sl@0: CONST char *head; sl@0: char *tail, *start; sl@0: char c; sl@0: int result, prefixLen; sl@0: Tcl_DString buffer; sl@0: Tcl_Obj *oldResult; sl@0: sl@0: separators = NULL; /* lint. */ sl@0: switch (tclPlatform) { sl@0: case TCL_PLATFORM_UNIX: sl@0: separators = "/"; sl@0: break; sl@0: case TCL_PLATFORM_WINDOWS: sl@0: separators = "/\\:"; sl@0: break; sl@0: case TCL_PLATFORM_MAC: sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: if (unquotedPrefix == NULL) { sl@0: separators = (strchr(pattern, ':') == NULL) ? "/" : ":"; sl@0: } else { sl@0: separators = ":"; sl@0: } sl@0: #else sl@0: separators = ":"; sl@0: #endif sl@0: break; sl@0: } sl@0: sl@0: Tcl_DStringInit(&buffer); sl@0: if (unquotedPrefix != NULL) { sl@0: start = Tcl_GetString(unquotedPrefix); sl@0: } else { sl@0: start = pattern; sl@0: } sl@0: sl@0: /* sl@0: * Perform tilde substitution, if needed. sl@0: */ sl@0: sl@0: if (start[0] == '~') { sl@0: sl@0: /* sl@0: * Find the first path separator after the tilde. sl@0: */ sl@0: for (tail = start; *tail != '\0'; tail++) { sl@0: if (*tail == '\\') { sl@0: if (strchr(separators, tail[1]) != NULL) { sl@0: break; sl@0: } sl@0: } else if (strchr(separators, *tail) != NULL) { sl@0: break; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Determine the home directory for the specified user. sl@0: */ sl@0: sl@0: c = *tail; sl@0: *tail = '\0'; sl@0: if (globFlags & TCL_GLOBMODE_NO_COMPLAIN) { sl@0: /* sl@0: * We will ignore any error message here, and we sl@0: * don't want to mess up the interpreter's result. sl@0: */ sl@0: head = DoTildeSubst(NULL, start+1, &buffer); sl@0: } else { sl@0: head = DoTildeSubst(interp, start+1, &buffer); sl@0: } sl@0: *tail = c; sl@0: if (head == NULL) { sl@0: if (globFlags & TCL_GLOBMODE_NO_COMPLAIN) { sl@0: return TCL_OK; sl@0: } else { sl@0: return TCL_ERROR; sl@0: } sl@0: } sl@0: if (head != Tcl_DStringValue(&buffer)) { sl@0: Tcl_DStringAppend(&buffer, head, -1); sl@0: } sl@0: if (unquotedPrefix != NULL) { sl@0: Tcl_DStringAppend(&buffer, tail, -1); sl@0: tail = pattern; sl@0: } sl@0: } else { sl@0: tail = pattern; sl@0: if (unquotedPrefix != NULL) { sl@0: Tcl_DStringAppend(&buffer,Tcl_GetString(unquotedPrefix),-1); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * We want to remember the length of the current prefix, sl@0: * in case we are using TCL_GLOBMODE_TAILS. Also if we sl@0: * are using TCL_GLOBMODE_DIR, we must make sure the sl@0: * prefix ends in a directory separator. sl@0: */ sl@0: prefixLen = Tcl_DStringLength(&buffer); sl@0: sl@0: if (prefixLen > 0) { sl@0: c = Tcl_DStringValue(&buffer)[prefixLen-1]; sl@0: if (strchr(separators, c) == NULL) { sl@0: /* sl@0: * If the prefix is a directory, make sure it ends in a sl@0: * directory separator. sl@0: */ sl@0: if (globFlags & TCL_GLOBMODE_DIR) { sl@0: Tcl_DStringAppend(&buffer,separators,1); sl@0: /* Try to borrow that separator from the tail */ sl@0: if (*tail == *separators) { sl@0: tail++; sl@0: } sl@0: } sl@0: prefixLen++; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * We need to get the old result, in case it is over-written sl@0: * below when we still need it. sl@0: */ sl@0: oldResult = Tcl_GetObjResult(interp); sl@0: Tcl_IncrRefCount(oldResult); sl@0: Tcl_ResetResult(interp); sl@0: sl@0: result = TclDoGlob(interp, separators, &buffer, tail, types); sl@0: sl@0: if (result != TCL_OK) { sl@0: if (globFlags & TCL_GLOBMODE_NO_COMPLAIN) { sl@0: /* Put back the old result and reset the return code */ sl@0: Tcl_SetObjResult(interp, oldResult); sl@0: result = TCL_OK; sl@0: } sl@0: } else { sl@0: /* sl@0: * Now we must concatenate the 'oldResult' and the current sl@0: * result, and then place that into the interpreter. sl@0: * sl@0: * If we only want the tails, we must strip off the prefix now. sl@0: * It may seem more efficient to pass the tails flag down into sl@0: * TclDoGlob, Tcl_FSMatchInDirectory, but those functions are sl@0: * continually adjusting the prefix as the various pieces of sl@0: * the pattern are assimilated, so that would add a lot of sl@0: * complexity to the code. This way is a little slower (when sl@0: * the -tails flag is given), but much simpler to code. sl@0: */ sl@0: int objc, i; sl@0: Tcl_Obj **objv; sl@0: sl@0: /* Ensure sole ownership */ sl@0: if (Tcl_IsShared(oldResult)) { sl@0: Tcl_DecrRefCount(oldResult); sl@0: oldResult = Tcl_DuplicateObj(oldResult); sl@0: Tcl_IncrRefCount(oldResult); sl@0: } sl@0: sl@0: Tcl_ListObjGetElements(NULL, Tcl_GetObjResult(interp), sl@0: &objc, &objv); sl@0: #ifdef MAC_TCL sl@0: /* adjust prefixLen if TclDoGlob prepended a ':' */ sl@0: if ((prefixLen > 0) && (objc > 0) sl@0: && (Tcl_DStringValue(&buffer)[0] != ':')) { sl@0: char *str = Tcl_GetStringFromObj(objv[0],NULL); sl@0: if (str[0] == ':') { sl@0: prefixLen++; sl@0: } sl@0: } sl@0: #endif sl@0: for (i = 0; i< objc; i++) { sl@0: Tcl_Obj* elt; sl@0: if (globFlags & TCL_GLOBMODE_TAILS) { sl@0: int len; sl@0: char *oldStr = Tcl_GetStringFromObj(objv[i],&len); sl@0: if (len == prefixLen) { sl@0: if ((pattern[0] == '\0') sl@0: || (strchr(separators, pattern[0]) == NULL)) { sl@0: elt = Tcl_NewStringObj(".",1); sl@0: } else { sl@0: elt = Tcl_NewStringObj("/",1); sl@0: } sl@0: } else { sl@0: elt = Tcl_NewStringObj(oldStr + prefixLen, sl@0: len - prefixLen); sl@0: } sl@0: } else { sl@0: elt = objv[i]; sl@0: } sl@0: /* Assumption that 'oldResult' is a valid list */ sl@0: Tcl_ListObjAppendElement(interp, oldResult, elt); sl@0: } sl@0: Tcl_SetObjResult(interp, oldResult); sl@0: } sl@0: /* sl@0: * Release our temporary copy. All code paths above must sl@0: * end here so we free our reference. sl@0: */ sl@0: Tcl_DecrRefCount(oldResult); sl@0: Tcl_DStringFree(&buffer); sl@0: return result; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * SkipToChar -- sl@0: * sl@0: * This function traverses a glob pattern looking for the next sl@0: * unquoted occurance of the specified character at the same braces sl@0: * nesting level. sl@0: * sl@0: * Results: sl@0: * Updates stringPtr to point to the matching character, or to sl@0: * the end of the string if nothing matched. The return value sl@0: * is 1 if a match was found at the top level, otherwise it is 0. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static int sl@0: SkipToChar(stringPtr, match) sl@0: char **stringPtr; /* Pointer string to check. */ sl@0: char *match; /* Pointer to character to find. */ sl@0: { sl@0: int quoted, level; sl@0: register char *p; sl@0: sl@0: quoted = 0; sl@0: level = 0; sl@0: sl@0: for (p = *stringPtr; *p != '\0'; p++) { sl@0: if (quoted) { sl@0: quoted = 0; sl@0: continue; sl@0: } sl@0: if ((level == 0) && (*p == *match)) { sl@0: *stringPtr = p; sl@0: return 1; sl@0: } sl@0: if (*p == '{') { sl@0: level++; sl@0: } else if (*p == '}') { sl@0: level--; sl@0: } else if (*p == '\\') { sl@0: quoted = 1; sl@0: } sl@0: } sl@0: *stringPtr = p; sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclDoGlob -- sl@0: * sl@0: * This recursive procedure forms the heart of the globbing sl@0: * code. It performs a depth-first traversal of the tree sl@0: * given by the path name to be globbed. The directory and sl@0: * remainder are assumed to be native format paths. The prefix sl@0: * contained in 'headPtr' is not used as a glob pattern, simply sl@0: * as a path specifier, so it can contain unquoted glob-sensitive sl@0: * characters (if the directories to which it points contain sl@0: * such strange characters). sl@0: * sl@0: * Results: sl@0: * The return value is a standard Tcl result indicating whether sl@0: * an error occurred in globbing. After a normal return the sl@0: * result in interp will be set to hold all of the file names sl@0: * given by the dir and rem arguments. After an error the sl@0: * result in interp will hold an error message. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: TclDoGlob(interp, separators, headPtr, tail, types) sl@0: Tcl_Interp *interp; /* Interpreter to use for error reporting sl@0: * (e.g. unmatched brace). */ sl@0: char *separators; /* String containing separator characters sl@0: * that should be used to identify globbing sl@0: * boundaries. */ sl@0: Tcl_DString *headPtr; /* Completely expanded prefix. */ sl@0: char *tail; /* The unexpanded remainder of the path. sl@0: * Must not be a pointer to a static string. */ sl@0: Tcl_GlobTypeData *types; /* List object containing list of acceptable sl@0: * types. May be NULL. */ sl@0: { sl@0: int baseLength, quoted, count; sl@0: int result = TCL_OK; sl@0: char *name, *p, *openBrace, *closeBrace, *firstSpecialChar, savedChar; sl@0: char lastChar = 0; sl@0: sl@0: int length = Tcl_DStringLength(headPtr); sl@0: sl@0: if (length > 0) { sl@0: lastChar = Tcl_DStringValue(headPtr)[length-1]; sl@0: } sl@0: sl@0: /* sl@0: * Consume any leading directory separators, leaving tail pointing sl@0: * just past the last initial separator. sl@0: */ sl@0: sl@0: count = 0; sl@0: name = tail; sl@0: for (; *tail != '\0'; tail++) { sl@0: if (*tail == '\\') { sl@0: /* sl@0: * If the first character is escaped, either we have a directory sl@0: * separator, or we have any other character. In the latter case sl@0: * the rest of tail is a pattern, and we must break from the loop. sl@0: * This is particularly important on Windows where '\' is both sl@0: * the escaping character and a directory separator. sl@0: */ sl@0: if (strchr(separators, tail[1]) != NULL) { sl@0: tail++; sl@0: } else { sl@0: break; sl@0: } sl@0: } else if (strchr(separators, *tail) == NULL) { sl@0: break; sl@0: } sl@0: if (tclPlatform != TCL_PLATFORM_MAC) { sl@0: if (*tail == '\\') { sl@0: Tcl_DStringAppend(headPtr, separators, 1); sl@0: } else { sl@0: Tcl_DStringAppend(headPtr, tail, 1); sl@0: } sl@0: } sl@0: count++; sl@0: } sl@0: sl@0: /* sl@0: * Deal with path separators. On the Mac, we have to watch out sl@0: * for multiple separators, since they are special in Mac-style sl@0: * paths. sl@0: */ sl@0: sl@0: switch (tclPlatform) { sl@0: case TCL_PLATFORM_MAC: sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: if (*separators == '/') { sl@0: if (((length == 0) && (count == 0)) sl@0: || ((length > 0) && (lastChar != ':'))) { sl@0: Tcl_DStringAppend(headPtr, ":", 1); sl@0: } sl@0: } else { sl@0: #endif sl@0: if (count == 0) { sl@0: if ((length > 0) && (lastChar != ':')) { sl@0: Tcl_DStringAppend(headPtr, ":", 1); sl@0: } sl@0: } else { sl@0: if (lastChar == ':') { sl@0: count--; sl@0: } sl@0: while (count-- > 0) { sl@0: Tcl_DStringAppend(headPtr, ":", 1); sl@0: } sl@0: } sl@0: #ifdef MAC_UNDERSTANDS_UNIX_PATHS sl@0: } sl@0: #endif sl@0: break; sl@0: case TCL_PLATFORM_WINDOWS: sl@0: /* sl@0: * If this is a drive relative path, add the colon and the sl@0: * trailing slash if needed. Otherwise add the slash if sl@0: * this is the first absolute element, or a later relative sl@0: * element. Add an extra slash if this is a UNC path. sl@0: sl@0: if (*name == ':') { sl@0: Tcl_DStringAppend(headPtr, ":", 1); sl@0: if (count > 1) { sl@0: Tcl_DStringAppend(headPtr, "/", 1); sl@0: } sl@0: } else if ((*tail != '\0') sl@0: && (((length > 0) sl@0: && (strchr(separators, lastChar) == NULL)) sl@0: || ((length == 0) && (count > 0)))) { sl@0: Tcl_DStringAppend(headPtr, "/", 1); sl@0: if ((length == 0) && (count > 1)) { sl@0: Tcl_DStringAppend(headPtr, "/", 1); sl@0: } sl@0: } sl@0: */ sl@0: sl@0: break; sl@0: case TCL_PLATFORM_UNIX: { sl@0: /* sl@0: * Add a separator if this is the first absolute element, or sl@0: * a later relative element. sl@0: sl@0: if ((*tail != '\0') sl@0: && (((length > 0) sl@0: && (strchr(separators, lastChar) == NULL)) sl@0: || ((length == 0) && (count > 0)))) { sl@0: Tcl_DStringAppend(headPtr, "/", 1); sl@0: } sl@0: */ sl@0: break; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Look for the first matching pair of braces or the first sl@0: * directory separator that is not inside a pair of braces. sl@0: */ sl@0: sl@0: openBrace = closeBrace = NULL; sl@0: quoted = 0; sl@0: for (p = tail; *p != '\0'; p++) { sl@0: if (quoted) { sl@0: quoted = 0; sl@0: } else if (*p == '\\') { sl@0: quoted = 1; sl@0: if (strchr(separators, p[1]) != NULL) { sl@0: break; /* Quoted directory separator. */ sl@0: } sl@0: } else if (strchr(separators, *p) != NULL) { sl@0: break; /* Unquoted directory separator. */ sl@0: } else if (*p == '{') { sl@0: openBrace = p; sl@0: p++; sl@0: if (SkipToChar(&p, "}")) { sl@0: closeBrace = p; /* Balanced braces. */ sl@0: break; sl@0: } sl@0: Tcl_SetResult(interp, "unmatched open-brace in file name", sl@0: TCL_STATIC); sl@0: return TCL_ERROR; sl@0: } else if (*p == '}') { sl@0: Tcl_SetResult(interp, "unmatched close-brace in file name", sl@0: TCL_STATIC); sl@0: return TCL_ERROR; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Substitute the alternate patterns from the braces and recurse. sl@0: */ sl@0: sl@0: if (openBrace != NULL) { sl@0: char *element; sl@0: Tcl_DString newName; sl@0: Tcl_DStringInit(&newName); sl@0: sl@0: /* sl@0: * For each element within in the outermost pair of braces, sl@0: * append the element and the remainder to the fixed portion sl@0: * before the first brace and recursively call TclDoGlob. sl@0: */ sl@0: sl@0: Tcl_DStringAppend(&newName, tail, openBrace-tail); sl@0: baseLength = Tcl_DStringLength(&newName); sl@0: length = Tcl_DStringLength(headPtr); sl@0: *closeBrace = '\0'; sl@0: for (p = openBrace; p != closeBrace; ) { sl@0: p++; sl@0: element = p; sl@0: SkipToChar(&p, ","); sl@0: Tcl_DStringSetLength(headPtr, length); sl@0: Tcl_DStringSetLength(&newName, baseLength); sl@0: Tcl_DStringAppend(&newName, element, p-element); sl@0: Tcl_DStringAppend(&newName, closeBrace+1, -1); sl@0: result = TclDoGlob(interp, separators, headPtr, sl@0: Tcl_DStringValue(&newName), types); sl@0: if (result != TCL_OK) { sl@0: break; sl@0: } sl@0: } sl@0: *closeBrace = '}'; sl@0: Tcl_DStringFree(&newName); sl@0: return result; sl@0: } sl@0: sl@0: /* sl@0: * At this point, there are no more brace substitutions to perform on sl@0: * this path component. The variable p is pointing at a quoted or sl@0: * unquoted directory separator or the end of the string. So we need sl@0: * to check for special globbing characters in the current pattern. sl@0: * We avoid modifying tail if p is pointing at the end of the string. sl@0: */ sl@0: sl@0: if (*p != '\0') { sl@0: sl@0: /* sl@0: * Note that we are modifying the string in place. This won't work sl@0: * if the string is a static. sl@0: */ sl@0: sl@0: savedChar = *p; sl@0: *p = '\0'; sl@0: firstSpecialChar = strpbrk(tail, "*[]?\\"); sl@0: *p = savedChar; sl@0: } else { sl@0: firstSpecialChar = strpbrk(tail, "*[]?\\"); sl@0: } sl@0: sl@0: if (firstSpecialChar != NULL) { sl@0: int ret; sl@0: Tcl_Obj *head = Tcl_NewStringObj(Tcl_DStringValue(headPtr),-1); sl@0: Tcl_IncrRefCount(head); sl@0: /* sl@0: * Look for matching files in the given directory. The sl@0: * implementation of this function is platform specific. For sl@0: * each file that matches, it will add the match onto the sl@0: * resultPtr given. sl@0: */ sl@0: if (*p == '\0') { sl@0: ret = Tcl_FSMatchInDirectory(interp, Tcl_GetObjResult(interp), sl@0: head, tail, types); sl@0: } else { sl@0: /* sl@0: * We do the recursion ourselves. This makes implementing sl@0: * Tcl_FSMatchInDirectory for each filesystem much easier. sl@0: */ sl@0: Tcl_GlobTypeData dirOnly = { TCL_GLOB_TYPE_DIR, 0, NULL, NULL }; sl@0: char save = *p; sl@0: Tcl_Obj *resultPtr; sl@0: sl@0: resultPtr = Tcl_NewListObj(0, NULL); sl@0: Tcl_IncrRefCount(resultPtr); sl@0: *p = '\0'; sl@0: ret = Tcl_FSMatchInDirectory(interp, resultPtr, sl@0: head, tail, &dirOnly); sl@0: *p = save; sl@0: if (ret == TCL_OK) { sl@0: int resLength; sl@0: ret = Tcl_ListObjLength(interp, resultPtr, &resLength); sl@0: if (ret == TCL_OK) { sl@0: int i; sl@0: for (i =0; i< resLength; i++) { sl@0: Tcl_Obj *elt; sl@0: Tcl_DString ds; sl@0: Tcl_ListObjIndex(interp, resultPtr, i, &elt); sl@0: Tcl_DStringInit(&ds); sl@0: Tcl_DStringAppend(&ds, Tcl_GetString(elt), -1); sl@0: if(tclPlatform == TCL_PLATFORM_MAC) { sl@0: Tcl_DStringAppend(&ds, ":",1); sl@0: } else { sl@0: Tcl_DStringAppend(&ds, "/",1); sl@0: } sl@0: ret = TclDoGlob(interp, separators, &ds, p+1, types); sl@0: Tcl_DStringFree(&ds); sl@0: if (ret != TCL_OK) { sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: Tcl_DecrRefCount(resultPtr); sl@0: } sl@0: Tcl_DecrRefCount(head); sl@0: return ret; sl@0: } sl@0: Tcl_DStringAppend(headPtr, tail, p-tail); sl@0: if (*p != '\0') { sl@0: return TclDoGlob(interp, separators, headPtr, p, types); sl@0: } else { sl@0: /* sl@0: * This is the code path reached by a command like 'glob foo'. sl@0: * sl@0: * There are no more wildcards in the pattern and no more sl@0: * unprocessed characters in the tail, so now we can construct sl@0: * the path, and pass it to Tcl_FSMatchInDirectory with an sl@0: * empty pattern to verify the existence of the file and check sl@0: * it is of the correct type (if a 'types' flag it given -- if sl@0: * no such flag was given, we could just use 'Tcl_FSLStat', but sl@0: * for simplicity we keep to a common approach). sl@0: */ sl@0: sl@0: Tcl_Obj *nameObj; sl@0: sl@0: switch (tclPlatform) { sl@0: case TCL_PLATFORM_MAC: { sl@0: if (strchr(Tcl_DStringValue(headPtr), ':') == NULL) { sl@0: Tcl_DStringAppend(headPtr, ":", 1); sl@0: } sl@0: break; sl@0: } sl@0: case TCL_PLATFORM_WINDOWS: { sl@0: if (Tcl_DStringLength(headPtr) == 0) { sl@0: if (((*name == '\\') && (name[1] == '/' || name[1] == '\\')) sl@0: || (*name == '/')) { sl@0: Tcl_DStringAppend(headPtr, "/", 1); sl@0: } else { sl@0: Tcl_DStringAppend(headPtr, ".", 1); sl@0: } sl@0: } sl@0: #if defined(__CYGWIN__) && defined(__WIN32__) sl@0: { sl@0: extern int cygwin_conv_to_win32_path sl@0: _ANSI_ARGS_((CONST char *, char *)); sl@0: char winbuf[MAX_PATH+1]; sl@0: sl@0: cygwin_conv_to_win32_path(Tcl_DStringValue(headPtr), winbuf); sl@0: Tcl_DStringFree(headPtr); sl@0: Tcl_DStringAppend(headPtr, winbuf, -1); sl@0: } sl@0: #endif /* __CYGWIN__ && __WIN32__ */ sl@0: /* sl@0: * Convert to forward slashes. This is required to pass sl@0: * some Tcl tests. We should probably remove the conversions sl@0: * here and in tclWinFile.c, since they aren't needed since sl@0: * the dropping of support for Win32s. sl@0: */ sl@0: for (p = Tcl_DStringValue(headPtr); *p != '\0'; p++) { sl@0: if (*p == '\\') { sl@0: *p = '/'; sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: case TCL_PLATFORM_UNIX: { sl@0: if (Tcl_DStringLength(headPtr) == 0) { sl@0: if ((*name == '\\' && name[1] == '/') || (*name == '/')) { sl@0: Tcl_DStringAppend(headPtr, "/", 1); sl@0: } else { sl@0: Tcl_DStringAppend(headPtr, ".", 1); sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: } sl@0: /* Common for all platforms */ sl@0: name = Tcl_DStringValue(headPtr); sl@0: nameObj = Tcl_NewStringObj(name, Tcl_DStringLength(headPtr)); sl@0: sl@0: Tcl_IncrRefCount(nameObj); sl@0: Tcl_FSMatchInDirectory(interp, Tcl_GetObjResult(interp), nameObj, sl@0: NULL, types); sl@0: Tcl_DecrRefCount(nameObj); sl@0: return TCL_OK; sl@0: } sl@0: } sl@0: sl@0: sl@0: /* sl@0: *--------------------------------------------------------------------------- sl@0: * sl@0: * TclFileDirname sl@0: * sl@0: * This procedure calculates the directory above a given sl@0: * path: basically 'file dirname'. It is used both by sl@0: * the 'dirname' subcommand of file and by code in tclIOUtil.c. sl@0: * sl@0: * Results: sl@0: * NULL if an error occurred, otherwise a Tcl_Obj owned by sl@0: * the caller (i.e. most likely with refCount 1). sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *--------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: Tcl_Obj* sl@0: TclFileDirname(interp, pathPtr) sl@0: Tcl_Interp *interp; /* Used for error reporting */ sl@0: Tcl_Obj *pathPtr; /* Path to take dirname of */ sl@0: { sl@0: int splitElements; sl@0: Tcl_Obj *splitPtr; sl@0: Tcl_Obj *splitResultPtr = NULL; sl@0: sl@0: /* sl@0: * The behaviour we want here is slightly different to sl@0: * the standard Tcl_FSSplitPath in the handling of home sl@0: * directories; Tcl_FSSplitPath preserves the "~" while sl@0: * this code computes the actual full path name, if we sl@0: * had just a single component. sl@0: */ sl@0: splitPtr = Tcl_FSSplitPath(pathPtr, &splitElements); sl@0: if ((splitElements == 1) && (Tcl_GetString(pathPtr)[0] == '~')) { sl@0: Tcl_DecrRefCount(splitPtr); sl@0: splitPtr = Tcl_FSGetNormalizedPath(interp, pathPtr); sl@0: if (splitPtr == NULL) { sl@0: return NULL; sl@0: } sl@0: splitPtr = Tcl_FSSplitPath(splitPtr, &splitElements); sl@0: } sl@0: sl@0: /* sl@0: * Return all but the last component. If there is only one sl@0: * component, return it if the path was non-relative, otherwise sl@0: * return the current directory. sl@0: */ sl@0: sl@0: if (splitElements > 1) { sl@0: splitResultPtr = Tcl_FSJoinPath(splitPtr, splitElements - 1); sl@0: } else if (splitElements == 0 || sl@0: (Tcl_FSGetPathType(pathPtr) == TCL_PATH_RELATIVE)) { sl@0: splitResultPtr = Tcl_NewStringObj( sl@0: ((tclPlatform == TCL_PLATFORM_MAC) ? ":" : "."), 1); sl@0: } else { sl@0: Tcl_ListObjIndex(NULL, splitPtr, 0, &splitResultPtr); sl@0: } sl@0: Tcl_IncrRefCount(splitResultPtr); sl@0: Tcl_DecrRefCount(splitPtr); sl@0: return splitResultPtr; sl@0: } sl@0: sl@0: /* sl@0: *--------------------------------------------------------------------------- sl@0: * sl@0: * Tcl_AllocStatBuf sl@0: * sl@0: * This procedure allocates a Tcl_StatBuf on the heap. It exists sl@0: * so that extensions may be used unchanged on systems where sl@0: * largefile support is optional. sl@0: * sl@0: * Results: sl@0: * A pointer to a Tcl_StatBuf which may be deallocated by being sl@0: * passed to ckfree(). sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *--------------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C Tcl_StatBuf * sl@0: Tcl_AllocStatBuf() { sl@0: return (Tcl_StatBuf *) ckalloc(sizeof(Tcl_StatBuf)); sl@0: }