Home | History | Annotate | Line # | Download | only in edquota
edquota.c revision 1.12
      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.12 1997/03/08 08:01:27 mikel 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 int
     83 main(argc, argv)
     84 	register char **argv;
     85 	int argc;
     86 {
     87 	register struct quotause *qup, *protoprivs, *curprivs;
     88 	extern char *optarg;
     89 	extern int optind;
     90 	register long id, protoid;
     91 	register int quotatype, tmpfd;
     92 	char *protoname;
     93 	int ch;
     94 	int tflag = 0, pflag = 0;
     95 
     96 	if (argc < 2)
     97 		usage();
     98 	if (getuid()) {
     99 		fprintf(stderr, "edquota: permission denied\n");
    100 		exit(1);
    101 	}
    102 	quotatype = USRQUOTA;
    103 	while ((ch = getopt(argc, argv, "ugtp:")) != -1) {
    104 		switch(ch) {
    105 		case 'p':
    106 			protoname = optarg;
    107 			pflag++;
    108 			break;
    109 		case 'g':
    110 			quotatype = GRPQUOTA;
    111 			break;
    112 		case 'u':
    113 			quotatype = USRQUOTA;
    114 			break;
    115 		case 't':
    116 			tflag++;
    117 			break;
    118 		default:
    119 			usage();
    120 		}
    121 	}
    122 	argc -= optind;
    123 	argv += optind;
    124 	if (pflag) {
    125 		if ((protoid = getentry(protoname, quotatype)) == -1)
    126 			exit(1);
    127 		protoprivs = getprivs(protoid, quotatype);
    128 		for (qup = protoprivs; qup; qup = qup->next) {
    129 			qup->dqblk.dqb_btime = 0;
    130 			qup->dqblk.dqb_itime = 0;
    131 		}
    132 		while (argc-- > 0) {
    133 			if ((id = getentry(*argv++, quotatype)) < 0)
    134 				continue;
    135 			putprivs(id, quotatype, protoprivs);
    136 		}
    137 		exit(0);
    138 	}
    139 	tmpfd = mkstemp(tmpfil);
    140 	fchown(tmpfd, getuid(), getgid());
    141 	if (tflag) {
    142 		protoprivs = getprivs(0, quotatype);
    143 		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
    144 			exit(1);
    145 		if (editit(tmpfil) && readtimes(protoprivs, tmpfd))
    146 			putprivs(0, quotatype, protoprivs);
    147 		freeprivs(protoprivs);
    148 		exit(0);
    149 	}
    150 	for ( ; argc > 0; argc--, argv++) {
    151 		if ((id = getentry(*argv, quotatype)) == -1)
    152 			continue;
    153 		curprivs = getprivs(id, quotatype);
    154 		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
    155 			continue;
    156 		if (editit(tmpfil) && readprivs(curprivs, tmpfd))
    157 			putprivs(id, quotatype, curprivs);
    158 		freeprivs(curprivs);
    159 	}
    160 	close(tmpfd);
    161 	unlink(tmpfil);
    162 	exit(0);
    163 }
    164 
    165 void
    166 usage()
    167 {
    168 	fprintf(stderr, "%s%s%s%s",
    169 		"Usage: edquota [-u] [-p username] username ...\n",
    170 		"\tedquota -g [-p groupname] groupname ...\n",
    171 		"\tedquota [-u] -t\n", "\tedquota -g -t\n");
    172 	exit(1);
    173 }
    174 
    175 /*
    176  * This routine converts a name for a particular quota type to
    177  * an identifier. This routine must agree with the kernel routine
    178  * getinoquota as to the interpretation of quota types.
    179  */
    180 int
    181 getentry(name, quotatype)
    182 	char *name;
    183 	int quotatype;
    184 {
    185 	struct passwd *pw;
    186 	struct group *gr;
    187 
    188 	if (alldigits(name))
    189 		return (atoi(name));
    190 	switch(quotatype) {
    191 	case USRQUOTA:
    192 		if (pw = getpwnam(name))
    193 			return (pw->pw_uid);
    194 		fprintf(stderr, "%s: no such user\n", name);
    195 		break;
    196 	case GRPQUOTA:
    197 		if (gr = getgrnam(name))
    198 			return (gr->gr_gid);
    199 		fprintf(stderr, "%s: no such group\n", name);
    200 		break;
    201 	default:
    202 		fprintf(stderr, "%d: unknown quota type\n", quotatype);
    203 		break;
    204 	}
    205 	sleep(1);
    206 	return (-1);
    207 }
    208 
    209 /*
    210  * Collect the requested quota information.
    211  */
    212 struct quotause *
    213 getprivs(id, quotatype)
    214 	register long id;
    215 	int quotatype;
    216 {
    217 	register struct fstab *fs;
    218 	register struct quotause *qup, *quptail;
    219 	struct quotause *quphead;
    220 	int qcmd, qupsize, fd;
    221 	char *qfpathname;
    222 	static int warned = 0;
    223 	extern int errno;
    224 
    225 	setfsent();
    226 	quphead = (struct quotause *)0;
    227 	qcmd = QCMD(Q_GETQUOTA, quotatype);
    228 	while (fs = getfsent()) {
    229 		if (strcmp(fs->fs_vfstype, "ffs"))
    230 			continue;
    231 		if (!hasquota(fs, quotatype, &qfpathname))
    232 			continue;
    233 		qupsize = sizeof(*qup) + strlen(qfpathname);
    234 		if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
    235 			fprintf(stderr, "edquota: out of memory\n");
    236 			exit(2);
    237 		}
    238 		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
    239 	    		if (errno == EOPNOTSUPP && !warned) {
    240 				warned++;
    241 				fprintf(stderr, "Warning: %s\n",
    242 				    "Quotas are not compiled into this kernel");
    243 				sleep(3);
    244 			}
    245 			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    246 				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
    247 				if (fd < 0 && errno != ENOENT) {
    248 					perror(qfpathname);
    249 					free(qup);
    250 					continue;
    251 				}
    252 				fprintf(stderr, "Creating quota file %s\n",
    253 				    qfpathname);
    254 				sleep(3);
    255 				(void) fchown(fd, getuid(),
    256 				    getentry(quotagroup, GRPQUOTA));
    257 				(void) fchmod(fd, 0640);
    258 			}
    259 			lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
    260 			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
    261 			case 0:			/* EOF */
    262 				/*
    263 				 * Convert implicit 0 quota (EOF)
    264 				 * into an explicit one (zero'ed dqblk)
    265 				 */
    266 				bzero((caddr_t)&qup->dqblk,
    267 				    sizeof(struct dqblk));
    268 				break;
    269 
    270 			case sizeof(struct dqblk):	/* OK */
    271 				break;
    272 
    273 			default:		/* ERROR */
    274 				fprintf(stderr, "edquota: read error in ");
    275 				perror(qfpathname);
    276 				close(fd);
    277 				free(qup);
    278 				continue;
    279 			}
    280 			close(fd);
    281 		}
    282 		strcpy(qup->qfname, qfpathname);
    283 		strcpy(qup->fsname, fs->fs_file);
    284 		if (quphead == NULL)
    285 			quphead = qup;
    286 		else
    287 			quptail->next = qup;
    288 		quptail = qup;
    289 		qup->next = 0;
    290 	}
    291 	endfsent();
    292 	return (quphead);
    293 }
    294 
    295 /*
    296  * Store the requested quota information.
    297  */
    298 void
    299 putprivs(id, quotatype, quplist)
    300 	long id;
    301 	int quotatype;
    302 	struct quotause *quplist;
    303 {
    304 	register struct quotause *qup;
    305 	int qcmd, fd;
    306 
    307 	qcmd = QCMD(Q_SETQUOTA, quotatype);
    308 	for (qup = quplist; qup; qup = qup->next) {
    309 		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
    310 			continue;
    311 		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
    312 			perror(qup->qfname);
    313 		} else {
    314 			lseek(fd,
    315 			    (off_t)(id * (long)sizeof (struct dqblk)), L_SET);
    316 			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
    317 			    sizeof (struct dqblk)) {
    318 				fprintf(stderr, "edquota: ");
    319 				perror(qup->qfname);
    320 			}
    321 			close(fd);
    322 		}
    323 	}
    324 }
    325 
    326 /*
    327  * Take a list of priviledges and get it edited.
    328  */
    329 int
    330 editit(tmpfile)
    331 	char *tmpfile;
    332 {
    333 	long omask;
    334 	int pid, stat;
    335 	extern char *getenv();
    336 
    337 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
    338  top:
    339 	if ((pid = fork()) < 0) {
    340 		extern errno;
    341 
    342 		if (errno == EPROCLIM) {
    343 			fprintf(stderr, "You have too many processes\n");
    344 			return(0);
    345 		}
    346 		if (errno == EAGAIN) {
    347 			sleep(1);
    348 			goto top;
    349 		}
    350 		perror("fork");
    351 		return (0);
    352 	}
    353 	if (pid == 0) {
    354 		register char *ed;
    355 
    356 		sigsetmask(omask);
    357 		setgid(getgid());
    358 		setuid(getuid());
    359 		if ((ed = getenv("EDITOR")) == (char *)0)
    360 			ed = _PATH_VI;
    361 		execlp(ed, ed, tmpfile, 0);
    362 		perror(ed);
    363 		exit(1);
    364 	}
    365 	waitpid(pid, &stat, 0);
    366 	sigsetmask(omask);
    367 	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
    368 		return (0);
    369 	return (1);
    370 }
    371 
    372 /*
    373  * Convert a quotause list to an ASCII file.
    374  */
    375 int
    376 writeprivs(quplist, outfd, name, quotatype)
    377 	struct quotause *quplist;
    378 	int outfd;
    379 	char *name;
    380 	int quotatype;
    381 {
    382 	register struct quotause *qup;
    383 	FILE *fd;
    384 
    385 	ftruncate(outfd, 0);
    386 	lseek(outfd, 0, L_SET);
    387 	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
    388 		fprintf(stderr, "edquota: ");
    389 		perror(tmpfil);
    390 		exit(1);
    391 	}
    392 	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
    393 	for (qup = quplist; qup; qup = qup->next) {
    394 		fprintf(fd, "%s: %s %d, limits (soft = %d, hard = %d)\n",
    395 		    qup->fsname, "blocks in use:",
    396 		    dbtob(qup->dqblk.dqb_curblocks) / 1024,
    397 		    dbtob(qup->dqblk.dqb_bsoftlimit) / 1024,
    398 		    dbtob(qup->dqblk.dqb_bhardlimit) / 1024);
    399 		fprintf(fd, "%s %d, limits (soft = %d, hard = %d)\n",
    400 		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
    401 		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
    402 	}
    403 	fclose(fd);
    404 	return (1);
    405 }
    406 
    407 /*
    408  * Merge changes to an ASCII file into a quotause list.
    409  */
    410 int
    411 readprivs(quplist, infd)
    412 	struct quotause *quplist;
    413 	int infd;
    414 {
    415 	register struct quotause *qup;
    416 	FILE *fd;
    417 	int cnt;
    418 	register char *cp;
    419 	struct dqblk dqblk;
    420 	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
    421 
    422 	lseek(infd, 0, L_SET);
    423 	fd = fdopen(dup(infd), "r");
    424 	if (fd == NULL) {
    425 		fprintf(stderr, "Can't re-read temp file!!\n");
    426 		return (0);
    427 	}
    428 	/*
    429 	 * Discard title line, then read pairs of lines to process.
    430 	 */
    431 	(void) fgets(line1, sizeof (line1), fd);
    432 	while (fgets(line1, sizeof (line1), fd) != NULL &&
    433 	       fgets(line2, sizeof (line2), fd) != NULL) {
    434 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    435 			fprintf(stderr, "%s: bad format\n", line1);
    436 			return (0);
    437 		}
    438 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    439 			fprintf(stderr, "%s: %s: bad format\n", fsp,
    440 			    &fsp[strlen(fsp) + 1]);
    441 			return (0);
    442 		}
    443 		cnt = sscanf(cp,
    444 		    " blocks in use: %d, limits (soft = %d, hard = %d)",
    445 		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
    446 		    &dqblk.dqb_bhardlimit);
    447 		if (cnt != 3) {
    448 			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
    449 			return (0);
    450 		}
    451 		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
    452 		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
    453 		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
    454 		if ((cp = strtok(line2, "\n")) == NULL) {
    455 			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
    456 			return (0);
    457 		}
    458 		cnt = sscanf(cp,
    459 		    "\tinodes in use: %d, limits (soft = %d, hard = %d)",
    460 		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
    461 		    &dqblk.dqb_ihardlimit);
    462 		if (cnt != 3) {
    463 			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
    464 			return (0);
    465 		}
    466 		for (qup = quplist; qup; qup = qup->next) {
    467 			if (strcmp(fsp, qup->fsname))
    468 				continue;
    469 			/*
    470 			 * Cause time limit to be reset when the quota
    471 			 * is next used if previously had no soft limit
    472 			 * or were under it, but now have a soft limit
    473 			 * and are over it.
    474 			 */
    475 			if (dqblk.dqb_bsoftlimit &&
    476 			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
    477 			    (qup->dqblk.dqb_bsoftlimit == 0 ||
    478 			     qup->dqblk.dqb_curblocks <
    479 			     qup->dqblk.dqb_bsoftlimit))
    480 				qup->dqblk.dqb_btime = 0;
    481 			if (dqblk.dqb_isoftlimit &&
    482 			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
    483 			    (qup->dqblk.dqb_isoftlimit == 0 ||
    484 			     qup->dqblk.dqb_curinodes <
    485 			     qup->dqblk.dqb_isoftlimit))
    486 				qup->dqblk.dqb_itime = 0;
    487 			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
    488 			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
    489 			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
    490 			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
    491 			qup->flags |= FOUND;
    492 			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
    493 			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
    494 				break;
    495 			fprintf(stderr,
    496 			    "%s: cannot change current allocation\n", fsp);
    497 			break;
    498 		}
    499 	}
    500 	fclose(fd);
    501 	/*
    502 	 * Disable quotas for any filesystems that have not been found.
    503 	 */
    504 	for (qup = quplist; qup; qup = qup->next) {
    505 		if (qup->flags & FOUND) {
    506 			qup->flags &= ~FOUND;
    507 			continue;
    508 		}
    509 		qup->dqblk.dqb_bsoftlimit = 0;
    510 		qup->dqblk.dqb_bhardlimit = 0;
    511 		qup->dqblk.dqb_isoftlimit = 0;
    512 		qup->dqblk.dqb_ihardlimit = 0;
    513 	}
    514 	return (1);
    515 }
    516 
    517 /*
    518  * Convert a quotause list to an ASCII file of grace times.
    519  */
    520 int
    521 writetimes(quplist, outfd, quotatype)
    522 	struct quotause *quplist;
    523 	int outfd;
    524 	int quotatype;
    525 {
    526 	register struct quotause *qup;
    527 	char *cvtstoa();
    528 	FILE *fd;
    529 
    530 	ftruncate(outfd, 0);
    531 	lseek(outfd, 0, L_SET);
    532 	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
    533 		fprintf(stderr, "edquota: ");
    534 		perror(tmpfil);
    535 		exit(1);
    536 	}
    537 	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
    538 	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
    539 	    qfextension[quotatype]);
    540 	for (qup = quplist; qup; qup = qup->next) {
    541 		fprintf(fd, "%s: block grace period: %s, ",
    542 		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
    543 		fprintf(fd, "file grace period: %s\n",
    544 		    cvtstoa(qup->dqblk.dqb_itime));
    545 	}
    546 	fclose(fd);
    547 	return (1);
    548 }
    549 
    550 /*
    551  * Merge changes of grace times in an ASCII file into a quotause list.
    552  */
    553 int
    554 readtimes(quplist, infd)
    555 	struct quotause *quplist;
    556 	int infd;
    557 {
    558 	register struct quotause *qup;
    559 	FILE *fd;
    560 	int cnt;
    561 	register char *cp;
    562 	time_t itime, btime, iseconds, bseconds;
    563 	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
    564 
    565 	lseek(infd, 0, L_SET);
    566 	fd = fdopen(dup(infd), "r");
    567 	if (fd == NULL) {
    568 		fprintf(stderr, "Can't re-read temp file!!\n");
    569 		return (0);
    570 	}
    571 	/*
    572 	 * Discard two title lines, then read lines to process.
    573 	 */
    574 	(void) fgets(line1, sizeof (line1), fd);
    575 	(void) fgets(line1, sizeof (line1), fd);
    576 	while (fgets(line1, sizeof (line1), fd) != NULL) {
    577 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    578 			fprintf(stderr, "%s: bad format\n", line1);
    579 			return (0);
    580 		}
    581 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    582 			fprintf(stderr, "%s: %s: bad format\n", fsp,
    583 			    &fsp[strlen(fsp) + 1]);
    584 			return (0);
    585 		}
    586 		cnt = sscanf(cp,
    587 		    " block grace period: %d %s file grace period: %d %s",
    588 		    &btime, bunits, &itime, iunits);
    589 		if (cnt != 4) {
    590 			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
    591 			return (0);
    592 		}
    593 		if (cvtatos(btime, bunits, &bseconds) == 0)
    594 			return (0);
    595 		if (cvtatos(itime, iunits, &iseconds) == 0)
    596 			return (0);
    597 		for (qup = quplist; qup; qup = qup->next) {
    598 			if (strcmp(fsp, qup->fsname))
    599 				continue;
    600 			qup->dqblk.dqb_btime = bseconds;
    601 			qup->dqblk.dqb_itime = iseconds;
    602 			qup->flags |= FOUND;
    603 			break;
    604 		}
    605 	}
    606 	fclose(fd);
    607 	/*
    608 	 * reset default grace periods for any filesystems
    609 	 * that have not been found.
    610 	 */
    611 	for (qup = quplist; qup; qup = qup->next) {
    612 		if (qup->flags & FOUND) {
    613 			qup->flags &= ~FOUND;
    614 			continue;
    615 		}
    616 		qup->dqblk.dqb_btime = 0;
    617 		qup->dqblk.dqb_itime = 0;
    618 	}
    619 	return (1);
    620 }
    621 
    622 /*
    623  * Convert seconds to ASCII times.
    624  */
    625 char *
    626 cvtstoa(time)
    627 	time_t time;
    628 {
    629 	static char buf[20];
    630 
    631 	if (time % (24 * 60 * 60) == 0) {
    632 		time /= 24 * 60 * 60;
    633 		sprintf(buf, "%d day%s", time, time == 1 ? "" : "s");
    634 	} else if (time % (60 * 60) == 0) {
    635 		time /= 60 * 60;
    636 		sprintf(buf, "%d hour%s", time, time == 1 ? "" : "s");
    637 	} else if (time % 60 == 0) {
    638 		time /= 60;
    639 		sprintf(buf, "%d minute%s", time, time == 1 ? "" : "s");
    640 	} else
    641 		sprintf(buf, "%d second%s", time, time == 1 ? "" : "s");
    642 	return (buf);
    643 }
    644 
    645 /*
    646  * Convert ASCII input times to seconds.
    647  */
    648 int
    649 cvtatos(time, units, seconds)
    650 	time_t time;
    651 	char *units;
    652 	time_t *seconds;
    653 {
    654 
    655 	if (bcmp(units, "second", 6) == 0)
    656 		*seconds = time;
    657 	else if (bcmp(units, "minute", 6) == 0)
    658 		*seconds = time * 60;
    659 	else if (bcmp(units, "hour", 4) == 0)
    660 		*seconds = time * 60 * 60;
    661 	else if (bcmp(units, "day", 3) == 0)
    662 		*seconds = time * 24 * 60 * 60;
    663 	else {
    664 		printf("%s: bad units, specify %s\n", units,
    665 		    "days, hours, minutes, or seconds");
    666 		return (0);
    667 	}
    668 	return (1);
    669 }
    670 
    671 /*
    672  * Free a list of quotause structures.
    673  */
    674 void
    675 freeprivs(quplist)
    676 	struct quotause *quplist;
    677 {
    678 	register struct quotause *qup, *nextqup;
    679 
    680 	for (qup = quplist; qup; qup = nextqup) {
    681 		nextqup = qup->next;
    682 		free(qup);
    683 	}
    684 }
    685 
    686 /*
    687  * Check whether a string is completely composed of digits.
    688  */
    689 int
    690 alldigits(s)
    691 	register char *s;
    692 {
    693 	register c;
    694 
    695 	c = *s++;
    696 	do {
    697 		if (!isdigit(c))
    698 			return (0);
    699 	} while (c = *s++);
    700 	return (1);
    701 }
    702 
    703 /*
    704  * Check to see if a particular quota is to be enabled.
    705  */
    706 int
    707 hasquota(fs, type, qfnamep)
    708 	register struct fstab *fs;
    709 	int type;
    710 	char **qfnamep;
    711 {
    712 	register char *opt;
    713 	char *cp, *index(), *strtok();
    714 	static char initname, usrname[100], grpname[100];
    715 	static char buf[BUFSIZ];
    716 
    717 	if (!initname) {
    718 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
    719 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
    720 		initname = 1;
    721 	}
    722 	strcpy(buf, fs->fs_mntops);
    723 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    724 		if (cp = index(opt, '='))
    725 			*cp++ = '\0';
    726 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    727 			break;
    728 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    729 			break;
    730 	}
    731 	if (!opt)
    732 		return (0);
    733 	if (cp) {
    734 		*qfnamep = cp;
    735 		return (1);
    736 	}
    737 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    738 	*qfnamep = buf;
    739 	return (1);
    740 }
    741