User Tools

Site Tools


solaris:fts_open.3c

Table of Contents

fts_open


NAME

fts_open, fts_children, fts_set, fts_set_clientptr, fts_get_clientptr, fts_get_stream, fts_close, fts_read - traverse a file hierarchy

SYNOPSIS

# include <fts.h>

FTS * fts_open(char * const *path_argv, int options,
int (*compar)(const FTSENT **, const FTSENT **));

FTSENT * fts_read(FTS *ftsp);

FTSENT * fts_children(FTS *ftsp, int options);

int fts_set(FTS *ftsp, FTSENT *f, int options);

void fts_set_clientptr(FTS *ftsp, void *clientdata);

void * fts_get_clientptr(FTS *ftsp);

FTS * fts_get_stream(FTSENT *f);

int fts_close(FTS *ftsp);

DESCRIPTION

The fts() functions are provided for traversing UNIX file hierarchies. A simple overview is that the fts_open() function returns a “handle” on a file hierarchy, which is then supplied to the other fts() functions.

The fts_read() function returns a pointer to a structure describing one of the files in a directory in the hierarchy.

The fts_children() function returns a pointer to a linked list of structures, each of which describes one of the files in a directory, present in the hierarchy.

In general, directories are visited two distinguishable times, which are, in pre-order, before any of their descendants are visited, and in post-order, after all of their descendants have been visited. Files are visited once. It is possible to walk the hierarchy logically, ignoring symbolic links, or physically, visiting symbolic links, to control the walk by ordering, pruning, or re-visiting portions of the hierarchy.

Two structures are defined in the include file <fts.h>. The first is FTS, the structure that represents the file hierarchy itself. The second is FTSENT, the structure that represents a file in the file hierarchy. Normally, an FTSENT structure is returned for every file in the file hierarchy. In this manual page, “file’’ and “FTSENT structure’’ are generally interchangeable.

The FTS structure contains space for a single pointer, which may be used to store application data or per-hierarchy state. The fts_set_clientptr()and fts_get_clientptr() functions may be used to set and retrieve this pointer respectively. This is likely to be useful only when accessed from the sort comparison function, which can determine the original FTS stream of its arguments using the fts_get_stream() function.

The FTSENT structure contains at least the following fields, which are described in greater detail below:

typedef struct _ftsent {
int fts_info; /* status for FTSENT structure */
char *fts_accpath; /* access path */
char *fts_path; /* root path */
size_t fts_pathlen; /* strlen(fts_path) */
char *fts_name; /* file name */
size_t fts_namelen; /* strlen(fts_name) */
long fts_level; /* depth (-1 to N) */
int fts_errno; /* file errno */
long long fts_number; /* local numeric value */
void *fts_pointer; /* local address value */
struct ftsent *fts_parent; /* parent directory */
struct ftsent *fts_link; /* next file structure */
struct ftsent *fts_cycle; /* cycle structure */
struct stat *fts_statp; /* stat(2) information */
} FTSENT;

These fields are defined as follows:

fts_info()

One of the following values describing the returned FTSENT structure and the file it represents. With the exception of directories without errors (FTS_D), all of these entries are terminal, that is, neither will they be revisited, nor will any of their descendants be visited

FTS_D

A directory being visited in pre-order

FTS_DC

A directory that causes a cycle in the tree. The fts_cycle field of the FTSENT structure will be filled in as well

FTS_DEFAULT

Any FTSENT structure that represents a file type not explicitly described by one of the other fts_info values

FTS_DNR

A directory which cannot be read. This is an error return, and the fts_errno field will be set to indicate what caused the error

FTS_DOT

A file named ’.’or ’..’which was not specified as a file name to fts_open() function. For more information, see the description for FTS_SEEDOT

FTS_DP

A directory being visited in post-order. The contents of the FTSENT structure will be unchanged from when the directory was visited in pre-order, except for the fts_info field

FTS_ERR

This is an error return, and the fts_errno field will be set to indicate what caused the error

FTS_F

A regular file

FTS_NS

A file for which no stat() function information is available. The contents of the fts_statp field are undefined. This is an error return, and the fts_errno field will be set to indicate what caused the error

FTS_NSOK

A file for which no stat() function information is requested. The contents of the fts_statp field are undefined. For more information, see the stat(2) man page

FTS_SL

A symbolic link.

FTS_SLNONE

A symbolic link with a non-existent target. The contents of the fts_statp field reference the file characteristic information for the symbolic link itself

fts_accpath()

A path for accessing the file from the current directory

fts_path()

