home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume01 / bdf
Encoding:
Internet Message Format  |  1991-08-27  |  4.3 KB

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