In order to open a connection to a file, or to perform other operations such as deleting a file, you need some way to refer to the file. Nearly all files have names that are strings--even files which are actually devices such as tape drives or terminals. These strings are called file names. You specify the file name to say which file you want to open or operate on.
This section describes the conventions for file names and how the operating system works with them.
In order to understand the syntax of file names, you need to understand how the file system is organized into a hierarchy of directories.
A directory is a file that contains information to associate other files with names; these associations are called links or directory entries. Sometimes, people speak of "files in a directory", but in reality, a directory only contains pointers to files, not the files themselves.
The name of a file contained in a directory entry is called a file name component. In general, a file name consists of a sequence of one or more such components, separated by the slash character (/). A file name which is just one component names a file with respect to its directory. A file name with multiple components names a directory, and then a file in that directory, and so on.
Some other documents, such as the POSIX standard, use the term pathname for what we call a file name, and either filename or pathname component for what this manual calls a file name component. We don't use this terminology because a "path" is something completely different (a list of directories to search), and we think that "pathname" used for something else will confuse users. We always use "file name" and "file name component" (or sometimes just "component", where the context is obvious) in GNU documentation. Some macros use the POSIX terminology in their names, such as PATH_MAX. These macros are defined by the POSIX standard, so we cannot change their names.
You can find more detailed information about operations on directories in Chapter 15.
A file name consists of file name components separated by slash (/) characters. On the systems that the GNU C library supports, multiple successive / characters are equivalent to a single / character.
The process of determining what file a file name refers to is called file name resolution. This is performed by examining the components that make up a file name in left-to-right order, and locating each successive component in the directory named by the previous component. Of course, each of the files that are referenced as directories must actually exist, be directories instead of regular files, and have the appropriate permissions to be accessible by the process; otherwise the file name resolution fails.
If a file name begins with a /, the first component in the file name is located in the root directory of the process (usually all processes on the system have the same root directory). Such a file name is called an absolute file name.
Otherwise, the first component in the file name is located in the current working directory (the section called “Working Directory”). This kind of file name is called a relative file name.
The file name components . ("dot") and .. ("dot-dot") have special meanings. Every directory has entries for these file name components. The file name component . refers to the directory itself, while the file name component .. refers to its parent directory (the directory that contains the link for the directory in question). As a special case, .. in the root directory refers to the root directory itself, since it has no parent; thus /.. is the same as /.
Here are some examples of file names:
The file named a, in the root directory.
The file named b, in the directory named a in the root directory.
The file named a, in the current working directory.
This is the same as /a/b.
The file named a, in the current working directory.
The file named a, in the parent directory of the current working directory.
A file name that names a directory may optionally end in a /. You can specify a file name of / to refer to the root directory, but the empty string is not a meaningful file name. If you want to refer to the current working directory, use a file name of . or ./.
Unlike some other operating systems, the GNU system doesn't have any built-in support for file types (or extensions) or file versions as part of its file name syntax. Many programs and utilities use conventions for file names--for example, files containing C source code usually have names suffixed with .c--but there is nothing in the file system itself that enforces this kind of convention.
Functions that accept file name arguments usually detect these errno error conditions relating to the file name syntax or trouble finding the named file. These errors are referred to throughout this manual as the usual file name errors.
The process does not have search permission for a directory component of the file name.
This error is used when either the total length of a file name is greater than PATH_MAX, or when an individual file name component has a length greater than NAME_MAX. the section called “Limits on File System Capacity”.
In the GNU system, there is no imposed limit on overall file name length, but some file systems may place limits on the length of a component.
This error is reported when a file referenced as a directory component in the file name doesn't exist, or when a component is a symbolic link whose target file does not exist. the section called “Symbolic Links”.
A file that is referenced as a directory component in the file name exists, but it isn't a directory.
Too many symbolic links were resolved while trying to look up the file name. The system has an arbitrary limit on the number of symbolic links that may be resolved in looking up a single file name, as a primitive way to detect loops. the section called “Symbolic Links”.
The rules for the syntax of file names discussed in the section called “File Names”, are the rules normally used by the GNU system and by other POSIX systems. However, other operating systems may use other conventions.
There are two reasons why it can be important for you to be aware of file name portability issues:
If your program makes assumptions about file name syntax, or contains embedded literal file name strings, it is more difficult to get it to run under other operating systems that use different syntax conventions.
Even if you are not concerned about running your program on machines that run other operating systems, it may still be possible to access files that use different naming conventions. For example, you may be able to access file systems on another computer running a different operating system over a network, or read and write disks in formats used by other operating systems.
The ISO C standard says very little about file name syntax, only that file names are strings. In addition to varying restrictions on the length of file names and what characters can validly appear in a file name, different operating systems use different conventions and syntax for concepts such as structured directories and file types or extensions. Some concepts such as file versions might be supported in some operating systems and not by others.
The POSIX.1 standard allows implementations to put additional restrictions on file name syntax, concerning what characters are permitted in file names and on the length of file name and file name component strings. However, in the GNU system, you do not need to worry about these restrictions; any character except the null character is permitted in a file name string, and there are no limits on the length of file name strings.