home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / util / sizeof.c < prev    next >
C/C++ Source or Header  |  1994-10-17  |  2KB  |  75 lines

  1. /*
  2.  * sizeof - report total size of files
  3.  *
  4.  * You might ask, why couldn't this be a shell program invoking ls -l?
  5.  * Well, apart from the variations in the format of ls -l, there's also
  6.  * the problem that the Berkloid ls -l doesn't follow symlinks unless
  7.  * asked to (with an unportable option).
  8.  */
  9.  
  10. #include <sys/types.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sys/stat.h>
  14.  
  15. int debug = 0;
  16. int indiv = 0;
  17. off_t multiplier = 1;
  18. char *progname;
  19.  
  20. extern void error();
  21.  
  22. /*
  23.  - main - do it all
  24.  */
  25. main(argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29.     int c;
  30.     int errflg = 0;
  31.     struct stat statbuf;
  32.     extern int optind;
  33.     extern char *optarg;
  34.     register off_t total = 0;
  35.  
  36.     progname = argv[0];
  37.  
  38.     while ((c = getopt(argc, argv, "im:x")) != EOF)
  39.         switch (c) {
  40.         case 'i':    /* Individual files. */
  41.             indiv = 1;
  42.             break;
  43.         case 'm':    /* Multiplier. */
  44.             multiplier = (off_t)atol(optarg);
  45.             break;
  46.         case 'x':    /* Debugging. */
  47.             debug++;
  48.             break;
  49.         case '?':
  50.         default:
  51.             errflg++;
  52.             break;
  53.         }
  54.     if (errflg) {
  55.         fprintf(stderr, "usage: %s ", progname);
  56.         fprintf(stderr, "[file] ...\n");
  57.         exit(2);
  58.     }
  59.  
  60.     for (; optind < argc; optind++)
  61.         if (strcmp(argv[optind], "-m") == 0) {
  62.             optind++;
  63.             if (optind < argc)
  64.                 multiplier = (off_t)atol(argv[optind]);
  65.         } else if (stat(argv[optind], &statbuf) >= 0) {
  66.             total += statbuf.st_size * multiplier;
  67.             if (debug || indiv)
  68.                 printf("%s %ld\n", argv[optind],
  69.                     (long)(statbuf.st_size * multiplier));
  70.         }
  71.     if (!indiv)
  72.         printf("%ld\n", (long)total);
  73.     exit(0);
  74. }
  75.