diff options
-rw-r--r-- | sysif.c | 63 | ||||
-rw-r--r-- | txr.1 | 79 |
2 files changed, 79 insertions, 63 deletions
@@ -301,13 +301,38 @@ static val mkdir_wrap(val path, val mode) } #endif +#if HAVE_CHMOD || HAVE_CHOWN || HAVE_SYS_STAT || HAVE_FILE_STAMP_CHANGE +static int get_fd(val stream, val self) +{ + val fd_in = if3(integerp(stream), stream, stream_get_prop(stream, fd_k)); + + if (stream && !fd_in) + uw_throwf(file_error_s, + lit("~a: stream ~s has no :fd property"), + self, stream, nao); + + if (!stream) + uw_throwf(file_error_s, + lit("~a: ~s isn't a stream object"), + self, stream, nao); + + return c_int(fd_in, self); +} +#endif + #if HAVE_SYS_STAT static int do_stat(val wpath, struct stat *buf) { - char *path = utf8_dup_to(c_str(wpath)); - int res = stat(path, buf); - free(path); - return res; + if (stringp(wpath)) { + char *path = utf8_dup_to(c_str(wpath)); + int res = stat(path, buf); + free(path); + return res; + } else { + val self = lit("stat"); + int fd = get_fd(wpath, self); + return fstat(fd, buf); + } } #ifdef S_IFLNK @@ -522,25 +547,6 @@ static val mkfifo_wrap(val path, val mode) #endif -#if HAVE_CHMOD || HAVE_CHOWN || HAVE_SYS_STAT || HAVE_FILE_STAMP_CHANGE -static int get_fd(val stream, val self) -{ - val fd_in = if3(integerp(stream), stream, stream_get_prop(stream, fd_k)); - - if (stream && !fd_in) - uw_throwf(file_error_s, - lit("~a: stream ~s has no :fd property"), - self, stream, nao); - - if (!stream) - uw_throwf(file_error_s, - lit("~a: ~s isn't a stream object"), - self, stream, nao); - - return c_int(fd_in, self); -} -#endif - #if HAVE_CHMOD #define CHM_O 4 @@ -1030,15 +1036,6 @@ static val exit_star_wrap(val status) #endif -#if HAVE_SYS_STAT -static int do_fstat(val stream, struct stat *buf) -{ - val self = lit("fstat"); - int fd = get_fd(stream, self); - return fstat(fd, buf); -} -#endif - time_t c_time(val time) { return if3(convert(time_t, -1) > 0, c_unum(time), c_num(time)); @@ -1143,7 +1140,7 @@ static val lstat_wrap(val path) val fstat_wrap(val stream) { - return stat_impl(stream, do_fstat, lit("fstat"), nil); + return stat_impl(stream, do_stat, lit("fstat"), nil); } #if HAVE_FILE_STAMP_CHANGE @@ -57742,10 +57742,9 @@ The .code stat structure defines the type of object which is returned by the -.codn stat , -.codn lstat , +.code stat and -.code fstat +.code lstat functions. Except for .codn path , .codn atime-nsec , @@ -57788,21 +57787,33 @@ fields take on values of zero. .coNP Functions @, stat @ lstat and @ fstat .synb -.mets (stat << path ) +.mets (stat > { path | < stream | << fd }) .mets (lstat << path ) -.mets (fstat > { stream | << fd }) +.mets (fstat > { path | stream | << fd }) .syne .desc The .code stat -function inquires the filesystem about the existence of an object -denoted by the string -.metn path . +function retrieves information about a filesystem object whose pathname +is given by the string argument +.metn path , +or else about a system object associated with the open stream +.metn stream , +or one associated with the integer file descriptor +.metn fd . + +If a +.meta stream +is specified, that stream must be of a kind from which the +.code fileno +function can retrieve a file descriptor, otherwise an exception of type +.code file-error +is thrown. + If the object is not found or cannot be accessed, an exception is thrown. - -Otherwise, information is retrieved about the object. The information takes -the form of a structure of type +Otherwise, information is retrieved and returned, in the form +of a structure of type .codn stat . If @@ -57817,35 +57828,43 @@ The .code lstat function behaves the same as .code stat -or objects which are not symbolic links. For a symbolic link, it retrieves +on objects which are not symbolic links. For a symbolic link, it retrieves information about the link itself, rather than its target. The -.code fstat -function retrieves information about the file system object associated with -the open stream -.meta stream -or numeric file descriptor -.metn fd . -If a -.meta stream -is specified, that stream must be of a kind from which the -.code fileno -function can retrieve a file descriptor, otherwise an exception of type -.code file-error -is thrown. - -The .code path slot of the returned structure holds a copy of their .meta path argument value. -In the case of -.codn fstat , -this slot is +When information is retrieved using a +.meta stream +or +.meta fd +argument, this slot is .codn nil . +The +.code fstat +function is an alias for +.codn stat . + +Note: until \*(TX 231, +.code stat +and +.code fstat +were distinct functions: +.code stat +accepted only +.meta path +arguments, whereas +.code fstat +function accepted only +.meta stream +or +.meta fd +arguments. + .coNP Variables @, s-ifmt @, s-iflnk @, s-ifreg @, s-ifblk ... , @ s-ixoth .desc The following variables exist, having integer values. These are bitmasks |