[comp.sources.misc] v19i004: df2_r4 - R3/R4 son of df2, Part01/01

wht@n4hgf.Mt-Park.GA.US (Warren Tucker) (04/30/91)

Submitted-by: Warren Tucker <wht@n4hgf.Mt-Park.GA.US>
Posting-number: Volume 19, Issue 4
Archive-name: df2_r4/part01

DF2 has proven popular.  Here is a child of it which supports
System V Release 3 or 4.  I am also submitting a patch to the
first df2 from Chip Salzenburg for the original to do NFS
support.

My goal was to customize a df for my needs and to make the code
available for others to do likewise.  Note my original had just
enough room for my longest mount directory :-).  80 characters is
not a lot of room for the wide range of stuff you want to print.
We all are likely to customize.

So, with this version and Chip's NFS patches to the original, we
have a better collection of code and fragments to each build our
idea of a "df -me" dream hack.

#!/bin/sh
# This is df2_r4, a shell archive (shar 3.46)
# made 04/29/1991 16:33 UTC by wht@n4hgf
# Source directory /u1/src/df2
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#   3200 -rw-r--r-- README
#   1140 -rw-r--r-- Makefile
#   4421 -rw-r--r-- df2.c
#
# ============= README ==============
if test -f 'README' -a X"$1" != X"-c"; then
	echo 'x - skipping README (File already exists)'
