Debian Codegraph @

unix/tclUnixFile.c

raw
1 /*
2 * tclUnixFile.c --
3 *
4 * This file contains wrappers around UNIX file handling functions.
5 * These wrappers mask differences between Windows and UNIX.
6 *
7 * Copyright (c) 1995-1998 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 */
12
13 #include "tclInt.h"
14 #include "tclFileSystem.h"
15
16 static int NativeMatchType(Tcl_Interp *interp, const char* nativeEntry,
17 const char* nativeName, Tcl_GlobTypeData *types);
18
19 /*
20 *---------------------------------------------------------------------------
21 *
22 * TclpFindExecutable --
23 *
24 * This function computes the absolute path name of the current
25 * application, given its argv[0] value. For Cygwin, argv[0] is
26 * ignored and the path is determined the same as under win32.
27 *
28 * Results:
29 * None.
30 *
31 * Side effects:
32 * The computed path name is stored as a ProcessGlobalValue.
33 *
34 *---------------------------------------------------------------------------
35 */
36
37 void
38 TclpFindExecutable(
39 const char *argv0) /* The value of the application's argv[0]
40 * (native). */
41 {
42 #ifdef __CYGWIN__
43 int length;
44 wchar_t buf[PATH_MAX] = L"";
45 char name[PATH_MAX * 3 + 1];
46
47 GetModuleFileNameW(NULL, buf, PATH_MAX);
48 cygwin_conv_path(3, buf, name, sizeof(name));
49 length = strlen(name);
50 if ((length > 4) && !strcasecmp(name + length - 4, ".exe")) {
51 /* Strip '.exe' part. */
52 length -= 4;
53 }
54 TclSetObjNameOfExecutable(
55 Tcl_NewStringObj(name, length), NULL);
56 #else
57 const char *name, *p;
58 Tcl_StatBuf statBuf;
59 Tcl_DString buffer, nameString, cwd, utfName;
60 Tcl_Obj *obj;
61
62 if (argv0 == NULL) {
63 return;
64 }
65 Tcl_DStringInit(&buffer);
66
67 name = argv0;
68 for (p = name; *p != '\0'; p++) {
69 if (*p == '/') {
70 /*
71 * The name contains a slash, so use the name directly without
72 * doing a path search.
73 */
74
75 goto gotName;
76 }
77 }
78
79 p = getenv("PATH"); /* INTL: Native. */
80 if (p == NULL) {
81 /*
82 * There's no PATH environment variable; use the default that is used
83 * by sh.
84 */
85
86 p = ":/bin:/usr/bin";
87 } else if (*p == '\0') {
88 /*
89 * An empty path is equivalent to ".".
90 */
91
92 p = "./";
93 }
94
95 /*
96 * Search through all the directories named in the PATH variable to see if
97 * argv[0] is in one of them. If so, use that file name.
98 */
99
100 while (1) {
101 while (TclIsSpaceProcM(*p)) {
102 p++;
103 }
104 name = p;
105 while ((*p != ':') && (*p != 0)) {
106 p++;
107 }
108 TclDStringClear(&buffer);
109 if (p != name) {
110 Tcl_DStringAppend(&buffer, name, p - name);
111 if (p[-1] != '/') {
112 TclDStringAppendLiteral(&buffer, "/");
113 }
114 }
115 name = Tcl_DStringAppend(&buffer, argv0, -1);
116
117 /*
118 * INTL: The following calls to access() and stat() should not be
119 * converted to Tclp routines because they need to operate on native
120 * strings directly.
121 */
122
123 if ((access(name, X_OK) == 0) /* INTL: Native. */
124 && (TclOSstat(name, &statBuf) == 0) /* INTL: Native. */
125 && S_ISREG(statBuf.st_mode)) {
126 goto gotName;
127 }
128 if (p[0] == '\0') {
129 break;
130 } else if (p[1] == 0) {
131 p = "./";
132 } else {
133 p++;
134 }
135 }
136 TclNewObj(obj);
137 TclSetObjNameOfExecutable(obj, NULL);
138 goto done;
139
140 /*
141 * If the name starts with "/" then just store it
142 */
143
144 gotName:
145 #ifdef DJGPP
146 if (name[1] == ':')
147 #else
148 if (name[0] == '/')
149 #endif
150 {
151 Tcl_ExternalToUtfDString(NULL, name, -1, &utfName);
152 TclSetObjNameOfExecutable(TclDStringToObj(&utfName), NULL);
153 goto done;
154 }
155
156 if (TclpGetCwd(NULL, &cwd) == NULL) {
157 TclNewObj(obj);
158 TclSetObjNameOfExecutable(obj, NULL);
159 goto done;
160 }
161
162 /*
163 * The name is relative to the current working directory. First strip off
164 * a leading "./", if any, then add the full path name of the current
165 * working directory.
166 */
167
168 if ((name[0] == '.') && (name[1] == '/')) {
169 name += 2;
170 }
171
172 Tcl_DStringInit(&nameString);
173 Tcl_DStringAppend(&nameString, name, -1);
174
175 Tcl_DStringFree(&buffer);
176 Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&cwd),
177 Tcl_DStringLength(&cwd), &buffer);
178 if (Tcl_DStringValue(&cwd)[Tcl_DStringLength(&cwd) -1] != '/') {
179 TclDStringAppendLiteral(&buffer, "/");
180 }
181 Tcl_DStringFree(&cwd);
182 TclDStringAppendDString(&buffer, &nameString);
183 Tcl_DStringFree(&nameString);
184
185 Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&buffer), -1, &utfName);
186 TclSetObjNameOfExecutable(TclDStringToObj(&utfName), NULL);
187
188 done:
189 Tcl_DStringFree(&buffer);
190 #endif
191 }
192
193 /*
194 *----------------------------------------------------------------------
195 *
196 * TclpMatchInDirectory --
197 *
198 * This routine is used by the globbing code to search a directory for
199 * all files which match a given pattern.
200 *
201 * Results:
202 * The return value is a standard Tcl result indicating whether an error
203 * occurred in globbing. Errors are left in interp, good results are
204 * [lappend]ed to resultPtr (which must be a valid object).
205 *
206 * Side effects:
207 * None.
208 *
209 *----------------------------------------------------------------------
210 */
211
212 int
213 TclpMatchInDirectory(
214 Tcl_Interp *interp, /* Interpreter to receive errors. */
215 Tcl_Obj *resultPtr, /* List object to lappend results. */
216 Tcl_Obj *pathPtr, /* Contains path to directory to search. */
217 const char *pattern, /* Pattern to match against. */
218 Tcl_GlobTypeData *types) /* Object containing list of acceptable types.
219 * May be NULL. In particular the directory
220 * flag is very important. */
221 {
222 const char *native;
223 Tcl_Obj *fileNamePtr;
224 int matchResult = 0;
225
226 if (types != NULL && types->type == TCL_GLOB_TYPE_MOUNT) {
227 /*
228 * The native filesystem never adds mounts.
229 */
230
231 return TCL_OK;
232 }
233
234 fileNamePtr = Tcl_FSGetTranslatedPath(interp, pathPtr);
235 if (fileNamePtr == NULL) {
236 return TCL_ERROR;
237 }
238
239 if (pattern == NULL || (*pattern == '\0')) {
240 /*
241 * Match a file directly.
242 */
243
244 Tcl_Obj *tailPtr;
245 const char *nativeTail;
246
247 native = (const char *)Tcl_FSGetNativePath(pathPtr);
248 tailPtr = TclPathPart(interp, pathPtr, TCL_PATH_TAIL);
249 nativeTail = (const char *)Tcl_FSGetNativePath(tailPtr);
250 matchResult = NativeMatchType(interp, native, nativeTail, types);
251 if (matchResult == 1) {
252 Tcl_ListObjAppendElement(interp, resultPtr, pathPtr);
253 }
254 Tcl_DecrRefCount(tailPtr);
255 Tcl_DecrRefCount(fileNamePtr);
256 } else {
257 TclDIR *d;
258 Tcl_DirEntry *entryPtr;
259 const char *dirName;
260 int dirLength, nativeDirLen;
261 int matchHidden, matchHiddenPat;
262 Tcl_StatBuf statBuf;
263 Tcl_DString ds; /* native encoding of dir */
264 Tcl_DString dsOrig; /* utf-8 encoding of dir */
265
266 Tcl_DStringInit(&dsOrig);
267 dirName = TclGetStringFromObj(fileNamePtr, &dirLength);
268 Tcl_DStringAppend(&dsOrig, dirName, dirLength);
269
270 /*
271 * Make sure that the directory part of the name really is a
272 * directory. If the directory name is "", use the name "." instead,
273 * because some UNIX systems don't treat "" like "." automatically.
274 * Keep the "" for use in generating file names, otherwise "glob
275 * foo.c" would return "./foo.c".
276 */
277
278 if (dirLength == 0) {
279 dirName = ".";
280 } else {
281 dirName = Tcl_DStringValue(&dsOrig);
282
283 /*
284 * Make sure we have a trailing directory delimiter.
285 */
286
287 if (dirName[dirLength-1] != '/') {
288 dirName = TclDStringAppendLiteral(&dsOrig, "/");
289 dirLength++;
290 }
291 }
292
293 /*
294 * Now open the directory for reading and iterate over the contents.
295 */
296
297 native = Tcl_UtfToExternalDString(NULL, dirName, -1, &ds);
298
299 if ((TclOSstat(native, &statBuf) != 0) /* INTL: Native. */
300 || !S_ISDIR(statBuf.st_mode)) {
301 Tcl_DStringFree(&dsOrig);
302 Tcl_DStringFree(&ds);
303 Tcl_DecrRefCount(fileNamePtr);
304 return TCL_OK;
305 }
306
307 d = TclOSopendir(native); /* INTL: Native. */
308 if (d == NULL) {
309 Tcl_DStringFree(&ds);
310 if (interp != NULL) {
311 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
312 "couldn't read directory \"%s\": %s",
313 Tcl_DStringValue(&dsOrig), Tcl_PosixError(interp)));
314 }
315 Tcl_DStringFree(&dsOrig);
316 Tcl_DecrRefCount(fileNamePtr);
317 return TCL_ERROR;
318 }
319
320 nativeDirLen = Tcl_DStringLength(&ds);
321
322 /*
323 * Check to see if -type or the pattern requests hidden files.
324 */
325
326 matchHiddenPat = (pattern[0] == '.')
327 || ((pattern[0] == '\\') && (pattern[1] == '.'));
328 matchHidden = matchHiddenPat
329 || (types && (types->perm & TCL_GLOB_PERM_HIDDEN));
330 while ((entryPtr = TclOSreaddir(d)) != NULL) { /* INTL: Native. */
331 Tcl_DString utfDs;
332 const char *utfname;
333
334 /*
335 * Skip this file if it doesn't agree with the hidden parameters
336 * requested by the user (via -type or pattern).
337 */
338
339 if (*entryPtr->d_name == '.') {
340 if (!matchHidden) {
341 continue;
342 }
343 } else {
344 #ifdef MAC_OSX_TCL
345 if (matchHiddenPat) {
346 continue;
347 }
348 /* Also need to check HFS hidden flag in TclMacOSXMatchType. */
349 #else
350 if (matchHidden) {
351 continue;
352 }
353 #endif
354 }
355
356 /*
357 * Now check to see if the file matches, according to both type
358 * and pattern. If so, add the file to the result.
359 */
360
361 utfname = Tcl_ExternalToUtfDString(NULL, entryPtr->d_name, -1, &utfDs);
362 if (Tcl_StringCaseMatch(utfname, pattern, 0)) {
363 int typeOk = 1;
364
365 if (types != NULL) {
366 Tcl_DStringSetLength(&ds, nativeDirLen);
367 native = Tcl_DStringAppend(&ds, entryPtr->d_name, -1);
368 matchResult = NativeMatchType(interp, native,
369 entryPtr->d_name, types);
370 typeOk = (matchResult == 1);
371 }
372 if (typeOk) {
373 Tcl_ListObjAppendElement(interp, resultPtr,
374 TclNewFSPathObj(pathPtr, utfname,
375 Tcl_DStringLength(&utfDs)));
376 }
377 }
378 Tcl_DStringFree(&utfDs);
379 if (matchResult < 0) {
380 break;
381 }
382 }
383
384 TclOSclosedir(d);
385 Tcl_DStringFree(&ds);
386 Tcl_DStringFree(&dsOrig);
387 Tcl_DecrRefCount(fileNamePtr);
388 }
389 if (matchResult < 0) {
390 return TCL_ERROR;
391 }
392 return TCL_OK;
393 }
394
395 /*
396 *----------------------------------------------------------------------
397 *
398 * NativeMatchType --
399 *
400 * This routine is used by the globbing code to check if a file matches a
401 * given type description.
402 *
403 * Results:
404 * The return value is 1, 0 or -1 indicating whether the file matches the
405 * given criteria, does not match them, or an error occurred (in which
406 * case an error is left in interp).
407 *
408 * Side effects:
409 * None.
410 *
411 *----------------------------------------------------------------------
412 */
413
414 static int
415 NativeMatchType(
416 Tcl_Interp *interp, /* Interpreter to receive errors. */
417 const char *nativeEntry, /* Native path to check. */
418 const char *nativeName, /* Native filename to check. */
419 Tcl_GlobTypeData *types) /* Type description to match against. */
420 {
421 Tcl_StatBuf buf;
422
423 if (types == NULL) {
424 /*
425 * Simply check for the file's existence, but do it with lstat, in
426 * case it is a link to a file which doesn't exist (since that case
427 * would not show up if we used 'access' or 'stat')
428 */
429
430 if (TclOSlstat(nativeEntry, &buf) != 0) {
431 return 0;
432 }
433 return 1;
434 }
435
436 if (types->perm != 0) {
437 if (TclOSstat(nativeEntry, &buf) != 0) {
438 /*
439 * Either the file has disappeared between the 'readdir' call and
440 * the 'stat' call, or the file is a link to a file which doesn't
441 * exist (which we could ascertain with lstat), or there is some
442 * other strange problem. In all these cases, we define this to
443 * mean the file does not match any defined permission, and
444 * therefore it is not added to the list of files to return.
445 */
446
447 return 0;
448 }
449
450 /*
451 * readonly means that there are NO write permissions (even for user),
452 * but execute is OK for anybody OR that the user immutable flag is
453 * set (where supported).
454 */
455
456 if (((types->perm & TCL_GLOB_PERM_RONLY) &&
457 #if defined(HAVE_CHFLAGS) && defined(UF_IMMUTABLE)
458 !(buf.st_flags & UF_IMMUTABLE) &&
459 #endif
460 (buf.st_mode & (S_IWOTH|S_IWGRP|S_IWUSR))) ||
461 ((types->perm & TCL_GLOB_PERM_R) &&
462 (access(nativeEntry, R_OK) != 0)) ||
463 ((types->perm & TCL_GLOB_PERM_W) &&
464 (access(nativeEntry, W_OK) != 0)) ||
465 ((types->perm & TCL_GLOB_PERM_X) &&
466 (access(nativeEntry, X_OK) != 0))
467 #ifndef MAC_OSX_TCL
468 || ((types->perm & TCL_GLOB_PERM_HIDDEN) &&
469 (*nativeName != '.'))
470 #endif /* MAC_OSX_TCL */
471 ) {
472 return 0;
473 }
474 }
475 if (types->type != 0) {
476 if (types->perm == 0) {
477 /*
478 * We haven't yet done a stat on the file.
479 */
480
481 if (TclOSstat(nativeEntry, &buf) != 0) {
482 /*
483 * Posix error occurred. The only ok case is if this is a link
484 * to a nonexistent file, and the user did 'glob -l'. So we
485 * check that here:
486 */
487
488 if ((types->type & TCL_GLOB_TYPE_LINK)
489 && (TclOSlstat(nativeEntry, &buf) == 0)
490 && S_ISLNK(buf.st_mode)) {
491 return 1;
492 }
493 return 0;
494 }
495 }
496
497 /*
498 * In order bcdpsfl as in 'find -t'
499 */
500
501 if ( ((types->type & TCL_GLOB_TYPE_BLOCK)&& S_ISBLK(buf.st_mode)) ||
502 ((types->type & TCL_GLOB_TYPE_CHAR) && S_ISCHR(buf.st_mode)) ||
503 ((types->type & TCL_GLOB_TYPE_DIR) && S_ISDIR(buf.st_mode)) ||
504 ((types->type & TCL_GLOB_TYPE_PIPE) && S_ISFIFO(buf.st_mode))||
505 #ifdef S_ISSOCK
506 ((types->type & TCL_GLOB_TYPE_SOCK) && S_ISSOCK(buf.st_mode))||
507 #endif /* S_ISSOCK */
508 ((types->type & TCL_GLOB_TYPE_FILE) && S_ISREG(buf.st_mode))) {
509 /*
510 * Do nothing - this file is ok.
511 */
512 } else {
513 #ifdef S_ISLNK
514 if ((types->type & TCL_GLOB_TYPE_LINK)
515 && (TclOSlstat(nativeEntry, &buf) == 0)
516 && S_ISLNK(buf.st_mode)) {
517 goto filetypeOK;
518 }
519 #endif /* S_ISLNK */
520 return 0;
521 }
522 }
523 filetypeOK:
524
525 /*
526 * If we're on OSX, we also have to worry about matching the file creator
527 * code (if specified). Do that now.
528 */
529
530 #ifdef MAC_OSX_TCL
531 if (types->macType != NULL || types->macCreator != NULL ||
532 (types->perm & TCL_GLOB_PERM_HIDDEN)) {
533 int matchResult;
534
535 if (types->perm == 0 && types->type == 0) {
536 /*
537 * We haven't yet done a stat on the file.
538 */
539
540 if (TclOSstat(nativeEntry, &buf) != 0) {
541 return 0;
542 }
543 }
544
545 matchResult = TclMacOSXMatchType(interp, nativeEntry, nativeName,
546 &buf, types);
547 if (matchResult != 1) {
548 return matchResult;
549 }
550 }
551 #endif /* MAC_OSX_TCL */
552
553 return 1;
554 }
555
556 /*
557 *---------------------------------------------------------------------------
558 *
559 * TclpGetUserHome --
560 *
561 * This function takes the specified user name and finds their home
562 * directory.
563 *
564 * Results:
565 * The result is a pointer to a string specifying the user's home
566 * directory, or NULL if the user's home directory could not be
567 * determined. Storage for the result string is allocated in bufferPtr;
568 * the caller must call Tcl_DStringFree() when the result is no longer
569 * needed.
570 *
571 * Side effects:
572 * None.
573 *
574 *----------------------------------------------------------------------
575 */
576
577 const char *
578 TclpGetUserHome(
579 const char *name, /* User name for desired home directory. */
580 Tcl_DString *bufferPtr) /* Uninitialized or free DString filled with
581 * name of user's home directory. */
582 {
583 struct passwd *pwPtr;
584 Tcl_DString ds;
585 const char *native = Tcl_UtfToExternalDString(NULL, name, -1, &ds);
586
587 pwPtr = TclpGetPwNam(native); /* INTL: Native. */
588 Tcl_DStringFree(&ds);
589
590 if (pwPtr == NULL) {
591 return NULL;
592 }
593 Tcl_ExternalToUtfDString(NULL, pwPtr->pw_dir, -1, bufferPtr);
594 return Tcl_DStringValue(bufferPtr);
595 }
596
597 /*
598 *---------------------------------------------------------------------------
599 *
600 * TclpObjAccess --
601 *
602 * This function replaces the library version of access().
603 *
604 * Results:
605 * See access() documentation.
606 *
607 * Side effects:
608 * See access() documentation.
609 *
610 *---------------------------------------------------------------------------
611 */
612
613 int
614 TclpObjAccess(
615 Tcl_Obj *pathPtr, /* Path of file to access */
616 int mode) /* Permission setting. */
617 {
618 const char *path = (const char *)Tcl_FSGetNativePath(pathPtr);
619
620 if (path == NULL) {
621 return -1;
622 }
623 return access(path, mode);
624 }
625
626 /*
627 *---------------------------------------------------------------------------
628 *
629 * TclpObjChdir --
630 *
631 * This function replaces the library version of chdir().
632 *
633 * Results:
634 * See chdir() documentation.
635 *
636 * Side effects:
637 * See chdir() documentation.
638 *
639 *---------------------------------------------------------------------------
640 */
641
642 int
643 TclpObjChdir(
644 Tcl_Obj *pathPtr) /* Path to new working directory */
645 {
646 const char *path = (const char *)Tcl_FSGetNativePath(pathPtr);
647
648 if (path == NULL) {
649 return -1;
650 }
651 return chdir(path);
652 }
653
654 /*
655 *----------------------------------------------------------------------
656 *
657 * TclpObjLstat --
658 *
659 * This function replaces the library version of lstat().
660 *
661 * Results:
662 * See lstat() documentation.
663 *
664 * Side effects:
665 * See lstat() documentation.
666 *
667 *----------------------------------------------------------------------
668 */
669
670 int
671 TclpObjLstat(
672 Tcl_Obj *pathPtr, /* Path of file to stat */
673 Tcl_StatBuf *bufPtr) /* Filled with results of stat call. */
674 {
675 return TclOSlstat((const char *)Tcl_FSGetNativePath(pathPtr), bufPtr);
676 }
677
678 /*
679 *---------------------------------------------------------------------------
680 *
681 * TclpGetNativeCwd --
682 *
683 * This function replaces the library version of getcwd().
684 *
685 * Results:
686 * The input and output are filesystem paths in native form. The result
687 * is either the given clientData, if the working directory hasn't
688 * changed, or a new clientData (owned by our caller), giving the new
689 * native path, or NULL if the current directory could not be determined.
690 * If NULL is returned, the caller can examine the standard Posix error
691 * codes to determine the cause of the problem.
692 *
693 * Side effects:
694 * None.
695 *
696 *----------------------------------------------------------------------
697 */
698
699 void *
700 TclpGetNativeCwd(
701 void *clientData)
702 {
703 char buffer[MAXPATHLEN+1];
704
705 #ifdef USEGETWD
706 if (getwd(buffer) == NULL) { /* INTL: Native. */
707 return NULL;
708 }
709 #else
710 if (getcwd(buffer, MAXPATHLEN+1) == NULL) { /* INTL: Native. */
711 return NULL;
712 }
713 #endif /* USEGETWD */
714
715 if ((clientData == NULL) || strcmp(buffer, (const char *) clientData)) {
716 char *newCd = (char *)ckalloc(strlen(buffer) + 1);
717
718 strcpy(newCd, buffer);
719 return newCd;
720 }
721
722 /*
723 * No change to pwd.
724 */
725
726 return clientData;
727 }
728
729 /*
730 *---------------------------------------------------------------------------
731 *
732 * TclpGetCwd --
733 *
734 * This function replaces the library version of getcwd(). (Obsolete
735 * function, only retained for old extensions which may call it
736 * directly).
737 *
738 * Results:
739 * The result is a pointer to a string specifying the current directory,
740 * or NULL if the current directory could not be determined. If NULL is
741 * returned, an error message is left in the interp's result. Storage for
742 * the result string is allocated in bufferPtr; the caller must call
743 * Tcl_DStringFree() when the result is no longer needed.
744 *
745 * Side effects:
746 * None.
747 *
748 *----------------------------------------------------------------------
749 */
750
751 const char *
752 TclpGetCwd(
753 Tcl_Interp *interp, /* If non-NULL, used for error reporting. */
754 Tcl_DString *bufferPtr) /* Uninitialized or free DString filled with
755 * name of current directory. */
756 {
757 char buffer[MAXPATHLEN+1];
758
759 #ifdef USEGETWD
760 if (getwd(buffer) == NULL) /* INTL: Native. */
761 #else
762 if (getcwd(buffer, MAXPATHLEN+1) == NULL) /* INTL: Native. */
763 #endif /* USEGETWD */
764 {
765 if (interp != NULL) {
766 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
767 "error getting working directory name: %s",
768 Tcl_PosixError(interp)));
769 }
770 return NULL;
771 }
772 return Tcl_ExternalToUtfDString(NULL, buffer, -1, bufferPtr);
773 }
774
775 /*
776 *---------------------------------------------------------------------------
777 *
778 * TclpReadlink --
779 *
780 * This function replaces the library version of readlink().
781 *
782 * Results:
783 * The result is a pointer to a string specifying the contents of the
784 * symbolic link given by 'path', or NULL if the symbolic link could not
785 * be read. Storage for the result string is allocated in bufferPtr; the
786 * caller must call Tcl_DStringFree() when the result is no longer
787 * needed.
788 *
789 * Side effects:
790 * See readlink() documentation.
791 *
792 *---------------------------------------------------------------------------
793 */
794
795 char *
796 TclpReadlink(
797 const char *path, /* Path of file to readlink (UTF-8). */
798 Tcl_DString *linkPtr) /* Uninitialized or free DString filled with
799 * contents of link (UTF-8). */
800 {
801 #ifndef DJGPP
802 char link[MAXPATHLEN];
803 int length;
804 const char *native;
805 Tcl_DString ds;
806
807 native = Tcl_UtfToExternalDString(NULL, path, -1, &ds);
808 length = readlink(native, link, sizeof(link)); /* INTL: Native. */
809 Tcl_DStringFree(&ds);
810
811 if (length < 0) {
812 return NULL;
813 }
814
815 Tcl_ExternalToUtfDString(NULL, link, length, linkPtr);
816 return Tcl_DStringValue(linkPtr);
817 #else
818 return NULL;
819 #endif /* !DJGPP */
820 }
821
822 /*
823 *----------------------------------------------------------------------
824 *
825 * TclpObjStat --
826 *
827 * This function replaces the library version of stat().
828 *
829 * Results:
830 * See stat() documentation.
831 *
832 * Side effects:
833 * See stat() documentation.
834 *
835 *----------------------------------------------------------------------
836 */
837
838 int
839 TclpObjStat(
840 Tcl_Obj *pathPtr, /* Path of file to stat */
841 Tcl_StatBuf *bufPtr) /* Filled with results of stat call. */
842 {
843 const char *path = (const char *)Tcl_FSGetNativePath(pathPtr);
844
845 if (path == NULL) {
846 return -1;
847 }
848 return TclOSstat(path, bufPtr);
849 }
850
851 #ifdef S_IFLNK
852
853 Tcl_Obj *
854 TclpObjLink(
855 Tcl_Obj *pathPtr,
856 Tcl_Obj *toPtr,
857 int linkAction)
858 {
859 if (toPtr != NULL) {
860 const char *src = (const char *)Tcl_FSGetNativePath(pathPtr);
861 const char *target = NULL;
862
863 if (src == NULL) {
864 return NULL;
865 }
866
867 /*
868 * If we're making a symbolic link and the path is relative, then we
869 * must check whether it exists _relative_ to the directory in which
870 * the src is found (not relative to the current cwd which is just not
871 * relevant in this case).
872 *
873 * If we're making a hard link, then a relative path is just converted
874 * to absolute relative to the cwd.
875 */
876
877 if ((linkAction & TCL_CREATE_SYMBOLIC_LINK)
878 && (Tcl_FSGetPathType(toPtr) == TCL_PATH_RELATIVE)) {
879 Tcl_Obj *dirPtr, *absPtr;
880
881 dirPtr = TclPathPart(NULL, pathPtr, TCL_PATH_DIRNAME);
882 if (dirPtr == NULL) {
883 return NULL;
884 }
885 absPtr = Tcl_FSJoinToPath(dirPtr, 1, &toPtr);
886 Tcl_IncrRefCount(absPtr);
887 if (Tcl_FSAccess(absPtr, F_OK) == -1) {
888 Tcl_DecrRefCount(absPtr);
889 Tcl_DecrRefCount(dirPtr);
890
891 /*
892 * Target doesn't exist.
893 */
894
895 errno = ENOENT;
896 return NULL;
897 }
898
899 /*
900 * Target exists; we'll construct the relative path we want below.
901 */
902
903 Tcl_DecrRefCount(absPtr);
904 Tcl_DecrRefCount(dirPtr);
905 } else {
906 target = (const char*)Tcl_FSGetNativePath(toPtr);
907 if (target == NULL) {
908 return NULL;
909 }
910 if (access(target, F_OK) == -1) {
911 /*
912 * Target doesn't exist.
913 */
914
915 errno = ENOENT;
916 return NULL;
917 }
918 }
919
920 if (access(src, F_OK) != -1) {
921 /*
922 * Src exists.
923 */
924
925 errno = EEXIST;
926 return NULL;
927 }
928
929 /*
930 * Check symbolic link flag first, since we prefer to create these.
931 */
932
933 if (linkAction & TCL_CREATE_SYMBOLIC_LINK) {
934 Tcl_DString ds;
935 Tcl_Obj *transPtr;
936 int length;
937
938 /*
939 * Now we don't want to link to the absolute, normalized path.
940 * Relative links are quite acceptable (but links to ~user are not
941 * -- these must be expanded first).
942 */
943
944 transPtr = Tcl_FSGetTranslatedPath(NULL, toPtr);
945 if (transPtr == NULL) {
946 return NULL;
947 }
948 target = Tcl_GetStringFromObj(transPtr, &length);
949 target = Tcl_UtfToExternalDString(NULL, target, length, &ds);
950 Tcl_DecrRefCount(transPtr);
951
952 if (symlink(target, src) != 0) {
953 toPtr = NULL;
954 }
955 Tcl_DStringFree(&ds);
956 } else if (linkAction & TCL_CREATE_HARD_LINK) {
957 if (link(target, src) != 0) {
958 return NULL;
959 }
960 } else {
961 errno = ENODEV;
962 return NULL;
963 }
964 return toPtr;
965 } else {
966 Tcl_Obj *linkPtr = NULL;
967
968 char link[MAXPATHLEN];
969 int length;
970 Tcl_DString ds;
971 Tcl_Obj *transPtr;
972
973 transPtr = Tcl_FSGetTranslatedPath(NULL, pathPtr);
974 if (transPtr == NULL) {
975 return NULL;
976 }
977 Tcl_DecrRefCount(transPtr);
978
979 length = readlink((const char *)Tcl_FSGetNativePath(pathPtr), link, sizeof(link));
980 if (length < 0) {
981 return NULL;
982 }
983
984 Tcl_ExternalToUtfDString(NULL, link, length, &ds);
985 linkPtr = TclDStringToObj(&ds);
986 Tcl_IncrRefCount(linkPtr);
987 return linkPtr;
988 }
989 }
990 #endif /* S_IFLNK */
991
992 /*
993 *---------------------------------------------------------------------------
994 *
995 * TclpFilesystemPathType --
996 *
997 * This function is part of the native filesystem support, and returns
998 * the path type of the given path. Right now it simply returns NULL. In
999 * the future it could return specific path types, like 'nfs', 'samba',
1000 * 'FAT32', etc.
1001 *
1002 * Results:
1003 * NULL at present.
1004 *
1005 * Side effects:
1006 * None.
1007 *
1008 *---------------------------------------------------------------------------
1009 */
1010
1011 Tcl_Obj *
1012 TclpFilesystemPathType(
1013 Tcl_Obj *pathPtr)
1014 {
1015 /*
1016 * All native paths are of the same type.
1017 */
1018
1019 return NULL;
1020 }
1021
1022 /*
1023 *---------------------------------------------------------------------------
1024 *
1025 * TclpNativeToNormalized --
1026 *
1027 * Convert native format to a normalized path object, with refCount of
1028 * zero.
1029 *
1030 * Currently assumes all native paths are actually normalized already, so
1031 * if the path given is not normalized this will actually just convert to
1032 * a valid string path, but not necessarily a normalized one.
1033 *
1034 * Results:
1035 * A valid normalized path.
1036 *
1037 * Side effects:
1038 * None.
1039 *
1040 *---------------------------------------------------------------------------
1041 */
1042
1043 Tcl_Obj *
1044 TclpNativeToNormalized(
1045 void *clientData)
1046 {
1047 Tcl_DString ds;
1048
1049 Tcl_ExternalToUtfDString(NULL, (const char *) clientData, -1, &ds);
1050 return TclDStringToObj(&ds);
1051 }
1052
1053 /*
1054 *---------------------------------------------------------------------------
1055 *
1056 * TclNativeCreateNativeRep --
1057 *
1058 * Create a native representation for the given path.
1059 *
1060 * Results:
1061 * The nativePath representation.
1062 *
1063 * Side effects:
1064 * Memory will be allocated. The path may need to be normalized.
1065 *
1066 *---------------------------------------------------------------------------
1067 */
1068
1069 void *
1070 TclNativeCreateNativeRep(
1071 Tcl_Obj *pathPtr)
1072 {
1073 char *nativePathPtr;
1074 const char *str;
1075 Tcl_DString ds;
1076 Tcl_Obj *validPathPtr;
1077 int len;
1078
1079 if (TclFSCwdIsNative() || Tcl_FSGetPathType(pathPtr) == TCL_PATH_ABSOLUTE) {
1080 /*
1081 * The cwd is native (or path is absolute), use the translated path
1082 * without worrying about normalization (this will also usually be
1083 * shorter so the utf-to-external conversion will be somewhat faster).
1084 */
1085
1086 validPathPtr = Tcl_FSGetTranslatedPath(NULL, pathPtr);
1087 if (validPathPtr == NULL) {
1088 return NULL;
1089 }
1090 } else {
1091 /*
1092 * Make sure the normalized path is set.
1093 */
1094
1095 validPathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr);
1096 if (validPathPtr == NULL) {
1097 return NULL;
1098 }
1099 Tcl_IncrRefCount(validPathPtr);
1100 }
1101
1102 str = Tcl_GetStringFromObj(validPathPtr, &len);
1103 Tcl_UtfToExternalDString(NULL, str, len, &ds);
1104 len = Tcl_DStringLength(&ds) + sizeof(char);
1105 if (strlen(Tcl_DStringValue(&ds)) < len - sizeof(char)) {
1106 /* See bug [3118489]: NUL in filenames */
1107 Tcl_DecrRefCount(validPathPtr);
1108 Tcl_DStringFree(&ds);
1109 return NULL;
1110 }
1111 Tcl_DecrRefCount(validPathPtr);
1112 nativePathPtr = (char *)ckalloc(len);
1113 memcpy(nativePathPtr, Tcl_DStringValue(&ds), len);
1114
1115 Tcl_DStringFree(&ds);
1116 return nativePathPtr;
1117 }
1118
1119 /*
1120 *---------------------------------------------------------------------------
1121 *
1122 * TclNativeDupInternalRep --
1123 *
1124 * Duplicate the native representation.
1125 *
1126 * Results:
1127 * The copied native representation, or NULL if it is not possible to
1128 * copy the representation.
1129 *
1130 * Side effects:
1131 * Memory will be allocated for the copy.
1132 *
1133 *---------------------------------------------------------------------------
1134 */
1135
1136 void *
1137 TclNativeDupInternalRep(
1138 void *clientData)
1139 {
1140 char *copy;
1141 size_t len;
1142
1143 if (clientData == NULL) {
1144 return NULL;
1145 }
1146
1147 /*
1148 * ASCII representation when running on Unix.
1149 */
1150
1151 len = (strlen((const char*) clientData) + 1) * sizeof(char);
1152
1153 copy = (char *)ckalloc(len);
1154 memcpy(copy, clientData, len);
1155 return copy;
1156 }
1157
1158 /*
1159 *---------------------------------------------------------------------------
1160 *
1161 * TclpUtime --
1162 *
1163 * Set the modification date for a file.
1164 *
1165 * Results:
1166 * 0 on success, -1 on error.
1167 *
1168 * Side effects:
1169 * None.
1170 *
1171 *---------------------------------------------------------------------------
1172 */
1173
1174 int
1175 TclpUtime(
1176 Tcl_Obj *pathPtr, /* File to modify */
1177 struct utimbuf *tval) /* New modification date structure */
1178 {
1179 return utime((const char *)Tcl_FSGetNativePath(pathPtr), tval);
1180 }
1181
1182 #ifdef __CYGWIN__
1183
1184 int
1185 TclOSstat(
1186 const char *name,
1187 void *cygstat)
1188 {
1189 struct stat buf;
1190 Tcl_StatBuf *statBuf = (Tcl_StatBuf *)cygstat;
1191 int result = stat(name, &buf);
1192
1193 statBuf->st_mode = buf.st_mode;
1194 statBuf->st_ino = buf.st_ino;
1195 statBuf->st_dev = buf.st_dev;
1196 statBuf->st_rdev = buf.st_rdev;
1197 statBuf->st_nlink = buf.st_nlink;
1198 statBuf->st_uid = buf.st_uid;
1199 statBuf->st_gid = buf.st_gid;
1200 statBuf->st_size = buf.st_size;
1201 statBuf->st_atime = buf.st_atime;
1202 statBuf->st_mtime = buf.st_mtime;
1203 statBuf->st_ctime = buf.st_ctime;
1204 return result;
1205 }
1206
1207 int
1208 TclOSlstat(
1209 const char *name,
1210 void *cygstat)
1211 {
1212 struct stat buf;
1213 Tcl_StatBuf *statBuf = (Tcl_StatBuf *)cygstat;
1214 int result = lstat(name, &buf);
1215
1216 statBuf->st_mode = buf.st_mode;
1217 statBuf->st_ino = buf.st_ino;
1218 statBuf->st_dev = buf.st_dev;
1219 statBuf->st_rdev = buf.st_rdev;
1220 statBuf->st_nlink = buf.st_nlink;
1221 statBuf->st_uid = buf.st_uid;
1222 statBuf->st_gid = buf.st_gid;
1223 statBuf->st_size = buf.st_size;
1224 statBuf->st_atime = buf.st_atime;
1225 statBuf->st_mtime = buf.st_mtime;
1226 statBuf->st_ctime = buf.st_ctime;
1227 return result;
1228 }
1229 #endif /* CYGWIN */
1230
1231 /*
1232 * Local Variables:
1233 * mode: c
1234 * c-basic-offset: 4
1235 * fill-column: 78
1236 * End:
1237 */