os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/mac/tclMacFile.c
Update contrib.
4 * This file implements the channel drivers for Macintosh
5 * files. It also comtains Macintosh version of other Tcl
6 * functions that deal with the file system.
8 * Copyright (c) 1995-1998 Sun Microsystems, Inc.
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * RCS: @(#) $Id: tclMacFile.c,v 1.27.2.1 2003/10/03 17:45:37 vincentdarley Exp $
17 * Note: This code eventually needs to support async I/O. In doing this
18 * we will need to keep track of all current async I/O. If exit to shell
19 * is called - we shouldn't exit until all asyc I/O completes.
24 #include "tclMacInt.h"
26 #include <Resources.h>
29 #include <Processes.h>
32 #include <MoreFiles.h>
33 #include <MoreFilesExtras.h>
34 #include <FSpCompat.h>
36 static int NativeMatchType(Tcl_Obj *tempName, Tcl_GlobTypeData *types,
37 HFileInfo fileInfo, OSType okType, OSType okCreator);
38 static OSErr FspLocationFromFsPath _ANSI_ARGS_((Tcl_Obj *pathPtr,
40 static OSErr FspLLocationFromFsPath _ANSI_ARGS_((Tcl_Obj *pathPtr,
43 static OSErr CreateAliasFile _ANSI_ARGS_((FSSpec *theAliasFile, FSSpec *targetFile));
46 FspLocationFromFsPath(pathPtr, specPtr)
50 CONST char *native = Tcl_FSGetNativePath(pathPtr);
51 return FSpLocationFromPath(strlen(native), native, specPtr);
55 FspLLocationFromFsPath(pathPtr, specPtr)
59 CONST char *native = Tcl_FSGetNativePath(pathPtr);
60 return FSpLLocationFromPath(strlen(native), native, specPtr);
65 *----------------------------------------------------------------------
67 * TclpFindExecutable --
69 * This procedure computes the absolute path name of the current
70 * application, given its argv[0] value. However, this
71 * implementation doesn't need the argv[0] value. NULL
72 * may be passed in its place.
78 * The variable tclExecutableName gets filled in with the file
79 * name for the application, if we figured it out. If we couldn't
80 * figure it out, Tcl_FindExecutable is set to NULL.
82 *----------------------------------------------------------------------
87 CONST char *argv0) /* The value of the application's argv[0]. */
89 ProcessSerialNumber psn;
94 Handle pathName = NULL;
98 TclInitSubsystems(argv0);
100 GetCurrentProcess(&psn);
101 info.processInfoLength = sizeof(ProcessInfoRec);
102 info.processName = appName;
103 info.processAppSpec = &fileSpec;
104 GetProcessInformation(&psn, &info);
106 if (tclExecutableName != NULL) {
107 ckfree(tclExecutableName);
108 tclExecutableName = NULL;
111 err = FSpPathFromLocation(&fileSpec, &pathLength, &pathName);
113 Tcl_ExternalToUtfDString(NULL, *pathName, pathLength, &ds);
115 DisposeHandle(pathName);
117 tclExecutableName = (char *) ckalloc((unsigned)
118 (Tcl_DStringLength(&ds) + 1));
119 strcpy(tclExecutableName, Tcl_DStringValue(&ds));
120 Tcl_DStringFree(&ds);
121 return tclExecutableName;
125 *----------------------------------------------------------------------
127 * TclpMatchInDirectory --
129 * This routine is used by the globbing code to search a
130 * directory for all files which match a given pattern.
134 * The return value is a standard Tcl result indicating whether an
135 * error occurred in globbing. Errors are left in interp, good
136 * results are lappended to resultPtr (which must be a valid object)
141 *---------------------------------------------------------------------- */
144 TclpMatchInDirectory(interp, resultPtr, pathPtr, pattern, types)
145 Tcl_Interp *interp; /* Interpreter to receive errors. */
146 Tcl_Obj *resultPtr; /* List object to lappend results. */
147 Tcl_Obj *pathPtr; /* Contains path to directory to search. */
148 CONST char *pattern; /* Pattern to match against. NULL or empty
149 * means pathPtr is actually a single file
151 Tcl_GlobTypeData *types; /* Object containing list of acceptable types.
152 * May be NULL. In particular the directory
153 * flag is very important. */
156 OSType okCreator = 0;
157 Tcl_Obj *fileNamePtr;
159 fileNamePtr = Tcl_FSGetTranslatedPath(interp, pathPtr);
160 if (fileNamePtr == NULL) {
165 if (types->macType != NULL) {
166 Tcl_GetOSTypeFromObj(NULL, types->macType, &okType);
168 if (types->macCreator != NULL) {
169 Tcl_GetOSTypeFromObj(NULL, types->macCreator, &okCreator);
173 if (pattern == NULL || (*pattern == '\0')) {
174 /* Match a single file directly */
176 CInfoPBRec paramBlock;
179 if (TclpObjLstat(fileNamePtr, &buf) != 0) {
180 /* File doesn't exist */
181 Tcl_DecrRefCount(fileNamePtr);
185 if (FspLLocationFromFsPath(fileNamePtr, &fileSpec) == noErr) {
186 paramBlock.hFileInfo.ioCompletion = NULL;
187 paramBlock.hFileInfo.ioNamePtr = fileSpec.name;
188 paramBlock.hFileInfo.ioVRefNum = fileSpec.vRefNum;
189 paramBlock.hFileInfo.ioFDirIndex = 0;
190 paramBlock.hFileInfo.ioDirID = fileSpec.parID;
192 PBGetCatInfo(¶mBlock, 0);
195 if (NativeMatchType(fileNamePtr, types, paramBlock.hFileInfo,
196 okType, okCreator)) {
198 char *fname = Tcl_GetStringFromObj(pathPtr,&fnameLen);
199 if ((fnameLen > 1) && (strchr(fname+1, ':') == NULL)) {
200 Tcl_ListObjAppendElement(interp, resultPtr,
201 Tcl_NewStringObj(fname+1, fnameLen-1));
203 Tcl_ListObjAppendElement(interp, resultPtr, pathPtr);
206 Tcl_DecrRefCount(fileNamePtr);
210 int fnameLen, result = TCL_OK;
219 Tcl_DString fileString;
222 Tcl_DStringInit(&dsOrig);
223 Tcl_DStringAppend(&dsOrig, Tcl_GetString(fileNamePtr), -1);
224 baseLength = Tcl_DStringLength(&dsOrig);
227 * Make sure that the directory part of the name really is a
231 Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&dsOrig),
232 Tcl_DStringLength(&dsOrig), &fileString);
234 err = FSpLocationFromPath(Tcl_DStringLength(&fileString),
235 Tcl_DStringValue(&fileString), &dirSpec);
236 Tcl_DStringFree(&fileString);
238 err = FSpGetDirectoryID(&dirSpec, &dirID, &isDirectory);
241 if ((err != noErr) || !isDirectory) {
243 * Check if we had a relative path (unix style relative path
244 * compatibility for glob)
246 Tcl_DStringFree(&dsOrig);
247 Tcl_DStringAppend(&dsOrig, ":", 1);
248 Tcl_DStringAppend(&dsOrig, Tcl_GetString(fileNamePtr), -1);
249 baseLength = Tcl_DStringLength(&dsOrig);
251 Tcl_UtfToExternalDString(NULL, Tcl_DStringValue(&dsOrig),
252 Tcl_DStringLength(&dsOrig), &fileString);
254 err = FSpLocationFromPath(Tcl_DStringLength(&fileString),
255 Tcl_DStringValue(&fileString), &dirSpec);
256 Tcl_DStringFree(&fileString);
258 err = FSpGetDirectoryID(&dirSpec, &dirID, &isDirectory);
261 if ((err != noErr) || !isDirectory) {
262 Tcl_DStringFree(&dsOrig);
263 Tcl_DecrRefCount(fileNamePtr);
268 /* Make sure we have a trailing directory delimiter */
269 if (Tcl_DStringValue(&dsOrig)[baseLength-1] != ':') {
270 Tcl_DStringAppend(&dsOrig, ":", 1);
275 * Now open the directory for reading and iterate over the contents.
278 pb.hFileInfo.ioVRefNum = dirSpec.vRefNum;
279 pb.hFileInfo.ioDirID = dirID;
280 pb.hFileInfo.ioNamePtr = (StringPtr) fileName;
281 pb.hFileInfo.ioFDirIndex = itemIndex = 1;
284 pb.hFileInfo.ioFDirIndex = itemIndex;
285 pb.hFileInfo.ioDirID = dirID;
286 err = PBGetCatInfoSync(&pb);
292 * Now check to see if the file matches.
295 Tcl_ExternalToUtfDString(NULL, (char *) fileName + 1, fileName[0],
297 if (Tcl_StringMatch(Tcl_DStringValue(&fileString), pattern)) {
299 Tcl_DStringSetLength(&dsOrig, baseLength);
300 Tcl_DStringAppend(&dsOrig, Tcl_DStringValue(&fileString), -1);
301 fname = Tcl_DStringValue(&dsOrig);
302 fnameLen = Tcl_DStringLength(&dsOrig);
305 * We use this tempName in calls to check the file's
306 * type below. We may also use it for the result.
308 tempName = Tcl_NewStringObj(fname, fnameLen);
309 Tcl_IncrRefCount(tempName);
311 /* Is the type acceptable? */
312 if (NativeMatchType(tempName, types, pb.hFileInfo,
313 okType, okCreator)) {
314 if ((fnameLen > 1) && (strchr(fname+1, ':') == NULL)) {
315 Tcl_ListObjAppendElement(interp, resultPtr,
316 Tcl_NewStringObj(fname+1, fnameLen-1));
318 Tcl_ListObjAppendElement(interp, resultPtr, tempName);
322 * This will free the object, unless it was inserted in
323 * the result list above.
325 Tcl_DecrRefCount(tempName);
327 Tcl_DStringFree(&fileString);
331 Tcl_DStringFree(&dsOrig);
332 Tcl_DecrRefCount(fileNamePtr);
339 Tcl_Obj *tempName, /* Path to check */
340 Tcl_GlobTypeData *types, /* Type description to match against */
341 HFileInfo fileInfo, /* MacOS file info */
342 OSType okType, /* Acceptable MacOS type, or zero */
343 OSType okCreator) /* Acceptable MacOS creator, or zero */
346 /* If invisible, don't return the file */
347 if (fileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) {
353 if (fileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) {
355 if ((types->perm == 0) ||
356 !(types->perm & TCL_GLOB_PERM_HIDDEN)) {
361 if (types->perm & TCL_GLOB_PERM_HIDDEN) {
365 if (types->perm != 0) {
367 ((types->perm & TCL_GLOB_PERM_RONLY) &&
368 !(fileInfo.ioFlAttrib & 1)) ||
369 ((types->perm & TCL_GLOB_PERM_R) &&
370 (TclpObjAccess(tempName, R_OK) != 0)) ||
371 ((types->perm & TCL_GLOB_PERM_W) &&
372 (TclpObjAccess(tempName, W_OK) != 0)) ||
373 ((types->perm & TCL_GLOB_PERM_X) &&
374 (TclpObjAccess(tempName, X_OK) != 0))
379 if (types->type != 0) {
380 if (TclpObjStat(tempName, &buf) != 0) {
381 /* Posix error occurred */
385 * In order bcdpfls as in 'find -t'
388 ((types->type & TCL_GLOB_TYPE_BLOCK) &&
389 S_ISBLK(buf.st_mode)) ||
390 ((types->type & TCL_GLOB_TYPE_CHAR) &&
391 S_ISCHR(buf.st_mode)) ||
392 ((types->type & TCL_GLOB_TYPE_DIR) &&
393 S_ISDIR(buf.st_mode)) ||
394 ((types->type & TCL_GLOB_TYPE_PIPE) &&
395 S_ISFIFO(buf.st_mode)) ||
396 ((types->type & TCL_GLOB_TYPE_FILE) &&
397 S_ISREG(buf.st_mode))
399 || ((types->type & TCL_GLOB_TYPE_SOCK) &&
400 S_ISSOCK(buf.st_mode))
403 /* Do nothing -- this file is ok */
407 if (types->type & TCL_GLOB_TYPE_LINK) {
408 if (TclpObjLstat(tempName, &buf) == 0) {
409 if (S_ISLNK(buf.st_mode)) {
420 if (((okType != 0) && (okType !=
421 fileInfo.ioFlFndrInfo.fdType)) ||
422 ((okCreator != 0) && (okCreator !=
423 fileInfo.ioFlFndrInfo.fdCreator))) {
432 *----------------------------------------------------------------------
436 * This function replaces the library version of access().
439 * See access documentation.
442 * See access documentation.
444 *----------------------------------------------------------------------
448 TclpObjAccess(pathPtr, mode)
460 err = FspLLocationFromFsPath(pathPtr, &fileSpec);
463 errno = TclMacOSErrorToPosixError(err);
468 * Fill the fpb & vpb struct up with info about file or directory.
470 FSpGetDirectoryID(&fileSpec, &dirID, &isDirectory);
471 vpb.ioVRefNum = fpb.ioVRefNum = fileSpec.vRefNum;
472 vpb.ioNamePtr = fpb.ioNamePtr = fileSpec.name;
474 fpb.ioDirID = fileSpec.parID;
480 err = PBGetCatInfoSync((CInfoPBPtr)&fpb);
483 err = PBHGetVInfoSync((HParmBlkPtr)&vpb);
486 * Use the Volume Info & File Info to determine
487 * access information. If we have got this far
488 * we know the directory is searchable or the file
489 * exists. (We have F_OK)
493 * Check to see if the volume is hardware or
494 * software locked. If so we arn't W_OK.
497 if ((vpb.ioVAtrb & 0x0080) || (vpb.ioVAtrb & 0x8000)) {
501 if (fpb.ioFlAttrib & 0x01) {
508 * Directories are always searchable and executable. But only
509 * files of type 'APPL' are executable.
511 if (!(fpb.ioFlAttrib & 0x10) && (mode & X_OK)
512 && (fpb.ioFlFndrInfo.fdType != 'APPL')) {
519 errno = TclMacOSErrorToPosixError(err);
527 *----------------------------------------------------------------------
531 * This function replaces the library version of chdir().
534 * See chdir() documentation.
537 * See chdir() documentation. Also the cache maintained used by
538 * Tcl_FSGetCwd() is deallocated and set to NULL.
540 *----------------------------------------------------------------------
544 TclpObjChdir(pathPtr)
552 err = FspLocationFromFsPath(pathPtr, &spec);
559 err = FSpGetDirectoryID(&spec, &dirID, &isFolder);
565 if (isFolder != true) {
570 err = FSpSetDefaultDir(&spec);
573 case afpAccessDenied:
586 *----------------------------------------------------------------------
590 * This function replaces the library version of getcwd().
593 * The result is a pointer to a string specifying the current
594 * directory, or NULL if the current directory could not be
595 * determined. If NULL is returned, an error message is left in the
596 * interp's result. Storage for the result string is allocated in
597 * bufferPtr; the caller must call Tcl_DStringFree() when the result
598 * is no longer needed.
603 *----------------------------------------------------------------------
607 TclpObjGetCwd(interp)
611 if (TclpGetCwd(interp, &ds) != NULL) {
612 Tcl_Obj *cwdPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1);
613 Tcl_IncrRefCount(cwdPtr);
614 Tcl_DStringFree(&ds);
623 Tcl_Interp *interp, /* If non-NULL, used for error reporting. */
624 Tcl_DString *bufferPtr) /* Uninitialized or free DString filled
625 * with name of current directory. */
629 Handle pathHandle = NULL;
631 if (FSpGetDefaultDir(&theSpec) != noErr) {
632 if (interp != NULL) {
633 Tcl_SetResult(interp, "error getting working directory name",
638 if (FSpPathFromLocation(&theSpec, &length, &pathHandle) != noErr) {
639 if (interp != NULL) {
640 Tcl_SetResult(interp, "error getting working directory name",
646 Tcl_ExternalToUtfDString(NULL, *pathHandle, length, bufferPtr);
648 DisposeHandle(pathHandle);
650 return Tcl_DStringValue(bufferPtr);
654 *----------------------------------------------------------------------
658 * This function replaces the library version of readlink().
661 * The result is a pointer to a string specifying the contents
662 * of the symbolic link given by 'path', or NULL if the symbolic
663 * link could not be read. Storage for the result string is
664 * allocated in bufferPtr; the caller must call Tcl_DStringFree()
665 * when the result is no longer needed.
668 * See readlink() documentation.
670 *---------------------------------------------------------------------------
675 CONST char *path, /* Path of file to readlink (UTF-8). */
676 Tcl_DString *linkPtr) /* Uninitialized or free DString filled
677 * with contents of link (UTF-8). */
687 Handle theString = NULL;
691 Tcl_UtfToExternalDString(NULL, path, -1, &ds);
694 * Remove ending colons if they exist.
697 while ((Tcl_DStringLength(&ds) != 0)
698 && (Tcl_DStringValue(&ds)[Tcl_DStringLength(&ds) - 1] == ':')) {
699 Tcl_DStringSetLength(&ds, Tcl_DStringLength(&ds) - 1);
702 end = strrchr(Tcl_DStringValue(&ds), ':');
704 strcpy(fileName + 1, Tcl_DStringValue(&ds));
706 strcpy(fileName + 1, end + 1);
707 Tcl_DStringSetLength(&ds, end + 1 - Tcl_DStringValue(&ds));
709 fileName[0] = (char) strlen(fileName + 1);
712 * Create the file spec for the directory of the file
713 * we want to look at.
717 err = FSpLocationFromPath(Tcl_DStringLength(&ds),
718 Tcl_DStringValue(&ds), &fileSpec);
720 Tcl_DStringFree(&ds);
725 FSMakeFSSpecCompat(0, 0, NULL, &fileSpec);
727 Tcl_DStringFree(&ds);
730 * Fill the fpb struct up with info about file or directory.
733 FSpGetDirectoryID(&fileSpec, &dirID, &isDirectory);
734 fpb.ioVRefNum = fileSpec.vRefNum;
736 fpb.ioNamePtr = (StringPtr) fileName;
739 err = PBGetCatInfoSync((CInfoPBPtr)&fpb);
741 errno = TclMacOSErrorToPosixError(err);
744 if (fpb.ioFlAttrib & 0x10) {
748 if (fpb.ioFlFndrInfo.fdFlags & 0x8000) {
750 * The file is a link!
760 * If we are here it's really a link - now find out
761 * where it points to.
763 err = FSMakeFSSpecCompat(fileSpec.vRefNum, dirID, (StringPtr) fileName,
766 err = ResolveAliasFile(&fileSpec, true, &isDirectory, &wasAlias);
768 if ((err == fnfErr) || wasAlias) {
769 err = FSpPathFromLocation(&fileSpec, &pathSize, &theString);
771 DisposeHandle(theString);
772 errno = ENAMETOOLONG;
780 Tcl_ExternalToUtfDString(NULL, *theString, pathSize, linkPtr);
781 DisposeHandle(theString);
783 return Tcl_DStringValue(linkPtr);
787 TclpObjStatAlias _ANSI_ARGS_((Tcl_Obj *pathPtr, Tcl_StatBuf *bufPtr,
788 Boolean resolveLink));
792 *----------------------------------------------------------------------
796 * This function replaces the library version of lstat().
799 * See lstat() documentation.
802 * See lstat() documentation.
804 *----------------------------------------------------------------------
808 TclpObjLstat(pathPtr, buf)
812 return TclpObjStatAlias(pathPtr, buf, FALSE);
816 *----------------------------------------------------------------------
820 * This function replaces the library version of stat().
823 * See stat() documentation.
826 * See stat() documentation.
828 *----------------------------------------------------------------------
832 TclpObjStat(pathPtr, bufPtr)
836 return TclpObjStatAlias(pathPtr, bufPtr, TRUE);
841 TclpObjStatAlias (Tcl_Obj *pathPtr, Tcl_StatBuf *bufPtr, Boolean resolveLink)
851 err = FspLocationFromFsPath(pathPtr, &fileSpec);
853 err = FspLLocationFromFsPath(pathPtr, &fileSpec);
856 errno = TclMacOSErrorToPosixError(err);
861 * Fill the fpb & vpb struct up with info about file or directory.
864 FSpGetDirectoryID(&fileSpec, &dirID, &isDirectory);
865 vpb.ioVRefNum = fpb.ioVRefNum = fileSpec.vRefNum;
866 vpb.ioNamePtr = fpb.ioNamePtr = fileSpec.name;
868 fpb.ioDirID = fileSpec.parID;
874 err = PBGetCatInfoSync((CInfoPBPtr)&fpb);
877 err = PBHGetVInfoSync((HParmBlkPtr)&vpb);
878 if (err == noErr && bufPtr != NULL) {
880 * Files are always readable by everyone.
883 bufPtr->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
886 * Use the Volume Info & File Info to fill out stat buf.
888 if (fpb.ioFlAttrib & 0x10) {
889 bufPtr->st_mode |= S_IFDIR;
890 bufPtr->st_nlink = 2;
892 bufPtr->st_nlink = 1;
893 if (fpb.ioFlFndrInfo.fdFlags & 0x8000) {
894 bufPtr->st_mode |= S_IFLNK;
896 bufPtr->st_mode |= S_IFREG;
899 if ((fpb.ioFlAttrib & 0x10) || (fpb.ioFlFndrInfo.fdType == 'APPL')) {
901 * Directories and applications are executable by everyone.
904 bufPtr->st_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
906 if ((fpb.ioFlAttrib & 0x01) == 0){
908 * If not locked, then everyone has write acces.
911 bufPtr->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
913 bufPtr->st_ino = fpb.ioDirID;
914 bufPtr->st_dev = fpb.ioVRefNum;
918 bufPtr->st_size = fpb.ioFlLgLen;
919 bufPtr->st_blksize = vpb.ioVAlBlkSiz;
920 bufPtr->st_blocks = (bufPtr->st_size + bufPtr->st_blksize - 1)
921 / bufPtr->st_blksize;
924 * The times returned by the Mac file system are in the
925 * local time zone. We convert them to GMT so that the
926 * epoch starts from GMT. This is also consistent with
927 * what is returned from "clock seconds".
930 bufPtr->st_atime = bufPtr->st_mtime = fpb.ioFlMdDat
931 - TclpGetGMTOffset() + tcl_mac_epoch_offset;
932 bufPtr->st_ctime = fpb.ioFlCrDat - TclpGetGMTOffset()
933 + tcl_mac_epoch_offset;
938 errno = TclMacOSErrorToPosixError(err);
941 return (err == noErr ? 0 : -1);
945 *----------------------------------------------------------------------
949 * Fakes a call to wait pid.
957 *----------------------------------------------------------------------
970 *----------------------------------------------------------------------
974 * This function replaces fopen. It supports paths with alises.
975 * Note, remember to undefine the fopen macro!
978 * See fopen documentation.
981 * See fopen documentation.
983 *----------------------------------------------------------------------
994 Handle pathString = NULL;
998 err = FSpLocationFromPath(strlen(path), path, &fileSpec);
999 if ((err != noErr) && (err != fnfErr)) {
1002 err = FSpPathFromLocation(&fileSpec, &size, &pathString);
1003 if ((err != noErr) && (err != fnfErr)) {
1008 f = fopen(*pathString, mode);
1009 HUnlock(pathString);
1010 DisposeHandle(pathString);
1015 *---------------------------------------------------------------------------
1017 * TclpGetUserHome --
1019 * This function takes the specified user name and finds their
1023 * The result is a pointer to a string specifying the user's home
1024 * directory, or NULL if the user's home directory could not be
1025 * determined. Storage for the result string is allocated in
1026 * bufferPtr; the caller must call Tcl_DStringFree() when the result
1027 * is no longer needed.
1032 *----------------------------------------------------------------------
1036 TclpGetUserHome(name, bufferPtr)
1037 CONST char *name; /* User name for desired home directory. */
1038 Tcl_DString *bufferPtr; /* Uninitialized or free DString filled
1039 * with name of user's home directory. */
1045 *----------------------------------------------------------------------
1047 * TclMacOSErrorToPosixError --
1049 * Given a Macintosh OSErr return the appropiate POSIX error.
1057 *----------------------------------------------------------------------
1061 TclMacOSErrorToPosixError(
1062 int error) /* A Macintosh error. */
1068 return ENAMETOOLONG;
1069 case afpObjectTypeErr:
1085 case afpAccessDenied:
1107 strcpy((char *) pathName + 1, path);
1108 pathName[0] = strlen(path);
1109 hpb.fileParam.ioNamePtr = pathName;
1110 hpb.fileParam.ioVRefNum = 0;
1111 hpb.fileParam.ioDirID = 0;
1114 err = PBHRstFLockSync(&hpb);
1116 err = PBHSetFLockSync(&hpb);
1120 errno = TclMacOSErrorToPosixError(err);
1129 *----------------------------------------------------------------------
1131 * TclpTempFileName --
1133 * This function returns a unique filename.
1136 * Returns a valid Tcl_Obj* with refCount 0, or NULL on failure.
1141 *----------------------------------------------------------------------
1147 char fileName[L_tmpnam];
1149 if (tmpnam(fileName) == NULL) { /* INTL: Native. */
1153 return TclpNativeToNormalized((ClientData) fileName);
1159 TclpObjLink(pathPtr, toPtr, linkAction)
1164 Tcl_Obj* link = NULL;
1166 if (toPtr != NULL) {
1167 if (TclpObjAccess(pathPtr, F_OK) != -1) {
1172 if (TclpObjAccess(toPtr, F_OK) == -1) {
1173 /* target doesn't exist */
1178 if (linkAction & TCL_CREATE_SYMBOLIC_LINK) {
1179 /* Needs to create a new link */
1185 err = FspLocationFromFsPath(toPtr, &spec);
1191 path = Tcl_FSGetNativePath(pathPtr);
1192 err = FSpLocationFromPath(strlen(path), path, &linkSpec);
1194 err = dupFNErr; /* EEXIST. */
1196 err = CreateAliasFile(&linkSpec, &spec);
1199 errno = TclMacOSErrorToPosixError(err);
1209 Tcl_Obj *transPtr = Tcl_FSGetTranslatedPath(NULL, pathPtr);
1210 if (transPtr == NULL) {
1213 if (TclpReadlink(Tcl_GetString(transPtr), &ds) != NULL) {
1214 link = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1);
1215 Tcl_IncrRefCount(link);
1216 Tcl_DStringFree(&ds);
1218 Tcl_DecrRefCount(transPtr);
1227 *---------------------------------------------------------------------------
1229 * TclpFilesystemPathType --
1231 * This function is part of the native filesystem support, and
1232 * returns the path type of the given path. Right now it simply
1233 * returns NULL. In the future it could return specific path
1234 * types, like 'HFS', 'HFS+', 'nfs', 'samba', 'FAT32', etc.
1242 *---------------------------------------------------------------------------
1245 TclpFilesystemPathType(pathObjPtr)
1246 Tcl_Obj* pathObjPtr;
1248 /* All native paths are of the same type */
1253 *---------------------------------------------------------------------------
1257 * Set the modification date for a file.
1260 * 0 on success, -1 on error.
1265 *---------------------------------------------------------------------------
1268 TclpUtime(pathPtr, tval)
1269 Tcl_Obj *pathPtr; /* File to modify */
1270 struct utimbuf *tval; /* New modification date structure */
1272 long gmt_offset=TclpGetGMTOffset();
1273 struct utimbuf local_tval;
1274 local_tval.actime=tval->actime+gmt_offset;
1275 local_tval.modtime=tval->modtime+gmt_offset;
1276 return utime(Tcl_FSGetNativePath(Tcl_FSGetNormalizedPath(NULL,pathPtr)),
1281 *---------------------------------------------------------------------------
1283 * CreateAliasFile --
1285 * Creates an alias file located at aliasDest referring to the targetFile.
1288 * 0 on success, OS error code on error.
1293 *---------------------------------------------------------------------------
1296 CreateAliasFile(FSSpec *theAliasFile, FSSpec *targetFile)
1300 AliasHandle theAlias;
1301 short saveRef, rsrc = -1;
1304 saveRef = CurResFile();
1305 /* set up the Finder information record for the alias file */
1306 cat.dirInfo.ioNamePtr = targetFile->name;
1307 cat.dirInfo.ioVRefNum = targetFile->vRefNum;
1308 cat.dirInfo.ioFDirIndex = 0;
1309 cat.dirInfo.ioDrDirID = targetFile->parID;
1310 err = PBGetCatInfoSync(&cat);
1311 if (err != noErr) goto bail;
1312 if ((cat.dirInfo.ioFlAttrib & 16) == 0) {
1314 switch (cat.hFileInfo.ioFlFndrInfo.fdType) {
1315 case 'APPL': fndrInfo.fdType = kApplicationAliasType; break;
1316 case 'APPC': fndrInfo.fdType = kApplicationCPAliasType; break;
1317 case 'APPD': fndrInfo.fdType = kApplicationDAAliasType; break;
1318 default: fndrInfo.fdType = cat.hFileInfo.ioFlFndrInfo.fdType; break;
1320 fndrInfo.fdCreator = cat.hFileInfo.ioFlFndrInfo.fdCreator;
1323 fndrInfo.fdType = kContainerFolderAliasType;
1324 fndrInfo.fdCreator = 'MACS';
1326 fndrInfo.fdFlags = kIsAlias;
1327 fndrInfo.fdLocation.v = 0;
1328 fndrInfo.fdLocation.h = 0;
1329 fndrInfo.fdFldr = 0;
1330 /* create new file and set the file information */
1331 FSpCreateResFile( theAliasFile, fndrInfo.fdCreator, fndrInfo.fdType, smSystemScript);
1332 if ((err = ResError()) != noErr) goto bail;
1333 err = FSpSetFInfo( theAliasFile, &fndrInfo);
1334 if (err != noErr) goto bail;
1335 /* save the alias resource */
1336 rsrc = FSpOpenResFile(theAliasFile, fsRdWrPerm);
1337 if (rsrc == -1) { err = ResError(); goto bail; }
1339 err = NewAlias(theAliasFile, targetFile, &theAlias);
1340 if (err != noErr) goto bail;
1341 AddResource((Handle) theAlias, rAliasType, 0, theAliasFile->name);
1342 if ((err = ResError()) != noErr) goto bail;
1347 if (rsrc != -1) CloseResFile(rsrc);
1348 UseResFile(saveRef);