[Previous]
[Contents]
[Next]

lstat()

get information about a file, directory, or symbolic link

Synopsis:

#include <sys/stat.h>
int lstat( const char *path, struct stat *buf );

Description:

The lstat() function obtains information about the file or directory referenced in path. This information is placed in the structure located at the address indicated by buf.

If the file isn't a symbolic link, the results of lstat() and stat() are identical. If the file is a symbolic link, lstat() returns information about the symbolic link, while stat() continues to resolve the pathname using the contents of the symbolic link and returns information about the resulting file.

In addition to those described for stat(), the following macro is defined in <sys/stat.h>:

S_ISLNK(m)
Test for a symbolic link.

The value m supplied to the macro is the value of the st_mode field of a stat structure. The macro evaluates to a nonzero value if the test is true, and zero if the test is false.

Returns:

0
Success.
-1
An error occurred - errno indicates the type of error detected.

Errors:

See stat().

Examples:

/*
 * Iterate through a list of files, and report 
 * for each if it is a symbolic link.
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

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

    for( n = 1; n < argc; ++n ) {
      if( lstat( argv[n], &sbuf ) == -1 ) {
        perror( argv[n] );
        ecode++;

      } else if( S_ISLNK( sbuf.st_mode ) ) {
        printf( "%s is a symbolic link\n", argv[n] );

      } else {
        printf( "%s is not a symbolic link\n", argv[n] );
      }
    }
    exit( ecode );
  }

Classification:

UNIX

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

errno, fstat(), fsys_fstat(), fsys_stat(), readlink(), stat()


[Previous]
[Contents]
[Next]