home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume14 / ds / part01 / ds.c
Encoding:
C/C++ Source or Header  |  1990-08-29  |  2.5 KB  |  115 lines

  1. /* ds.c */
  2.  
  3.  
  4. /* 
  5.  *   ds - disk space
  6.  *   usage:
  7.  *          ds [-gmkv] [path]
  8.  *                 -g   - Report size in gigabytes
  9.  *                 -m   - Report size in megabytes
  10.  *                 -k   - Report size in kilobytes
  11.  *                 -v   - Show label (bytes,kilobytes,or megabytes)
  12.  *                 path - Directory or file designating the partition
  13.  *                        to be reported. Default is current directory.
  14.  *
  15.  *          The report is displayed in integer units, unless the result is
  16.  *  less than 1.0, in which case the report is extended to one decimal point.
  17.  *  Divisors are decimal, not binary; i.e. 1 'kilobyte' is equal to 1000 bytes,
  18.  *  not 1024 bytes.
  19.  */
  20.  
  21. /*
  22.  *
  23.  * Copyright (C) 1990 by Mike Macgirvin. All Rights Reserved.
  24.  *
  25.  * Permission to use, copy, modify, and distribute this software and its
  26.  * documentation for any purpose and without fee is hereby granted, provided
  27.  * that the above copyright notice appear in all copies and that both that
  28.  * copyright notice and this permission notice appear in supporting
  29.  * documentation.  This software is provided "as is" without express or
  30.  * implied warranty.
  31.  *
  32.  */
  33.  
  34.  
  35.  
  36. #include <stdio.h>
  37. #include <strings.h>
  38. #include <sys/types.h>
  39. #include <sys/param.h>
  40. #include <sys/vfs.h>
  41.  
  42. #define K_BYTES 1.0E+3
  43. #define M_BYTES 1.0E+6
  44. #define G_BYTES 1.0E+9
  45.  
  46. #define STRCPY (void)strcpy
  47.  
  48. extern int optind;
  49. extern char *optarg;
  50.  
  51. struct statfs stats;
  52.  
  53. char path[MAXPATHLEN];
  54. char label[32];
  55. char fmt[32];
  56. main(argc,argv)
  57.      int argc;
  58.      char **argv;
  59. {
  60.   int c;
  61.   int sflag = 0;
  62.   int vflag = 0;
  63.   double space;
  64.   while((c = getopt(argc,argv,"gmkv")) != EOF) {
  65.     switch(c) {
  66.     case 'g':
  67.       sflag = 3;
  68.       break;
  69.     case 'm':
  70.       sflag = 2;
  71.       break;
  72.     case 'k':
  73.       sflag = 1;
  74.       break;
  75.     case 'v':
  76.       vflag ++;
  77.       break;
  78.     }
  79.   }
  80.   if(optind < argc)
  81.     STRCPY(path,argv[optind]);
  82.   else
  83.     STRCPY(path,".");
  84.      
  85.   if(statfs(path,&stats)) {
  86.     perror("statfs");
  87.     exit(1);
  88.   }
  89.   space = (double) (stats.f_bavail * stats.f_bsize);
  90.   switch(sflag) {
  91.   case 1:
  92.     space = space / K_BYTES;
  93.     STRCPY(label," Kilobytes");
  94.     break;
  95.   case 2:
  96.     space = space / M_BYTES;
  97.     STRCPY(label," Megabytes");
  98.     break;
  99.   case 3:
  100.     space = space / G_BYTES;
  101.     STRCPY(label," Gigabytes");
  102.     break;
  103.   default:
  104.     STRCPY(label," Bytes");
  105.     break;
  106.   }
  107.   if(space < 1.0)
  108.     STRCPY(fmt,"%-.1f%s\n");
  109.   else
  110.     STRCPY(fmt,"%-.0f%s\n");
  111.  
  112.   (void) printf(fmt,space,(vflag) ? label : "");
  113.  
  114. }
  115.