Chapter Contents

Previous

Next
lstat

lstat



Get File or Link Status

Portability: POSIX.1 conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <sys/types.h>
#include <sys/stat.h>

int lstat(const char *pathname, struct stat *buf);


DESCRIPTION

lstat gets status information about a file or symbolic link. pathname is the file. buf is the memory area for storing the status information.

The status information is returned in the stat structure defined in <sys/stat.h> :
mode_t st_mode indicates the permissions set for the file.
ino_t st_ino is the file serial number.
dev_t st_dev is the numeric ID of the device.
nlink_t st_nlink is the number of links to the file.
uid_t st_uid is the numeric user ID of the owner of the file.
gid_t st_gid is the numeric group ID of the file.
off_t st_size indicates the file size in bytes for regular files.
time_t st_atime indicates the most recent access time.
time_t st_ctime indicates the most recent time the file status was changed.
time_t st_mtime indicates the most recent time the file contents were changed.

The pathname must be specified as an USS HFS file. For programs not compiled with the posix option, a style prefix may be required. See Low-level and Standard I/O for information on specifying USS filenames.

The <sys/stat.h> header file contains a collection of macros that you can use to examine properties of a mode_t value from the st_mode field. For a list of these macros, see the documentation on the fstat function.


RETURN VALUE

lstat returns 0 if it is successful and -1 if it is not successful.


EXAMPLE

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>

main()
{

   char flname[] ="link.file";
   char lkname[] ="link.example";
   int fd;
   struct stat info;

      /* Create a file for linking.                           */
   if ((fd = open(flname,O_CREAT)) < 0)
      perror("open() error");
   else {
      close(fd);
         /* Check the status of the file.                     */
      puts("Status before link:");
      system("ls -il link.*");
         /* Create an alternative path for the existing file. */
      if (link(flname,lkname) != 0) {
         perror("link() error");
         unlink(flname);
      }
      else {
         puts("Status after linking:");
         system("ls -il link.*");
         if (lstat(lkname, &info) != 0)
            perror("lstat() error");
         else {
            printf("\nlstat() for link %s returned:\n", lkname);
            printf("inode: %d\n", (int) info.st_ino);
            printf("dev id: %d\n", (int) info.st_dev);
            printf("user id: %d\n", (int) info.st_uid);
            printf("links: %d\n",  info.st_nlink);
            printf("file size: %d\n", (int) info.st_size);
            printf("mode: %08x\n",  info.st_mode);
            printf("created: %s", ctime(&info.st_createtime));
         }
      }
      unlink(flname);
      unlink(lkname);
   }
}


RELATED FUNCTIONS

fstat , stat , symlink


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.