The path for the file relative to the root of the traversal. This path contains the path specified to fts_open() function as a prefix

fts_pathlen()

The length of the string referenced by the fts_path() function

fts_name()

The name of the file

fts_namelen()

The length of the string referenced by the fts_name() function

fts_level()

The depth of the traversal, numbered from -1 to N, where this file is found. The FTSENT structure representing the parent of the starting point (or root) of the traversal is numbered FTS_ROOTPARENTLEVEL (-1). The FTSENT structure for the root itself is numbered FTS_ROOTLEVEL (0) fts_errno() Upon return of a FTSENT structure from the fts_children ()or the fts_read() functions, with its fts_info field set to FTS_DNR, FTS_ERR or FTS_NS, the fts_errno field contains the value of the external variable errno specifying the cause of the error. Otherwise, the contents of the fts_errno field are undefined fts_number() This field is provided for the use of the application program and is not modified by the fts() functions. It is initialized to 0 fts_pointer() This field is provided for the use of the application program and is not modified by the fts() functions. It is initialized to NULL fts_parent() A pointer to the FTSENT structure referencing the file in the hierarchy immediately above the current file, that is, the directory of which this file is a member. A parent structure for the initial entry point is provided as well, however, only the fts_level, the fts_number and the fts_pointer fields are guaranteed to be initialized fts_link() Upon return from the fts_children() function, the fts_link field points to the next structure in the NULL-terminated linked list of directory members. Otherwise, the contents of the fts_link field are undefined fts_cycle() If a directory causes a cycle in the hierarchy (see FTS_DC), either because of a hard link between two directories, or a symbolic link pointing to a directory, the fts_cycle field of the structure will point to the FTSENT structure in the hierarchy that references the same file as the current FTSENT structure. Otherwise, the contents of the fts_cycle field are undefined. fts_statp() A pointer to the stat() information for the file. For more information, see the stat(2) man page. A single buffer is used for all of the paths of all of the files in the file hierarchy. Therefore, the fts_path and the fts_accpath fields are guaranteed to be NULL-terminated only for the file most recently returned by the fts_read() function. To use these fields to reference any files represented by other FTSENT structures, it is required that the path buffer be modified using the information contained in that FTSENT structure’s fts_pathlen field. Any such modifications should be undone before further calls to the fts_read() are attempted. The fts_name field is always NULL-terminated. Routines
The
fts_open() function takes a pointer to an array of character pointers naming one or more paths which make up a logical file hierarchy to be traversed. The array must be terminated by a NULL pointer. There are a number of options, at least one of which, either FTS_LOGICAL or FTS_PHYSICAL must be specified. The options are selected by performing the OR operation for the following values: FTS_COMFOLLOW This option causes any symbolic link specified as a root path to be followed immediately, whether or not FTS_LOGICAL is also specified FTS_LOGICAL This option causes the fts() routines to return FTSENT structures for the targets of symbolic links, instead of the symbolic links themselves. If this option is set, the only symbolic links for which FTSENT structures are returned to the application are those referencing non-existent files. Either FTS_LOGICAL or FTS_PHYSICAL must be provided to the fts_open() function FTS_NOCHDIR To allow descending to arbitrary depths (independent of {PATH_MAX}) and improve performance, the fts() functions change directories as they walk the file hierarchy. As a result, an application cannot rely on being in any particular directory during the traversal. The FTS_NOCHDIR option turns off this feature, and the fts() functions will not change the current directory Note - The applications should not themselves change their current directory and try to access files unless FTS_NOCHDIR is specified and absolute pathnames were provided as arguments to fts_open() function. FTS_NOSTAT By default, returned FTSENT structures reference file characteristic information (the statp field) for each file visited. This option relaxes that requirement as a performance optimization, allowing the fts() functions to set the fts_info field to FTS_NSOK and leave the contents of the statp field undefined FTS_PHYSICAL This option causes the fts() routines to return FTSENT structures for symbolic links themselves instead of the target files they point to. If this option is set, FTSENT structures for all symbolic links in the hierarchy are returned to the application. Either FTS_LOGICAL or FTS_PHYSICAL must be provided to the fts_open() function FTS_SEEDOT By default, any files named ’.’ or ’..’ encountered in the file hierarchy are ignored, unless they are specified as path elements to the fts_open() function. This option causes the fts() routines to return FTSENT structures for them FTS_XDEV This option prevents fts() from descending into directories that have a different device number than the file from which the descent began The argument compar() specifies a user-defined function which may be used to order the traversal of the hierarchy. It takes pointers to two FTSENT structures as arguments and returns an integer less than, or greater than 0 if the file referenced by the first argument should come before, or after the file referenced by the second argument. A return value of 0 indicates ordering of the two files is irrelevant. The fts_accpath, the fts_path and the fts_pathlen fields of the FTSENT structures may never be used in this comparison. If the fts_info field is set to FTS_NS or FTS_NSOK, the fts_statp field may not be used in this comparison. If the compar() argument is NULL, the directory traversal order is in the order listed in path_argv() for the root paths, and in the order listed in the directory for everything else. The fts_read() function returns a pointer to an FTSENT structure describing a file in the hierarchy. Directories, that are readable and do not cause cycles, are visited at least twice, once in pre-order and once in post-order. All other files are visited at least once. Hard links between directories that do not cause cycles or symbolic links to symbolic links may cause files to be visited more than once, or directories more than twice. If all the members of the hierarchy have been returned, the fts_read() function returns NULL and sets the external variable errno to 0. If an error unrelated to a file in the hierarchy occurs, the fts_read() function returns NULL and sets errno appropriately. If an error related to a returned file occurs, a pointer to an FTSENT structure is returned, and errno may or may not have been set. For more information, see the description for the fts_info() function. The FTSENT structures returned by the fts_read() function may be overwritten after a call to the fts_close() function on the same file hierarchy stream, or, after a call to the fts_read() function on the same file hierarchy stream unless they represent a file of type directory, in which case they will not be overwritten until after a call to the fts_read() function after the FTSENT structure has been returned by the fts_read() function in post-order. The fts_children() function returns a pointer to an FTSENT structure describing the first entry in a NULL-terminated linked list of the files in the directory represented by the FTSENT structure most recently returned by the fts_read() function. The list is linked through the fts_link field of the FTSENT structure, and is ordered by the user-specified comparison function, if any. Repeated calls to the fts_children() will recreate this linked list. As a special case, if the fts_read() function has not yet been called for a hierarchy, the fts_children() function will return a pointer to the files in the logical directory specified to the fts_open() function, that is, the arguments specified to the fts_open() function. Otherwise, if the FTSENT structure most recently returned by the fts_read() function is not a directory being visited in pre-order, or the directory does not contain any files, the fts_children() function returns NULL and sets errno to 0. If an error occurs, the fts_children() function returns NULL and sets errno appropriately. The FTSENT structures returned by the fts_children() function may be overwritten after a call to the fts_children(), the fts_close() or the fts_read() functions on the same file hierarchy stream. Option may be set to the following value: FTS_NAMEONLY Only the names of the files are needed. The contents of all the fields in the returned linked list of structures are undefined with the exception of the fts_name and the fts_namelen fields The fts_set() function allows the user application to determine further processing for the file f of the stream ftsp. The fts_set() function returns 0 on success, and -1, if an error occurs. Option must be set to one of the following values: FTS_AGAIN Revisits the file, and any file type can be revisited. The next call to the fts_read() function will return the referenced file. The fts_stat and fts_info fields of the structure will be reinitialized at that time, but no other fields will have been changed. This option is meaningful only for the most recently returned file from the fts_read() function. Normal use is for post-order directory visits, where it causes the directory to be revisited (in both pre and post-order) as well as all of its descendants FTS_FOLLOW The referenced file must be a symbolic link. If the referenced file is the one most recently returned by the fts_read() function, the next call to the fts_read() function returns the file with the fts_info and fts_statp fields reinitialized to reflect the target of the symbolic link instead of the symbolic link itself. If the file is one of those most recently returned by the fts_children() function, the fts_info and fts_statp fields of the structure, when returned by the fts_read() function, will reflect the target of the symbolic link instead of the symbolic link itself. In either case, if the target of the symbolic link does not exist, the fields of the returned structure will be unchanged and the fts_info field will be set to FTS_SLNONE If the target of the link is a directory, a pre-order return, followed by the return of all of its descendants, followed by a post-order return, is done. FTS_SKIP No descendants of this file are visited. The file may be one of those most recently returned by either the fts_children() function or the fts_read() function. The fts_close() function closes a file hierarchy stream ftsp and restores the current directory to the directory from which the fts_open() function was called to open ftsp. The fts_close() function returns 0 on success, and -1 if an error occurs. ===== ERRORS ===== The fts_open() function will fail if: EACCES Search permission is denied for a component of a path EAGAIN There is not enough memory available EILSEQ The path argument includes non-UTF8 characters and the file system accepts only file names where all characters are part of the UTF-8 character codeset EINTR A signal was caught during the execution of the fts_open() function EINVAL The value of the path_argv() or options argument is invalid EIO An error occurred while reading from the file system ELOOP Too many symbolic links were encountered in resolving path. A loop exists in symbolic links encountered during resolution of the path argument EMFILE The maximum number of file descriptors {OPEN_MAX} are currently open in the calling process EMULTIHOP Components of path require hopping to multiple remote machines and the file system does not allow it ENAMETOOLONG The length of an intermediate path exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX} ENFILE The maximum allowable number of files is currently open in the system ENOENT A component of path does not name an existing file or path is an empty string ENOLINK The path argument points to a remote machine, and the link to that machine is no longer active ENOMEM The physical memory limits of the system are exceeded ENOTDIR A component of the path prefix is not a directory ENXIO The path argument names a character or block device special file, and the corresponding I/O device has been retired by the fault management framework EOVERFLOW The file size in bytes or the number of blocks allocated to the file or the file serial number cannot be represented correctly in the FTSENT structure The fts_close() function will fail if: EINTR A signal was caught during the execution of the fts_close() function EIO An I/O error occurred while reading from the file system ENOLINK The path argument points to a remote machine and the link to that machine is no longer active The fts_read() function will fail if: EACCES Search permission is denied for a component of a path EAGAIN There is not enough memory available EINTR A signal was caught during the execution of the fts_read() function EIO An I/O error occurred while reading the file system ELOOP Too many symbolic links were encountered in resolving path. A loop exists in symbolic links encountered during resolution of a path EMFILE The maximum number of file descriptors {OPEN_MAX} are currently open in the calling process EMULTIHOP Components of path require hopping to multiple remote machines and the file system does not allow it ENAMETOOLONG The length of a path component exceeds NAME_MAX characters, or the length of path the exceeds PATH_MAX characters ENFILE Too many files are currently open on the system ENOENT A component of dirname does not name an existing directory or dirname is an empty string ENOLINK The path argument points to a remote machine, and the link to that machine is no longer active ENOMEM The physical memory limits of the system are exceeded ENOTDIR A component of the path prefix is not a directory EOVERFLOW The file size in bytes or the number of blocks allocated to the file or the file serial number cannot be represented correctly in the FTSENT structure The fts_children() function will fail if: EACCES Search permission is denied on a component of a path EAGAIN There is not enough memory available EINTR A signal was caught during the execution of the fts_children() function EINVAL The value of the options argument is invalid EIO An I/O error occurred while reading the file system ELOOP Too many symbolic links were encountered in resolving path. A loop exists in symbolic links encountered during resolution of a path EMFILE There are {OPEN_MAX} file descriptors currently open in the calling process EMULTIHOP Components of path require hopping to multiple remote machines and the file system does not allow it ENAMETOOLONG Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX} ENFILE The maximum allowable number of files is currently open in the system ENOENT A component of dirname does not name an existing directory or dirname is an empty string ENOLINK The path argument points to a remote machine, and the link to that machine is no longer active ENOMEM The physical limits of the system are exceeded ENOTDIR A component of dirname is not a directory EOVERFLOW The file size in bytes or the number of blocks allocated to the file or the file serial number cannot be represented correctly in the FTSENT structure The fts_set() function will fail if: EINVAL The value of the options argument is invalid ===== EXAMPLES ===== Example 1 Breadth-wise Listing of Contents of a Directory Hierarchy The following example displays a breadth-wise listing of the contents of a directory hierarchy: #include <fts.h>
#include <stdio.h>
#include <string.h> int compare(const FTSENT **, const FTSENT **); int main(int argc, char * const argv[])
{
FTS * fhandle = NULL;
FTSENT * child = NULL;
FTSENT * parent = NULL; fhandle = fts_open(argv + 1, FTS_COMFOLLOW, &compare); if (fhandle != NULL) {
while 1) != NULL) {
child = fts_children(fhandle, 0); while 2) {
printf(”%s/%s\n”, child→fts_path, child→fts_name);
child = child→fts_link;
}
}
fts_close(fhandle);
} return (0);
} int compare(const FTSENT ** first, const FTSENT ** second) {
return (strcmp3);
} ===== ATTRIBUTES ===== See attributes(7) for descriptions of the following attributes: ===== SEE ALSO ===== fchdir(2), fstat(2), fstatat(2), statvfs(2),
opendir(3C), readdir(3C), ftw(3C), qsort**(3C)


1)
parent = fts_read(fhandle
2)
child != NULL
3)
*first)→fts_name, (*second)→fts_name
solaris/fts_open.3c.txt · Last modified: 2023/07/19 08:57 by A User Not Logged in