Section 4.17. symlink and readlink Functions

team bbl


4.17. symlink and readlink Functions

A symbolic link is created with the symlink function.

[View full width]

#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.

[View full width]

#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



    Advanced Programming in the UNIX Environment
    Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
    ISBN: 0321525949
    EAN: 2147483647
    Year: 2005
    Pages: 370

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net