5    Files and Directories


5.1    Directories


5.1.1    Format of Files and Directories

On the operating system, the internal format of directories is defined in the following location:

The link count of a directory is the total number of directories that are listed in that directory, including "." and "..".


5.1.2    Directory Operations


5.1.2.2    Description

On the operating system, the pointer returned by readdir() points to data that may be overwritten by another call to readdir() on the same directory stream. Overwriting occurs when the number of entries buffered by readdir() is exhausted and the next set of entries is read by the function.

On the operating system, the readdir() function does buffer several directory entries per actual read operation.

On the operating system, if a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), a subsequent call to readdir() may or may not accurately reflect the contents of the directory.

On the operating system, upon return from the closedir() function, dir no longer points to an accessible object of type DIR because the memory in which the object resided has been freed.

On the operating system, if the dirp argument passed to any of the file operation functions does not refer to a currently-open directory stream, the following occurs:

The function fails and errno is set to [EBADF].

On the operating system, using a previously-opened directory stream after one of the exec() family of functions causes the following to happen:

The previously-opened directory stream is no longer accessible because the opendir() function sets the close-on-exec (FD_CLOEXEC) flag on the file descriptor for the directory file.

On the operating system, after a call to the fork() function, if both the parent and child processes use the readdir() function or a combination of readdir() and rewinddir() on a directory stream opened prior to the fork() the behavior is as follows:

Each process affects the other process's current directory offset (dd_loc in DIR).


5.1.2.4    Errors

For the opendir() function, the operating system detects the conditions and returns the corresponding errno values for [EMFILE] and [ENFILE].

For the readdir() function, the operating system detects the conditions and returns the corresponding errno value for [EBADF].

For the closedir() function, the operating system detects the conditions and returns the corresponding errno value for [EBADF].


5.2    Working Directory


5.2.2    Working Directory Pathname


5.2.2.2    Description

On the operating system, if the argument buf to getcwd() is a NULL pointer, getcwd() behaves as follows:

Using malloc(), getcwd() allocates an array of size bytes and copies the absolute pathname into the array. The pointer to this array is then returned. This pointer can be used as the argument to a subsequent call to free().


5.2.2.3    Returns

On the operating system, after an error on a call to getcwd(), the contents of buf are indeterminate.


5.2.2.4    Errors

For the getcwd() function, the operating system detects the conditions and returns the corresponding errno value for [EACCES].


5.3    General File Creation


5.3.1    Open a File


5.3.1.2    Description

On the operating system, if O_RDWR is set in oflag, and open( path, oflag, mode) is called to open a FIFO, the following results:

The file descriptor returned by open() can be used for reading and writing bytes to and from the FIFO.

A newly-created file's group ID is set to the group ID of the directory in which the file is being created.

On the operating system, when O_CREAT is set in oflag and bits in mode other than the file permission bits are set, the results are as follows:

The S_ISUID, S_ISGID, and any bits in mode other than valid file permission bits are ignored. The file permissions are the bitwise OR of the file permission bits in the mode argument and the process's file creation mask.

On the operating system, if O_EXCL is set and O_CREAT is not set in oflag, the following results:

The O_EXCL bit is silently ignored and the open() function is attempted.

When opening a character special file that supports nonblocking opens with O_NONBLOCK set, the behavior subsequent to the immediate return is as follows:

Any errors resulting from the open operation are not reported.

On the operating system, when O_NONBLOCK is set in oflag, and path is not a character special file that supports non-blocking opens, and path is not a FIFO being opened with O_RDONLY or O_WRONLY set in oflag, the following behavior occurs:

O_NONBLOCK is ignored.

On the operating system, if O_TRUNC is set in oflag and path refers to a file type other than a regular file, a FIFO special file, or a terminal device file, the effect is as follows:

O_TRUNC is ignored.

On the operating system, setting both O_TRUNC and O_RDONLY in oflag has the following effect:

The file is truncated if the process has write access to it. If the process does not have write access to the file, the open() function returns -1 and sets errno to [EACCES].


5.3.3    Set File Creation Mask


5.3.3.2    Description

On the operating system, the bits of the cmask argument to umask(), other than the file permission bits and the set-id-on-execution bits (S_ISUID and S_ISGID) are ignored.


