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