/[baselayout]/trunk/src/mountinfo.c
Gentoo

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2865 - (show annotations) (download) (as text)
Mon Sep 3 14:02:15 2007 UTC (7 years, 3 months ago) by uberlord
File MIME type: text/x-csrc
File size: 6080 byte(s)
Add --quiet to mountinfo
1 /*
2 mountinfo.c
3 Obtains information about mounted filesystems.
4
5 Copyright 2007 Gentoo Foundation
6 */
7
8 #define APPLET "mountinfo"
9
10 #include <sys/types.h>
11
12 #if defined(__DragonFly__) || defined(__FreeBSD__) || \
13 defined(__NetBSD__) || defined(__OpenBSD__)
14 #define BSD
15 #include <sys/param.h>
16 #include <sys/ucred.h>
17 #include <sys/mount.h>
18 #endif
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <regex.h>
27
28 #include "builtins.h"
29 #include "einfo.h"
30 #include "rc.h"
31 #include "rc-misc.h"
32 #include "strlist.h"
33
34
35 #ifdef BSD
36 static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
37 regex_t *fstype_regex, regex_t *skip_fstype_regex,
38 char **mounts, bool node, bool fstype)
39 {
40 struct statfs *mnts;
41 int nmnts;
42 int i;
43 char **list = NULL;
44
45 if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
46 eerrorx ("getmntinfo: %s", strerror (errno));
47
48 for (i = 0; i < nmnts; i++) {
49 if (node_regex &&
50 regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
51 continue;
52 if (skip_node_regex &&
53 regexec (skip_node_regex, mnts[i].f_mntfromname, 0, NULL, 0) == 0)
54 continue;
55 if (fstype_regex &&
56 regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
57 continue;
58 if (skip_fstype_regex &&
59 regexec (skip_fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) == 0)
60 continue;
61
62 if (mounts) {
63 bool found = false;
64 int j;
65 char *mnt;
66 STRLIST_FOREACH (mounts, mnt, j)
67 if (strcmp (mnt, mnts[i].f_mntonname) == 0) {
68 found = true;
69 break;
70 }
71 if (! found)
72 continue;
73 }
74
75 list = rc_strlist_addsortc (list, node ?
76 mnts[i].f_mntfromname :
77 fstype ? mnts[i].f_fstypename :
78 mnts[i].f_mntonname);
79 }
80
81 return (list);
82 }
83
84 #elif defined (__linux__)
85 static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
86 regex_t *fstype_regex, regex_t *skip_fstype_regex,
87 char **mounts, bool node, bool fstype)
88 {
89 FILE *fp;
90 char buffer[PATH_MAX * 3];
91 char *p;
92 char *from;
93 char *to;
94 char *fst;
95 char **list = NULL;
96
97 if ((fp = fopen ("/proc/mounts", "r")) == NULL)
98 eerrorx ("getmntinfo: %s", strerror (errno));
99
100 while (fgets (buffer, sizeof (buffer), fp)) {
101 p = buffer;
102 from = strsep (&p, " ");
103 if (node_regex &&
104 regexec (node_regex, from, 0, NULL, 0) != 0)
105 continue;
106 if (skip_node_regex &&
107 regexec (skip_node_regex, from, 0, NULL, 0) == 0)
108 continue;
109
110 to = strsep (&p, " ");
111 fst = strsep (&p, " ");
112 /* Skip the really silly rootfs */
113 if (strcmp (fst, "rootfs") == 0)
114 continue;
115 if (fstype_regex &&
116 regexec (fstype_regex, fst, 0, NULL, 0) != 0)
117 continue;
118 if (skip_fstype_regex &&
119 regexec (skip_fstype_regex, fst, 0, NULL, 0) == 0)
120 continue;
121
122 if (mounts) {
123 bool found = false;
124 int j;
125 char *mnt;
126 STRLIST_FOREACH (mounts, mnt, j)
127 if (strcmp (mnt, to) == 0) {
128 found = true;
129 break;
130 }
131 if (! found)
132 continue;
133 }
134
135 list = rc_strlist_addsortc (list, node ? from : fstype ? fst : to);
136 }
137 fclose (fp);
138
139 return (list);
140 }
141
142 #else
143 # error "Operating system not supported!"
144 #endif
145
146 static regex_t *get_regex (char *string)
147 {
148 regex_t *reg = rc_xmalloc (sizeof (regex_t));
149 int result;
150 char buffer[256];
151
152 if ((result = regcomp (reg, string, REG_EXTENDED | REG_NOSUB)) != 0)
153 {
154 regerror (result, reg, buffer, sizeof (buffer));
155 eerrorx ("%s: invalid regex `%s'", APPLET, buffer);
156 }
157
158 return (reg);
159 }
160
161 #include "_usage.h"
162 #define getoptstring "f:F:n:N:op:P:qs" getoptstring_COMMON
163 static struct option longopts[] = {
164 { "fstype-regex", 1, NULL, 'f'},
165 { "skip-fstype-regex", 1, NULL, 'F'},
166 { "node-regex", 1, NULL, 'n'},
167 { "skip-node-regex", 1, NULL, 'N'},
168 { "point-regex", 1, NULL, 'p'},
169 { "skip-point-regex", 1, NULL, 'P'},
170 { "node", 0, NULL, 'o'},
171 { "fstype", 0, NULL, 's'},
172 { "quiet", 0, NULL, 'q'},
173 longopts_COMMON
174 { NULL, 0, NULL, 0}
175 };
176 #include "_usage.c"
177
178 int mountinfo (int argc, char **argv)
179 {
180 int i;
181 regex_t *fstype_regex = NULL;
182 regex_t *node_regex = NULL;
183 regex_t *point_regex = NULL;
184 regex_t *skip_fstype_regex = NULL;
185 regex_t *skip_node_regex = NULL;
186 regex_t *skip_point_regex = NULL;
187 char **nodes = NULL;
188 char *n;
189 bool node = false;
190 bool fstype = false;
191 char **mounts = NULL;
192 int opt;
193 bool quiet = false;
194 int result;
195
196 #define DO_REG(_var) \
197 if (_var) free (_var); \
198 _var = get_regex (optarg);
199
200 while ((opt = getopt_long (argc, argv, getoptstring,
201 longopts, (int *) 0)) != -1)
202 {
203 switch (opt) {
204 case 'f':
205 DO_REG (fstype_regex);
206 break;
207 case 'F':
208 DO_REG (skip_fstype_regex);
209 break;
210 case 'n':
211 DO_REG (node_regex);
212 break;
213 case 'N':
214 DO_REG (skip_node_regex);
215 break;
216 case 'o':
217 node = true;
218 fstype = false;
219 break;
220 case 'p':
221 DO_REG (point_regex);
222 break;
223 case 'P':
224 DO_REG (skip_point_regex);
225 break;
226 case 'q':
227 quiet = true;
228 break;
229 case 's':
230 node = false;
231 fstype = true;
232 break;
233
234 case_RC_COMMON_GETOPT
235 }
236 }
237
238 while (optind < argc) {
239 if (argv[optind][0] != '/')
240 eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]);
241 mounts = rc_strlist_add (mounts, argv[optind++]);
242 }
243
244 nodes = find_mounts (node_regex, skip_node_regex,
245 fstype_regex, skip_fstype_regex,
246 mounts, node, fstype);
247
248 if (node_regex)
249 regfree (node_regex);
250 if (skip_node_regex)
251 regfree (skip_node_regex);
252 if (fstype_regex)
253 regfree (fstype_regex);
254 if (skip_fstype_regex)
255 regfree (skip_fstype_regex);
256
257 rc_strlist_reverse (nodes);
258
259 result = EXIT_FAILURE;
260 STRLIST_FOREACH (nodes, n, i) {
261 if (point_regex && regexec (point_regex, n, 0, NULL, 0) != 0)
262 continue;
263 if (skip_point_regex && regexec (skip_point_regex, n, 0, NULL, 0) == 0)
264 continue;
265 if (! quiet)
266 printf ("%s\n", n);
267 result = EXIT_SUCCESS;
268 }
269 rc_strlist_free (nodes);
270
271 if (point_regex)
272 regfree (point_regex);
273 if (skip_point_regex)
274 regfree (skip_point_regex);
275
276 exit (result);
277 }

  ViewVC Help
Powered by ViewVC 1.1.20