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