Place the contents of a symbolic link into a buffer
#include <unistd.h> int readlink( const char* path, char* buf, size_t bufsiz );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
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 doesn't include a NULL terminator. Its length must be determined from the stat structure returned by lstat(), 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.
The number of bytes placed in the buffer, or -1 if an error occurs (errno is set).
/* * 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]; int 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 ); } } return( ecode ); }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |