Home | History | Annotate | Line # | Download | only in quot
quot.c revision 1.2
      1  1.1   ws /*
      2  1.1   ws  * Copyright (C) 1991, 1994 Wolfgang Solfrank.
      3  1.1   ws  * Copyright (C) 1991, 1994 TooLs GmbH.
      4  1.1   ws  * All rights reserved.
      5  1.1   ws  *
      6  1.1   ws  * Redistribution and use in source and binary forms, with or without
      7  1.1   ws  * modification, are permitted provided that the following conditions
      8  1.1   ws  * are met:
      9  1.1   ws  * 1. Redistributions of source code must retain the above copyright
     10  1.1   ws  *    notice, this list of conditions and the following disclaimer.
     11  1.1   ws  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   ws  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   ws  *    documentation and/or other materials provided with the distribution.
     14  1.1   ws  * 3. All advertising materials mentioning features or use of this software
     15  1.1   ws  *    must display the following acknowledgement:
     16  1.1   ws  *	This product includes software developed by TooLs GmbH.
     17  1.1   ws  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     18  1.1   ws  *    derived from this software without specific prior written permission.
     19  1.1   ws  *
     20  1.1   ws  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     21  1.1   ws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1   ws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1   ws  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  1.1   ws  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     25  1.1   ws  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     26  1.1   ws  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27  1.1   ws  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     28  1.1   ws  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     29  1.1   ws  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1   ws  */
     31  1.2  cgd 
     32  1.2  cgd #ifndef lint
     33  1.2  cgd static char rcsid[] = "$Id: quot.c,v 1.2 1994/02/11 00:23:42 cgd Exp $";
     34  1.2  cgd #endif /* not lint */
     35  1.2  cgd 
     36  1.1   ws #include <stdio.h>
     37  1.1   ws #include <stdlib.h>
     38  1.1   ws #include <string.h>
     39  1.1   ws #include <errno.h>
     40  1.1   ws #include <pwd.h>
     41  1.1   ws #include <sys/param.h>
     42  1.1   ws #include <sys/mount.h>
     43  1.1   ws #include <ufs/fs.h>
     44  1.1   ws #include <ufs/quota.h>
     45  1.1   ws #include <ufs/inode.h>
     46  1.1   ws 
     47  1.1   ws /* some flags of what to do: */
     48  1.1   ws static char estimate;
     49  1.1   ws static char count;
     50  1.1   ws static char unused;
     51  1.1   ws static int (*func)();
     52  1.1   ws static long blocksize;
     53  1.1   ws static char *header;
     54  1.1   ws static int headerlen;
     55  1.1   ws 
     56  1.2  cgd /*
     57  1.2  cgd  * Original BSD quot doesn't round to number of frags/blocks,
     58  1.2  cgd  * doesn't account for indirection blocks and gets it totally
     59  1.2  cgd  * wrong if the	size is a multiple of the blocksize.
     60  1.2  cgd  * The new code always counts the number of 512 byte blocks
     61  1.2  cgd  * instead of the number of kilobytes and converts them	to
     62  1.2  cgd  * kByte when done (on request).
     63  1.2  cgd  */
     64  1.1   ws #ifdef	COMPAT
     65  1.1   ws #define	SIZE(n)	(n)
     66  1.1   ws #else
     67  1.1   ws #define	SIZE(n)	(((n) * 512 + blocksize - 1)/blocksize)
     68  1.1   ws #endif
     69  1.1   ws 
     70  1.1   ws #define	INOCNT(fs)	((fs)->fs_ipg)
     71  1.1   ws #define	INOSZ(fs)	(sizeof(struct dinode) * INOCNT(fs))
     72  1.1   ws 
     73  1.1   ws static struct dinode *get_inode(fd,super,ino)
     74  1.1   ws 	struct fs *super;
     75  1.1   ws 	ino_t ino;
     76  1.1   ws {
     77  1.1   ws 	static struct dinode *ip;
     78  1.1   ws 	static ino_t last;
     79  1.1   ws 
     80  1.1   ws 	if (fd < 0) {		/* flush cache */
     81  1.1   ws 		if (ip) {
     82  1.1   ws 			free(ip);
     83  1.1   ws 			ip = 0;
     84  1.1   ws 		}
     85  1.1   ws 		return 0;
     86  1.1   ws 	}
     87  1.1   ws 
     88  1.1   ws 	if (!ip || ino < last || ino >= last + INOCNT(super)) {
     89  1.1   ws 		if (!ip
     90  1.1   ws 		    && !(ip = (struct dinode *)malloc(INOSZ(super)))) {
     91  1.1   ws 			perror("allocate inodes");
     92  1.1   ws 			exit(1);
     93  1.1   ws 		}
     94  1.1   ws 		last = (ino / INOCNT(super)) * INOCNT(super);
     95  1.1   ws 		if (lseek(fd,itod(super,last) << super->fs_fshift,0) < 0
     96  1.1   ws 		    || read(fd,ip,INOSZ(super)) != INOSZ(super)) {
     97  1.1   ws 			perror("read inodes");
     98  1.1   ws 			exit(1);
     99  1.1   ws 		}
    100  1.1   ws 	}
    101  1.1   ws 
    102  1.1   ws 	return ip + ino % INOCNT(super);
    103  1.1   ws }
    104  1.1   ws 
    105  1.1   ws #ifdef	COMPAT
    106  1.1   ws #define	actualblocks(super,ip)	((ip)->di_blocks/2)
    107  1.1   ws #else
    108  1.1   ws #define	actualblocks(super,ip)	((ip)->di_blocks)
    109  1.1   ws #endif
    110  1.1   ws 
    111  1.1   ws static virtualblocks(super,ip)
    112  1.1   ws 	struct fs *super;
    113  1.1   ws 	struct dinode *ip;
    114  1.1   ws {
    115  1.1   ws 	register off_t nblk, sz;
    116  1.1   ws 
    117  1.1   ws 	sz = ip->di_size;
    118  1.1   ws #ifdef	COMPAT
    119  1.1   ws 	if (lblkno(super,sz) >= NDADDR) {
    120  1.1   ws 		nblk = blkroundup(super,sz);
    121  1.1   ws 		if (sz == nblk)
    122  1.1   ws 			nblk += super->fs_bsize;
    123  1.1   ws 	}
    124  1.1   ws 
    125  1.1   ws 	return sz / 1024;
    126  1.1   ws 
    127  1.1   ws #else	/* COMPAT */
    128  1.1   ws 
    129  1.1   ws 	if (lblkno(super,sz) >= NDADDR) {
    130  1.1   ws 		nblk = blkroundup(super,sz);
    131  1.1   ws 		sz = lblkno(super,nblk);
    132  1.1   ws 		sz = (sz - NDADDR + NINDIR(super) - 1) / NINDIR(super);
    133  1.1   ws 		while (sz > 0) {
    134  1.1   ws 			nblk += sz * super->fs_bsize;
    135  1.1   ws 			/* sz - 1 rounded up */
    136  1.1   ws 			sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
    137  1.1   ws 		}
    138  1.1   ws 	} else
    139  1.1   ws 		nblk = fragroundup(super,sz);
    140  1.1   ws 
    141  1.1   ws 	return nblk / 512;
    142  1.1   ws #endif	/* COMPAT */
    143  1.1   ws }
    144  1.1   ws 
    145  1.1   ws static isfree(ip)
    146  1.1   ws 	struct dinode *ip;
    147  1.1   ws {
    148  1.1   ws #ifdef	COMPAT
    149  1.1   ws 	return (ip->di_mode&IFMT) == 0;
    150  1.1   ws #else	/* COMPAT */
    151  1.1   ws 
    152  1.1   ws 	switch (ip->di_mode&IFMT) {
    153  1.1   ws 	case IFIFO:
    154  1.1   ws 	case IFLNK:		/* should check FASTSYMLINK? */
    155  1.1   ws 	case IFDIR:
    156  1.1   ws 	case IFREG:
    157  1.1   ws 		return 0;
    158  1.1   ws 	default:
    159  1.1   ws 		return 1;
    160  1.1   ws 	}
    161  1.1   ws #endif
    162  1.1   ws }
    163  1.1   ws 
    164  1.1   ws static struct user {
    165  1.1   ws 	uid_t uid;
    166  1.1   ws 	char *name;
    167  1.1   ws 	daddr_t space;
    168  1.1   ws 	long count;
    169  1.1   ws 	daddr_t spc30;
    170  1.1   ws 	daddr_t spc60;
    171  1.1   ws 	daddr_t spc90;
    172  1.1   ws } *users;
    173  1.1   ws static int nusers;
    174  1.1   ws 
    175  1.1   ws static inituser()
    176  1.1   ws {
    177  1.1   ws 	register i;
    178  1.1   ws 	register struct user *usr;
    179  1.1   ws 
    180  1.1   ws 	if (!nusers) {
    181  1.1   ws 		nusers = 8;
    182  1.2  cgd 		if (!(users =
    183  1.2  cgd 		    (struct user *)calloc(nusers,sizeof(struct user)))) {
    184  1.1   ws 			perror("allocate users");
    185  1.1   ws 			exit(1);
    186  1.1   ws 		}
    187  1.1   ws 	} else {
    188  1.1   ws 		for (usr = users, i = nusers; --i >= 0; usr++) {
    189  1.1   ws 			usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
    190  1.1   ws 			usr->count = 0;
    191  1.1   ws 		}
    192  1.1   ws 	}
    193  1.1   ws }
    194  1.1   ws 
    195  1.1   ws static usrrehash()
    196  1.1   ws {
    197  1.1   ws 	register i;
    198  1.1   ws 	register struct user *usr, *usrn;
    199  1.1   ws 	struct user *svusr;
    200  1.1   ws 
    201  1.1   ws 	svusr = users;
    202  1.1   ws 	nusers <<= 1;
    203  1.1   ws 	if (!(users = (struct user *)calloc(nusers,sizeof(struct user)))) {
    204  1.1   ws 		perror("allocate users");
    205  1.1   ws 		exit(1);
    206  1.1   ws 	}
    207  1.1   ws 	for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
    208  1.2  cgd 		for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
    209  1.2  cgd 		    usrn--) {
    210  1.1   ws 			if (usrn <= users)
    211  1.1   ws 				usrn = users + nusers;
    212  1.1   ws 		}
    213  1.1   ws 		*usrn = *usr;
    214  1.1   ws 	}
    215  1.1   ws }
    216  1.1   ws 
    217  1.1   ws static struct user *user(uid)
    218  1.1   ws 	uid_t uid;
    219  1.1   ws {
    220  1.1   ws 	register struct user *usr;
    221  1.1   ws 	register i;
    222  1.1   ws 	struct passwd *pwd;
    223  1.1   ws 
    224  1.1   ws 	while (1) {
    225  1.2  cgd 		for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
    226  1.2  cgd 		    usr--) {
    227  1.1   ws 			if (!usr->name) {
    228  1.1   ws 				usr->uid = uid;
    229  1.1   ws 
    230  1.1   ws 				if (!(pwd = getpwuid(uid))) {
    231  1.1   ws 					if (usr->name = (char *)malloc(7))
    232  1.1   ws 						sprintf(usr->name,"#%d",uid);
    233  1.1   ws 				} else {
    234  1.2  cgd 					if (usr->name = (char *)
    235  1.2  cgd 					    malloc(strlen(pwd->pw_name) + 1))
    236  1.1   ws 						strcpy(usr->name,pwd->pw_name);
    237  1.1   ws 				}
    238  1.1   ws 				if (!usr->name) {
    239  1.1   ws 					perror("allocate users");
    240  1.1   ws 					exit(1);
    241  1.1   ws 				}
    242  1.1   ws 
    243  1.1   ws 				return usr;
    244  1.1   ws 
    245  1.1   ws 			} else if (usr->uid == uid)
    246  1.1   ws 				return usr;
    247  1.1   ws 
    248  1.1   ws 			if (usr <= users)
    249  1.1   ws 				usr = users + nusers;
    250  1.1   ws 		}
    251  1.1   ws 		usrrehash();
    252  1.1   ws 	}
    253  1.1   ws }
    254  1.1   ws 
    255  1.1   ws static cmpusers(u1,u2)
    256  1.1   ws 	struct user *u1, *u2;
    257  1.1   ws {
    258  1.1   ws 	return u2->space - u1->space;
    259  1.1   ws }
    260  1.2  cgd 
    261  1.2  cgd #define	sortusers(users)	(qsort((users),nusers,sizeof(struct user), \
    262  1.2  cgd 				    cmpusers))
    263  1.1   ws 
    264  1.1   ws static uses(uid,blks,act)
    265  1.1   ws 	uid_t uid;
    266  1.1   ws 	daddr_t blks;
    267  1.1   ws 	time_t act;
    268  1.1   ws {
    269  1.1   ws 	static time_t today;
    270  1.1   ws 	register struct user *usr;
    271  1.1   ws 
    272  1.1   ws 	if (!today)
    273  1.1   ws 		time(&today);
    274  1.1   ws 
    275  1.1   ws 	usr = user(uid);
    276  1.1   ws 	usr->count++;
    277  1.1   ws 	usr->space += blks;
    278  1.1   ws 
    279  1.1   ws 	if (today - act > 90L * 24L * 60L * 60L)
    280  1.1   ws 		usr->spc90 += blks;
    281  1.1   ws 	if (today - act > 60L * 24L * 60L * 60L)
    282  1.1   ws 		usr->spc60 += blks;
    283  1.1   ws 	if (today - act > 30L * 24L * 60L * 60L)
    284  1.1   ws 		usr->spc30 += blks;
    285  1.1   ws }
    286  1.1   ws 
    287  1.1   ws #ifdef	COMPAT
    288  1.1   ws #define	FSZCNT	500
    289  1.1   ws #else
    290  1.1   ws #define	FSZCNT	512
    291  1.1   ws #endif
    292  1.1   ws struct fsizes {
    293  1.1   ws 	struct fsizes *fsz_next;
    294  1.1   ws 	daddr_t fsz_first, fsz_last;
    295  1.1   ws 	ino_t fsz_count[FSZCNT];
    296  1.1   ws 	daddr_t fsz_sz[FSZCNT];
    297  1.1   ws } *fsizes;
    298  1.1   ws 
    299  1.1   ws static initfsizes()
    300  1.1   ws {
    301  1.1   ws 	register struct fsizes *fp;
    302  1.1   ws 	register i;
    303  1.1   ws 
    304  1.1   ws 	for (fp = fsizes; fp; fp = fp->fsz_next) {
    305  1.1   ws 		for (i = FSZCNT; --i >= 0;) {
    306  1.1   ws 			fp->fsz_count[i] = 0;
    307  1.1   ws 			fp->fsz_sz[i] = 0;
    308  1.1   ws 		}
    309  1.1   ws 	}
    310  1.1   ws }
    311  1.1   ws 
    312  1.1   ws static dofsizes(fd,super,name)
    313  1.1   ws 	struct fs *super;
    314  1.1   ws 	char *name;
    315  1.1   ws {
    316  1.1   ws 	ino_t inode, maxino;
    317  1.1   ws 	struct dinode *ip;
    318  1.1   ws 	daddr_t sz, ksz;
    319  1.1   ws 	struct fsizes *fp, **fsp;
    320  1.1   ws 	register i;
    321  1.1   ws 
    322  1.1   ws 	maxino = super->fs_ncg * super->fs_ipg - 1;
    323  1.1   ws #ifdef	COMPAT
    324  1.1   ws 	if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes)))) {
    325  1.1   ws 		perror("alloc fsize structure");
    326  1.1   ws 		exit(1);
    327  1.1   ws 	}
    328  1.1   ws #endif	/* COMPAT */
    329  1.1   ws 	for (inode = 0; inode < maxino; inode++) {
    330  1.1   ws 		errno = 0;
    331  1.1   ws 		if ((ip = get_inode(fd,super,inode))
    332  1.1   ws #ifdef	COMPAT
    333  1.1   ws 		    && ((ip->di_mode&IFMT) == IFREG
    334  1.1   ws 			|| (ip->di_mode&IFMT) == IFDIR)
    335  1.1   ws #else	/* COMPAT */
    336  1.1   ws 		    && !isfree(ip)
    337  1.1   ws #endif	/* COMPAT */
    338  1.1   ws 		    ) {
    339  1.2  cgd 			sz = estimate ? virtualblocks(super,ip) :
    340  1.2  cgd 			    actualblocks(super,ip);
    341  1.1   ws #ifdef	COMPAT
    342  1.1   ws 			if (sz >= FSZCNT) {
    343  1.1   ws 				fsizes->fsz_count[FSZCNT-1]++;
    344  1.1   ws 				fsizes->fsz_sz[FSZCNT-1] += sz;
    345  1.1   ws 			} else {
    346  1.1   ws 				fsizes->fsz_count[sz]++;
    347  1.1   ws 				fsizes->fsz_sz[sz] += sz;
    348  1.1   ws 			}
    349  1.1   ws #else	/* COMPAT */
    350  1.1   ws 			ksz = SIZE(sz);
    351  1.1   ws 			for (fsp = &fsizes; fp = *fsp; fsp = &fp->fsz_next) {
    352  1.1   ws 				if (ksz < fp->fsz_last)
    353  1.1   ws 					break;
    354  1.1   ws 			}
    355  1.1   ws 			if (!fp || ksz < fp->fsz_first) {
    356  1.2  cgd 				if (!(fp = (struct fsizes *)
    357  1.2  cgd 				    malloc(sizeof(struct fsizes)))) {
    358  1.1   ws 					perror("alloc fsize structure");
    359  1.1   ws 					exit(1);
    360  1.1   ws 				}
    361  1.1   ws 				fp->fsz_next = *fsp;
    362  1.1   ws 				*fsp = fp;
    363  1.1   ws 				fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
    364  1.1   ws 				fp->fsz_last = fp->fsz_first + FSZCNT;
    365  1.1   ws 				for (i = FSZCNT; --i >= 0;) {
    366  1.1   ws 					fp->fsz_count[i] = 0;
    367  1.1   ws 					fp->fsz_sz[i] = 0;
    368  1.1   ws 				}
    369  1.1   ws 			}
    370  1.1   ws 			fp->fsz_count[ksz % FSZCNT]++;
    371  1.1   ws 			fp->fsz_sz[ksz % FSZCNT] += sz;
    372  1.1   ws #endif	/* COMPAT */
    373  1.1   ws 		} else if (errno) {
    374  1.1   ws 			perror(name);
    375  1.1   ws 			exit(1);
    376  1.1   ws 		}
    377  1.1   ws 	}
    378  1.1   ws 	sz = 0;
    379  1.1   ws 	for (fp = fsizes; fp; fp = fp->fsz_next) {
    380  1.1   ws 		for (i = 0; i < FSZCNT; i++) {
    381  1.1   ws 			if (fp->fsz_count[i])
    382  1.1   ws 				printf("%d\t%d\t%d\n",fp->fsz_first + i,
    383  1.2  cgd 				    fp->fsz_count[i],
    384  1.2  cgd 				    SIZE(sz += fp->fsz_sz[i]));
    385  1.1   ws 		}
    386  1.1   ws 	}
    387  1.1   ws }
    388  1.1   ws 
    389  1.1   ws static douser(fd,super,name)
    390  1.1   ws 	struct fs *super;
    391  1.1   ws 	char *name;
    392  1.1   ws {
    393  1.1   ws 	ino_t inode, maxino;
    394  1.1   ws 	struct user *usr, *usrs;
    395  1.1   ws 	struct dinode *ip;
    396  1.1   ws 	register n;
    397  1.1   ws 
    398  1.1   ws 	maxino = super->fs_ncg * super->fs_ipg - 1;
    399  1.1   ws 	for (inode = 0; inode < maxino; inode++) {
    400  1.1   ws 		errno = 0;
    401  1.1   ws 		if ((ip = get_inode(fd,super,inode))
    402  1.1   ws 		    && !isfree(ip))
    403  1.1   ws 			uses(ip->di_uid,
    404  1.2  cgd 			    estimate ? virtualblocks(super,ip) :
    405  1.2  cgd 				actualblocks(super,ip),
    406  1.2  cgd 			    ip->di_atime);
    407  1.1   ws 		else if (errno) {
    408  1.1   ws 			perror(name);
    409  1.1   ws 			exit(1);
    410  1.1   ws 		}
    411  1.1   ws 	}
    412  1.1   ws 	if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user)))) {
    413  1.1   ws 		perror("allocate users");
    414  1.1   ws 		exit(1);
    415  1.1   ws 	}
    416  1.1   ws 	bcopy(users,usrs,nusers * sizeof(struct user));
    417  1.1   ws 	sortusers(usrs);
    418  1.1   ws 	for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
    419  1.1   ws 		printf("%5d",SIZE(usr->space));
    420  1.1   ws 		if (count)
    421  1.1   ws 			printf("\t%5d",usr->count);
    422  1.1   ws 		printf("\t%-8s",usr->name);
    423  1.1   ws 		if (unused)
    424  1.1   ws 			printf("\t%5d\t%5d\t%5d",
    425  1.1   ws 			       SIZE(usr->spc30),
    426  1.1   ws 			       SIZE(usr->spc60),
    427  1.1   ws 			       SIZE(usr->spc90));
    428  1.1   ws 		printf("\n");
    429  1.1   ws 	}
    430  1.1   ws 	free(usrs);
    431  1.1   ws }
    432  1.1   ws 
    433  1.1   ws static donames(fd,super,name)
    434  1.1   ws 	struct fs *super;
    435  1.1   ws 	char *name;
    436  1.1   ws {
    437  1.1   ws 	int c;
    438  1.1   ws 	ino_t inode, inode1;
    439  1.1   ws 	ino_t maxino;
    440  1.1   ws 	struct dinode *ip;
    441  1.1   ws 
    442  1.1   ws 	maxino = super->fs_ncg * super->fs_ipg - 1;
    443  1.1   ws 	/* first skip the name of the filesystem */
    444  1.1   ws 	while ((c = getchar()) != EOF && (c < '0' || c > '9'))
    445  1.1   ws 		while ((c = getchar()) != EOF && c != '\n');
    446  1.1   ws 	ungetc(c,stdin);
    447  1.1   ws 	inode1 = -1;
    448  1.1   ws 	while (scanf("%d",&inode) == 1) {
    449  1.1   ws 		if (inode < 0 || inode > maxino) {
    450  1.1   ws 			fprintf(stderr,"illegal inode %d\n",inode);
    451  1.1   ws 			return;
    452  1.1   ws 		}
    453  1.1   ws 		errno = 0;
    454  1.1   ws 		if ((ip = get_inode(fd,super,inode))
    455  1.1   ws 		    && !isfree(ip)) {
    456  1.1   ws 			printf("%s\t",user(ip->di_uid)->name);
    457  1.1   ws 			/* now skip whitespace */
    458  1.1   ws 			while ((c = getchar()) == ' ' || c == '\t');
    459  1.1   ws 			/* and print out the remainder of the input line */
    460  1.1   ws 			while (c != EOF && c != '\n') {
    461  1.1   ws 				putchar(c);
    462  1.1   ws 				c = getchar();
    463  1.1   ws 			}
    464  1.1   ws 			putchar('\n');
    465  1.1   ws 			inode1 = inode;
    466  1.1   ws 		} else {
    467  1.1   ws 			if (errno) {
    468  1.1   ws 				perror(name);
    469  1.1   ws 				exit(1);
    470  1.1   ws 			}
    471  1.1   ws 			/* skip this line */
    472  1.1   ws 			while ((c = getchar()) != EOF && c != '\n');
    473  1.1   ws 		}
    474  1.1   ws 		if (c == EOF)
    475  1.1   ws 			break;
    476  1.1   ws 	}
    477  1.1   ws }
    478  1.1   ws 
    479  1.1   ws static usage()
    480  1.1   ws {
    481  1.1   ws #ifdef	COMPAT
    482  1.1   ws 	fprintf(stderr,"Usage: quot [-nfcvha] [filesystem ...]\n");
    483  1.1   ws #else	/* COMPAT */
    484  1.1   ws 	fprintf(stderr,"Usage: quot [ -acfhknv ] [ filesystem ... ]\n");
    485  1.1   ws #endif	/* COMPAT */
    486  1.1   ws 	exit(1);
    487  1.1   ws }
    488  1.1   ws 
    489  1.1   ws static char superblock[SBSIZE];
    490  1.1   ws 
    491  1.1   ws quot(name,mp)
    492  1.1   ws 	char *name, *mp;
    493  1.1   ws {
    494  1.1   ws 	int fd;
    495  1.1   ws 
    496  1.1   ws 	get_inode(-1);		/* flush cache */
    497  1.1   ws 	inituser();
    498  1.1   ws 	initfsizes();
    499  1.1   ws 	if ((fd = open(name,0)) < 0
    500  1.1   ws 	    || lseek(fd,SBOFF,0) != SBOFF
    501  1.1   ws 	    || read(fd,superblock,SBSIZE) != SBSIZE) {
    502  1.1   ws 		perror(name);
    503  1.1   ws 		close(fd);
    504  1.1   ws 		return;
    505  1.1   ws 	}
    506  1.1   ws 	if (((struct fs *)superblock)->fs_magic != FS_MAGIC) {
    507  1.1   ws 		fprintf(stderr,"%s: not a BSD filesystem\n",name);
    508  1.1   ws 		close(fd);
    509  1.1   ws 		return;
    510  1.1   ws 	}
    511  1.1   ws 	printf("%s:",name);
    512  1.1   ws 	if (mp)
    513  1.1   ws 		printf(" (%s)",mp);
    514  1.1   ws 	putchar('\n');
    515  1.1   ws 	(*func)(fd,superblock,name);
    516  1.1   ws 	close(fd);
    517  1.1   ws }
    518  1.1   ws 
    519  1.1   ws int main(argc,argv)
    520  1.1   ws 	char **argv;
    521  1.1   ws {
    522  1.1   ws 	int fd;
    523  1.1   ws 	char all = 0;
    524  1.1   ws 	FILE *fp;
    525  1.1   ws 	struct statfs *mp;
    526  1.1   ws 	char dev[MNAMELEN + 1];
    527  1.1   ws 	char *nm;
    528  1.1   ws 	int cnt;
    529  1.1   ws 
    530  1.1   ws 	func = douser;
    531  1.1   ws #ifndef	COMPAT
    532  1.1   ws 	header = getbsize(&headerlen,&blocksize);
    533  1.1   ws #endif
    534  1.1   ws 	while (--argc > 0 && **++argv == '-') {
    535  1.1   ws 		while (*++*argv) {
    536  1.1   ws 			switch (**argv) {
    537  1.1   ws 			case 'n':
    538  1.1   ws 				func = donames;
    539  1.1   ws 				break;
    540  1.1   ws 			case 'c':
    541  1.1   ws 				func = dofsizes;
    542  1.1   ws 				break;
    543  1.1   ws 			case 'a':
    544  1.1   ws 				all = 1;
    545  1.1   ws 				break;
    546  1.1   ws 			case 'f':
    547  1.1   ws 				count = 1;
    548  1.1   ws 				break;
    549  1.1   ws 			case 'h':
    550  1.1   ws 				estimate = 1;
    551  1.1   ws 				break;
    552  1.1   ws #ifndef	COMPAT
    553  1.1   ws 			case 'k':
    554  1.1   ws 				blocksize = 1024;
    555  1.1   ws 				break;
    556  1.1   ws #endif	/* COMPAT */
    557  1.1   ws 			case 'v':
    558  1.1   ws 				unused = 1;
    559  1.1   ws 				break;
    560  1.1   ws 			default:
    561  1.1   ws 				usage();
    562  1.1   ws 			}
    563  1.1   ws 		}
    564  1.1   ws 	}
    565  1.1   ws 	if (all) {
    566  1.1   ws 		cnt = getmntinfo(&mp,MNT_NOWAIT);
    567  1.1   ws 		for (; --cnt >= 0; mp++) {
    568  1.1   ws 			if (mp->f_type == MOUNT_UFS) {
    569  1.1   ws 				if (nm = strrchr(mp->f_mntfromname,'/')) {
    570  1.1   ws 					sprintf(dev,"/dev/r%s",nm + 1);
    571  1.1   ws 					nm = dev;
    572  1.1   ws 				} else
    573  1.1   ws 					nm = mp->f_mntfromname;
    574  1.1   ws 				quot(nm,mp->f_mntonname);
    575  1.1   ws 			}
    576  1.1   ws 		}
    577  1.1   ws 	}
    578  1.1   ws 	while (--argc >= 0)
    579  1.1   ws 		quot(*argv++,0);
    580  1.1   ws 	return 0;
    581  1.1   ws }
    582