Home | History | Annotate | Line # | Download | only in edquota
edquota.c revision 1.18
      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.18 1999/08/16 03:12:31 simonb 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 	extern int errno;
    245 
    246 	setfsent();
    247 	quptail = NULL;
    248 	quphead = (struct quotause *)0;
    249 	qcmd = QCMD(Q_GETQUOTA, quotatype);
    250 	while ((fs = getfsent()) != NULL) {
    251 		if (strcmp(fs->fs_vfstype, "ffs"))
    252 			continue;
    253 		if (!hasquota(fs, quotatype, &qfpathname))
    254 			continue;
    255 		qupsize = sizeof(*qup) + strlen(qfpathname);
    256 		if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
    257 			errx(2, "out of memory");
    258 		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
    259 	    		if (errno == EOPNOTSUPP && !warned) {
    260 				warned++;
    261 				warnx(
    262 				    "Quotas are not compiled into this kernel");
    263 				sleep(3);
    264 			}
    265 			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    266 				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
    267 				if (fd < 0 && errno != ENOENT) {
    268 					warnx("open `%s'", qfpathname);
    269 					free(qup);
    270 					continue;
    271 				}
    272 				warnx("Creating quota file %s", qfpathname);
    273 				sleep(3);
    274 				(void) fchown(fd, getuid(),
    275 				    getentry(quotagroup, GRPQUOTA));
    276 				(void) fchmod(fd, 0640);
    277 			}
    278 			(void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
    279 			    SEEK_SET);
    280 			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
    281 			case 0:			/* EOF */
    282 				/*
    283 				 * Convert implicit 0 quota (EOF)
    284 				 * into an explicit one (zero'ed dqblk)
    285 				 */
    286 				memset((caddr_t)&qup->dqblk, 0,
    287 				    sizeof(struct dqblk));
    288 				break;
    289 
    290 			case sizeof(struct dqblk):	/* OK */
    291 				break;
    292 
    293 			default:		/* ERROR */
    294 				warn("read error in `%s'", qfpathname);
    295 				close(fd);
    296 				free(qup);
    297 				continue;
    298 			}
    299 			close(fd);
    300 		}
    301 		strcpy(qup->qfname, qfpathname);
    302 		strcpy(qup->fsname, fs->fs_file);
    303 		if (quphead == NULL)
    304 			quphead = qup;
    305 		else
    306 			quptail->next = qup;
    307 		quptail = qup;
    308 		qup->next = 0;
    309 	}
    310 	endfsent();
    311 	return (quphead);
    312 }
    313 
    314 /*
    315  * Store the requested quota information.
    316  */
    317 void
    318 putprivs(id, quotatype, quplist)
    319 	long id;
    320 	int quotatype;
    321 	struct quotause *quplist;
    322 {
    323 	struct quotause *qup;
    324 	int qcmd, fd;
    325 
    326 	qcmd = QCMD(Q_SETQUOTA, quotatype);
    327 	for (qup = quplist; qup; qup = qup->next) {
    328 		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
    329 			continue;
    330 		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
    331 			warnx("open `%s'", qup->qfname);
    332 		} else {
    333 			(void)lseek(fd,
    334 			    (off_t)(id * (long)sizeof (struct dqblk)),
    335 			    SEEK_SET);
    336 			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
    337 			    sizeof (struct dqblk))
    338 				warnx("writing `%s'", qup->qfname);
    339 			close(fd);
    340 		}
    341 	}
    342 }
    343 
    344 /*
    345  * Take a list of privileges and get it edited.
    346  */
    347 int
    348 editit(tmpfile)
    349 	char *tmpfile;
    350 {
    351 	long omask;
    352 	int pid, stat;
    353 
    354 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
    355  top:
    356 	if ((pid = fork()) < 0) {
    357 
    358 		if (errno == EPROCLIM) {
    359 			warnx("You have too many processes");
    360 			return(0);
    361 		}
    362 		if (errno == EAGAIN) {
    363 			sleep(1);
    364 			goto top;
    365 		}
    366 		warn("fork");
    367 		return (0);
    368 	}
    369 	if (pid == 0) {
    370 		char *ed;
    371 
    372 		sigsetmask(omask);
    373 		setgid(getgid());
    374 		setuid(getuid());
    375 		if ((ed = getenv("EDITOR")) == (char *)0)
    376 			ed = _PATH_VI;
    377 		execlp(ed, ed, tmpfile, 0);
    378 		err(1, "%s", ed);
    379 	}
    380 	waitpid(pid, &stat, 0);
    381 	sigsetmask(omask);
    382 	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
    383 		return (0);
    384 	return (1);
    385 }
    386 
    387 /*
    388  * Convert a quotause list to an ASCII file.
    389  */
    390 int
    391 writeprivs(quplist, outfd, name, quotatype)
    392 	struct quotause *quplist;
    393 	int outfd;
    394 	char *name;
    395 	int quotatype;
    396 {
    397 	struct quotause *qup;
    398 	FILE *fd;
    399 
    400 	ftruncate(outfd, 0);
    401 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    402 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    403 		errx(1, "fdopen `%s'", tmpfil);
    404 	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
    405 	for (qup = quplist; qup; qup = qup->next) {
    406 		fprintf(fd, "%s: %s %d, limits (soft = %d, hard = %d)\n",
    407 		    qup->fsname, "blocks in use:",
    408 		    dbtob(qup->dqblk.dqb_curblocks) / 1024,
    409 		    dbtob(qup->dqblk.dqb_bsoftlimit) / 1024,
    410 		    dbtob(qup->dqblk.dqb_bhardlimit) / 1024);
    411 		fprintf(fd, "%s %d, limits (soft = %d, hard = %d)\n",
    412 		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
    413 		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
    414 	}
    415 	fclose(fd);
    416 	return (1);
    417 }
    418 
    419 /*
    420  * Merge changes to an ASCII file into a quotause list.
    421  */
    422 int
    423 readprivs(quplist, infd)
    424 	struct quotause *quplist;
    425 	int infd;
    426 {
    427 	struct quotause *qup;
    428 	FILE *fd;
    429 	int cnt;
    430 	char *cp;
    431 	struct dqblk dqblk;
    432 	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
    433 
    434 	(void)lseek(infd, (off_t)0, SEEK_SET);
    435 	fd = fdopen(dup(infd), "r");
    436 	if (fd == NULL) {
    437 		warn("Can't re-read temp file");
    438 		return (0);
    439 	}
    440 	/*
    441 	 * Discard title line, then read pairs of lines to process.
    442 	 */
    443 	(void) fgets(line1, sizeof (line1), fd);
    444 	while (fgets(line1, sizeof (line1), fd) != NULL &&
    445 	       fgets(line2, sizeof (line2), fd) != NULL) {
    446 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    447 			warnx("%s: bad format", line1);
    448 			return (0);
    449 		}
    450 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    451 			warnx("%s: %s: bad format", fsp,
    452 			    &fsp[strlen(fsp) + 1]);
    453 			return (0);
    454 		}
    455 		cnt = sscanf(cp,
    456 		    " blocks in use: %d, limits (soft = %d, hard = %d)",
    457 		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
    458 		    &dqblk.dqb_bhardlimit);
    459 		if (cnt != 3) {
    460 			warnx("%s:%s: bad format", fsp, cp);
    461 			return (0);
    462 		}
    463 		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
    464 		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
    465 		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
    466 		if ((cp = strtok(line2, "\n")) == NULL) {
    467 			warnx("%s: %s: bad format", fsp, line2);
    468 			return (0);
    469 		}
    470 		cnt = sscanf(cp,
    471 		    "\tinodes in use: %d, limits (soft = %d, hard = %d)",
    472 		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
    473 		    &dqblk.dqb_ihardlimit);
    474 		if (cnt != 3) {
    475 			warnx("%s: %s: bad format", fsp, line2);
    476 			return (0);
    477 		}
    478 		for (qup = quplist; qup; qup = qup->next) {
    479 			if (strcmp(fsp, qup->fsname))
    480 				continue;
    481 			/*
    482 			 * Cause time limit to be reset when the quota
    483 			 * is next used if previously had no soft limit
    484 			 * or were under it, but now have a soft limit
    485 			 * and are over it.
    486 			 */
    487 			if (dqblk.dqb_bsoftlimit &&
    488 			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
    489 			    (qup->dqblk.dqb_bsoftlimit == 0 ||
    490 			     qup->dqblk.dqb_curblocks <
    491 			     qup->dqblk.dqb_bsoftlimit))
    492 				qup->dqblk.dqb_btime = 0;
    493 			if (dqblk.dqb_isoftlimit &&
    494 			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
    495 			    (qup->dqblk.dqb_isoftlimit == 0 ||
    496 			     qup->dqblk.dqb_curinodes <
    497 			     qup->dqblk.dqb_isoftlimit))
    498 				qup->dqblk.dqb_itime = 0;
    499 			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
    500 			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
    501 			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
    502 			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
    503 			qup->flags |= FOUND;
    504 			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
    505 			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
    506 				break;
    507 			warnx("%s: cannot change current allocation", fsp);
    508 			break;
    509 		}
    510 	}
    511 	fclose(fd);
    512 	/*
    513 	 * Disable quotas for any filesystems that have not been found.
    514 	 */
    515 	for (qup = quplist; qup; qup = qup->next) {
    516 		if (qup->flags & FOUND) {
    517 			qup->flags &= ~FOUND;
    518 			continue;
    519 		}
    520 		qup->dqblk.dqb_bsoftlimit = 0;
    521 		qup->dqblk.dqb_bhardlimit = 0;
    522 		qup->dqblk.dqb_isoftlimit = 0;
    523 		qup->dqblk.dqb_ihardlimit = 0;
    524 	}
    525 	return (1);
    526 }
    527 
    528 /*
    529  * Convert a quotause list to an ASCII file of grace times.
    530  */
    531 int
    532 writetimes(quplist, outfd, quotatype)
    533 	struct quotause *quplist;
    534 	int outfd;
    535 	int quotatype;
    536 {
    537 	struct quotause *qup;
    538 	FILE *fd;
    539 
    540 	ftruncate(outfd, 0);
    541 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    542 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    543 		err(1, "fdopen `%s'", tmpfil);
    544 	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
    545 	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
    546 	    qfextension[quotatype]);
    547 	for (qup = quplist; qup; qup = qup->next) {
    548 		fprintf(fd, "%s: block grace period: %s, ",
    549 		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
    550 		fprintf(fd, "file grace period: %s\n",
    551 		    cvtstoa(qup->dqblk.dqb_itime));
    552 	}
    553 	fclose(fd);
    554 	return (1);
    555 }
    556 
    557 /*
    558  * Merge changes of grace times in an ASCII file into a quotause list.
    559  */
    560 int
    561 readtimes(quplist, infd)
    562 	struct quotause *quplist;
    563 	int infd;
    564 {
    565 	struct quotause *qup;
    566 	FILE *fd;
    567 	int cnt;
    568 	char *cp;
    569 	long litime, lbtime;
    570 	time_t itime, btime, iseconds, bseconds;
    571 	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
    572 
    573 	(void)lseek(infd, (off_t)0, SEEK_SET);
    574 	fd = fdopen(dup(infd), "r");
    575 	if (fd == NULL) {
    576 		warnx("Can't re-read temp file!!");
    577 		return (0);
    578 	}
    579 	/*
    580 	 * Discard two title lines, then read lines to process.
    581 	 */
    582 	(void) fgets(line1, sizeof (line1), fd);
    583 	(void) fgets(line1, sizeof (line1), fd);
    584 	while (fgets(line1, sizeof (line1), fd) != NULL) {
    585 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    586 			warnx("%s: bad format", line1);
    587 			return (0);
    588 		}
    589 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    590 			warnx("%s: %s: bad format", fsp,
    591 			    &fsp[strlen(fsp) + 1]);
    592 			return (0);
    593 		}
    594 		cnt = sscanf(cp,
    595 		    " block grace period: %ld %s file grace period: %ld %s",
    596 		    &lbtime, bunits, &litime, iunits);
    597 		if (cnt != 4) {
    598 			warnx("%s:%s: bad format", fsp, cp);
    599 			return (0);
    600 		}
    601 		itime = (time_t)litime;
    602 		btime = (time_t)lbtime;
    603 		if (cvtatos(btime, bunits, &bseconds) == 0)
    604 			return (0);
    605 		if (cvtatos(itime, iunits, &iseconds) == 0)
    606 			return (0);
    607 		for (qup = quplist; qup; qup = qup->next) {
    608 			if (strcmp(fsp, qup->fsname))
    609 				continue;
    610 			qup->dqblk.dqb_btime = bseconds;
    611 			qup->dqblk.dqb_itime = iseconds;
    612 			qup->flags |= FOUND;
    613 			break;
    614 		}
    615 	}
    616 	fclose(fd);
    617 	/*
    618 	 * reset default grace periods for any filesystems
    619 	 * that have not been found.
    620 	 */
    621 	for (qup = quplist; qup; qup = qup->next) {
    622 		if (qup->flags & FOUND) {
    623 			qup->flags &= ~FOUND;
    624 			continue;
    625 		}
    626 		qup->dqblk.dqb_btime = 0;
    627 		qup->dqblk.dqb_itime = 0;
    628 	}
    629 	return (1);
    630 }
    631 
    632 /*
    633  * Convert seconds to ASCII times.
    634  */
    635 char *
    636 cvtstoa(time)
    637 	time_t time;
    638 {
    639 	static char buf[20];
    640 
    641 	if (time % (24 * 60 * 60) == 0) {
    642 		time /= 24 * 60 * 60;
    643 		snprintf(buf, sizeof buf, "%ld day%s", (long)time,
    644 		    time == 1 ? "" : "s");
    645 	} else if (time % (60 * 60) == 0) {
    646 		time /= 60 * 60;
    647 		sprintf(buf, "%ld hour%s", (long)time, time == 1 ? "" : "s");
    648 	} else if (time % 60 == 0) {
    649 		time /= 60;
    650 		sprintf(buf, "%ld minute%s", (long)time, time == 1 ? "" : "s");
    651 	} else
    652 		sprintf(buf, "%ld second%s", (long)time, time == 1 ? "" : "s");
    653 	return (buf);
    654 }
    655 
    656 /*
    657  * Convert ASCII input times to seconds.
    658  */
    659 int
    660 cvtatos(time, units, seconds)
    661 	time_t time;
    662 	char *units;
    663 	time_t *seconds;
    664 {
    665 
    666 	if (memcmp(units, "second", 6) == 0)
    667 		*seconds = time;
    668 	else if (memcmp(units, "minute", 6) == 0)
    669 		*seconds = time * 60;
    670 	else if (memcmp(units, "hour", 4) == 0)
    671 		*seconds = time * 60 * 60;
    672 	else if (memcmp(units, "day", 3) == 0)
    673 		*seconds = time * 24 * 60 * 60;
    674 	else {
    675 		printf("%s: bad units, specify %s\n", units,
    676 		    "days, hours, minutes, or seconds");
    677 		return (0);
    678 	}
    679 	return (1);
    680 }
    681 
    682 /*
    683  * Free a list of quotause structures.
    684  */
    685 void
    686 freeprivs(quplist)
    687 	struct quotause *quplist;
    688 {
    689 	struct quotause *qup, *nextqup;
    690 
    691 	for (qup = quplist; qup; qup = nextqup) {
    692 		nextqup = qup->next;
    693 		free(qup);
    694 	}
    695 }
    696 
    697 /*
    698  * Check whether a string is completely composed of digits.
    699  */
    700 int
    701 alldigits(s)
    702 	char *s;
    703 {
    704 	int c;
    705 
    706 	c = *s++;
    707 	do {
    708 		if (!isdigit(c))
    709 			return (0);
    710 	} while ((c = *s++) != 0);
    711 	return (1);
    712 }
    713 
    714 /*
    715  * Check to see if a particular quota is to be enabled.
    716  */
    717 int
    718 hasquota(fs, type, qfnamep)
    719 	struct fstab *fs;
    720 	int type;
    721 	char **qfnamep;
    722 {
    723 	char *opt;
    724 	char *cp;
    725 	static char initname, usrname[100], grpname[100];
    726 	static char buf[BUFSIZ];
    727 
    728 	if (!initname) {
    729 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
    730 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
    731 		initname = 1;
    732 	}
    733 	strcpy(buf, fs->fs_mntops);
    734 	cp = NULL;
    735 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    736 		if ((cp = strchr(opt, '=')) != NULL)
    737 			*cp++ = '\0';
    738 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    739 			break;
    740 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    741 			break;
    742 	}
    743 	if (!opt)
    744 		return (0);
    745 	if (cp) {
    746 		*qfnamep = cp;
    747 		return (1);
    748 	}
    749 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    750 	*qfnamep = buf;
    751 	return (1);
    752 }
    753