[Previous]
[Contents]
[Next]

readlink()

place the contents of a symbolic link into a buffer

Synopsis:

#include <unistd.h>
int readlink( const char *path, 
              char *buf, 
              size_t bufsiz );

Description:

The readlink() function places the contents of the symbolic link named by path into the buffer pointed to by buf, which has a size of bufsiz. The contents of the returned symbolic link don't include a NULL terminator. Its length must be determined from the stat structure returned by the lstat() function, or by the return value of the readlink() call.

If readlink() is successful, up to bufsiz bytes from the contents of the symbolic link are placed in buf.

Returns:

On success, the number of bytes placed in the buffer. Otherwise, -1 is returned, and errno is set to indicate the error.

Errors:

EACCES
Search permission is denied for a component of the path prefix.
EINVAL
The named file isn't a symbolic link.
ELOOP
A loop exists in the symbolic links encountered during resolution of the path argument, and more than SYMLOOP_MAX symbolic links were encountered.
ENAMETOOLONG
A component of the path exceeded NAME_MAX characters, or the entire pathname exceeded PATH_MAX characters.
ENOENT
The named file doesn't exist.
ENOSYS
Links aren't supported by the resource manager associated with path.
ENOTDIR
A component of the path prefix named by path isn't a directory.

Examples:

/*
 * read the contents of the named symbolic links
 */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

char buf[PATH_MAX + 1];

void main( int argc, char **argv )
{
  int n;
  int len;
  int ecode = 0;

  for( n = 1; n < argc; ++n ) {
    if(( len = readlink( argv[n], buf, PATH_MAX )) == -1) {
      perror( argv[n] );
      ecode++;
    }
    else {
      buf[len] = '\0';
      printf( "%s -> %s\n", argv[n], buf );
    }
  }

  exit( ecode );
}

Classification:

POSIX 1003.1

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

errno, lstat(), symlink()


[Previous]
[Contents]
[Next]