training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

ex-4.1.c (1040B)


      1 /*
      2  * Advanced Programming in the UNIX(r) Environment
      3  *
      4  * Exercise 4.1
      5 */
      6 
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <sys/stat.h>
     10 
     11 int
     12 main(int argc, char *argv[])
     13 {
     14    int		i;
     15    struct stat	buf;
     16    char		*ptr;
     17 
     18    for(i = 1; i < argc; i++) {
     19       printf("%s: ", argv[i]);
     20       /*
     21        * Do not use lstat(2)
     22       */
     23       if(stat(argv[i], &buf) < 0) {
     24          perror("stat");
     25 	 continue;
     26       }
     27 
     28       if( S_ISREG(buf.st_mode))
     29          ptr = "regular";
     30       else if( S_ISDIR(buf.st_mode))
     31          ptr = "directory";
     32       else if( S_ISCHR(buf.st_mode))
     33          ptr = "character special";
     34       else if( S_ISBLK(buf.st_mode))
     35          ptr = "block special";
     36       else if( S_ISFIFO(buf.st_mode))
     37          ptr = "fifo";
     38 #ifdef S_ISLNK
     39       else if( S_ISLNK(buf.st_mode))
     40          ptr = "symbolic link";
     41 #endif
     42 #ifdef S_ISSOCK
     43       else if( S_ISDIR(buf.st_mode))
     44          ptr = "socket";
     45 #endif
     46       else
     47          ptr = "** unknown mode **";
     48 
     49       printf("%s\n", ptr);
     50    }
     51 
     52    return EXIT_FAILURE;
     53 } /* eof main() */
     54