Blame extension/filefuncs.3am

Packit 575503
.TH FILEFUNCS 3am "Jan 15 2013" "Free Software Foundation" "GNU Awk Extension Modules"
Packit 575503
.SH NAME
Packit 575503
filefuncs \- provide some file related functionality to gawk
Packit 575503
.SH SYNOPSIS
Packit 575503
.ft CW
Packit 575503
@load "filefuncs"
Packit 575503
.sp
Packit 575503
result = chdir("/some/directory")
Packit 575503
.sp
Packit 575503
result = stat("/some/path", statdata [, follow])
Packit 575503
.sp
Packit 575503
flags = or(FTS_PHYSICAL, ...)
Packit 575503
.br
Packit 575503
result = fts(pathlist, flags, filedata)
Packit 575503
.ft R
Packit 575503
.SH DESCRIPTION
Packit 575503
The
Packit 575503
.I filefuncs
Packit 575503
extension adds several functions that provide access to
Packit 575503
file-related facilities.
Packit 575503
.SS chdir()
Packit 575503
The
Packit 575503
.B chdir()
Packit 575503
function is a direct hook to the
Packit 575503
.IR chdir (2)
Packit 575503
system call to change the current directory.
Packit 575503
It returns zero
Packit 575503
upon success or less than zero upon error.
Packit 575503
In the latter case it updates
Packit 575503
.BR ERRNO .
Packit 575503
.SS stat()
Packit 575503
The
Packit 575503
.B stat()
Packit 575503
function provides a hook into the
Packit 575503
.IR stat (2)
Packit 575503
system call.
Packit 575503
It returns zero
Packit 575503
upon success or less than zero upon error.
Packit 575503
In the latter case it updates
Packit 575503
.BR ERRNO .
Packit 575503
By default, it uses
Packit 575503
.IR lstat (2).
Packit 575503
However, if passed a third argument, it uses
Packit 575503
.IR stat (2),
Packit 575503
instead.
Packit 575503
.PP
Packit 575503
In all cases, it clears the
Packit 575503
.B statdata
Packit 575503
array.
Packit 575503
When the call is successful,
Packit 575503
.B stat()
Packit 575503
fills the
Packit 575503
.B statdata
Packit 575503
array with information retrieved from the filesystem, as follows:
Packit 575503
.TP
Packit 575503
\fBstatdata["name"]\fP
Packit 575503
The name of the file.
Packit 575503
.TP
Packit 575503
\fBstatdata["dev"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_dev
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["ino"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_ino
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["mode"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_mode
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["nlink"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_nlink
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["uid"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_uid
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["gid"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_gid
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["size"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_size
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["atime"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_atime
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["mtime"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_mtime
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["ctime"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_ctime
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
.TP
Packit 575503
\fBstatdata["rdev"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_rdev
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
This element is only present for device files.
Packit 575503
.TP
Packit 575503
\fBstatdata["major"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_major
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
This element is only present for device files.
Packit 575503
.TP
Packit 575503
\fBstatdata["minor"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_minor
Packit 575503
field in the
Packit 575503
.IR "struct stat" .
Packit 575503
This element is only present for device files.
Packit 575503
.TP
Packit 575503
\fBstatdata["blksize"]\fP
Packit 575503
Corresponds to the
Packit 575503
.I st_blksize
Packit 575503
field in the
Packit 575503
.IR "struct stat" ,
Packit 575503
if this field is present on your system.
Packit 575503
(It is present on all modern systems that we know of.)
Packit 575503
.TP
Packit 575503
\fBstatdata["pmode"]\fP
Packit 575503
A human-readable version of the mode value, such as printed by
Packit 575503
.IR ls (1).
Packit 575503
For example, \fB"-rwxr-xr-x"\fP.
Packit 575503
.TP
Packit 575503
\fBstatdata["linkval"]\fP
Packit 575503
If the named file is a symbolic link, this element will exist
Packit 575503
and its value is the value of the symbolic link (where the
Packit 575503
symbolic link points to).
Packit 575503
.TP
Packit 575503
\fBstatdata["type"]\fP
Packit 575503
The type of the file as a string. One of
Packit 575503
\fB"file"\fP,
Packit 575503
\fB"blockdev"\fP,
Packit 575503
\fB"chardev"\fP,
Packit 575503
\fB"directory"\fP,
Packit 575503
\fB"socket"\fP,
Packit 575503
\fB"fifo"\fP,
Packit 575503
\fB"symlink"\fP,
Packit 575503
\fB"door"\fP,
Packit 575503
or
Packit 575503
\fB"unknown"\fP.
Packit 575503
Not all systems support all file types.
Packit 575503
.SS fts()
Packit 575503
The
Packit 575503
.B fts()
Packit 575503
function provides a hook to the
Packit 575503
.IR fts (3)
Packit 575503
set of routines for traversing file hierarchies.
Packit 575503
Instead of returning data about one file at a time in a stream,
Packit 575503
it fills in a multi-dimensional array with data about each file and
Packit 575503
directory encountered in the requested hierarchies.
Packit 575503
.PP
Packit 575503
The arguments are as follows:
Packit 575503
.TP
Packit 575503
.B pathlist
Packit 575503
An array of filenames.  The element values are used; the index values are ignored.
Packit 575503
.TP
Packit 575503
.B flags
Packit 575503
This should be the bitwise OR of one or more of the following
Packit 575503
predefined flag values.  At least one of
Packit 575503
.B FTS_LOGICAL
Packit 575503
or
Packit 575503
.B FTS_PHYSICAL
Packit 575503
must be provided; otherwise
Packit 575503
.B fts()
Packit 575503
returns an error value and sets
Packit 575503
.BR ERRNO .
Packit 575503
.RS
Packit 575503
.TP
Packit 575503
.B FTS_LOGICAL
Packit 575503
Do a ``logical'' file traversal, where the information returned for
Packit 575503
a symbolic link refers to the linked-to file, and not to the
Packit 575503
symbolic link itself.
Packit 575503
This flag is mutually exclusive with
Packit 575503
.BR FTS_PHYSICAL .
Packit 575503
.TP
Packit 575503
.B FTS_PHYSICAL
Packit 575503
Do a ``physical'' file traversal, where the information returned for
Packit 575503
a symbolic link refers to the symbolic link itself.
Packit 575503
This flag is mutually exclusive with
Packit 575503
.BR FTS_LOGICAL .
Packit 575503
.TP
Packit 575503
.B FTS_NOCHDIR
Packit 575503
As a performance optimization, the
Packit 575503
.IR fts (3)
Packit 575503
routines change directory as they traverse a file hierarchy.
Packit 575503
This flag disables that optimization.
Packit 575503
.TP
Packit 575503
.B FTS_COMFOLLOW
Packit 575503
Immediately follow a symbolic link named in
Packit 575503
.BR pathlist ,
Packit 575503
whether or not
Packit 575503
.B FTS_LOGICAL
Packit 575503
is set.
Packit 575503
.TP
Packit 575503
.B FTS_SEEDOT
Packit 575503
By default, the
Packit 575503
.IR fts (3)
Packit 575503
routines do not return entries for ``.'' and ``..''.
Packit 575503
This option causes entries for ``..'' to also be included.
Packit 575503
(The AWK extension always includes an entry for ``.'', see below.)
Packit 575503
.TP
Packit 575503
.B FTS_XDEV
Packit 575503
During a traversal, do not cross onto a different mounted filesystem.
Packit 575503
.RE
Packit 575503
.TP
Packit 575503
.B filedata
Packit 575503
The
Packit 575503
.B filedata
Packit 575503
array is first cleared.
Packit 575503
Then,
Packit 575503
.B fts()
Packit 575503
creates an element in
Packit 575503
.B filedata
Packit 575503
for every element in
Packit 575503
.BR pathlist .
Packit 575503
The index is the name of the directory or file given in
Packit 575503
.BR pathlist .
Packit 575503
The element for this index is itself an array.
Packit 575503
There are two cases.
Packit 575503
.RS
Packit 575503
.TP
Packit 575503
The path is a file.
Packit 575503
In this case, the array contains two or three elements:
Packit 575503
.RS
Packit 575503
.TP
Packit 575503
\fB"path"\fP
Packit 575503
The full path to this file, starting from the ``root'' that was given
Packit 575503
in the
Packit 575503
.B pathlist
Packit 575503
array.
Packit 575503
.TP
Packit 575503
\fB"stat"\fP
Packit 575503
This element is itself an array, containing the same information as provided
Packit 575503
by the
Packit 575503
.B stat()
Packit 575503
function described earlier for its
Packit 575503
.B statdata
Packit 575503
argument.
Packit 575503
The element may not be present if
Packit 575503
.IR stat (2)
Packit 575503
for the file failed.
Packit 575503
.TP
Packit 575503
\fB"error"\fP
Packit 575503
If some kind of error was encountered, the array will also
Packit 575503
contain an element named \fB"error"\fP, which is a string describing the error.
Packit 575503
.RE
Packit 575503
.TP
Packit 575503
The path is a directory.
Packit 575503
In this case, the array contains one element for each entry in the directory.
Packit 575503
If an entry is a file, that element is as for files, just described.
Packit 575503
If the entry is a directory, that element is (recursively), an array describing
Packit 575503
the subdirectory.
Packit 575503
If
Packit 575503
.B FTS_SEEDOT
Packit 575503
was provided in the flags, then there will also be an element named
Packit 575503
\fB".."\fP.  This element will be an array containing the data
Packit 575503
as provided by
Packit 575503
.BR stat() .
Packit 575503
.sp
Packit 575503
In addition, there will be an element whose index is \fB"."\fP.
Packit 575503
This element is an array containing the same two or three elements
Packit 575503
as for a file:
Packit 575503
\fB"path"\fP,
Packit 575503
\fB"stat"\fP,
Packit 575503
and
Packit 575503
\fB"error"\fP.
Packit 575503
.RE
Packit 575503
.PP
Packit 575503
The
Packit 575503
.B fts()
Packit 575503
function returns 0 if there were no errors. Otherwise it returns \-1.
Packit 575503
.SH NOTES
Packit 575503
The AWK
Packit 575503
.B fts()
Packit 575503
extension does not exactly mimic the interface of the
Packit 575503
.IR fts (3)
Packit 575503
routines, choosing instead to provide an interface that is based
Packit 575503
on associative arrays, which should be more comfortable to use from
Packit 575503
an AWK program.  This includes the lack of a comparison function, since
Packit 575503
.I gawk
Packit 575503
already provides powerful array sorting facilities.  While an
Packit 575503
.IR fts_read() \-like
Packit 575503
interface could have been provided, this felt less natural than
Packit 575503
simply creating a multi-dimensional array to represent the file
Packit 575503
hierarchy and its information.
Packit 575503
.PP
Packit 575503
Nothing prevents AWK code from changing the predefined
Packit 575503
.BI FTS_ xx
Packit 575503
values, but doing so may cause strange results when
Packit 575503
the changed values are passed to
Packit 575503
.BR fts() .
Packit 575503
.SH BUGS
Packit 575503
There are many more file-related functions for which AWK
Packit 575503
interfaces would be desirable.
Packit 575503
.SH EXAMPLE
Packit 575503
See
Packit 575503
.B test/fts.awk
Packit 575503
in the
Packit 575503
.I gawk
Packit 575503
distribution for an example.
Packit 575503
.SH "SEE ALSO"
Packit 575503
.IR "GAWK: Effective AWK Programming" ,
Packit 575503
.IR fnmatch (3am),
Packit 575503
.IR fork (3am),
Packit 575503
.IR inplace (3am),
Packit 575503
.IR ordchr (3am),
Packit 575503
.IR readdir (3am),
Packit 575503
.IR readfile (3am),
Packit 575503
.IR revoutput (3am),
Packit 575503
.IR rwarray (3am),
Packit 575503
.IR time (3am).
Packit 575503
.PP
Packit 575503
.IR chdir (2),
Packit 575503
.IR fts (3),
Packit 575503
.IR stat (2).
Packit 575503
.SH AUTHOR
Packit 575503
Arnold Robbins,
Packit 575503
.BR arnold@skeeve.com .
Packit 575503
.SH COPYING PERMISSIONS
Packit 575503
Copyright \(co 2012, 2013,
Packit 575503
Free Software Foundation, Inc.
Packit 575503
.PP
Packit 575503
Permission is granted to make and distribute verbatim copies of
Packit 575503
this manual page provided the copyright notice and this permission
Packit 575503
notice are preserved on all copies.
Packit 575503
.ig
Packit 575503
Permission is granted to process this file through troff and print the
Packit 575503
results, provided the printed document carries copying permission
Packit 575503
notice identical to this one except for the removal of this paragraph
Packit 575503
(this paragraph not being relevant to the printed manual page).
Packit 575503
..
Packit 575503
.PP
Packit 575503
Permission is granted to copy and distribute modified versions of this
Packit 575503
manual page under the conditions for verbatim copying, provided that
Packit 575503
the entire resulting derived work is distributed under the terms of a
Packit 575503
permission notice identical to this one.
Packit 575503
.PP
Packit 575503
Permission is granted to copy and distribute translations of this
Packit 575503
manual page into another language, under the above conditions for
Packit 575503
modified versions, except that this permission notice may be stated in
Packit 575503
a translation approved by the Foundation.
Packit 575503
.\" vim: set filetype=nroff :