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