---
name: "fts_set"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/fts/3"
---

# fts\_set(3)

traverse a file hierarchy

Part of [fts(3)](/fts/3.md).

## NAME

**fts** – traverse a file hierarchy

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<fts.h\>**

*FTS* *\**\

**fts\_open**(*char \* const \*path\_argv*, *int options*, *int (\*compar)(const FTSENT \* const \*, const FTSENT \* const \*)*);

*FTS* *\**\

**fts\_openat**(*int dirfd*, *char \* const \*path\_argv*, *int options*, *int (\*compar)(const FTSENT \* const \*, const FTSENT \* const \*)*);

*FTS* *\**\

**fts\_open\_b**(*char \* const \*path\_argv*, *int options*, *int (^compar)(const FTSENT \* const \*, const FTSENT \* const \*)*);

*FTSENT* *\**\

**fts\_read**(*FTS \*ftsp*);

*FTSENT* *\**\

**fts\_children**(*FTS \*ftsp*, *int options*);

*int*\

**fts\_set**(*FTS \*ftsp*, *FTSENT \*f*, *int instr*);

*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**() and **fts\_open\_b**() functions return 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 the file hierarchy. The **fts\_children**() function returns a pointer to a linked list of structures, each of which describes one of the files contained in a directory in the hierarchy. In general, directories are visited two distinguishable times; 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), order the walk of the hierarchy or prune and/or re-visit portions of the hierarchy.

Two structures are defined (and typedef'd) 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. 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 two get functions are also available as macros of the same name.

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, they will not 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 immediately follows FTS\_D, in place of FTS\_DP, when the directory could not be entered, or could be entered but not 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**() or **fts\_open\_b**() (see 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(2)](/stat/2.md) information was 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(2)](/stat/2.md) information was requested. The contents of the *fts\_statp* field are undefined.

  - 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**() or **fts\_open\_b**() as a prefix.

- *fts\_pathlen*

  The length of the string referenced by *fts\_path*.

- *fts\_name*

  The name of the file.

- *fts\_namelen*

  The length of the string referenced by *fts\_name*.

- *fts\_level*

  The depth of the traversal, numbered from -1 to N, where this file was found. The *FTSENT* structure representing the parent of the starting point (or root) of the traversal is numbered FTS\_ROOTPARENTLEVEL (-1), and 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 **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, i.e., 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*, *fts\_number*, *fts\_pointer* and *fts\_dirfd* 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 [stat(2)](/stat/2.md) information for the file.

- *fts\_dirfd*

  A file descriptor open on this directory entry. It is set only for directory entries (FTS\_D) and is -1 for all other entry types. To access a file using fd-relative operations without relying on path-based syscalls, required in [capsicum(4)](/capsicum/4.md) capability mode, use *fts\_parent-\>fts\_dirfd* with [openat(2)](/openat/2.md) and *fts\_name*. The descriptor is valid until the directory's post-order visit (FTS\_DP) and must not be closed by the caller. For root-level entries opened with **fts\_open**(), *fts\_dirfd* is -1.

A single buffer is used for all of the paths of all of the files in the file hierarchy. Therefore, the *fts\_path* and *fts\_accpath* fields are guaranteed to be NUL-terminated *only* for the file most recently returned by **fts\_read**(). To use these fields to reference any files represented by other *FTSENT* structures will require 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 **fts\_read**() are attempted. The *fts\_name* field is always NUL-terminated.

### Thread Safety

The **fts** functions can safely be used in multi-threaded programs provided no two threads access the same *FTS* or *FTSENT* structure simultaneously. However, unless the FTS\_NOCHDIR flag was passed to **fts\_open**() or **fts\_open\_b**(), calls to **fts\_read**() and **fts\_children**() may change the current working directory, which will affect all threads. Conversely, changing the current working directory either during or between calls to **fts\_read**() or **fts\_children**() (even in a single-thread program) may cause **fts** to malfunction unless the FTS\_NOCHDIR flag was passed to **fts\_open**() or **fts\_open\_b**() and all paths in *path\_argv* were absolute.

