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