5.3.3.3    Returns

On the operating system, umask() returns the previous value of the file mode creation mask in the least-significant 12 bits of the mode_t return value. The remaining bits are all set to zero. These 12 bits include the 9 file permission bits, the 2 set-id-on-execution bits (S_ISUID and S_ISGID), and the sticky bit. (See chmod(1) for information on the sticky bit.)


5.3.4    Link to a File


5.3.4.2    Description

The operating system does not support linking of files across file systems.

The operating system does not support linking directories through the link() function even for superuser processes.

The operating system does not require that the calling process have permission to access the existing file when linking files.


5.4    Special File Creation


5.4.1    Make a Directory


5.4.1.2    Description

On the operating system, bits in the mode argument to the mkdir() function, other than the file permission bits, are ignored.

A newly-created directory's group ID is set to the group ID of the new directory's parent directory.


5.4.2    Make a FIFO Special File


5.4.2.2    Description

On the operating system, bits in the mode argument to the mkfifo() function, other than the file permission bits, set-id-on-execution bits (S_ISUID and S_ISGID), and the sticky bit are ignored. (See chmod(1) for information on the sticky bit.)

A newly-created FIFO's group ID is set to the group ID of the FIFO's parent directory.


5.5    File Removal


5.5.2    Remove a Directory


5.5.2.2    Description

If the named directory is the root directory or the current working directory of any process, other than the process calling rmdir() and the directory contains no entries other than "." and "..", the rmdir() function succeeds.


5.5.2.4    Errors

On the operating system, if the directory indicated in the call to rmdir() is being used by another process, rmdir() succeeds.

If the named directory is the current working directory of the calling process, rmdir() returns -1 and sets errno to [EBUSY].


5.5.3    Rename a File


5.5.3.2    Description

On the operating system, in a call to rename( old,new ), if the old argument points to the pathname of a directory, write access permission is not required for the directory named by old, or, if it exists, for the directory named by new.


5.6    File Characteristics


5.6.1    File Characteristics: Header and Data Structure

If file is a file type other than a regular file, the st_size field in the struct stat returned by stat( file, buf ) has the following meaning:


5.6.1.2    <sys/stat.h> File Modes

On the operating system, the following bits are ORed with S_IRWXU, S_IRWXG, and S_IRWXO in the st_mode field of the struct stat returned by stat( file, buf ):


5.6.2    Get File Status


5.6.3    File Accessibility


5.6.3.2    Description

On the operating system, the access() function does indicate success for X_OK if the process has appropriate privileges yet none of the file's execute permission bits is set.


5.6.3.4    Errors

For the access() function, the operating system detects the conditions and returns the corresponding errno value for [EINVAL] .


5.6.4    Change File Modes


5.6.4.2    Description

On the operating system, a process running as user root may change file access modes regardless of the file's owner.

On the operating system, chmod() has no effect on the file descriptors for those files open at the time of the chmod() call.


5.6.5    Change Owner and Group of a File


5.6.5.2    Description

On the operating system, if the path argument to chown() refers to a regular file and if the call is made by a process with appropriate privileges, the set-user-ID (S_ISUID) and set-group-ID (S_ISGID) bits of the file mode are not cleared upon successful return from chown().


5.6.5.4    Errors

For the chown() function, the operating system detects the conditions and returns the corresponding errno value for [EINVAL]. If the owner or group ID is larger than 65535, -1 is returned and errno is set to [EINVAL].


5.6.6    Set File Access and Modification Times


5.6.6.2    Description

The operating system adds no extensions to the utimbuf structure.


5.7    Configurable Pathname Variables


5.7.1    Get Configurable Pathname Variables


5.7.1.2    Description

Although the operating system checks for existence and permissions on the path argument to pathconf() neither the path nor fildes argument is needed to return the value of the pathname variable name. Therefore, the return value of pathconf() or fpathconf() is not affected by the type of file to which the argument refers.


5.7.1.4    Errors

For the pathconf() function, the operating system detects the conditions and returns the corresponding errno values for [EACCES], [EINVAL], [ENAMETOOLONG], [ENOENT], and [ENOTDIR].

For the fpathconf() function, the operating system detects the conditions and return the corresponding errno values for [EINVAL]. The fildes argument to fpathconf() is ignored.