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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20