home *** CD-ROM | disk | FTP | other *** search
- /* ds.c */
-
-
- /*
- * ds - disk space
- * usage:
- * ds [-gmkv] [path]
- * -g - Report size in gigabytes
- * -m - Report size in megabytes
- * -k - Report size in kilobytes
- * -v - Show label (bytes,kilobytes,or megabytes)
- * path - Directory or file designating the partition
- * to be reported. Default is current directory.
- *
- * The report is displayed in integer units, unless the result is
- * less than 1.0, in which case the report is extended to one decimal point.
- * Divisors are decimal, not binary; i.e. 1 'kilobyte' is equal to 1000 bytes,
- * not 1024 bytes.
- */
-
- /*
- *
- * Copyright (C) 1990 by Mike Macgirvin. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation. This software is provided "as is" without express or
- * implied warranty.
- *
- */
-
-
-
- #include <stdio.h>
- #include <strings.h>
- #include <sys/types.h>
- #include <sys/param.h>
- #include <sys/vfs.h>
-
- #define K_BYTES 1.0E+3
- #define M_BYTES 1.0E+6
- #define G_BYTES 1.0E+9
-
- #define STRCPY (void)strcpy
-
- extern int optind;
- extern char *optarg;
-
- struct statfs stats;
-
- char path[MAXPATHLEN];
- char label[32];
- char fmt[32];
- main(argc,argv)
- int argc;
- char **argv;
- {
- int c;
- int sflag = 0;
- int vflag = 0;
- double space;
- while((c = getopt(argc,argv,"gmkv")) != EOF) {
- switch(c) {
- case 'g':
- sflag = 3;
- break;
- case 'm':
- sflag = 2;
- break;
- case 'k':
- sflag = 1;
- break;
- case 'v':
- vflag ++;
- break;
- }
- }
- if(optind < argc)
- STRCPY(path,argv[optind]);
- else
- STRCPY(path,".");
-
- if(statfs(path,&stats)) {
- perror("statfs");
- exit(1);
- }
- space = (double) (stats.f_bavail * stats.f_bsize);
- switch(sflag) {
- case 1:
- space = space / K_BYTES;
- STRCPY(label," Kilobytes");
- break;
- case 2:
- space = space / M_BYTES;
- STRCPY(label," Megabytes");
- break;
- case 3:
- space = space / G_BYTES;
- STRCPY(label," Gigabytes");
- break;
- default:
- STRCPY(label," Bytes");
- break;
- }
- if(space < 1.0)
- STRCPY(fmt,"%-.1f%s\n");
- else
- STRCPY(fmt,"%-.0f%s\n");
-
- (void) printf(fmt,space,(vflag) ? label : "");
-
- }
-