home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / help.jn / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-19  |  1.4 KB  |  66 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4.  
  5. main(argc,argv)
  6. int argc;
  7. char *argv[];
  8. {
  9.  
  10.     int laststat=1,i=1;
  11.     char *name;
  12.     struct stat buf;
  13.     struct stat *pb = &buf;
  14.     if(argc<=1)
  15.     {
  16.         printf("requires an argument\n");
  17.         exit(1);
  18.     }
  19.     for(i=1;i<argc;i++)
  20.     {
  21.         name = argv[i];
  22.         if(lstat(name,pb)== -1)
  23.             printf("lstat: %s not found, i=%d\n",name,i);
  24.         else
  25.         { 
  26.             printstat(pb,name);
  27.             if((pb->st_mode&S_IFLNK)==S_IFLNK)
  28.             {
  29.                 char realname[257]; int i,jhn;
  30.                 for(i=0;i<257;i++)realname[i]='\0';
  31.                 readlink(name,realname,257);
  32.                 printf( 
  33.                 "---symbolic link, here's what it points to:---\n");
  34.                 laststat=0;/*for exiting with 0 status (true)*/
  35.                 jhn=stat(name,pb);
  36.                 if(jhn==0)
  37.                 printstat(pb,realname);
  38.                 else {printf("\"%s\" not found--this could be a real problem!!\n",realname);exit(1);}
  39.             }
  40.             else
  41.                 laststat=1; /*for exiting with false status*/
  42.         }
  43.     }
  44.     exit(laststat);
  45. }
  46. printstat(pb,name)
  47. struct stat *pb;
  48. char *name;
  49. {
  50.     char *ptime, *ctime();
  51.     printf("%s: ---------\n",name);
  52.     printf("file-mode=%o(oct)   inode=%d   st_dev=%x(hex)   #links=%d\n",
  53.     pb->st_mode,
  54.     pb->st_ino, 
  55.     pb->st_dev,
  56.     pb->st_nlink);
  57.     printf("owner=%d   group=%d   filesize=%d bytes\n",
  58.     pb->st_uid,pb->st_gid,pb->st_size);
  59.     ptime=ctime(&(pb->st_atime));
  60.     printf("data last accessed       %s",ptime);
  61.     ptime=ctime(&(pb->st_mtime));
  62.     printf("data last modified       %s",ptime);
  63.     ptime=ctime(&(pb->st_ctime));
  64.     printf("file status last changed %s",ptime);
  65. }
  66.