Node:Symbolic Links, Next:Deleting Files, Previous:Hard Links, Up:File System Interface
The GNU system supports soft links or symbolic links. This is a kind of "file" that is essentially a pointer to another file name. Unlike hard links, symbolic links can be made to directories or across file systems with no restrictions. You can also make a symbolic link to a name which is not the name of any file. (Opening this link will fail until a file by that name is created.) Likewise, if the symbolic link points to an existing file which is later deleted, the symbolic link continues to point to the same file name even though the name no longer names any file.
The reason symbolic links work the way they do is that special things
happen when you try to open the link. The open
function realizes
you have specified the name of a link, reads the file name contained in
the link, and opens that file name instead. The stat
function
likewise operates on the file that the symbolic link points to, instead
of on the link itself.
By contrast, other operations such as deleting or renaming the file
operate on the link itself. The functions readlink
and
lstat
also refrain from following symbolic links, because their
purpose is to obtain information about the link. link
, the
function that makes a hard link, does too. It makes a hard link to the
symbolic link, which one rarely wants.
Some systems have for some functions operating on files have a limit on
how many symbolic links are followed when resolving a path name. The
limit if it exists is published in the sys/param.h
header file.
int MAXSYMLINKS | Macro |
The macro |
Prototypes for most of the functions listed in this section are in
unistd.h
.
int symlink (const char *oldname, const char *newname) | Function |
The symlink function makes a symbolic link to oldname named
newname.
The normal return value from
|
int readlink (const char *filename, char *buffer, size_t size) | Function |
The readlink function gets the value of the symbolic link
filename. The file name that the link points to is copied into
buffer. This file name string is not null-terminated;
readlink normally returns the number of characters copied. The
size argument specifies the maximum number of characters to copy,
usually the allocation size of buffer.
If the return value equals size, you cannot tell whether or not
there was room to return the entire name. So make a bigger buffer and
call char * readlink_malloc (const char *filename) { int size = 100; while (1) { char *buffer = (char *) xmalloc (size); int nchars = readlink (filename, buffer, size); if (nchars < 0) return NULL; if (nchars < size) return buffer; free (buffer); size *= 2; } } A value of
|
In some situations it is desirable to resolve all the to get the real
name of a file where no prefix names a symbolic link which is followed
and no filename in the path is .
or ..
. This is for
instance desirable if files have to be compare in which case different
names can refer to the same inode.
char * canonicalize_file_name (const char *name) | Function |
The In any of the path components except the last one is missing the
function returns a NULL pointer. This is also what is returned if the
length of the path reaches or exceeds
This function is a GNU extension and is declared in |
The Unix standard includes a similar function which differs from
canonicalize_file_name
in that the user has to provide the buffer
where the result is placed in.
char * realpath (const char *restrict name, char *restrict resolved) | Function |
The One other difference is that the buffer resolved will contain the
part of the path component which does not exist or is not readable if
the function returns This function is declared in |
The advantage of using this function is that it is more widely available. The drawback is that it reports failures for long path on systems which have no limits on the file name length.