home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / unix_c / utils / bdf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  4.0 KB  |  162 lines

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