### Fn fts\_open

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 *or*'ing 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\_COMFOLLOWDIR

  This option is similar to FTS\_COMFOLLOW, but only follows symbolic links to directories.

- 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. This has the side-effect that 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 that 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**().

- 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. The roots and any directories encountered during traversal( FTS\_D, FTS\_DC, FTS\_DP) are still fully populated.

- FTS\_NOSTAT\_TYPE

  This option is similar to FTS\_NOSTAT, but attempts to populate *fts\_info* based on information from the *d\_type* field of *struct* *dirent*.

- 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, unless they are specified as path arguments to **fts\_open**(), any files named ‘.’ or ‘..’ encountered in the file hierarchy are ignored. 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 *compar* argument points to a user-defined function which may be used to order the traversal of the hierarchy. It takes two pointers to pointers to *FTSENT* structures as arguments and should return a negative value, zero, or a positive value to indicate if the file referenced by its first argument comes before, in any order with respect to, or after, the file referenced by its second argument. The *fts\_accpath*, *fts\_path* and *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 either. 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.

### Fn fts\_open\_b

The **fts\_open\_b**() function is identical to **fts\_open**() except that it takes a block pointer instead of a function pointer. The block is copied before **fts\_open\_b**() returns, so the original can safely go out of scope or be released.

### Fn fts\_openat

The **fts\_openat**() function is identical to **fts\_open**() except that it accepts a file descriptor *dirfd* as its first argument. If *dirfd* is AT\_FDCWD, the behaviour is identical to **fts\_open**(). Otherwise, *dirfd* must be an open file descriptor referring to a directory, and the traversal is rooted there. This allows **fts\_openat**() to be used inside Capsicum capability mode ([capsicum(4)](/capsicum/4.md)), where path-based operations are not permitted. The **fts\_openat**() function duplicates *dirfd* internally, so the caller may close it after **fts\_openat**() returns.

### Fn fts\_read

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, **fts\_read**() returns NULL and sets the external variable *errno* to 0. If an error unrelated to a file in the hierarchy occurs, **fts\_read**() 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 (see *fts\_info*). Note that **fts\_read**() will not set *errno* to 0 if called again with the same *ftsp* argument after the FTS\_STOP flag has been set or the end of the stream has been reached.

The *FTSENT* structures returned by **fts\_read**() may be overwritten after a call to **fts\_close**() on the same file hierarchy stream, or, after a call to **fts\_read**() 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 **fts\_read**() after the *FTSENT* structure has been returned by the **fts\_read**() function in post-order.

### Fn fts\_children

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 **fts\_read**(). 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 **fts\_children**() will recreate this linked list.

As a special case, if **fts\_read**() has not yet been called for a hierarchy, **fts\_children**() will return a pointer to the files in the logical directory specified to **fts\_open**() or **fts\_open\_b**(), i.e., the arguments specified to **fts\_open**() or **fts\_open\_b**(). Otherwise, if the *FTSENT* structure most recently returned by **fts\_read**() is not a directory being visited in pre-order, or the directory does not contain any files, **fts\_children**() returns NULL and sets *errno* to zero. If an error occurs, **fts\_children**() returns NULL and sets *errno* appropriately.

The *FTSENT* structures returned by **fts\_children**() may be overwritten after a call to **fts\_children**(), **fts\_close**() or **fts\_read**() 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 *fts\_namelen* fields.

### Fn fts\_set

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. Its *instr* argument must have one of the following values:

