Home | History | Annotate | Line # | Download | only in edquota
edquota.c revision 1.14
      1 /*
      2  * Copyright (c) 1980, 1990, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Robert Elz at The University of Melbourne.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 static char copyright[] =
     39 "@(#) Copyright (c) 1980, 1990, 1993\n\
     40 	The Regents of the University of California.  All rights reserved.\n";
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 /* static char sccsid[] = "from: @(#)edquota.c	8.3 (Berkeley) 4/27/95"; */
     45 static char *rcsid = "$NetBSD: edquota.c,v 1.14 1997/08/25 19:32:04 kleink Exp $";
     46 #endif /* not lint */
     47 
     48 /*
     49  * Disk quota editor.
     50  */
     51 #include <sys/param.h>
     52 #include <sys/stat.h>
     53 #include <sys/file.h>
     54 #include <sys/wait.h>
     55 #include <sys/queue.h>
     56 #include <ufs/ufs/quota.h>
     57 #include <errno.h>
     58 #include <fstab.h>
     59 #include <pwd.h>
     60 #include <grp.h>
     61 #include <ctype.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 #include "pathnames.h"
     67 
     68 char *qfname = QUOTAFILENAME;
     69 char *qfextension[] = INITQFNAMES;
     70 char *quotagroup = QUOTAGROUP;
     71 char tmpfil[] = _PATH_TMP;
     72 
     73 struct quotause {
     74 	struct	quotause *next;
     75 	long	flags;
     76 	struct	dqblk dqblk;
     77 	char	fsname[MAXPATHLEN + 1];
     78 	char	qfname[1];	/* actually longer */
     79 } *getprivs();
     80 #define	FOUND	0x01
     81 
     82 void	usage __P((void));
     83 int	getentry __P((char *, int));
     84 struct quotause *
     85 	getprivs __P((long, int));
     86 void	putprivs __P((long, int, struct quotause *));
     87 int	editit __P((char *));
     88 int	writeprivs __P((struct quotause *, int, char *, int));
     89 int	readprivs __P((struct quotause *, int));
     90 int	writetimes __P((struct quotause *, int, int));
     91 int	readtimes __P((struct quotause *, int));
     92 char *	cvtstoa __P((time_t));
     93 int	cvtatos __P((time_t, char *, time_t *));
     94 void	freeprivs __P((struct quotause *));
     95 int	alldigits __P((char *));
     96 int	hasquota __P((struct fstab *, int, char **));
     97 
     98 int
     99 main(argc, argv)
    100 	register char **argv;
    101 	int argc;
    102 {
    103 	register struct quotause *qup, *protoprivs, *curprivs;
    104 	extern char *optarg;
    105 	extern int optind;
    106 	register long id, protoid;
    107 	register int quotatype, tmpfd;
    108 	char *protoname;
    109 	int ch;
    110 	int tflag = 0, pflag = 0;
    111 
    112 	if (argc < 2)
    113 		usage();
    114 	if (getuid()) {
    115 		fprintf(stderr, "edquota: permission denied\n");
    116 		exit(1);
    117 	}
    118 	quotatype = USRQUOTA;
    119 	while ((ch = getopt(argc, argv, "ugtp:")) != -1) {
    120 		switch(ch) {
    121 		case 'p':
    122 			protoname = optarg;
    123 			pflag++;
    124 			break;
    125 		case 'g':
    126 			quotatype = GRPQUOTA;
    127 			break;
    128 		case 'u':
    129 			quotatype = USRQUOTA;
    130 			break;
    131 		case 't':
    132 			tflag++;
    133 			break;
    134 		default:
    135 			usage();
    136 		}
    137 	}
    138 	argc -= optind;
    139 	argv += optind;
    140 	if (pflag) {
    141 		if ((protoid = getentry(protoname, quotatype)) == -1)
    142 			exit(1);
    143 		protoprivs = getprivs(protoid, quotatype);
    144 		for (qup = protoprivs; qup; qup = qup->next) {
    145 			qup->dqblk.dqb_btime = 0;
    146 			qup->dqblk.dqb_itime = 0;
    147 		}
    148 		while (argc-- > 0) {
    149 			if ((id = getentry(*argv++, quotatype)) < 0)
    150 				continue;
    151 			putprivs(id, quotatype, protoprivs);
    152 		}
    153 		exit(0);
    154 	}
    155 	tmpfd = mkstemp(tmpfil);
    156 	fchown(tmpfd, getuid(), getgid());
    157 	if (tflag) {
    158 		protoprivs = getprivs(0, quotatype);
    159 		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
    160 			exit(1);
    161 		if (editit(tmpfil) && readtimes(protoprivs, tmpfd))
    162 			putprivs(0, quotatype, protoprivs);
    163 		freeprivs(protoprivs);
    164 		exit(0);
    165 	}
    166 	for ( ; argc > 0; argc--, argv++) {
    167 		if ((id = getentry(*argv, quotatype)) == -1)
    168 			continue;
    169 		curprivs = getprivs(id, quotatype);
    170 		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
    171 			continue;
    172 		if (editit(tmpfil) && readprivs(curprivs, tmpfd))
    173 			putprivs(id, quotatype, curprivs);
    174 		freeprivs(curprivs);
    175 	}
    176 	close(tmpfd);
    177 	unlink(tmpfil);
    178 	exit(0);
    179 }
    180 
    181 void
    182 usage()
    183 {
    184 	fprintf(stderr, "%s%s%s%s",
    185 		"Usage: edquota [-u] [-p username] username ...\n",
    186 		"\tedquota -g [-p groupname] groupname ...\n",
    187 		"\tedquota [-u] -t\n", "\tedquota -g -t\n");
    188 	exit(1);
    189 }
    190 
    191 /*
    192  * This routine converts a name for a particular quota type to
    193  * an identifier. This routine must agree with the kernel routine
    194  * getinoquota as to the interpretation of quota types.
    195  */
    196 int
    197 getentry(name, quotatype)
    198 	char *name;
    199 	int quotatype;
    200 {
    201 	struct passwd *pw;
    202 	struct group *gr;
    203 
    204 	if (alldigits(name))
    205 		return (atoi(name));
    206 	switch(quotatype) {
    207 	case USRQUOTA:
    208 		if (pw = getpwnam(name))
    209 			return (pw->pw_uid);
    210 		fprintf(stderr, "%s: no such user\n", name);
    211 		break;
    212 	case GRPQUOTA:
    213 		if (gr = getgrnam(name))
    214 			return (gr->gr_gid);
    215 		fprintf(stderr, "%s: no such group\n", name);
    216 		break;
    217 	default:
    218 		fprintf(stderr, "%d: unknown quota type\n", quotatype);
    219 		break;
    220 	}
    221 	sleep(1);
    222 	return (-1);
    223 }
    224 
    225 /*
    226  * Collect the requested quota information.
    227  */
    228 struct quotause *
    229 getprivs(id, quotatype)
    230 	register long id;
    231 	int quotatype;
    232 {
    233 	register struct fstab *fs;
    234 	register struct quotause *qup, *quptail;
    235 	struct quotause *quphead;
    236 	int qcmd, qupsize, fd;
    237 	char *qfpathname;
    238 	static int warned = 0;
    239 	extern int errno;
    240 
    241 	setfsent();
    242 	quphead = (struct quotause *)0;
    243 	qcmd = QCMD(Q_GETQUOTA, quotatype);
    244 	while (fs = getfsent()) {
    245 		if (strcmp(fs->fs_vfstype, "ffs"))
    246 			continue;
    247 		if (!hasquota(fs, quotatype, &qfpathname))
    248 			continue;
    249 		qupsize = sizeof(*qup) + strlen(qfpathname);
    250 		if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
    251 			fprintf(stderr, "edquota: out of memory\n");
    252 			exit(2);
    253 		}
    254 		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
    255 	    		if (errno == EOPNOTSUPP && !warned) {
    256 				warned++;
    257 				fprintf(stderr, "Warning: %s\n",
    258 				    "Quotas are not compiled into this kernel");
    259 				sleep(3);
    260 			}
    261 			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    262 				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
    263 				if (fd < 0 && errno != ENOENT) {
    264 					perror(qfpathname);
    265 					free(qup);
    266 					continue;
    267 				}
    268 				fprintf(stderr, "Creating quota file %s\n",
    269 				    qfpathname);
    270 				sleep(3);
    271 				(void) fchown(fd, getuid(),
    272 				    getentry(quotagroup, GRPQUOTA));
    273 				(void) fchmod(fd, 0640);
    274 			}
    275 			(void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
    276 			    SEEK_SET);
    277 			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
    278 			case 0:			/* EOF */
    279 				/*
    280 				 * Convert implicit 0 quota (EOF)
    281 				 * into an explicit one (zero'ed dqblk)
    282 				 */
    283 				bzero((caddr_t)&qup->dqblk,
    284 				    sizeof(struct dqblk));
    285 				break;
    286 
    287 			case sizeof(struct dqblk):	/* OK */
    288 				break;
    289 
    290 			default:		/* ERROR */
    291 				fprintf(stderr, "edquota: read error in ");
    292 				perror(qfpathname);
    293 				close(fd);
    294 				free(qup);
    295 				continue;
    296 			}
    297 			close(fd);
    298 		}
    299 		strcpy(qup->qfname, qfpathname);
    300 		strcpy(qup->fsname, fs->fs_file);
    301 		if (quphead == NULL)
    302 			quphead = qup;
    303 		else
    304 			quptail->next = qup;
    305 		quptail = qup;
    306 		qup->next = 0;
    307 	}
    308 	endfsent();
    309 	return (quphead);
    310 }
    311 
    312 /*
    313  * Store the requested quota information.
    314  */
    315 void
    316 putprivs(id, quotatype, quplist)
    317 	long id;
    318 	int quotatype;
    319 	struct quotause *quplist;
    320 {
    321 	register struct quotause *qup;
    322 	int qcmd, fd;
    323 
    324 	qcmd = QCMD(Q_SETQUOTA, quotatype);
    325 	for (qup = quplist; qup; qup = qup->next) {
    326 		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
    327 			continue;
    328 		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
    329 			perror(qup->qfname);
    330 		} else {
    331 			(void)lseek(fd,
    332 			    (off_t)(id * (long)sizeof (struct dqblk)),
    333 			    SEEK_SET);
    334 			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
    335 			    sizeof (struct dqblk)) {
    336 				fprintf(stderr, "edquota: ");
    337 				perror(qup->qfname);
    338 			}
    339 			close(fd);
    340 		}
    341 	}
    342 }
    343 
    344 /*
    345  * Take a list of priviledges and get it edited.
    346  */
    347 int
    348 editit(tmpfile)
    349 	char *tmpfile;
    350 {
    351 	long omask;
    352 	int pid, stat;
    353 	extern char *getenv();
    354 
    355 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
    356  top:
    357 	if ((pid = fork()) < 0) {
    358 		extern errno;
    359 
    360 		if (errno == EPROCLIM) {
    361 			fprintf(stderr, "You have too many processes\n");
    362 			return(0);
    363 		}
    364 		if (errno == EAGAIN) {
    365 			sleep(1);
    366 			goto top;
    367 		}
    368 		perror("fork");
    369 		return (0);
    370 	}
    371 	if (pid == 0) {
    372 		register char *ed;
    373 
    374 		sigsetmask(omask);
    375 		setgid(getgid());
    376 		setuid(getuid());
    377 		if ((ed = getenv("EDITOR")) == (char *)0)
    378 			ed = _PATH_VI;
    379 		execlp(ed, ed, tmpfile, 0);
    380 		perror(ed);
    381 		exit(1);
    382 	}
    383 	waitpid(pid, &stat, 0);
    384 	sigsetmask(omask);
    385 	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
    386 		return (0);
    387 	return (1);
    388 }
    389 
    390 /*
    391  * Convert a quotause list to an ASCII file.
    392  */
    393 int
    394 writeprivs(quplist, outfd, name, quotatype)
    395 	struct quotause *quplist;
    396 	int outfd;
    397 	char *name;
    398 	int quotatype;
    399 {
    400 	register struct quotause *qup;
    401 	FILE *fd;
    402 
    403 	ftruncate(outfd, 0);
    404 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    405 	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
    406 		fprintf(stderr, "edquota: ");
    407 		perror(tmpfil);
    408 		exit(1);
    409 	}
    410 	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
    411 	for (qup = quplist; qup; qup = qup->next) {
    412 		fprintf(fd, "%s: %s %d, limits (soft = %d, hard = %d)\n",
    413 		    qup->fsname, "blocks in use:",
    414 		    dbtob(qup->dqblk.dqb_curblocks) / 1024,
    415 		    dbtob(qup->dqblk.dqb_bsoftlimit) / 1024,
    416 		    dbtob(qup->dqblk.dqb_bhardlimit) / 1024);
    417 		fprintf(fd, "%s %d, limits (soft = %d, hard = %d)\n",
    418 		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
    419 		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
    420 	}
    421 	fclose(fd);
    422 	return (1);
    423 }
    424 
    425 /*
    426  * Merge changes to an ASCII file into a quotause list.
    427  */
    428 int
    429 readprivs(quplist, infd)
    430 	struct quotause *quplist;
    431 	int infd;
    432 {
    433 	register struct quotause *qup;
    434 	FILE *fd;
    435 	int cnt;
    436 	register char *cp;
    437 	struct dqblk dqblk;
    438 	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
    439 
    440 	(void)lseek(infd, (off_t)0, SEEK_SET);
    441 	fd = fdopen(dup(infd), "r");
    442 	if (fd == NULL) {
    443 		fprintf(stderr, "Can't re-read temp file!!\n");
    444 		return (0);
    445 	}
    446 	/*
    447 	 * Discard title line, then read pairs of lines to process.
    448 	 */
    449 	(void) fgets(line1, sizeof (line1), fd);
    450 	while (fgets(line1, sizeof (line1), fd) != NULL &&
    451 	       fgets(line2, sizeof (line2), fd) != NULL) {
    452 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    453 			fprintf(stderr, "%s: bad format\n", line1);
    454 			return (0);
    455 		}
    456 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    457 			fprintf(stderr, "%s: %s: bad format\n", fsp,
    458 			    &fsp[strlen(fsp) + 1]);
    459 			return (0);
    460 		}
    461 		cnt = sscanf(cp,
    462 		    " blocks in use: %d, limits (soft = %d, hard = %d)",
    463 		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
    464 		    &dqblk.dqb_bhardlimit);
    465 		if (cnt != 3) {
    466 			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
    467 			return (0);
    468 		}
    469 		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
    470 		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
    471 		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
    472 		if ((cp = strtok(line2, "\n")) == NULL) {
    473 			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
    474 			return (0);
    475 		}
    476 		cnt = sscanf(cp,
    477 		    "\tinodes in use: %d, limits (soft = %d, hard = %d)",
    478 		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
    479 		    &dqblk.dqb_ihardlimit);
    480 		if (cnt != 3) {
    481 			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
    482 			return (0);
    483 		}
    484 		for (qup = quplist; qup; qup = qup->next) {
    485 			if (strcmp(fsp, qup->fsname))
    486 				continue;
    487 			/*
    488 			 * Cause time limit to be reset when the quota
    489 			 * is next used if previously had no soft limit
    490 			 * or were under it, but now have a soft limit
    491 			 * and are over it.
    492 			 */
    493 			if (dqblk.dqb_bsoftlimit &&
    494 			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
    495 			    (qup->dqblk.dqb_bsoftlimit == 0 ||
    496 			     qup->dqblk.dqb_curblocks <
    497 			     qup->dqblk.dqb_bsoftlimit))
    498 				qup->dqblk.dqb_btime = 0;
    499 			if (dqblk.dqb_isoftlimit &&
    500 			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
    501 			    (qup->dqblk.dqb_isoftlimit == 0 ||
    502 			     qup->dqblk.dqb_curinodes <
    503 			     qup->dqblk.dqb_isoftlimit))
    504 				qup->dqblk.dqb_itime = 0;
    505 			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
    506 			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
    507 			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
    508 			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
    509 			qup->flags |= FOUND;
    510 			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
    511 			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
    512 				break;
    513 			fprintf(stderr,
    514 			    "%s: cannot change current allocation\n", fsp);
    515 			break;
    516 		}
    517 	}
    518 	fclose(fd);
    519 	/*
    520 	 * Disable quotas for any filesystems that have not been found.
    521 	 */
    522 	for (qup = quplist; qup; qup = qup->next) {
    523 		if (qup->flags & FOUND) {
    524 			qup->flags &= ~FOUND;
    525 			continue;
    526 		}
    527 		qup->dqblk.dqb_bsoftlimit = 0;
    528 		qup->dqblk.dqb_bhardlimit = 0;
    529 		qup->dqblk.dqb_isoftlimit = 0;
    530 		qup->dqblk.dqb_ihardlimit = 0;
    531 	}
    532 	return (1);
    533 }
    534 
    535 /*
    536  * Convert a quotause list to an ASCII file of grace times.
    537  */
    538 int
    539 writetimes(quplist, outfd, quotatype)
    540 	struct quotause *quplist;
    541 	int outfd;
    542 	int quotatype;
    543 {
    544 	register struct quotause *qup;
    545 	char *cvtstoa();
    546 	FILE *fd;
    547 
    548 	ftruncate(outfd, 0);
    549 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    550 	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
    551 		fprintf(stderr, "edquota: ");
    552 		perror(tmpfil);
    553 		exit(1);
    554 	}
    555 	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
    556 	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
    557 	    qfextension[quotatype]);
    558 	for (qup = quplist; qup; qup = qup->next) {
    559 		fprintf(fd, "%s: block grace period: %s, ",
    560 		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
    561 		fprintf(fd, "file grace period: %s\n",
    562 		    cvtstoa(qup->dqblk.dqb_itime));
    563 	}
    564 	fclose(fd);
    565 	return (1);
    566 }
    567 
    568 /*
    569  * Merge changes of grace times in an ASCII file into a quotause list.
    570  */
    571 int
    572 readtimes(quplist, infd)
    573 	struct quotause *quplist;
    574 	int infd;
    575 {
    576 	register struct quotause *qup;
    577 	FILE *fd;
    578 	int cnt;
    579 	register char *cp;
    580 	time_t itime, btime, iseconds, bseconds;
    581 	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
    582 
    583 	(void)lseek(infd, (off_t)0, SEEK_SET);
    584 	fd = fdopen(dup(infd), "r");
    585 	if (fd == NULL) {
    586 		fprintf(stderr, "Can't re-read temp file!!\n");
    587 		return (0);
    588 	}
    589 	/*
    590 	 * Discard two title lines, then read lines to process.
    591 	 */
    592 	(void) fgets(line1, sizeof (line1), fd);
    593 	(void) fgets(line1, sizeof (line1), fd);
    594 	while (fgets(line1, sizeof (line1), fd) != NULL) {
    595 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    596 			fprintf(stderr, "%s: bad format\n", line1);
    597 			return (0);
    598 		}
    599 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    600 			fprintf(stderr, "%s: %s: bad format\n", fsp,
    601 			    &fsp[strlen(fsp) + 1]);
    602 			return (0);
    603 		}
    604 		cnt = sscanf(cp,
    605 		    " block grace period: %d %s file grace period: %d %s",
    606 		    &btime, bunits, &itime, iunits);
    607 		if (cnt != 4) {
    608 			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
    609 			return (0);
    610 		}
    611 		if (cvtatos(btime, bunits, &bseconds) == 0)
    612 			return (0);
    613 		if (cvtatos(itime, iunits, &iseconds) == 0)
    614 			return (0);
    615 		for (qup = quplist; qup; qup = qup->next) {
    616 			if (strcmp(fsp, qup->fsname))
    617 				continue;
    618 			qup->dqblk.dqb_btime = bseconds;
    619 			qup->dqblk.dqb_itime = iseconds;
    620 			qup->flags |= FOUND;
    621 			break;
    622 		}
    623 	}
    624 	fclose(fd);
    625 	/*
    626 	 * reset default grace periods for any filesystems
    627 	 * that have not been found.
    628 	 */
    629 	for (qup = quplist; qup; qup = qup->next) {
    630 		if (qup->flags & FOUND) {
    631 			qup->flags &= ~FOUND;
    632 			continue;
    633 		}
    634 		qup->dqblk.dqb_btime = 0;
    635 		qup->dqblk.dqb_itime = 0;
    636 	}
    637 	return (1);
    638 }
    639 
    640 /*
    641  * Convert seconds to ASCII times.
    642  */
    643 char *
    644 cvtstoa(time)
    645 	time_t time;
    646 {
    647 	static char buf[20];
    648 
    649 	if (time % (24 * 60 * 60) == 0) {
    650 		time /= 24 * 60 * 60;
    651 		sprintf(buf, "%d day%s", time, time == 1 ? "" : "s");
    652 	} else if (time % (60 * 60) == 0) {
    653 		time /= 60 * 60;
    654 		sprintf(buf, "%d hour%s", time, time == 1 ? "" : "s");
    655 	} else if (time % 60 == 0) {
    656 		time /= 60;
    657 		sprintf(buf, "%d minute%s", time, time == 1 ? "" : "s");
    658 	} else
    659 		sprintf(buf, "%d second%s", time, time == 1 ? "" : "s");
    660 	return (buf);
    661 }
    662 
    663 /*
    664  * Convert ASCII input times to seconds.
    665  */
    666 int
    667 cvtatos(time, units, seconds)
    668 	time_t time;
    669 	char *units;
    670 	time_t *seconds;
    671 {
    672 
    673 	if (bcmp(units, "second", 6) == 0)
    674 		*seconds = time;
    675 	else if (bcmp(units, "minute", 6) == 0)
    676 		*seconds = time * 60;
    677 	else if (bcmp(units, "hour", 4) == 0)
    678 		*seconds = time * 60 * 60;
    679 	else if (bcmp(units, "day", 3) == 0)
    680 		*seconds = time * 24 * 60 * 60;
    681 	else {
    682 		printf("%s: bad units, specify %s\n", units,
    683 		    "days, hours, minutes, or seconds");
    684 		return (0);
    685 	}
    686 	return (1);
    687 }
    688 
    689 /*
    690  * Free a list of quotause structures.
    691  */
    692 void
    693 freeprivs(quplist)
    694 	struct quotause *quplist;
    695 {
    696 	register struct quotause *qup, *nextqup;
    697 
    698 	for (qup = quplist; qup; qup = nextqup) {
    699 		nextqup = qup->next;
    700 		free(qup);
    701 	}
    702 }
    703 
    704 /*
    705  * Check whether a string is completely composed of digits.
    706  */
    707 int
    708 alldigits(s)
    709 	register char *s;
    710 {
    711 	register c;
    712 
    713 	c = *s++;
    714 	do {
    715 		if (!isdigit(c))
    716 			return (0);
    717 	} while (c = *s++);
    718 	return (1);
    719 }
    720 
    721 /*
    722  * Check to see if a particular quota is to be enabled.
    723  */
    724 int
    725 hasquota(fs, type, qfnamep)
    726 	register struct fstab *fs;
    727 	int type;
    728 	char **qfnamep;
    729 {
    730 	register char *opt;
    731 	char *cp, *index(), *strtok();
    732 	static char initname, usrname[100], grpname[100];
    733 	static char buf[BUFSIZ];
    734 
    735 	if (!initname) {
    736 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
    737 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
    738 		initname = 1;
    739 	}
    740 	strcpy(buf, fs->fs_mntops);
    741 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    742 		if (cp = index(opt, '='))
    743 			*cp++ = '\0';
    744 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    745 			break;
    746 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    747 			break;
    748 	}
    749 	if (!opt)
    750 		return (0);
    751 	if (cp) {
    752 		*qfnamep = cp;
    753 		return (1);
    754 	}
    755 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    756 	*qfnamep = buf;
    757 	return (1);
    758 }
    759