gawk: Internal File Description
1
1 16.6.1 Using 'chdir()' and 'stat()'
1 -----------------------------------
1
1 This minor node shows how to use the new functions at the 'awk' level
1 once they've been integrated into the running 'gawk' interpreter. Using
1 'chdir()' is very straightforward. It takes one argument, the new
1 directory to change to:
1
1 @load "filefuncs"
1 ...
1 newdir = "/home/arnold/funstuff"
1 ret = chdir(newdir)
1 if (ret < 0) {
1 printf("could not change to %s: %s\n", newdir, ERRNO) > "/dev/stderr"
1 exit 1
1 }
1 ...
1
1 The return value is negative if the 'chdir()' failed, and 'ERRNO'
1 (⇒Built-in Variables) is set to a string indicating the error.
1
1 Using 'stat()' is a bit more complicated. The C 'stat()' function
1 fills in a structure that has a fair amount of information. The right
1 way to model this in 'awk' is to fill in an associative array with the
1 appropriate information:
1
1 file = "/home/arnold/.profile"
1 ret = stat(file, fdata)
1 if (ret < 0) {
1 printf("could not stat %s: %s\n",
1 file, ERRNO) > "/dev/stderr"
1 exit 1
1 }
1 printf("size of %s is %d bytes\n", file, fdata["size"])
1
1 The 'stat()' function always clears the data array, even if the
1 'stat()' fails. It fills in the following elements:
1
1 '"name"'
1 The name of the file that was 'stat()'ed.
1
1 '"dev"'
1 '"ino"'
1 The file's device and inode numbers, respectively.
1
1 '"mode"'
1 The file's mode, as a numeric value. This includes both the file's
1 type and its permissions.
1
1 '"nlink"'
1 The number of hard links (directory entries) the file has.
1
1 '"uid"'
1 '"gid"'
1 The numeric user and group ID numbers of the file's owner.
1
1 '"size"'
1 The size in bytes of the file.
1
1 '"blocks"'
1 The number of disk blocks the file actually occupies. This may not
1 be a function of the file's size if the file has holes.
1
1 '"atime"'
1 '"mtime"'
1 '"ctime"'
1 The file's last access, modification, and inode update times,
1 respectively. These are numeric timestamps, suitable for
1 formatting with 'strftime()' (⇒Time Functions).
1
1 '"pmode"'
1 The file's "printable mode." This is a string representation of
1 the file's type and permissions, such as is produced by 'ls
1 -l'--for example, '"drwxr-xr-x"'.
1
1 '"type"'
1 A printable string representation of the file's type. The value is
1 one of the following:
1
1 '"blockdev"'
1 '"chardev"'
1 The file is a block or character device ("special file").
1
1 '"directory"'
1 The file is a directory.
1
1 '"fifo"'
1 The file is a named pipe (also known as a FIFO).
1
1 '"file"'
1 The file is just a regular file.
1
1 '"socket"'
1 The file is an 'AF_UNIX' ("Unix domain") socket in the
1 filesystem.
1
1 '"symlink"'
1 The file is a symbolic link.
1
1 '"devbsize"'
1 The size of a block for the element indexed by '"blocks"'. This
1 information is derived from either the 'DEV_BSIZE' constant defined
1 in '<sys/param.h>' on most systems, or the 'S_BLKSIZE' constant in
1 '<sys/stat.h>' on BSD systems. For some other systems, "a priori"
1 knowledge is used to provide a value. Where no value can be
1 determined, it defaults to 512.
1
1 Several additional elements may be present, depending upon the
1 operating system and the type of the file. You can test for them in
1 Elements::):
1
1 '"blksize"'
1 The preferred block size for I/O to the file. This field is not
1 present on all POSIX-like systems in the C 'stat' structure.
1
1 '"linkval"'
1 If the file is a symbolic link, this element is the name of the
1 file the link points to (i.e., the value of the link).
1
1 '"rdev"'
1 '"major"'
1 '"minor"'
1 If the file is a block or character device file, then these values
1 represent the numeric device number and the major and minor
1 components of that number, respectively.
1