Free Software Foundation
Feb 21 2018
filefuncs - provide some file related functionality to gawk
result = chdir("/some/directory")
result = stat("/some/path", statdata [, follow])
flags = or(FTS_PHYSICAL, ...)
result = fts(pathlist, flags, filedata)
The filefuncs extension adds several functions that provide access to file-related facilities.
The chdir() function is a direct hook to the chdir(2) system call to change the current directory. It returns zero upon success or less than zero upon error. In the latter case it updates ERRNO.
The stat() function provides a hook into the stat(2) system call. It returns zero upon success or less than zero upon error. In the latter case it updates ERRNO. By default, it uses lstat(2). However, if passed a third argument, it uses stat(2), instead.
In all cases, it clears the statdata array. When the call is successful, stat() fills the statdata array with information retrieved from the filesystem, as follows:
|The name of the file.|
|Corresponds to the st_dev field in the struct stat.|
|Corresponds to the st_ino field in the struct stat.|
|Corresponds to the st_mode field in the struct stat.|
|Corresponds to the st_nlink field in the struct stat.|
|Corresponds to the st_uid field in the struct stat.|
|Corresponds to the st_gid field in the struct stat.|
|Corresponds to the st_size field in the struct stat.|
|Corresponds to the st_atime field in the struct stat.|
|Corresponds to the st_mtime field in the struct stat.|
|Corresponds to the st_ctime field in the struct stat.|
|Corresponds to the st_rdev field in the struct stat. This element is only present for device files.|
|Corresponds to the st_major field in the struct stat. This element is only present for device files.|
|Corresponds to the st_minor field in the struct stat. This element is only present for device files.|
|Corresponds to the st_blksize field in the struct stat, if this field is present on your system. (It is present on all modern systems that we know of.)|
|A human-readable version of the mode value, such as printed by ls(1). For example, "-rwxr-xr-x".|
|If the named file is a symbolic link, this element will exist and its value is the value of the symbolic link (where the symbolic link points to).|
|The type of the file as a string. One of "file", "blockdev", "chardev", "directory", "socket", "fifo", "symlink", "door", or "unknown". Not all systems support all file types.|
The fts() function provides a hook to the fts(3) set of routines for traversing file hierarchies. Instead of returning data about one file at a time in a stream, it fills in a multi-dimensional array with data about each file and directory encountered in the requested hierarchies.
The fts() function returns 0 if there were no errors. Otherwise it returns -1.
The arguments are as follows:
|An array of filenames. The element values are used; the index values are ignored.|
|flags||This should be the bitwise OR of one or more of the following predefined flag values. At least one of FTS_LOGICAL or FTS_PHYSICAL must be provided; otherwise fts() returns an error value and sets ERRNO.
|The filedata array is first cleared. Then, fts() creates an element in filedata for every element in pathlist. The index is the name of the directory or file given in pathlist. The element for this index is itself an array. There are two cases.
The AWK fts() extension does not exactly mimic the interface of the fts(3) routines, choosing instead to provide an interface that is based on associative arrays, which should be more comfortable to use from an AWK program. This includes the lack of a comparison function, since gawk already provides powerful array sorting facilities. While an fts_read()-like interface could have been provided, this felt less natural than simply creating a multi-dimensional array to represent the file hierarchy and its information.
Nothing prevents AWK code from changing the predefined FTS_xx values, but doing so may cause strange results when the changed values are passed to fts().
There are many more file-related functions for which AWK interfaces would be desirable.
It’s not clear why I thought adding FTS_SKIP was a good idea.
See test/fts.awk in the gawk distribution for an example.
Arnold Robbins, arnold.
Copyright © 2012, 2013, 2018, Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this manual page provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual page under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual page into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation.