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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2865 - (hide annotations) (download) (as text)
Mon Sep 3 14:02:15 2007 UTC (6 years, 7 months ago) by uberlord
File MIME type: text/x-csrc
File size: 6080 byte(s)
Add --quiet to mountinfo
1 uberlord 2547 /*
2     mountinfo.c
3     Obtains information about mounted filesystems.
4    
5     Copyright 2007 Gentoo Foundation
6     */
7    
8 vapier 2618 #define APPLET "mountinfo"
9    
10 uberlord 2803 #include <sys/types.h>
11    
12 uberlord 2794 #if defined(__DragonFly__) || defined(__FreeBSD__) || \
13     defined(__NetBSD__) || defined(__OpenBSD__)
14     #define BSD
15 uberlord 2547 #include <sys/param.h>
16     #include <sys/ucred.h>
17     #include <sys/mount.h>
18     #endif
19    
20     #include <errno.h>
21 uberlord 2580 #include <getopt.h>
22 uberlord 2547 #include <limits.h>
23     #include <stdio.h>
24     #include <stdlib.h>
25     #include <string.h>
26 uberlord 2803 #include <regex.h>
27 uberlord 2547
28 uberlord 2799 #include "builtins.h"
29 uberlord 2547 #include "einfo.h"
30     #include "rc.h"
31     #include "rc-misc.h"
32     #include "strlist.h"
33    
34 uberlord 2580
35 uberlord 2794 #ifdef BSD
36 uberlord 2771 static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
37     regex_t *fstype_regex, regex_t *skip_fstype_regex,
38 uberlord 2784 char **mounts, bool node, bool fstype)
39 uberlord 2547 {
40 uberlord 2577 struct statfs *mnts;
41     int nmnts;
42     int i;
43     char **list = NULL;
44 uberlord 2547
45 uberlord 2577 if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
46     eerrorx ("getmntinfo: %s", strerror (errno));
47 uberlord 2547
48 uberlord 2577 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 uberlord 2771 if (skip_node_regex &&
53     regexec (skip_node_regex, mnts[i].f_mntfromname, 0, NULL, 0) == 0)
54     continue;
55 uberlord 2577 if (fstype_regex &&
56     regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
57     continue;
58 uberlord 2771 if (skip_fstype_regex &&
59     regexec (skip_fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) == 0)
60     continue;
61 uberlord 2547
62 uberlord 2577 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 uberlord 2547
75 uberlord 2784 list = rc_strlist_addsortc (list, node ?
76 uberlord 2577 mnts[i].f_mntfromname :
77 uberlord 2784 fstype ? mnts[i].f_fstypename :
78 uberlord 2577 mnts[i].f_mntonname);
79     }
80 uberlord 2547
81 uberlord 2577 return (list);
82 uberlord 2547 }
83    
84     #elif defined (__linux__)
85 uberlord 2771 static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
86     regex_t *fstype_regex, regex_t *skip_fstype_regex,
87 uberlord 2784 char **mounts, bool node, bool fstype)
88 uberlord 2547 {
89 uberlord 2577 FILE *fp;
90     char buffer[PATH_MAX * 3];
91     char *p;
92     char *from;
93     char *to;
94 uberlord 2784 char *fst;
95 uberlord 2577 char **list = NULL;
96 uberlord 2569
97 uberlord 2577 if ((fp = fopen ("/proc/mounts", "r")) == NULL)
98     eerrorx ("getmntinfo: %s", strerror (errno));
99 uberlord 2547
100 uberlord 2577 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 uberlord 2771 if (skip_node_regex &&
107     regexec (skip_node_regex, from, 0, NULL, 0) == 0)
108     continue;
109 uberlord 2569
110 uberlord 2577 to = strsep (&p, " ");
111 uberlord 2784 fst = strsep (&p, " ");
112 uberlord 2577 /* Skip the really silly rootfs */
113 uberlord 2784 if (strcmp (fst, "rootfs") == 0)
114 uberlord 2577 continue;
115     if (fstype_regex &&
116 uberlord 2784 regexec (fstype_regex, fst, 0, NULL, 0) != 0)
117 uberlord 2577 continue;
118 uberlord 2771 if (skip_fstype_regex &&
119 uberlord 2784 regexec (skip_fstype_regex, fst, 0, NULL, 0) == 0)
120 uberlord 2771 continue;
121 uberlord 2547
122 uberlord 2577 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 uberlord 2547
135 uberlord 2784 list = rc_strlist_addsortc (list, node ? from : fstype ? fst : to);
136 uberlord 2577 }
137     fclose (fp);
138 uberlord 2547
139 uberlord 2577 return (list);
140 uberlord 2547 }
141    
142     #else
143     # error "Operating system not supported!"
144     #endif
145    
146 uberlord 2771 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 uberlord 2734 #include "_usage.h"
162 uberlord 2865 #define getoptstring "f:F:n:N:op:P:qs" getoptstring_COMMON
163 vapier 2603 static struct option longopts[] = {
164 uberlord 2771 { "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 uberlord 2784 { "node", 0, NULL, 'o'},
171     { "fstype", 0, NULL, 's'},
172 uberlord 2865 { "quiet", 0, NULL, 'q'},
173 uberlord 2734 longopts_COMMON
174 vapier 2603 { NULL, 0, NULL, 0}
175     };
176 vapier 2618 #include "_usage.c"
177 vapier 2603
178 uberlord 2799 int mountinfo (int argc, char **argv)
179 uberlord 2547 {
180 uberlord 2577 int i;
181     regex_t *fstype_regex = NULL;
182     regex_t *node_regex = NULL;
183 uberlord 2771 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 uberlord 2577 char **nodes = NULL;
188 uberlord 2784 char *n;
189     bool node = false;
190     bool fstype = false;
191 uberlord 2577 char **mounts = NULL;
192 uberlord 2698 int opt;
193 uberlord 2865 bool quiet = false;
194 uberlord 2771 int result;
195 uberlord 2547
196 uberlord 2771 #define DO_REG(_var) \
197     if (_var) free (_var); \
198     _var = get_regex (optarg);
199    
200 uberlord 2698 while ((opt = getopt_long (argc, argv, getoptstring,
201     longopts, (int *) 0)) != -1)
202 uberlord 2771 {
203 uberlord 2698 switch (opt) {
204 uberlord 2771 case 'f':
205     DO_REG (fstype_regex);
206     break;
207 uberlord 2580 case 'F':
208 uberlord 2771 DO_REG (skip_fstype_regex);
209 uberlord 2580 break;
210 uberlord 2771 case 'n':
211     DO_REG (node_regex);
212     break;
213 uberlord 2580 case 'N':
214 uberlord 2771 DO_REG (skip_node_regex);
215 uberlord 2580 break;
216 uberlord 2865 case 'o':
217     node = true;
218     fstype = false;
219     break;
220 uberlord 2771 case 'p':
221     DO_REG (point_regex);
222 uberlord 2580 break;
223 uberlord 2771 case 'P':
224     DO_REG (skip_point_regex);
225 uberlord 2580 break;
226 uberlord 2865 case 'q':
227     quiet = true;
228 uberlord 2580 break;
229 uberlord 2771 case 's':
230 uberlord 2784 node = false;
231     fstype = true;
232 uberlord 2580 break;
233    
234 uberlord 2771 case_RC_COMMON_GETOPT
235 uberlord 2577 }
236 uberlord 2771 }
237 uberlord 2547
238 uberlord 2580 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 uberlord 2577 }
243 uberlord 2547
244 uberlord 2771 nodes = find_mounts (node_regex, skip_node_regex,
245     fstype_regex, skip_fstype_regex,
246 uberlord 2784 mounts, node, fstype);
247 uberlord 2771
248 uberlord 2577 if (node_regex)
249     regfree (node_regex);
250 uberlord 2771 if (skip_node_regex)
251     regfree (skip_node_regex);
252 uberlord 2577 if (fstype_regex)
253     regfree (fstype_regex);
254 uberlord 2771 if (skip_fstype_regex)
255     regfree (skip_fstype_regex);
256 uberlord 2547
257 uberlord 2771 rc_strlist_reverse (nodes);
258 uberlord 2547
259 uberlord 2577 result = EXIT_FAILURE;
260 uberlord 2784 STRLIST_FOREACH (nodes, n, i) {
261     if (point_regex && regexec (point_regex, n, 0, NULL, 0) != 0)
262 uberlord 2577 continue;
263 uberlord 2784 if (skip_point_regex && regexec (skip_point_regex, n, 0, NULL, 0) == 0)
264 uberlord 2771 continue;
265 uberlord 2865 if (! quiet)
266     printf ("%s\n", n);
267 uberlord 2577 result = EXIT_SUCCESS;
268     }
269     rc_strlist_free (nodes);
270 uberlord 2547
271 uberlord 2771 if (point_regex)
272     regfree (point_regex);
273     if (skip_point_regex)
274     regfree (skip_point_regex);
275 uberlord 2547
276 uberlord 2577 exit (result);
277 uberlord 2547 }

  ViewVC Help
Powered by ViewVC 1.1.20