- FTS\_AGAIN

  Re-visit the file; any file type may be re-visited. The next call to **fts\_read**() 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 **fts\_read**(). Normal use is for post-order directory visits, where it causes the directory to be re-visited (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 **fts\_read**(), the next call to **fts\_read**() 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 **fts\_children**(), the *fts\_info* and *fts\_statp* fields of the structure, when returned by **fts\_read**(), 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, the 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 **fts\_children**() or **fts\_read**().

### Fn fts\_set\_clientptr , Fn fts\_get\_clientptr

The **fts\_set\_clientptr**() function sets the client data pointer for the stream *ftsp* to *clientdata*. The **fts\_get\_clientptr**() function returns the client data pointer associated with *ftsp*. This can be used to pass per-stream data to the comparison function.

For performance reasons, **fts\_get\_clientptr**() may be shadowed by a preprocessor macro.

### Fn fts\_get\_stream

The **fts\_get\_stream**() function returns the **fts** stream associated with the file entry *f*. A typical use for this would be for a comparison function to first call **fts\_get\_stream**() on one of its arguments, then call **fts\_get\_clientptr**() to obtain the client data pointer, which in turn points to information necessary to correctly order the two entries.

For performance reasons, **fts\_get\_stream**() may be shadowed by a preprocessor macro.

### Fn fts\_close

The **fts\_close**() function closes a file hierarchy stream *ftsp* and restores the current directory to the directory from which **fts\_open**() or **fts\_open\_b**() was called to open *ftsp*.

## RETURN VALUES

The **fts\_open**() and **fts\_open\_b**() functions return a pointer to the new **fts** stream on success and NULL on failure.

The **fts\_read**() function returns a pointer to the next file entry on success, or if an error occurs that relates specifically to that file entry. On reaching the end of the file hierarchy, it returns NULL and sets the external variable *errno* to 0. On failure, it returns NULL and sets *errno* to an appropriate non-zero value. If called again after the FTS\_STOP flag has been set or the end of the stream has been reached, **fts\_read**() returns NULL and leaves *errno* untouched.

The **fts\_children**() function returns a pointer to a linked list of file entries on success. On reaching the end of the file hierarchy, it returns NULL and sets the external variable *errno* to 0. On failure, it returns NULL and sets *errno* to an appropriate non-zero value.

The **fts\_set**() function returns 0 on success and -1 if its *instr* argument is invalid.

The **fts\_get\_clientptr**() function returns the client data pointer associated with its argument, or NULL if none has been set.

The **fts\_get\_stream**() function returns a pointer to the **fts** stream associated with its argument.

The **fts\_close**() function returns 0 on success, and -1 if an error occurs.

## ERRORS

The **fts\_open**() and **fts\_open\_b**() functions may fail and set *errno* for any of the errors specified for the library functions [open(2)](/open/2.md) and [malloc(3)](/malloc/3.md). The **fts\_open\_b**() function may also fail and set *errno* to ENOSYS if the blocks runtime is missing.

The **fts\_close**() function may fail and set *errno* for any of the errors specified for the library functions [chdir(2)](/chdir/2.md) and [close(2)](/close/2.md).

The **fts\_read**() and **fts\_children**() functions may fail and set *errno* for any of the errors specified for the library functions [chdir(2)](/chdir/2.md), [malloc(3)](/malloc/3.md), [opendir(3)](/opendir/3.md), [readdir(3)](/readdir/3.md) and [stat(2)](/stat/2.md).

In addition, the **fts\_children**(), **fts\_open**(), and **fts\_set**() functions may fail and set *errno* as follows:

- \[EINVAL\]

  The options were invalid, or the list was empty.

## SEE ALSO

[find(1)](/find/1.md), [chdir(2)](/chdir/2.md), [stat(2)](/stat/2.md), [ftw(3)](/ftw/3.md), [qsort(3)](/qsort/3.md)

## HISTORY

The **fts** interface was first introduced in 4.4BSD. The **fts\_get\_clientptr**(), **fts\_get\_stream**(), and **fts\_set\_clientptr**() functions were introduced in FreeBSD 5.0, principally to provide for alternative interfaces to the **fts** functionality using different data structures. Blocks support and the FTS\_COMFOLLOWDIR and FTS\_NOSTAT options were added in FreeBSD 15.0 based on similar functionality in macOS.

## BUGS

The **fts\_open**() function will automatically set the FTS\_NOCHDIR option if the FTS\_LOGICAL option is provided, or if it cannot [open(2)](/open/2.md) the current directory.
