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