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