[comp.sources.misc] v14i064: 'ds': Disk Space Available

mike@relgyro.stanford.edu (Mike Macgirvin) (08/30/90)

Posting-number: Volume 14, Issue 64
Submitted-by: mike@relgyro.stanford.edu (Mike Macgirvin)
Archive-name: ds/part01

	A program to report the available disk space, and ONLY the available
disk space. This is possibly BSD specific. I wrote this after several
generations of programs which used 'awk' to grab the one field of interest
from 'df'. With no arguments, it reports the number of bytes available
in the filesystem containing the current directory. Options can report the
free space in other units, and with a label. Note that 1k == 1000 bytes, not
1024. You may wish to change this.



#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	ds.c
# This archive created: Mon Aug 27 14:55:39 1990
export PATH; PATH=/bin:$PATH
echo shar: extracting "'ds.c'" '(2514 characters)'
if test -f 'ds.c'
then
	echo shar: will not over-write existing file "'ds.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'ds.c'
	X/* ds.c */
	X
	X
	X/* 
	X *   ds - disk space
	X *   usage:
	X *          ds [-gmkv] [path]
	X *                 -g   - Report size in gigabytes
	X *                 -m   - Report size in megabytes
	X *                 -k   - Report size in kilobytes
	X *                 -v   - Show label (bytes,kilobytes,or megabytes)
	X *                 path - Directory or file designating the partition
	X *                        to be reported. Default is current directory.
	X *
	X *          The report is displayed in integer units, unless the result is
	X *  less than 1.0, in which case the report is extended to one decimal point.
	X *  Divisors are decimal, not binary; i.e. 1 'kilobyte' is equal to 1000 bytes,
	X *  not 1024 bytes.
	X */
	X
	X/*
	X *
	X * Copyright (C) 1990 by Mike Macgirvin. All Rights Reserved.
	X *
	X * Permission to use, copy, modify, and distribute this software and its
	X * documentation for any purpose and without fee is hereby granted, provided
	X * that the above copyright notice appear in all copies and that both that
	X * copyright notice and this permission notice appear in supporting
	X * documentation.  This software is provided "as is" without express or
	X * implied warranty.
	X *
	X */
	X
	X
	X
	X#include <stdio.h>
	X#include <strings.h>
	X#include <sys/types.h>
	X#include <sys/param.h>
	X#include <sys/vfs.h>
	X
	X#define K_BYTES 1.0E+3
	X#define M_BYTES 1.0E+6
	X#define G_BYTES 1.0E+9
	X
	X#define STRCPY (void)strcpy
	X
	Xextern int optind;
	Xextern char *optarg;
	X
	Xstruct statfs stats;
	X
	Xchar path[MAXPATHLEN];
	Xchar label[32];
	Xchar fmt[32];
	Xmain(argc,argv)
	X     int argc;
	X     char **argv;
	X{
	X  int c;
	X  int sflag = 0;
	X  int vflag = 0;
	X  double space;
	X  while((c = getopt(argc,argv,"gmkv")) != EOF) {
	X    switch(c) {
	X    case 'g':
	X      sflag = 3;
	X      break;
	X    case 'm':
	X      sflag = 2;
	X      break;
	X    case 'k':
	X      sflag = 1;
	X      break;
	X    case 'v':
	X      vflag ++;
	X      break;
	X    }
	X  }
	X  if(optind < argc)
	X    STRCPY(path,argv[optind]);
	X  else
	X    STRCPY(path,".");
	X     
	X  if(statfs(path,&stats)) {
	X    perror("statfs");
	X    exit(1);
	X  }
	X  space = (double) (stats.f_bavail * stats.f_bsize);
	X  switch(sflag) {
	X  case 1:
	X    space = space / K_BYTES;
	X    STRCPY(label," Kilobytes");
	X    break;
	X  case 2:
	X    space = space / M_BYTES;
	X    STRCPY(label," Megabytes");
	X    break;
	X  case 3:
	X    space = space / G_BYTES;
	X    STRCPY(label," Gigabytes");
	X    break;
	X  default:
	X    STRCPY(label," Bytes");
	X    break;
	X  }
	X  if(space < 1.0)
	X    STRCPY(fmt,"%-.1f%s\n");
	X  else
	X    STRCPY(fmt,"%-.0f%s\n");
	X
	X  (void) printf(fmt,space,(vflag) ? label : "");
	X
	X}
SHAR_EOF
if test 2514 -ne "`wc -c < 'ds.c'`"
then
	echo shar: error transmitting "'ds.c'" '(should have been 2514 characters)'
fi
fi # end of overwriting check
#	End of shell archive
exit 0
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+  Mike Macgirvin       mike@Jimi-Hendrix.Stanford.EDU +
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++