else
echo 'x - extracting README (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
XThis is an updated version of df2, originally posted to comp.sources.misc as 
X
X> Posting-number: Volume 18, Issue 81
X> Archive-name: df2/part01
X
XPaul Slootman has ported it to System 5 Release 4 (does he REALLY
Xhave all that disk ?? :->).
X
Xit is still easy to make, though the needed library and the compile
Xoptions have prompted a Makefile.  Read and edit the Makefile as
Xnecessary and just make it.
X
XX-Organization: DataCount Register Informatici
X                Postbus 888
X                7640 AW  Wierden
X                The Netherlands
X>Date: Thu, 25 Apr 91 15:43 MDT
X>From: emory!dri.nl!slootman (Paul Slootman)
X>To: wht@n4hgf.mt-park.ga.us
X>Subject: df2 ported to SysV.4
X
XHi,
X
XI snarfed your df2 from the net, and have ported it to System V.4
X(on an ICL DRS6000 Sparc). It needed a few adjustments to allow
Xfor the virtual filesystems, but seems to be running ok for now.
XI'm going to extend it to take into account that some filesystems
Xhave a requirement that some percentage of the disk MUST be free
X(i.e. a normal user may only use up to 90%; beyond that he gets a
X"filesystem full" error). This is something the standard df doesn't
Xhandle. (When it's 89% full, that's what it says. When it's 90%
Xfull, df states 100% full -- which can be confusing "Hey just a
Xmoment ago it said 89%!!!").
XThe percentage is rounded now. That's the usual way.
XA small cosmetic adjustment has also been made - the filesystem
Xnames are longer. Eg.:
X                                   Size (kb)                  Inodes        
XFilesystem      |Direct.| Total   Free    Used   Used| Total  Free  Used   Used
X----------------+-------+----------------------------+-------------------------
X/dev/dsk/c0d0s7 |/      |  28623   10731   17892  63%| 10240   8968   1272  12%
X/proc           |/proc  |      0       0       0   0%|  1002    885    117  12%
X/dev/fd         |/dev/fd|      0       0       0   0%|    50      0     50 100%
X/dev/dsk/c0d0s2 |/stand |  10240    3566    6674  65%|   200    112     88  44%
X/dev/dsk/c0d0s8 |/var   |  24964   13621   11343  45%| 13376  12468    908   7%
X/dev/dsk/c0d0s9 |/dsk00 | 101102   20340   80762  80%| 10240   4470   5770  56%
X/dev/dsk/c0d0s10|/dsk01 |  33679   28435    5244  16%|  3456   2602    854  25%
X/dev/dsk/c0d0s11|/dsk02 |  84490   22899   61591  73%| 35840  28262   7578  21%
X/dev/dsk/c0d0s12|/dsk03 |  63297   26060   37237  59%| 27136  24587   2549   9%
X/dev/dsk/c0d0s14|/dsk04 |  73916   28332   45584  62%| 31232  28899   2333   7%
X/dev/dsk/c0d0s15|/dsk09 |  84490   14107   70383  83%| 35840  33104   2736   8%
X/dev/dsk/c0d0s16|/tmp   |  28223   23147    5076  18%| 12288  12094    194   2%
X/dev/dsk/c1d0s0 |/usr   | 108809   21860   86949  80%| 58048  49097   8951  15%
X/dev/dsk/c1d0s2 |/dsk10 |  52758    6804   45954  87%| 22528  19389   3139  14%
X/dev/dsk/c1d0s3 |/dsk11 | 169005   25563  143442  85%| 71168  60238  10930  15%
X/dev/dsk/c1d0s4 |/dsk12 |  84490   14421   70069  83%| 35840  32931   2909   8%
X/dev/dsk/c1d0s5 |/dsk19 | 182634   25472  157162  86%| 77312  70749   6563   8%
X----------------+-------+----------------------------+-------------------------
X                |total  |1130720  285358  845362  74%|445796 388855  56941  12%
X
SHAR_EOF
chmod 0644 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 3200 -eq "$Wc_c" ||
	echo 'README: original size 3200, current size' "$Wc_c"
fi
# ============= Makefile ==============
if test -f 'Makefile' -a X"$1" != X"-c"; then
	echo 'x - skipping Makefile (File already exists)'
else
echo 'x - extracting Makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
X#+------------------------------------------------------------
X#  Makefile for df2
X#  Set PROGRAM to the executable name
X#-------------------------------------------------------------
X#+:EDITS:
X#:04-26-1991-03:15-wht@n4hgf-since we have two versions and -lm, use a Makefile
X
X#-->   comment if you have System 5 Release 3
X#    uncomment if you have System 5 Release 4
X#SYSVR4 = -DSYSVR4
X
X#--> your compiler may vary
XCC = cc
X#CC = gcc -fwritable-strings -fpcc-struct-return 
X
X#--> where do you want to install it?
XLBIN = /usr/local/bin
X
X# probably don't need to change below here
XCFLAGS = -O $(SYSVR4)
XLDFLAGS =
XLIBS = -lm
XSHELL = /bin/sh
XPROGRAM = df2
XINSTALLED = $(LBIN)/$(PROGRAM)
XSRC = $(PROGRAM).c
XOBJ = $(PROGRAM).o
X.SUFFIXES:
X.SUFFIXES: .c .o .h
X.c.o:; $(CC) -c $(CFLAGS) $*.c
X
X$(PROGRAM): $(OBJ)
X	rm -rf $@
X	$(CC) -o $@ $(LDFLAGS) $(OBJ) $(LIBS)
X
Xinstall: $(PROGRAM)
X	rm -f $(INSTALLED) $(INSTALLED)~
X	if [ -f $(INSTALLED) ]; then mv $(INSTALLED) $(INSTALLED)~; fi
X	cp $(PROGRAM) $(INSTALLED)
X	strip $(INSTALLED)
X	[ -x /usr/bin/mcs ] && /usr/bin/mcs -d $(INSTALLED)
X	chmod o+x $(INSTALLED)
X
Xlint:
X	lint -u -x $(SRC) > $(PROGRAM).lint
SHAR_EOF
chmod 0644 Makefile ||
echo 'restore of Makefile failed'
Wc_c="`wc -c < 'Makefile'`"
test 1140 -eq "$Wc_c" ||
	echo 'Makefile: original size 1140, current size' "$Wc_c"
fi
# ============= df2.c ==============
if test -f 'df2.c' -a X"$1" != X"-c"; then
	echo 'x - skipping df2.c (File already exists)'
else
echo 'x - extracting df2.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'df2.c' &&
X/* CHK=0x90B3 */
Xchar *rev = "1.10";
X/*+-------------------------------------------------------------------------
X	df2.c - another df-like utility
X	wht@n4hgf.Mt-Park.GA.US
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:04-26-1991-04:26-wht@n4hgf-merge R3 code with R4 */
X/*:04-25-1991-15:45-slootman@dri.nl-port to SysV.4 */
X/*:04-07-1991-02:58-wht@n4hgf-creation */
X
X#include <stdio.h>
X#include <ctype.h>
X#include <math.h>
X#include <sys/errno.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#ifdef SYSVR4
X#include <sys/statvfs.h>
X#include <sys/mnttab.h>
X#define statfs statvfs
X#else
X#include <sys/statfs.h>
X#include <mnttab.h>
X#endif /* SYSVR4 */
X
Xextern int errno;
Xextern char *sys_errlist[];
X
Xchar *heading[] =
X{
X"                                   Size (kb)                  Inodes        ",
X#ifdef SYSVR4
X"Filesystem      |Direct.| Total   Free    Used   Used| Total  Free  Used   Used",
X#else
X"Filesystem|Mount Dir    | Total   Free    Used   Used| Total  Free  Used   Used",
X#endif /* SYSVR4 */
X(char *)0
X};
X
X#ifdef SYSVR4
Xchar *sep =
X"----------------+-------+----------------------------+-------------------------";
X#else
Xchar *sep =
X"----------+-------------+----------------------------+-------------------------";
X#endif /* SYSVR4 */
X
X/*+-------------------------------------------------------------------------
X	main(argc,argv)
X--------------------------------------------------------------------------*/
X/*ARGSUSED*/
Xmain(argc,argv)
Xint argc;
Xchar **argv;
X{
Xchar **cpptr = heading;
Xchar *etc_mnttab = "/etc/mnttab";
XFILE *fpmt;
Xstruct mnttab mnttab_buf;
Xstruct mnttab *mt = &mnttab_buf;
Xstruct statfs statf_buf;
Xstruct statfs *fs = &statf_buf;
X
Xlong kb_size;
Xlong kb_free;
Xlong kb_used;
Xint kb_used_pct;
Xlong kb_size_total = 0;
Xlong kb_free_total = 0;
Xlong kb_used_total = 0;
X
Xlong ino_size;
Xlong ino_free;
Xlong ino_used;
Xint ino_used_pct;
Xlong ino_size_total = 0;
Xlong ino_free_total = 0;
Xlong ino_used_total = 0;
X
X#ifdef SYSVR4
Xchar *fmt_line ="%-16.16s|%-7.7s|%7ld %7ld %7ld %3d%%|%6ld %6ld %6ld %3d%%\n";
X#else
Xchar *fmt_line ="%-10.10s|%-13.13s|%7ld %7ld %7ld %3d%%|%6ld %6ld %6ld %3d%%\n";
X#endif /* SYSVR4 */
X
X	if(argc != 1)
X	{
X		(void)fprintf(stderr,"df2 %s: no arguments required or accepted\n",rev);
X		exit(1);
X	}
X
X	if(!(fpmt = fopen(etc_mnttab,"r")))
X	{
X		perror(etc_mnttab);
X		exit(1);
X	}
X
X	while(*cpptr)
X		(void)printf("%s\n",*cpptr++);
X	(void)printf("%s\n",sep);
X
X	while	(
X#ifdef SYSVR4
X		!getmntent(fpmt,mt)
X#else
X		fread((char *)mt,1,sizeof(*mt),fpmt) == sizeof(*mt)
X#endif /* SYSVR4 */
X			)
X	{
X		if(
X#ifdef SYSVR4
X			statvfs(mt->mnt_mountp,fs)
X#else
X			statfs(mt->mt_filsys,fs,sizeof(*fs),0)
X#endif
X				)
X		{
X#ifdef SYSVR4
X			(void)printf("%-16.16s statvfs: %s\n",
X				mt->mnt_special,sys_errlist[errno]);
X#else
X			(void)printf("%-10.10s statfs: %s\n",
X				mt->mt_dev,sys_errlist[errno]);
X#endif /* SYSVR4 */
X			continue;
X		}
X
X#ifdef SYSVR4
X		kb_size = fs->f_blocks * fs->f_frsize / 1024L;
X		kb_free = fs->f_bfree * fs->f_frsize / 1024L;
X#else
X		kb_size = fs->f_blocks  / 2L;
X		kb_free = fs->f_bfree / 2L;
X#endif /* SYSVR4 */
X		kb_used = kb_size - kb_free;
X
X		if(kb_size>0) 
X		{
X			kb_used_pct = (int)floor(
X				((double)kb_used * 100) / (double)kb_size + 0.5);
X		}
X		else
X			kb_used_pct = 0;
X
X		ino_size = fs->f_files;
X		ino_free = fs->f_ffree;
X		ino_used = ino_size - ino_free;
X		if(ino_size>0)
X		{
X			ino_used_pct = (int)floor(
X				((double)ino_used * 100) / (double)ino_size + 0.5);
X		}
X		else
X			ino_used_pct=0;
X
X#ifdef SYSVR4
X		(void)printf(fmt_line,
X			mt->mnt_special,mt->mnt_mountp,
X			kb_size,kb_free,kb_used,kb_used_pct,
X			ino_size,ino_free,ino_used,ino_used_pct);
X#else
X		(void)printf(fmt_line,
X			mt->mt_dev,mt->mt_filsys,
X			kb_size,kb_free,kb_used,kb_used_pct,
X			ino_size,ino_free,ino_used,ino_used_pct);
X#endif /* SYSVR4 */
X
X		kb_size_total  += kb_size;
X		kb_free_total  += kb_free;
X		kb_used_total  += kb_used;
X
X		ino_size_total  += ino_size;
X		ino_free_total  += ino_free;
X		ino_used_total  += ino_used;
X	}
X
X	kb_used_pct = (int)(kb_used_total * 100 / kb_size_total);
X	ino_used_pct = (int)(ino_used_total * 100L / (long)ino_size_total);
X
X	(void)printf("%s\n",sep);
X	(void)printf(fmt_line, "","total",
X		kb_size_total,kb_free_total,kb_used_total,kb_used_pct,
X		ino_size_total,ino_free_total,ino_used_total,ino_used_pct);
X
X	exit(0);
X	/*NOTREACHED*/
X}	/* end of main */
X
X/* vi: set tabstop=4 shiftwidth=4: */
X/* end of df2.c */
SHAR_EOF
chmod 0644 df2.c ||
echo 'restore of df2.c failed'
Wc_c="`wc -c < 'df2.c'`"
test 4421 -eq "$Wc_c" ||
	echo 'df2.c: original size 4421, current size' "$Wc_c"
fi
exit 0

---------------------------------------------------------------------
Warren Tucker, TuckerWare  emory!n4hgf!wht or wht@n4hgf.Mt-Park.GA.US
Ker-au'-lo-phon.   An  8-foot  partial  flue-stop, having metal pipes
surmounted by adjustable rings, and with a hole bored near the top of
each pipe.  Tone soft and "reedy".

exit 0 # Just in case...
-- 
Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
Sterling Software, IMD           UUCP:     uunet!sparky!kent
Phone:    (402) 291-8300         FAX:      (402) 291-4362
Please send comp.sources.misc-related mail to kent@uunet.uu.net.