[net.sources] Source for dirstat.c

phil (12/28/82)

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dir.h>

/*
 * dirstat - print out useful info about directories
 *	     options:   -v  verbose
 *	     Phil Hochstetler
 */

#define P(x)	((long)(x) > 1L ? 's':' ')
struct  stat buf;
struct  direct dir;
int	verbose=0;

main(argc, argv)
int  argc;
char **argv;
{
	register FILE *fp;
	register char *t;
	long count;
	off_t blocks;
	float dirsiz;
	extern char _sobuf[];

	setbuf(stdout, _sobuf);
	if (argc <= 1) 
		usage();
	while (--argc > 0) {
		++argv;
		if (argv[0][0] == '-') {
			t = *argv;
			while (*++t) {
				switch(*t) {
				case 'v':
					verbose++;
					break;
				default:
					usage();
				}
			}
			++argv;
		}
		if (stat(*argv, &buf) != 0) {
			printf("can't stat %s\n", *argv);
			continue;
		}
		if ((buf.st_mode&S_IFMT) != S_IFDIR) {
			printf("%s: not a directory\n", *argv);
			continue;
		}
		if ((fp=fopen(*argv, "r")) == NULL) {
			printf("can't open %s\n", *argv);
			continue;
		}
		count = 0L;
		while (fread(&dir, sizeof(struct direct), 1, fp)) {
			if (dir.d_ino == 0) continue;
			if (verbose)
				printf("%.14s\n", dir.d_name);
			count++;
		}
		fclose(fp);
		blocks = (buf.st_size+(off_t)BUFSIZ-1L)/(off_t)BUFSIZ;
		dirsiz = (float)count/(float)(buf.st_size>>4);
		printf("%3D block%c %4D slots %4D used %6.1f%%:  %s\n",
			blocks, P(blocks), buf.st_size>>4, count, 
			dirsiz*100.0, *argv);
	}
	fflush(stdout);
}
usage()
{
	printf("usage: dirstat [-v] directory[s]\n");
	fflush(stdout);
	exit(1);
}