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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20