quotautil.c revision 1.2 1 1.2 christos /* $NetBSD: quotautil.c,v 1.2 2011/03/06 23:26:05 christos 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.2 christos __RCSID("$NetBSD: quotautil.c,v 1.2 2011/03/06 23:26:05 christos 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/quota2.h>
64 1.1 christos #include <ufs/ufs/quota1.h>
65 1.1 christos
66 1.1 christos #include "quotautil.h"
67 1.1 christos
68 1.1 christos const char *qfextension[MAXQUOTAS] = INITQFNAMES;
69 1.1 christos const char *qfname = QUOTAFILENAME;
70 1.1 christos
71 1.1 christos /*
72 1.1 christos * Check to see if a particular quota is to be enabled.
73 1.1 christos */
74 1.1 christos int
75 1.1 christos hasquota(char *buf, size_t len, struct fstab *fs, int type)
76 1.1 christos {
77 1.1 christos char *opt;
78 1.1 christos char *cp = NULL;
79 1.1 christos static char initname, usrname[100], grpname[100];
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.1 christos strlcpy(buf, fs->fs_mntops, len);
89 1.1 christos for (opt = strtok(buf, ","); 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.1 christos int
109 1.1 christos alldigits(const char *s)
110 1.1 christos {
111 1.1 christos unsigned char c;
112 1.1 christos
113 1.1 christos c = *s++;
114 1.1 christos do {
115 1.1 christos if (!isdigit(c))
116 1.1 christos return 0;
117 1.1 christos } while ((c = *s++) != 0);
118 1.1 christos return 1;
119 1.1 christos }
120 1.2 christos
121 1.2 christos /*
122 1.2 christos * Check to see if target appears in list of size cnt.
123 1.2 christos */
124 1.2 christos int
125 1.2 christos oneof(const char *target, char *list[], int cnt)
126 1.2 christos {
127 1.2 christos int i;
128 1.2 christos
129 1.2 christos for (i = 0; i < cnt; i++)
130 1.2 christos if (strcmp(target, list[i]) == 0)
131 1.2 christos return i;
132 1.2 christos return -1;
133 1.2 christos }
134