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