Team BBL
Previous Page Next Page

4.17. symlink and readlink Functions

A symbolic link is created with the symlink function.

#include <unistd.h>

int symlink(const char *actualpath, const char
 *sympath);

Returns: 0 if OK, 1 on error


A new directory entry, sympath, is created that points to actualpath. It is not required that actualpath exist when the symbolic link is created. (We saw this in the example at the end of the previous section.) Also, actualpath and sympath need not reside in the same file system.

Because the open function follows a symbolic link, we need a way to open the link itself and read the name in the link. The readlink function does this.

#include <unistd.h>

ssize_t readlink(const char* restrict pathname,
 char *restrict buf,
                 size_t bufsize);

Returns: number of bytes read if OK, 1 on error


This function combines the actions of open, read, and close. If the function is successful, it returns the number of bytes placed into buf. The contents of the symbolic link that are returned in buf are not null terminated.

    Team BBL
    Previous Page Next Page