home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / bdf < prev    next >
Internet Message Format  |  1991-08-07  |  4KB

  1. From: paul@vixie.UUCP (Paul Vixie Esq)
  2. Newsgroups: comp.sources.misc
  3. Subject: v02i014: A Berkeley-style DF System V
  4. Message-ID: <7126@ncoast.UUCP>
  5. Date: 26 Jan 88 03:48:30 GMT
  6. Approved: allbery@ncoast.UUCP
  7.  
  8. Comp.sources.misc: Volume 2, Issue 15
  9. Submitted-By: Paul Vixie Esq <paul@vixie.UUCP>
  10. Archive-Name: bdf
  11.  
  12. [Someone remind me to post "space" -- better than Berzerk df, and doesn't
  13.  have to parse someone else's grotty output.  ++bsa]
  14.  
  15. /* bdf - berkeley DF for system-V systems
  16.  * vix 16jan88 [written - from scratch]
  17.  *
  18.  * This code is public-domain, but please leave this notice intact and give me
  19.  * appropriate credit if you write a man page for it.  If you send me bug fixes
  20.  * and enhancements, I will release new versions from time to time.
  21.  *    Paul Vixie, paul%vixie@uunet.uu.net
  22.  *
  23.  * This is not nearly good enough for the obfuscated C contest.  Next time?
  24.  *
  25.  * Known to work on INTERACTIVE 386/ix, and should therefore work perfectly
  26.  * on Microport/386 and whatever the AT&T 6386 calls its OS.  Should work okay
  27.  * on 3B's.  Should work, in fact, anywhere where the output of 'df -t' is
  28.  * the same as 'System V/386', which may or may not be all SysV.[23] systems.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33.  
  34. #define TRUE 1
  35. #define FALSE 0
  36.  
  37. static char *PROGNAME = "bdf";
  38. static void usage() {
  39.     fprintf(stderr, "usage:  %s [-i]\n", PROGNAME);
  40.     exit(4);
  41. }
  42.  
  43. main(argc, argv)
  44.     int argc;
  45.     char *argv[];
  46. {
  47.     void bdf();
  48.     int iflag = FALSE;
  49.     int optind = argc;
  50.     char command_buf[300], *ptr = command_buf;
  51.  
  52.     if (!(PROGNAME = strrchr(argv[0], '/'))) PROGNAME = argv[0];
  53.     if (argc > 1)
  54.         if (argv[1][0] == '-')
  55.             if (argv[1][1] == 'i') { iflag = TRUE; optind = 2; }
  56.             else usage();
  57.         else optind = 1;
  58.  
  59.     ptr += sprintf(ptr, "/bin/df -t");
  60.     for (;  optind < argc;  optind++)
  61.         ptr += sprintf(ptr, " %s", argv[optind]);
  62.  
  63.     bdf(iflag, command_buf);
  64. }
  65.  
  66. static void
  67. bdf(iflag, df_command)
  68.     int iflag;
  69.     char *df_command;
  70. {
  71.     void output_part1(), output_part2(), output_part3();
  72.     void header_part1(), header_part2(), header_part3();
  73.     char filesys[50], device[50];
  74.     int fblocks, finodes, tblocks, tinodes;
  75.     FILE *df;
  76.  
  77.     if (!(df = popen(df_command, "r"))) {
  78.         fprintf(stderr, "error executing <%s> command\n", df_command);
  79.         perror("popen");
  80.         exit(2);
  81.     }
  82.  
  83.     header_part1();
  84.     if (iflag) header_part2();
  85.     header_part3();
  86.  
  87.     while (EOF != fscanf(df, "%s (%s ): %d blocks %d i-nodes",
  88.                 filesys, device, &fblocks, &finodes)) {
  89.         if (EOF == fscanf(df, " total: %d blocks %d i-nodes",
  90.                 &tblocks, &tinodes)) {
  91.             perror("fscanf#2");
  92.             exit(2);
  93.         }
  94.  
  95.         output_part1(device, fblocks/2, tblocks/2);
  96.         if (iflag)
  97.             output_part2(finodes, tinodes);
  98.         output_part3(filesys);
  99.     }
  100. }
  101.  
  102. /*************
  103. Filesystem    kbytes    used   avail capacity iused   ifree  %iused  Mounted on
  104. /dev/dsk/0s1 xxxxxxx xxxxxxx xxxxxxx   xxx%  xxxxxx  xxxxxx   xxx%   /foo/bar
  105. *************/
  106.  
  107. static void
  108. header_part1() {
  109.     printf("Filesystem    kbytes    used   avail capacity");
  110. }
  111. static void
  112. header_part2() {
  113.     printf(" iused   ifree  %%iused");
  114. }
  115. static void
  116. header_part3() {
  117.     printf("  Mounted on\n");
  118. }
  119.  
  120. static void
  121. output_part1(device, free_kb, total_kb)
  122.     char *device;
  123.     int free_kb, total_kb;
  124. {
  125.     int used_kb = total_kb - free_kb;
  126.     int capacity = (100 * used_kb) / total_kb;
  127.  
  128.     printf("%12s %7d %7d %7d   %3d%%  ",
  129.         device, total_kb, used_kb, free_kb, capacity);
  130. }
  131.  
  132. static void
  133. output_part2(free_inodes, total_inodes)
  134.     int free_inodes, total_inodes;
  135. {
  136.     int used_inodes = total_inodes - free_inodes;
  137.     int percent_used = (100 * used_inodes) / total_inodes;
  138.  
  139.     printf("%6d  %6d   %3d%% ", used_inodes, free_inodes, percent_used);
  140. }
  141.  
  142. static void
  143. output_part3(filesys)
  144.     char *filesys;
  145. {
  146.     printf("  %s\n", filesys);
  147. }
  148.  
  149. -- 
  150. Paul A Vixie Esq
  151. paul%vixie@uunet.uu.net
  152. {uunet,ptsfa,hoptoad}!vixie!paul
  153. San Francisco, (415) 647-7023
  154.