Guest User

http://superuser.com/questions/503559

a guest
Nov 11th, 2012
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7.  
  8. int main(int argc, char *argv[]) {
  9.     struct stat sb;
  10.     int count;
  11.     struct DIR *d;
  12.  
  13.     if (argc != 2) {
  14.         fprintf(stderr, "Usage%s <pathname>\n", argv[0]);
  15.         exit(EXIT_FAILURE);
  16.     }
  17.  
  18.     if (stat(argv[1], &sb) == -1) {
  19.         perror("stat");
  20.         exit(EXIT_FAILURE);
  21.     }
  22.  
  23.     printf("File type               ");
  24.  
  25.     switch (sb.st_mode & S_IFMT) {
  26.     case S_IFDIR:       printf("directory (%s)\n", argv[1]);
  27.         if ((d = opendir(argv[1])) != NULL) {
  28.                 for (count = 0; readdir(d) != NULL; count++);
  29.                 closedir(d);
  30.         }
  31.         printf("Files in directory        %ld\n", (long) count - 2);        break;
  32.     case S_IFBLK:  printf("block device\n");            break;
  33.     case S_IFCHR:  printf("character device\n");        break;
  34.     case S_IFIFO:  printf("FIFO/pipe\n");               break;
  35.     case S_IFLNK:  printf("symlink\n");                 break;
  36.     case S_IFREG:  printf("regular file\n");            break;
  37.     case S_IFSOCK: printf("socket\n");                  break;
  38.     default:       printf("unknown?\n");                break;
  39.     }
  40.  
  41.     printf("\n");
  42.     printf("I-node number             %ld\n", (long) sb.st_ino);
  43.     printf("Mode                      %lo (octal)\n", (unsigned long) sb.st_mode);
  44.     printf("Link count                %ld\n", (long) sb.st_nlink);
  45.     printf("Ownership                 UID=%ld   GID=%ld\n", (long) sb.st_uid, (long) sb.st_gid);
  46.     printf("Preferred I/O block size  %ld bytes\n", (long) sb.st_blksize);
  47.     printf("File size                 %lld bytes\n", (long long) sb.st_size);
  48.     printf("Blocks allocated          %lld\n", (long long) sb.st_blocks);
  49.     printf("Last status change        %s", ctime(&sb.st_ctime));
  50.     printf("Last file access          %s", ctime(&sb.st_atime));
  51.     printf("Last file modification    %s", ctime(&sb.st_mtime));
  52.  
  53.     exit(EXIT_SUCCESS);
  54. }
Add Comment
Please, Sign In to add comment