place the contents of a symbolic link into a buffer
#include <unistd.h> int readlink( const char *path, char *buf, size_t bufsiz );
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.
On success, the number of bytes placed in the buffer. Otherwise, -1 is returned, and errno is set to indicate the error.
/* * 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 ); }
POSIX 1003.1
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |