quotautil.c revision 1.4 1 /* $NetBSD: quotautil.c,v 1.4 2012/01/25 01:24:07 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.4 2012/01/25 01:24:07 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 *qfnamep = QUOTAFILENAME;
69
70 /*
71 * Check to see if a particular quota is to be enabled.
72 */
73 int
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
80 if (!initname) {
81 (void)snprintf(usrname, sizeof(usrname), "%s%s",
82 qfextension[USRQUOTA], qfname);
83 (void)snprintf(grpname, sizeof(grpname), "%s%s",
84 qfextension[GRPQUOTA], qfname);
85 initname = 1;
86 }
87 strlcpy(buf, fs->fs_mntops, len);
88 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
89 if ((cp = strchr(opt, '=')) != NULL)
90 *cp++ = '\0';
91 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
92 break;
93 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
94 break;
95 }
96 if (!opt)
97 return 0;
98 if (cp) {
99 strlcpy(buf, cp, len);
100 return 1;
101 }
102 (void)snprintf(buf, len, "%s/%s.%s", fs->fs_file, qfname,
103 qfextension[type]);
104 return 1;
105 }
106
107 /*
108 * Check to see if target appears in list of size cnt.
109 */
110 int
111 oneof(const char *target, char *list[], int cnt)
112 {
113 int i;
114
115 for (i = 0; i < cnt; i++)
116 if (strcmp(target, list[i]) == 0)
117 return i;
118 return -1;
119 }
120