Home | History | Annotate | Line # | Download | only in quota
      1  1.6  dholland /*	$NetBSD: quotautil.c,v 1.6 2012/01/30 06:14:43 dholland Exp $ */
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1980, 1990, 1993
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to Berkeley by
      8  1.1  christos  * Robert Elz at The University of Melbourne.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     19  1.1  christos  *    may be used to endorse or promote products derived from this software
     20  1.1  christos  *    without specific prior written permission.
     21  1.1  christos  *
     22  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  christos  * SUCH DAMAGE.
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos #include <sys/cdefs.h>
     36  1.1  christos #ifndef lint
     37  1.1  christos __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
     38  1.1  christos  The Regents of the University of California.  All rights reserved.");
     39  1.1  christos #endif /* not lint */
     40  1.1  christos 
     41  1.1  christos #ifndef lint
     42  1.1  christos #if 0
     43  1.1  christos static char sccsid[] = "@(#)quota.c	8.4 (Berkeley) 4/28/95";
     44  1.1  christos #else
     45  1.6  dholland __RCSID("$NetBSD: quotautil.c,v 1.6 2012/01/30 06:14:43 dholland Exp $");
     46  1.1  christos #endif
     47  1.1  christos #endif /* not lint */
     48  1.1  christos 
     49  1.1  christos #include <sys/param.h>
     50  1.1  christos #include <sys/types.h>
     51  1.1  christos 
     52  1.1  christos #include <ctype.h>
     53  1.1  christos #include <stdio.h>
     54  1.1  christos #include <stdlib.h>
     55  1.1  christos #include <string.h>
     56  1.1  christos #include <time.h>
     57  1.1  christos #include <unistd.h>
     58  1.1  christos #include <fstab.h>
     59  1.1  christos #include <errno.h>
     60  1.1  christos #include <limits.h>
     61  1.1  christos #include <inttypes.h>
     62  1.1  christos 
     63  1.1  christos #include <ufs/ufs/quota1.h>
     64  1.1  christos 
     65  1.1  christos #include "quotautil.h"
     66  1.1  christos 
     67  1.3    bouyer const char *qfextension[] = INITQFNAMES;
     68  1.6  dholland const char *qfname = QUOTAFILENAME;
     69  1.1  christos 
     70  1.1  christos /*
     71  1.1  christos  * Check to see if a particular quota is to be enabled.
     72  1.1  christos  */
     73  1.1  christos int
     74  1.1  christos hasquota(char *buf, size_t len, struct fstab *fs, int type)
     75  1.1  christos {
     76  1.1  christos 	char *opt;
     77  1.1  christos 	char *cp = NULL;
     78  1.1  christos 	static char initname, usrname[100], grpname[100];
     79  1.5  dholland 	char optbuf[256];
     80  1.1  christos 
     81  1.1  christos 	if (!initname) {
     82  1.1  christos 		(void)snprintf(usrname, sizeof(usrname), "%s%s",
     83  1.1  christos 		    qfextension[USRQUOTA], qfname);
     84  1.1  christos 		(void)snprintf(grpname, sizeof(grpname), "%s%s",
     85  1.1  christos 		    qfextension[GRPQUOTA], qfname);
     86  1.1  christos 		initname = 1;
     87  1.1  christos 	}
     88  1.5  dholland 	strlcpy(optbuf, fs->fs_mntops, sizeof(optbuf));
     89  1.5  dholland 	for (opt = strtok(optbuf, ","); opt; opt = strtok(NULL, ",")) {
     90  1.1  christos 		if ((cp = strchr(opt, '=')) != NULL)
     91  1.1  christos 			*cp++ = '\0';
     92  1.1  christos 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
     93  1.1  christos 			break;
     94  1.1  christos 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
     95  1.1  christos 			break;
     96  1.1  christos 	}
     97  1.1  christos 	if (!opt)
     98  1.1  christos 		return 0;
     99  1.1  christos 	if (cp) {
    100  1.1  christos 		strlcpy(buf, cp, len);
    101  1.1  christos 		return 1;
    102  1.1  christos 	}
    103  1.1  christos 	(void)snprintf(buf, len, "%s/%s.%s", fs->fs_file, qfname,
    104  1.1  christos 	    qfextension[type]);
    105  1.1  christos 	return 1;
    106  1.1  christos }
    107  1.1  christos 
    108  1.2  christos /*
    109  1.2  christos  * Check to see if target appears in list of size cnt.
    110  1.2  christos  */
    111  1.2  christos int
    112  1.2  christos oneof(const char *target, char *list[], int cnt)
    113  1.2  christos {
    114  1.2  christos 	int i;
    115  1.2  christos 
    116  1.2  christos 	for (i = 0; i < cnt; i++)
    117  1.2  christos 		if (strcmp(target, list[i]) == 0)
    118  1.2  christos 			return i;
    119  1.2  christos 	return -1;
    120  1.2  christos }
    121