📄 tclunixfile.c
字号:
((types->type & TCL_GLOB_TYPE_CHAR) && S_ISCHR(buf.st_mode)) || ((types->type & TCL_GLOB_TYPE_DIR) && S_ISDIR(buf.st_mode)) || ((types->type & TCL_GLOB_TYPE_PIPE) && S_ISFIFO(buf.st_mode)) || ((types->type & TCL_GLOB_TYPE_FILE) && S_ISREG(buf.st_mode))#ifdef S_ISSOCK || ((types->type & TCL_GLOB_TYPE_SOCK) && S_ISSOCK(buf.st_mode))#endif /* S_ISSOCK */ ) { /* Do nothing -- this file is ok */ } else {#ifdef S_ISLNK if (types->type & TCL_GLOB_TYPE_LINK) { if (TclOSlstat(nativeEntry, &buf) == 0) { if (S_ISLNK(buf.st_mode)) { return 1; } } }#endif /* S_ISLNK */ return 0; } } } return 1;}/* *--------------------------------------------------------------------------- * * TclpGetUserHome -- * * This function takes the specified user name and finds their * home directory. * * Results: * The result is a pointer to a string specifying the user's home * directory, or NULL if the user's home directory could not be * determined. Storage for the result string is allocated in * bufferPtr; the caller must call Tcl_DStringFree() when the result * is no longer needed. * * Side effects: * None. * *---------------------------------------------------------------------- */char *TclpGetUserHome(name, bufferPtr) CONST char *name; /* User name for desired home directory. */ Tcl_DString *bufferPtr; /* Uninitialized or free DString filled * with name of user's home directory. */{ struct passwd *pwPtr; Tcl_DString ds; CONST char *native; native = Tcl_UtfToExternalDString(NULL, name, -1, &ds); pwPtr = getpwnam(native); /* INTL: Native. */ Tcl_DStringFree(&ds); if (pwPtr == NULL) { endpwent(); return NULL; } Tcl_ExternalToUtfDString(NULL, pwPtr->pw_dir, -1, bufferPtr); endpwent(); return Tcl_DStringValue(bufferPtr);}/* *--------------------------------------------------------------------------- * * TclpObjAccess -- * * This function replaces the library version of access(). * * Results: * See access() documentation. * * Side effects: * See access() documentation. * *--------------------------------------------------------------------------- */int TclpObjAccess(pathPtr, mode) Tcl_Obj *pathPtr; /* Path of file to access */ int mode; /* Permission setting. */{ CONST char *path = Tcl_FSGetNativePath(pathPtr); if (path == NULL) { return -1; } else { return access(path, mode); }}/* *--------------------------------------------------------------------------- * * TclpObjChdir -- * * This function replaces the library version of chdir(). * * Results: * See chdir() documentation. * * Side effects: * See chdir() documentation. * *--------------------------------------------------------------------------- */int TclpObjChdir(pathPtr) Tcl_Obj *pathPtr; /* Path to new working directory */{ CONST char *path = Tcl_FSGetNativePath(pathPtr); if (path == NULL) { return -1; } else { return chdir(path); }}/* *---------------------------------------------------------------------- * * TclpObjLstat -- * * This function replaces the library version of lstat(). * * Results: * See lstat() documentation. * * Side effects: * See lstat() documentation. * *---------------------------------------------------------------------- */int TclpObjLstat(pathPtr, bufPtr) Tcl_Obj *pathPtr; /* Path of file to stat */ Tcl_StatBuf *bufPtr; /* Filled with results of stat call. */{ return TclOSlstat(Tcl_FSGetNativePath(pathPtr), bufPtr);}/* *--------------------------------------------------------------------------- * * TclpObjGetCwd -- * * This function replaces the library version of getcwd(). * * Results: * The result is a pointer to a string specifying the current * directory, or NULL if the current directory could not be * determined. If NULL is returned, an error message is left in the * interp's result. Storage for the result string is allocated in * bufferPtr; the caller must call Tcl_DStringFree() when the result * is no longer needed. * * Side effects: * None. * *---------------------------------------------------------------------- */Tcl_Obj* TclpObjGetCwd(interp) Tcl_Interp *interp;{ Tcl_DString ds; if (TclpGetCwd(interp, &ds) != NULL) { Tcl_Obj *cwdPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1); Tcl_IncrRefCount(cwdPtr); Tcl_DStringFree(&ds); return cwdPtr; } else { return NULL; }}/* Older string based version */CONST char *TclpGetCwd(interp, bufferPtr) Tcl_Interp *interp; /* If non-NULL, used for error reporting. */ Tcl_DString *bufferPtr; /* Uninitialized or free DString filled * with name of current directory. */{ char buffer[MAXPATHLEN+1];#ifdef USEGETWD if (getwd(buffer) == NULL) { /* INTL: Native. */#else if (getcwd(buffer, MAXPATHLEN + 1) == NULL) { /* INTL: Native. */#endif if (interp != NULL) { Tcl_AppendResult(interp, "error getting working directory name: ", Tcl_PosixError(interp), (char *) NULL); } return NULL; } return Tcl_ExternalToUtfDString(NULL, buffer, -1, bufferPtr);}/* *--------------------------------------------------------------------------- * * TclpReadlink -- * * This function replaces the library version of readlink(). * * Results: * The result is a pointer to a string specifying the contents * of the symbolic link given by 'path', or NULL if the symbolic * link could not be read. Storage for the result string is * allocated in bufferPtr; the caller must call Tcl_DStringFree() * when the result is no longer needed. * * Side effects: * See readlink() documentation. * *--------------------------------------------------------------------------- */char *TclpReadlink(path, linkPtr) CONST char *path; /* Path of file to readlink (UTF-8). */ Tcl_DString *linkPtr; /* Uninitialized or free DString filled * with contents of link (UTF-8). */{#ifndef DJGPP char link[MAXPATHLEN]; int length; CONST char *native; Tcl_DString ds; native = Tcl_UtfToExternalDString(NULL, path, -1, &ds); length = readlink(native, link, sizeof(link)); /* INTL: Native. */ Tcl_DStringFree(&ds); if (length < 0) { return NULL; } Tcl_ExternalToUtfDString(NULL, link, length, linkPtr); return Tcl_DStringValue(linkPtr);#else return NULL;#endif}/* *---------------------------------------------------------------------- * * TclpObjStat -- * * This function replaces the library version of stat(). * * Results: * See stat() documentation. * * Side effects: * See stat() documentation. * *---------------------------------------------------------------------- */int TclpObjStat(pathPtr, bufPtr) Tcl_Obj *pathPtr; /* Path of file to stat */ Tcl_StatBuf *bufPtr; /* Filled with results of stat call. */{ CONST char *path = Tcl_FSGetNativePath(pathPtr); if (path == NULL) { return -1; } else { return TclOSstat(path, bufPtr); }}#ifdef S_IFLNKTcl_Obj* TclpObjLink(pathPtr, toPtr, linkAction) Tcl_Obj *pathPtr; Tcl_Obj *toPtr; int linkAction;{ if (toPtr != NULL) { CONST char *src = Tcl_FSGetNativePath(pathPtr); CONST char *target = Tcl_FSGetNativePath(toPtr); if (src == NULL || target == NULL) { return NULL; } if (access(src, F_OK) != -1) { /* src exists */ errno = EEXIST; return NULL; } if (access(target, F_OK) == -1) { /* target doesn't exist */ errno = ENOENT; return NULL; } /* * Check symbolic link flag first, since we prefer to * create these. */ if (linkAction & TCL_CREATE_SYMBOLIC_LINK) { if (symlink(target, src) != 0) return NULL; } else if (linkAction & TCL_CREATE_HARD_LINK) { if (link(target, src) != 0) return NULL; } else { errno = ENODEV; return NULL; } return toPtr; } else { Tcl_Obj* linkPtr = NULL; char link[MAXPATHLEN]; int length; Tcl_DString ds; if (Tcl_FSGetTranslatedPath(NULL, pathPtr) == NULL) { return NULL; } length = readlink(Tcl_FSGetNativePath(pathPtr), link, sizeof(link)); if (length < 0) { return NULL; } Tcl_ExternalToUtfDString(NULL, link, length, &ds); linkPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)); Tcl_DStringFree(&ds); if (linkPtr != NULL) { Tcl_IncrRefCount(linkPtr); } return linkPtr; }}#endif/* *--------------------------------------------------------------------------- * * TclpFilesystemPathType -- * * This function is part of the native filesystem support, and * returns the path type of the given path. Right now it simply * returns NULL. In the future it could return specific path * types, like 'nfs', 'samba', 'FAT32', etc. * * Results: * NULL at present. * * Side effects: * None. * *--------------------------------------------------------------------------- */Tcl_Obj*TclpFilesystemPathType(pathObjPtr) Tcl_Obj* pathObjPtr;{ /* All native paths are of the same type */ return NULL;}/* *--------------------------------------------------------------------------- * * TclpUtime -- * * Set the modification date for a file. * * Results: * 0 on success, -1 on error. * * Side effects: * None. * *--------------------------------------------------------------------------- */int TclpUtime(pathPtr, tval) Tcl_Obj *pathPtr; /* File to modify */ struct utimbuf *tval; /* New modification date structure */{ return utime(Tcl_FSGetNativePath(pathPtr),tval);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -