4 * This file contains routines for converting file names betwen
5 * native and network form.
7 * Copyright (c) 1995-1996 Sun Microsystems, Inc.
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17 #include "tclRegexp.h"
20 * This variable indicates whether the cleanup procedure has been
21 * registered for this file yet.
24 static int initialized = 0;
27 * The following regular expression matches the root portion of a Windows
28 * absolute or volume relative path. It will match both UNC and drive relative
32 #define WIN_ROOT_PATTERN "^(([a-zA-Z]:)|[/\\][/\\]+([^/\\]+)[/\\]+([^/\\]+)|([/\\]))([/\\])*"
35 * The following regular expression matches the root portion of a Macintosh
36 * absolute path. It will match degenerate Unix-style paths, tilde paths,
37 * Unix-style paths, and Mac paths.
40 #define MAC_ROOT_PATTERN "^((/+([.][.]?/+)*([.][.]?)?)|(~[^:/]*)(/[^:]*)?|(~[^:]*)(:.*)?|/+([.][.]?/+)*([^:/]+)(/[^:]*)?|([^:]+):.*)$"
43 * The following variables are used to hold precompiled regular expressions
44 * for use in filename matching.
47 static regexp *winRootPatternPtr = NULL;
48 static regexp *macRootPatternPtr = NULL;
51 * The following variable is set in the TclPlatformInit call to one
52 * of: TCL_PLATFORM_UNIX, TCL_PLATFORM_MAC, or TCL_PLATFORM_WINDOWS.
55 TclPlatformType tclPlatform = TCL_PLATFORM_UNIX;
58 * Prototypes for local procedures defined in this file:
61 static char * DoTildeSubst _ANSI_ARGS_((Tcl_Interp *interp,
62 char *user, Tcl_DString *resultPtr));
63 static char * ExtractWinRoot _ANSI_ARGS_((char *path,
64 Tcl_DString *resultPtr, int offset));
65 static void FileNameCleanup _ANSI_ARGS_((ClientData clientData));
66 static int SkipToChar _ANSI_ARGS_((char **stringPtr,
68 static char * SplitMacPath _ANSI_ARGS_((char *path,
69 Tcl_DString *bufPtr));
70 static char * SplitWinPath _ANSI_ARGS_((char *path,
71 Tcl_DString *bufPtr));
72 static char * SplitUnixPath _ANSI_ARGS_((char *path,
73 Tcl_DString *bufPtr));
76 *----------------------------------------------------------------------
80 * This procedure is a Tcl_ExitProc used to clean up the static
81 * data structures used in this file.
87 * Deallocates storage used by the procedures in this file.
89 *----------------------------------------------------------------------
93 FileNameCleanup(clientData)
94 ClientData clientData; /* Not used. */
96 if (winRootPatternPtr != NULL) {
97 ckfree((char *)winRootPatternPtr);
98 winRootPatternPtr = (regexp *) NULL;
100 if (macRootPatternPtr != NULL) {
101 ckfree((char *)macRootPatternPtr);
102 macRootPatternPtr = (regexp *) NULL;
108 *----------------------------------------------------------------------
112 * Matches the root portion of a Windows path and appends it
113 * to the specified Tcl_DString.
116 * Returns the position in the path immediately after the root
117 * including any trailing slashes.
118 * Appends a cleaned up version of the root to the Tcl_DString
119 * at the specified offest.
122 * Modifies the specified Tcl_DString.
124 *----------------------------------------------------------------------
128 ExtractWinRoot(path, resultPtr, offset)
129 char *path; /* Path to parse. */
130 Tcl_DString *resultPtr; /* Buffer to hold result. */
131 int offset; /* Offset in buffer where result should be
137 * Initialize the path name parser for Windows path names.
140 if (winRootPatternPtr == NULL) {
141 winRootPatternPtr = TclRegComp(WIN_ROOT_PATTERN);
143 Tcl_CreateExitHandler(FileNameCleanup, NULL);
149 * Match the root portion of a Windows path name.
152 if (!TclRegExec(winRootPatternPtr, path, path)) {
156 Tcl_DStringSetLength(resultPtr, offset);
158 if (winRootPatternPtr->startp[2] != NULL) {
159 Tcl_DStringAppend(resultPtr, winRootPatternPtr->startp[2], 2);
160 if (winRootPatternPtr->startp[6] != NULL) {
161 Tcl_DStringAppend(resultPtr, "/", 1);
163 } else if (winRootPatternPtr->startp[4] != NULL) {
164 Tcl_DStringAppend(resultPtr, "//", 2);
165 length = winRootPatternPtr->endp[3]
166 - winRootPatternPtr->startp[3];
167 Tcl_DStringAppend(resultPtr, winRootPatternPtr->startp[3], length);
168 Tcl_DStringAppend(resultPtr, "/", 1);
169 length = winRootPatternPtr->endp[4]
170 - winRootPatternPtr->startp[4];
171 Tcl_DStringAppend(resultPtr, winRootPatternPtr->startp[4], length);
173 Tcl_DStringAppend(resultPtr, "/", 1);
175 return winRootPatternPtr->endp[0];
179 *----------------------------------------------------------------------
183 * Determines whether a given path is relative to the current
184 * directory, relative to the current volume, or absolute.
187 * Returns one of TCL_PATH_ABSOLUTE, TCL_PATH_RELATIVE, or
188 * TCL_PATH_VOLUME_RELATIVE.
193 *----------------------------------------------------------------------
197 Tcl_GetPathType(path)
200 Tcl_PathType type = TCL_PATH_ABSOLUTE;
202 switch (tclPlatform) {
203 case TCL_PLATFORM_UNIX:
205 * Paths that begin with / or ~ are absolute.
208 if ((path[0] != '/') && (path[0] != '~')) {
209 type = TCL_PATH_RELATIVE;
213 case TCL_PLATFORM_MAC:
214 if (path[0] == ':') {
215 type = TCL_PATH_RELATIVE;
216 } else if (path[0] != '~') {
219 * Since we have eliminated the easy cases, use the
220 * root pattern to look for the other types.
223 if (!macRootPatternPtr) {
224 macRootPatternPtr = TclRegComp(MAC_ROOT_PATTERN);
226 Tcl_CreateExitHandler(FileNameCleanup, NULL);
230 if (!TclRegExec(macRootPatternPtr, path, path)
231 || (macRootPatternPtr->startp[2] != NULL)) {
232 type = TCL_PATH_RELATIVE;
237 case TCL_PLATFORM_WINDOWS:
238 if (path[0] != '~') {
241 * Since we have eliminated the easy cases, check for
242 * drive relative paths using the regular expression.
245 if (!winRootPatternPtr) {
246 winRootPatternPtr = TclRegComp(WIN_ROOT_PATTERN);
248 Tcl_CreateExitHandler(FileNameCleanup, NULL);
252 if (TclRegExec(winRootPatternPtr, path, path)) {
253 if (winRootPatternPtr->startp[5]
254 || (winRootPatternPtr->startp[2]
255 && !(winRootPatternPtr->startp[6]))) {
256 type = TCL_PATH_VOLUME_RELATIVE;
259 type = TCL_PATH_RELATIVE;
268 *----------------------------------------------------------------------
272 * Split a path into a list of path components. The first element
273 * of the list will have the same path type as the original path.
276 * Returns a standard Tcl result. The interpreter result contains
277 * a list of path components.
278 * *argvPtr will be filled in with the address of an array
279 * whose elements point to the elements of path, in order.
280 * *argcPtr will get filled in with the number of valid elements
281 * in the array. A single block of memory is dynamically allocated
282 * to hold both the argv array and a copy of the path elements.
283 * The caller must eventually free this memory by calling ckfree()
284 * on *argvPtr. Note: *argvPtr and *argcPtr are only modified
285 * if the procedure returns normally.
290 *----------------------------------------------------------------------
294 Tcl_SplitPath(path, argcPtr, argvPtr)
295 char *path; /* Pointer to string containing a path. */
296 int *argcPtr; /* Pointer to location to fill in with
297 * the number of elements in the path. */
298 char ***argvPtr; /* Pointer to place to store pointer to array
299 * of pointers to path elements. */
304 Tcl_DStringInit(&buffer);
307 * Perform platform specific splitting. These routines will leave the
308 * result in the specified buffer. Individual elements are terminated
309 * with a null character.
312 p = NULL; /* Needed only to prevent gcc warnings. */
313 switch (tclPlatform) {
314 case TCL_PLATFORM_UNIX:
315 p = SplitUnixPath(path, &buffer);
318 case TCL_PLATFORM_WINDOWS:
319 p = SplitWinPath(path, &buffer);
322 case TCL_PLATFORM_MAC:
323 p = SplitMacPath(path, &buffer);
328 * Compute the number of elements in the result.
331 size = Tcl_DStringLength(&buffer);
333 for (i = 0; i < size; i++) {
340 * Allocate a buffer large enough to hold the contents of the
341 * DString plus the argv pointers and the terminating NULL pointer.
344 *argvPtr = (char **) ckalloc((unsigned)
345 ((((*argcPtr) + 1) * sizeof(char *)) + size));
348 * Position p after the last argv pointer and copy the contents of
352 p = (char *) &(*argvPtr)[(*argcPtr) + 1];
353 memcpy((VOID *) p, (VOID *) Tcl_DStringValue(&buffer), (size_t) size);
356 * Now set up the argv pointers.
359 for (i = 0; i < *argcPtr; i++) {
361 while ((*p++) != '\0') {}
363 (*argvPtr)[i] = NULL;
365 Tcl_DStringFree(&buffer);
369 *----------------------------------------------------------------------
373 * This routine is used by Tcl_SplitPath to handle splitting
377 * Stores a null separated array of strings in the specified
383 *----------------------------------------------------------------------
387 SplitUnixPath(path, bufPtr)
388 char *path; /* Pointer to string containing a path. */
389 Tcl_DString *bufPtr; /* Pointer to DString to use for the result. */
392 char *p, *elementStart;
395 * Deal with the root directory as a special case.
398 if (path[0] == '/') {
399 Tcl_DStringAppend(bufPtr, "/", 2);
406 * Split on slashes. Embedded elements that start with tilde will be
407 * prefixed with "./" so they are not affected by tilde substitution.
412 while ((*p != '\0') && (*p != '/')) {
415 length = p - elementStart;
417 if ((elementStart[0] == '~') && (elementStart != path)) {
418 Tcl_DStringAppend(bufPtr, "./", 2);
420 Tcl_DStringAppend(bufPtr, elementStart, length);
421 Tcl_DStringAppend(bufPtr, "", 1);
427 return Tcl_DStringValue(bufPtr);
431 *----------------------------------------------------------------------
435 * This routine is used by Tcl_SplitPath to handle splitting
439 * Stores a null separated array of strings in the specified
445 *----------------------------------------------------------------------
449 SplitWinPath(path, bufPtr)
450 char *path; /* Pointer to string containing a path. */
451 Tcl_DString *bufPtr; /* Pointer to DString to use for the result. */
454 char *p, *elementStart;
456 p = ExtractWinRoot(path, bufPtr, 0);
459 * Terminate the root portion, if we matched something.
463 Tcl_DStringAppend(bufPtr, "", 1);
467 * Split on slashes. Embedded elements that start with tilde will be
468 * prefixed with "./" so they are not affected by tilde substitution.
473 while ((*p != '\0') && (*p != '/') && (*p != '\\')) {
476 length = p - elementStart;
478 if ((elementStart[0] == '~') && (elementStart != path)) {
479 Tcl_DStringAppend(bufPtr, "./", 2);
481 Tcl_DStringAppend(bufPtr, elementStart, length);
482 Tcl_DStringAppend(bufPtr, "", 1);
484 } while (*p++ != '\0');
486 return Tcl_DStringValue(bufPtr);
490 *----------------------------------------------------------------------
494 * This routine is used by Tcl_SplitPath to handle splitting
498 * Returns a newly allocated argv array.
503 *----------------------------------------------------------------------
507 SplitMacPath(path, bufPtr)
508 char *path; /* Pointer to string containing a path. */
509 Tcl_DString *bufPtr; /* Pointer to DString to use for the result. */
511 int isMac = 0; /* 1 if is Mac-style, 0 if Unix-style path. */
513 char *p, *elementStart;
516 * Initialize the path name parser for Macintosh path names.
519 if (macRootPatternPtr == NULL) {
520 macRootPatternPtr = TclRegComp(MAC_ROOT_PATTERN);
522 Tcl_CreateExitHandler(FileNameCleanup, NULL);
528 * Match the root portion of a Mac path name.
531 i = 0; /* Needed only to prevent gcc warnings. */
532 if (TclRegExec(macRootPatternPtr, path, path) == 1) {
534 * Treat degenerate absolute paths like / and /../.. as
535 * Mac relative file names for lack of anything else to do.
538 if (macRootPatternPtr->startp[2] != NULL) {
539 Tcl_DStringAppend(bufPtr, ":", 1);
540 Tcl_DStringAppend(bufPtr, path, macRootPatternPtr->endp[0]
541 - macRootPatternPtr->startp[0] + 1);
542 return Tcl_DStringValue(bufPtr);
545 if (macRootPatternPtr->startp[5] != NULL) {
548 * Unix-style tilde prefixed paths.
553 } else if (macRootPatternPtr->startp[7] != NULL) {
556 * Mac-style tilde prefixed paths.
561 } else if (macRootPatternPtr->startp[10] != NULL) {
564 * Normal Unix style paths.
569 } else if (macRootPatternPtr->startp[12] != NULL) {
572 * Normal Mac style paths.
579 length = macRootPatternPtr->endp[i]
580 - macRootPatternPtr->startp[i];
583 * Append the element and terminate it with a : and a null. Note that
584 * we are forcing the DString to contain an extra null at the end.
587 Tcl_DStringAppend(bufPtr, macRootPatternPtr->startp[i], length);
588 Tcl_DStringAppend(bufPtr, ":", 2);
589 p = macRootPatternPtr->endp[i];
591 isMac = (strchr(path, ':') != NULL);
598 * p is pointing at the first colon in the path. There
599 * will always be one, since this is a Mac-style path.
603 while ((p = strchr(p, ':')) != NULL) {
604 length = p - elementStart;
607 Tcl_DStringAppend(bufPtr, "::", 3);
612 * If this is a simple component, drop the leading colon.
615 if ((elementStart[1] != '~')
616 && (strchr(elementStart+1, '/') == NULL)) {
620 Tcl_DStringAppend(bufPtr, elementStart, length);
621 Tcl_DStringAppend(bufPtr, "", 1);
625 if (elementStart[1] != '\0' || elementStart == path) {
626 if ((elementStart[1] != '~') && (elementStart[1] != '\0')
627 && (strchr(elementStart+1, '/') == NULL)) {
630 Tcl_DStringAppend(bufPtr, elementStart, -1);
631 Tcl_DStringAppend(bufPtr, "", 1);
636 * Split on slashes, suppress extra /'s, and convert .. to ::.
641 while ((*p != '\0') && (*p != '/')) {
644 length = p - elementStart;
646 if ((length == 1) && (elementStart[0] == '.')) {
647 Tcl_DStringAppend(bufPtr, ":", 2);
648 } else if ((length == 2) && (elementStart[0] == '.')
649 && (elementStart[1] == '.')) {
650 Tcl_DStringAppend(bufPtr, "::", 3);
652 if (*elementStart == '~') {
653 Tcl_DStringAppend(bufPtr, ":", 1);
655 Tcl_DStringAppend(bufPtr, elementStart, length);
656 Tcl_DStringAppend(bufPtr, "", 1);
664 return Tcl_DStringValue(bufPtr);
668 *----------------------------------------------------------------------
672 * Combine a list of paths in a platform specific manner.
675 * Appends the joined path to the end of the specified
676 * returning a pointer to the resulting string. Note that
677 * the Tcl_DString must already be initialized.
680 * Modifies the Tcl_DString.
682 *----------------------------------------------------------------------
686 Tcl_JoinPath(argc, argv, resultPtr)
689 Tcl_DString *resultPtr; /* Pointer to previously initialized DString. */
691 int oldLength, length, i, needsSep;
695 Tcl_DStringInit(&buffer);
696 oldLength = Tcl_DStringLength(resultPtr);
698 switch (tclPlatform) {
699 case TCL_PLATFORM_UNIX:
700 for (i = 0; i < argc; i++) {
703 * If the path is absolute, reset the result buffer.
704 * Consume any duplicate leading slashes or a ./ in
705 * front of a tilde prefixed path that isn't at the
706 * beginning of the path.
710 Tcl_DStringSetLength(resultPtr, oldLength);
711 Tcl_DStringAppend(resultPtr, "/", 1);
715 } else if (*p == '~') {
716 Tcl_DStringSetLength(resultPtr, oldLength);
717 } else if ((Tcl_DStringLength(resultPtr) != oldLength)
718 && (p[0] == '.') && (p[1] == '/')
728 * Append a separator if needed.
731 length = Tcl_DStringLength(resultPtr);
732 if ((length != oldLength)
733 && (Tcl_DStringValue(resultPtr)[length-1] != '/')) {
734 Tcl_DStringAppend(resultPtr, "/", 1);
739 * Append the element, eliminating duplicate and trailing
743 Tcl_DStringSetLength(resultPtr, (int) (length + strlen(p)));
744 dest = Tcl_DStringValue(resultPtr) + length;
745 for (; *p != '\0'; p++) {
747 while (p[1] == '/') {
757 length = dest - Tcl_DStringValue(resultPtr);
758 Tcl_DStringSetLength(resultPtr, length);
762 case TCL_PLATFORM_WINDOWS:
764 * Iterate over all of the components. If a component is
765 * absolute, then reset the result and start building the
766 * path from the current component on.
769 for (i = 0; i < argc; i++) {
770 p = ExtractWinRoot(argv[i], resultPtr, oldLength);
771 length = Tcl_DStringLength(resultPtr);
774 * If the pointer didn't move, then this is a relative path
775 * or a tilde prefixed path.
780 * Remove the ./ from tilde prefixed elements unless
781 * it is the first component.
784 if ((length != oldLength)
786 && ((p[1] == '/') || (p[1] == '\\'))
789 } else if (*p == '~') {
790 Tcl_DStringSetLength(resultPtr, oldLength);
797 * Check to see if we need to append a separator.
801 if (length != oldLength) {
802 c = Tcl_DStringValue(resultPtr)[length-1];
803 if ((c != '/') && (c != ':')) {
804 Tcl_DStringAppend(resultPtr, "/", 1);
809 * Append the element, eliminating duplicate and
813 length = Tcl_DStringLength(resultPtr);
814 Tcl_DStringSetLength(resultPtr, (int) (length + strlen(p)));
815 dest = Tcl_DStringValue(resultPtr) + length;
816 for (; *p != '\0'; p++) {
817 if ((*p == '/') || (*p == '\\')) {
818 while ((p[1] == '/') || (p[1] == '\\')) {
828 length = dest - Tcl_DStringValue(resultPtr);
829 Tcl_DStringSetLength(resultPtr, length);
834 case TCL_PLATFORM_MAC:
836 for (i = 0; i < argc; i++) {
837 Tcl_DStringSetLength(&buffer, 0);
838 p = SplitMacPath(argv[i], &buffer);
839 if ((*p != ':') && (*p != '\0')
840 && (strchr(p, ':') != NULL)) {
841 Tcl_DStringSetLength(resultPtr, oldLength);
843 Tcl_DStringAppend(resultPtr, p, length);
849 * Now append the rest of the path elements, skipping
850 * : unless it is the first element of the path, and
851 * watching out for :: et al. so we don't end up with
852 * too many colons in the result.
855 for (; *p != '\0'; p += length+1) {
856 if (p[0] == ':' && p[1] == '\0') {
857 if (Tcl_DStringLength(resultPtr) != oldLength) {
870 Tcl_DStringAppend(resultPtr, ":", 1);
873 needsSep = (c == ':') ? 0 : 1;
876 Tcl_DStringAppend(resultPtr, p, length);
882 Tcl_DStringFree(&buffer);
883 return Tcl_DStringValue(resultPtr);
887 *----------------------------------------------------------------------
889 * Tcl_TranslateFileName --
891 * Converts a file name into a form usable by the native system
892 * interfaces. If the name starts with a tilde, it will produce
893 * a name where the tilde and following characters have been
894 * replaced by the home directory location for the named user.
897 * The result is a pointer to a static string containing
898 * the new name. If there was an error in processing the
899 * name, then an error message is left in interp->result
900 * and the return value is NULL. The result will be stored
901 * in bufferPtr; the caller must call Tcl_DStringFree(bufferPtr)
902 * to free the name if the return value was not NULL.
905 * Information may be left in bufferPtr.
907 *----------------------------------------------------------------------
911 Tcl_TranslateFileName(interp, name, bufferPtr)
912 Tcl_Interp *interp; /* Interpreter in which to store error
913 * message (if necessary). */
914 char *name; /* File name, which may begin with "~"
915 * (to indicate current user's home directory)
916 * or "~<user>" (to indicate any user's
917 * home directory). */
918 Tcl_DString *bufferPtr; /* May be used to hold result. Must not hold
919 * anything at the time of the call, and need
920 * not even be initialized. */
925 * Handle tilde substitutions, if needed.
928 if (name[0] == '~') {
933 Tcl_SplitPath(name, &argc, &argv);
936 * Strip the trailing ':' off of a Mac path
937 * before passing the user name to DoTildeSubst.
940 if (tclPlatform == TCL_PLATFORM_MAC) {
941 length = strlen(argv[0]);
942 argv[0][length-1] = '\0';
945 Tcl_DStringInit(&temp);
946 argv[0] = DoTildeSubst(interp, argv[0]+1, &temp);
947 if (argv[0] == NULL) {
948 Tcl_DStringFree(&temp);
949 ckfree((char *)argv);
952 Tcl_DStringInit(bufferPtr);
953 Tcl_JoinPath(argc, argv, bufferPtr);
954 Tcl_DStringFree(&temp);
957 Tcl_DStringInit(bufferPtr);
958 Tcl_JoinPath(1, &name, bufferPtr);
962 * Convert forward slashes to backslashes in Windows paths because
963 * some system interfaces don't accept forward slashes.
967 if (tclPlatform == TCL_PLATFORM_WINDOWS) {
968 for (p = Tcl_DStringValue(bufferPtr); *p != '\0'; p++) {
975 return Tcl_DStringValue(bufferPtr);
979 *----------------------------------------------------------------------
983 * This function returns a pointer to the beginning of the
984 * extension part of a file name.
987 * Returns a pointer into name which indicates where the extension
988 * starts. If there is no extension, returns NULL.
993 *----------------------------------------------------------------------
997 TclGetExtension(name)
998 char *name; /* File name to parse. */
1003 * First find the last directory separator.
1006 lastSep = NULL; /* Needed only to prevent gcc warnings. */
1007 switch (tclPlatform) {
1008 case TCL_PLATFORM_UNIX:
1009 lastSep = strrchr(name, '/');
1012 case TCL_PLATFORM_MAC:
1013 if (strchr(name, ':') == NULL) {
1014 lastSep = strrchr(name, '/');
1016 lastSep = strrchr(name, ':');
1020 case TCL_PLATFORM_WINDOWS:
1022 for (p = name; *p != '\0'; p++) {
1023 if (strchr("/\\:", *p) != NULL) {
1029 p = strrchr(name, '.');
1030 if ((p != NULL) && (lastSep != NULL)
1036 * Back up to the first period in a series of contiguous dots.
1037 * This is needed so foo..o will be split on the first dot.
1041 while ((p > name) && *(p-1) == '.') {
1049 *----------------------------------------------------------------------
1053 * Given a string following a tilde, this routine returns the
1054 * corresponding home directory.
1057 * The result is a pointer to a static string containing the home
1058 * directory in native format. If there was an error in processing
1059 * the substitution, then an error message is left in interp->result
1060 * and the return value is NULL. On success, the results are appended
1061 * to resultPtr, and the contents of resultPtr are returned.
1064 * Information may be left in resultPtr.
1066 *----------------------------------------------------------------------
1070 DoTildeSubst(interp, user, resultPtr)
1071 Tcl_Interp *interp; /* Interpreter in which to store error
1072 * message (if necessary). */
1073 char *user; /* Name of user whose home directory should be
1074 * substituted, or "" for current user. */
1075 Tcl_DString *resultPtr; /* May be used to hold result. Must not hold
1076 * anything at the time of the call, and need
1077 * not even be initialized. */
1081 if (*user == '\0') {
1082 dir = TclGetEnv("HOME");
1085 Tcl_ResetResult(interp);
1086 Tcl_AppendResult(interp, "couldn't find HOME environment ",
1087 "variable to expand path", (char *) NULL);
1091 Tcl_JoinPath(1, &dir, resultPtr);
1094 /* lint, TclGetuserHome() always NULL under windows. */
1095 if (TclGetUserHome(user, resultPtr) == NULL) {
1097 Tcl_ResetResult(interp);
1098 Tcl_AppendResult(interp, "user \"", user, "\" doesn't exist",
1104 return resultPtr->string;
1108 *----------------------------------------------------------------------
1112 * This procedure is invoked to process the "glob" Tcl command.
1113 * See the user documentation for details on what it does.
1116 * A standard Tcl result.
1119 * See the user documentation.
1121 *----------------------------------------------------------------------
1126 Tcl_GlobCmd(dummy, interp, argc, argv)
1127 ClientData dummy; /* Not used. */
1128 Tcl_Interp *interp; /* Current interpreter. */
1129 int argc; /* Number of arguments. */
1130 char **argv; /* Argument strings. */
1132 int i, noComplain, firstArg;
1134 int result = TCL_OK;
1136 char *separators, *head, *tail;
1139 for (firstArg = 1; (firstArg < argc) && (argv[firstArg][0] == '-');
1141 if (strcmp(argv[firstArg], "-nocomplain") == 0) {
1143 } else if (strcmp(argv[firstArg], "--") == 0) {
1147 Tcl_AppendResult(interp, "bad switch \"", argv[firstArg],
1148 "\": must be -nocomplain or --", (char *) NULL);
1152 if (firstArg >= argc) {
1153 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1154 " ?switches? name ?name ...?\"", (char *) NULL);
1158 Tcl_DStringInit(&buffer);
1159 separators = NULL; /* Needed only to prevent gcc warnings. */
1160 for (i = firstArg; i < argc; i++) {
1161 switch (tclPlatform) {
1162 case TCL_PLATFORM_UNIX:
1165 case TCL_PLATFORM_WINDOWS:
1166 separators = "/\\:";
1168 case TCL_PLATFORM_MAC:
1169 separators = (strchr(argv[i], ':') == NULL) ? "/" : ":";
1173 Tcl_DStringSetLength(&buffer, 0);
1176 * Perform tilde substitution, if needed.
1179 if (argv[i][0] == '~') {
1183 * Find the first path separator after the tilde.
1186 for (tail = argv[i]; *tail != '\0'; tail++) {
1187 if (*tail == '\\') {
1188 if (strchr(separators, tail[1]) != NULL) {
1191 } else if (strchr(separators, *tail) != NULL) {
1197 * Determine the home directory for the specified user. Note that
1198 * we don't allow special characters in the user name.
1203 p = strpbrk(argv[i]+1, "\\[]*?{}");
1205 head = DoTildeSubst(interp, argv[i]+1, &buffer);
1208 Tcl_ResetResult(interp);
1209 Tcl_AppendResult(interp, "globbing characters not ",
1210 "supported in user names", (char *) NULL);
1217 Tcl_ResetResult(interp);
1224 if (head != Tcl_DStringValue(&buffer)) {
1225 Tcl_DStringAppend(&buffer, head, -1);
1231 result = TclDoGlob(interp, separators, &buffer, tail);
1232 if (result != TCL_OK) {
1235 * We should in fact pass down the nocomplain flag
1236 * or save the interp result or use another mecanism
1237 * so the interp result is not mangled on errors in that case.
1238 * but that would a bigger change than reasonable for a patch
1240 * (see fileName.test 15.2-15.4 for expected behaviour)
1242 Tcl_ResetResult(interp);
1251 if ((*interp->result == 0) && !noComplain) {
1254 Tcl_AppendResult(interp, "no files matched glob pattern",
1255 (argc == 2) ? " \"" : "s \"", (char *) NULL);
1256 for (i = firstArg; i < argc; i++) {
1257 Tcl_AppendResult(interp, sep, argv[i], (char *) NULL);
1260 Tcl_AppendResult(interp, "\"", (char *) NULL);
1264 Tcl_DStringFree(&buffer);
1269 *----------------------------------------------------------------------
1273 * This function traverses a glob pattern looking for the next
1274 * unquoted occurance of the specified character at the same braces
1278 * Updates stringPtr to point to the matching character, or to
1279 * the end of the string if nothing matched. The return value
1280 * is 1 if a match was found at the top level, otherwise it is 0.
1285 *----------------------------------------------------------------------
1289 SkipToChar(stringPtr, match)
1290 char **stringPtr; /* Pointer string to check. */
1291 char *match; /* Pointer to character to find. */
1299 for (p = *stringPtr; *p != '\0'; p++) {
1304 if ((level == 0) && (*p == *match)) {
1310 } else if (*p == '}') {
1312 } else if (*p == '\\') {
1321 *----------------------------------------------------------------------
1325 * This recursive procedure forms the heart of the globbing
1326 * code. It performs a depth-first traversal of the tree
1327 * given by the path name to be globbed. The directory and
1328 * remainder are assumed to be native format paths.
1331 * The return value is a standard Tcl result indicating whether
1332 * an error occurred in globbing. After a normal return the
1333 * result in interp will be set to hold all of the file names
1334 * given by the dir and rem arguments. After an error the
1335 * result in interp will hold an error message.
1340 *----------------------------------------------------------------------
1344 TclDoGlob(interp, separators, headPtr, tail)
1345 Tcl_Interp *interp; /* Interpreter to use for error reporting
1346 * (e.g. unmatched brace). */
1347 char *separators; /* String containing separator characters
1348 * that should be used to identify globbing
1350 Tcl_DString *headPtr; /* Completely expanded prefix. */
1351 char *tail; /* The unexpanded remainder of the path. */
1353 int baseLength, quoted, count;
1354 int result = TCL_OK;
1355 char *p, *openBrace, *closeBrace, *name, *firstSpecialChar, savedChar;
1357 int length = Tcl_DStringLength(headPtr);
1360 lastChar = Tcl_DStringValue(headPtr)[length-1];
1364 * Consume any leading directory separators, leaving tail pointing
1365 * just past the last initial separator.
1370 for (; *tail != '\0'; tail++) {
1371 if ((*tail == '\\') && (strchr(separators, tail[1]) != NULL)) {
1373 } else if (strchr(separators, *tail) == NULL) {
1380 * Deal with path separators. On the Mac, we have to watch out
1381 * for multiple separators, since they are special in Mac-style
1385 switch (tclPlatform) {
1386 case TCL_PLATFORM_MAC:
1387 if (*separators == '/') {
1388 if (((length == 0) && (count == 0))
1389 || ((length > 0) && (lastChar != ':'))) {
1390 Tcl_DStringAppend(headPtr, ":", 1);
1394 if ((length > 0) && (lastChar != ':')) {
1395 Tcl_DStringAppend(headPtr, ":", 1);
1398 if (lastChar == ':') {
1401 while (count-- > 0) {
1402 Tcl_DStringAppend(headPtr, ":", 1);
1407 case TCL_PLATFORM_WINDOWS:
1409 * If this is a drive relative path, add the colon and the
1410 * trailing slash if needed. Otherwise add the slash if
1411 * this is the first absolute element, or a later relative
1412 * element. Add an extra slash if this is a UNC path.
1416 Tcl_DStringAppend(headPtr, ":", 1);
1418 Tcl_DStringAppend(headPtr, "/", 1);
1420 } else if ((*tail != '\0')
1422 && (strchr(separators, lastChar) == NULL))
1423 || ((length == 0) && (count > 0)))) {
1424 Tcl_DStringAppend(headPtr, "/", 1);
1425 if ((length == 0) && (count > 1)) {
1426 Tcl_DStringAppend(headPtr, "/", 1);
1431 case TCL_PLATFORM_UNIX:
1433 * Add a separator if this is the first absolute element, or
1434 * a later relative element.
1439 && (strchr(separators, lastChar) == NULL))
1440 || ((length == 0) && (count > 0)))) {
1441 Tcl_DStringAppend(headPtr, "/", 1);
1447 * Look for the first matching pair of braces or the first
1448 * directory separator that is not inside a pair of braces.
1451 openBrace = closeBrace = NULL;
1453 for (p = tail; *p != '\0'; p++) {
1456 } else if (*p == '\\') {
1458 if (strchr(separators, p[1]) != NULL) {
1459 break; /* Quoted directory separator. */
1461 } else if (strchr(separators, *p) != NULL) {
1462 break; /* Unquoted directory separator. */
1463 } else if (*p == '{') {
1466 if (SkipToChar(&p, "}")) {
1467 closeBrace = p; /* Balanced braces. */
1470 Tcl_SetResult(interp, "unmatched open-brace in file name",
1473 } else if (*p == '}') {
1474 Tcl_SetResult(interp, "unmatched close-brace in file name",
1481 * Substitute the alternate patterns from the braces and recurse.
1484 if (openBrace != NULL) {
1486 Tcl_DString newName;
1487 Tcl_DStringInit(&newName);
1490 * For each element within in the outermost pair of braces,
1491 * append the element and the remainder to the fixed portion
1492 * before the first brace and recursively call TclDoGlob.
1495 Tcl_DStringAppend(&newName, tail, openBrace-tail);
1496 baseLength = Tcl_DStringLength(&newName);
1497 length = Tcl_DStringLength(headPtr);
1499 for (p = openBrace; p != closeBrace; ) {
1502 SkipToChar(&p, ",");
1503 Tcl_DStringSetLength(headPtr, length);
1504 Tcl_DStringSetLength(&newName, baseLength);
1505 Tcl_DStringAppend(&newName, element, p-element);
1506 Tcl_DStringAppend(&newName, closeBrace+1, -1);
1507 result = TclDoGlob(interp, separators,
1508 headPtr, Tcl_DStringValue(&newName));
1509 if (result != TCL_OK) {
1514 Tcl_DStringFree(&newName);
1519 * At this point, there are no more brace substitutions to perform on
1520 * this path component. The variable p is pointing at a quoted or
1521 * unquoted directory separator or the end of the string. So we need
1522 * to check for special globbing characters in the current pattern.
1523 * We avoid modifying tail if p is pointing at the end of the string.
1529 firstSpecialChar = strpbrk(tail, "*[]?\\");
1532 firstSpecialChar = strpbrk(tail, "*[]?\\");
1535 if (firstSpecialChar != NULL) {
1537 * Look for matching files in the current directory. The
1538 * implementation of this function is platform specific, but may
1539 * recursively call TclDoGlob. For each file that matches, it will
1540 * add the match onto the interp->result, or call TclDoGlob if there
1541 * are more characters to be processed.
1544 return TclMatchFiles(interp, separators, headPtr, tail, p);
1546 Tcl_DStringAppend(headPtr, tail, p-tail);
1548 return TclDoGlob(interp, separators, headPtr, p);
1552 * There are no more wildcards in the pattern and no more unprocessed
1553 * characters in the tail, so now we can construct the path and verify
1554 * the existence of the file.
1557 switch (tclPlatform) {
1558 case TCL_PLATFORM_MAC:
1559 if (strchr(Tcl_DStringValue(headPtr), ':') == NULL) {
1560 Tcl_DStringAppend(headPtr, ":", 1);
1562 name = Tcl_DStringValue(headPtr);
1563 if (TclAccess(name, F_OK) == 0) {
1564 if ((name[1] != '\0') && (strchr(name+1, ':') == NULL)) {
1565 Tcl_AppendElement(interp, name+1);
1567 Tcl_AppendElement(interp, name);
1571 case TCL_PLATFORM_WINDOWS: {
1575 * We need to convert slashes to backslashes before checking
1576 * for the existence of the file. Once we are done, we need
1577 * to convert the slashes back.
1580 if (Tcl_DStringLength(headPtr) == 0) {
1581 if (((*name == '\\') && (name[1] == '/' || name[1] == '\\'))
1582 || (*name == '/')) {
1583 Tcl_DStringAppend(headPtr, "\\", 1);
1585 Tcl_DStringAppend(headPtr, ".", 1);
1588 for (p = Tcl_DStringValue(headPtr); *p != '\0'; p++) {
1595 name = Tcl_DStringValue(headPtr);
1596 exists = (TclAccess(name, F_OK) == 0);
1597 for (p = name; *p != '\0'; p++) {
1603 Tcl_AppendElement(interp, name);
1607 case TCL_PLATFORM_UNIX:
1608 if (Tcl_DStringLength(headPtr) == 0) {
1609 if ((*name == '\\' && name[1] == '/') || (*name == '/')) {
1610 Tcl_DStringAppend(headPtr, "/", 1);
1612 Tcl_DStringAppend(headPtr, ".", 1);
1615 name = Tcl_DStringValue(headPtr);
1616 if (TclAccess(name, F_OK) == 0) {
1617 Tcl_AppendElement(interp, name);