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

Diff of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2734 Revision 2771
29#include "rc-misc.h" 29#include "rc-misc.h"
30#include "strlist.h" 30#include "strlist.h"
31 31
32 32
33#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__) 33#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
34static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex, 34static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
35 regex_t *fstype_regex, regex_t *skip_fstype_regex,
35 char **mounts, bool list_nodes, bool list_fstype) 36 char **mounts, bool list_nodes, bool list_fstype)
36{ 37{
37 struct statfs *mnts; 38 struct statfs *mnts;
38 int nmnts; 39 int nmnts;
39 int i; 40 int i;
44 45
45 for (i = 0; i < nmnts; i++) { 46 for (i = 0; i < nmnts; i++) {
46 if (node_regex && 47 if (node_regex &&
47 regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0) 48 regexec (node_regex, mnts[i].f_mntfromname, 0, NULL, 0) != 0)
48 continue; 49 continue;
50 if (skip_node_regex &&
51 regexec (skip_node_regex, mnts[i].f_mntfromname, 0, NULL, 0) == 0)
52 continue;
49 if (fstype_regex && 53 if (fstype_regex &&
50 regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0) 54 regexec (fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) != 0)
55 continue;
56 if (skip_fstype_regex &&
57 regexec (skip_fstype_regex, mnts[i].f_fstypename, 0, NULL, 0) == 0)
51 continue; 58 continue;
52 59
53 if (mounts) { 60 if (mounts) {
54 bool found = false; 61 bool found = false;
55 int j; 62 int j;
71 78
72 return (list); 79 return (list);
73} 80}
74 81
75#elif defined (__linux__) 82#elif defined (__linux__)
76static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex, 83static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
84 regex_t *fstype_regex, regex_t *skip_fstype_regex,
77 char **mounts, bool list_nodes, bool list_fstype) 85 char **mounts, bool list_nodes, bool list_fstype)
78{ 86{
79 FILE *fp; 87 FILE *fp;
80 char buffer[PATH_MAX * 3]; 88 char buffer[PATH_MAX * 3];
81 char *p; 89 char *p;
91 p = buffer; 99 p = buffer;
92 from = strsep (&p, " "); 100 from = strsep (&p, " ");
93 if (node_regex && 101 if (node_regex &&
94 regexec (node_regex, from, 0, NULL, 0) != 0) 102 regexec (node_regex, from, 0, NULL, 0) != 0)
95 continue; 103 continue;
104 if (skip_node_regex &&
105 regexec (skip_node_regex, from, 0, NULL, 0) == 0)
106 continue;
96 107
97 to = strsep (&p, " "); 108 to = strsep (&p, " ");
98 fstype = strsep (&p, " "); 109 fstype = strsep (&p, " ");
99 /* Skip the really silly rootfs */ 110 /* Skip the really silly rootfs */
100 if (strcmp (fstype, "rootfs") == 0) 111 if (strcmp (fstype, "rootfs") == 0)
101 continue; 112 continue;
102 if (fstype_regex && 113 if (fstype_regex &&
103 regexec (fstype_regex, fstype, 0, NULL, 0) != 0) 114 regexec (fstype_regex, fstype, 0, NULL, 0) != 0)
115 continue;
116 if (skip_fstype_regex &&
117 regexec (skip_fstype_regex, fstype, 0, NULL, 0) == 0)
104 continue; 118 continue;
105 119
106 if (mounts) { 120 if (mounts) {
107 bool found = false; 121 bool found = false;
108 int j; 122 int j;
128 142
129#else 143#else
130# error "Operating system not supported!" 144# error "Operating system not supported!"
131#endif 145#endif
132 146
147static regex_t *get_regex (char *string)
148{
149 regex_t *reg = rc_xmalloc (sizeof (regex_t));
150 int result;
151 char buffer[256];
152
153 if ((result = regcomp (reg, string, REG_EXTENDED | REG_NOSUB)) != 0)
154 {
155 regerror (result, reg, buffer, sizeof (buffer));
156 eerrorx ("%s: invalid regex `%s'", APPLET, buffer);
157 }
158
159 return (reg);
160}
161
133#include "_usage.h" 162#include "_usage.h"
134#define getoptstring "F:N:S:fnrV" getoptstring_COMMON 163#define getoptstring "f:F:n:N:p:P:os" getoptstring_COMMON
135static struct option longopts[] = { 164static struct option longopts[] = {
165 { "fstype-regex", 1, NULL, 'f'},
136 { "fstype-regex", 1, NULL, 'F'}, 166 { "skip-fstype-regex", 1, NULL, 'F'},
167 { "node-regex", 1, NULL, 'n'},
137 { "node-regex", 1, NULL, 'N'}, 168 { "skip-node-regex", 1, NULL, 'N'},
169 { "point-regex", 1, NULL, 'p'},
138 { "skip-regex", 1, NULL, 'S'}, 170 { "skip-point-regex", 1, NULL, 'P'},
171 { "list-nodes", 0, NULL, 'o'},
139 { "fstype", 0, NULL, 'f'}, 172 { "list-fstype", 0, NULL, 's'},
140 { "node", 0, NULL, 'n'},
141 { "reverse", 0, NULL, 'r'},
142 longopts_COMMON 173 longopts_COMMON
143 { NULL, 0, NULL, 0} 174 { NULL, 0, NULL, 0}
144}; 175};
145#include "_usage.c" 176#include "_usage.c"
146 177
147int main (int argc, char **argv) 178int main (int argc, char **argv)
148{ 179{
149 int i; 180 int i;
150 regex_t *fstype_regex = NULL; 181 regex_t *fstype_regex = NULL;
151 regex_t *node_regex = NULL; 182 regex_t *node_regex = NULL;
183 regex_t *point_regex = NULL;
184 regex_t *skip_fstype_regex = NULL;
152 regex_t *skip_regex = NULL; 185 regex_t *skip_node_regex = NULL;
186 regex_t *skip_point_regex = NULL;
153 char **nodes = NULL; 187 char **nodes = NULL;
154 char *node; 188 char *node;
155 int result;
156 char buffer[256];
157 bool list_nodes = false; 189 bool list_nodes = false;
158 bool list_fstype = false; 190 bool list_fstype = false;
159 bool reverse = false;
160 char **mounts = NULL; 191 char **mounts = NULL;
161 int opt; 192 int opt;
193 int result;
194
195#define DO_REG(_var) \
196 if (_var) free (_var); \
197 _var = get_regex (optarg);
162 198
163 while ((opt = getopt_long (argc, argv, getoptstring, 199 while ((opt = getopt_long (argc, argv, getoptstring,
164 longopts, (int *) 0)) != -1) 200 longopts, (int *) 0)) != -1)
201 {
165 switch (opt) { 202 switch (opt) {
203 case 'f':
204 DO_REG (fstype_regex);
205 break;
166 case 'F': 206 case 'F':
167 if (fstype_regex) 207 DO_REG (skip_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; 208 break;
177 209 case 'n':
210 DO_REG (node_regex);
211 break;
178 case 'N': 212 case 'N':
179 if (node_regex) 213 DO_REG (skip_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; 214 break;
189
190 case 'S': 215 case 'p':
191 if (skip_regex) 216 DO_REG (point_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; 217 break;
201
202 case 'f': 218 case 'P':
203 list_fstype = true; 219 DO_REG (skip_point_regex);
204 list_nodes = false;
205 break; 220 break;
206
207 case 'n': 221 case 'o':
208 list_nodes = true; 222 list_nodes = true;
209 list_fstype = false; 223 list_fstype = false;
210 break; 224 break;
211
212 case 'r': 225 case 's':
213 reverse = true; 226 list_nodes = false;
227 list_fstype = true;
214 break; 228 break;
215 229
216 case_RC_COMMON_GETOPT 230 case_RC_COMMON_GETOPT
217 } 231 }
232 }
218 233
219 while (optind < argc) { 234 while (optind < argc) {
220 if (argv[optind][0] != '/') 235 if (argv[optind][0] != '/')
221 eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]); 236 eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]);
222 mounts = rc_strlist_add (mounts, argv[optind++]); 237 mounts = rc_strlist_add (mounts, argv[optind++]);
223 } 238 }
239
224 nodes = find_mounts (node_regex, fstype_regex, mounts, 240 nodes = find_mounts (node_regex, skip_node_regex,
241 fstype_regex, skip_fstype_regex,
225 list_nodes, list_fstype); 242 mounts, list_nodes, list_fstype);
226 243
227 if (node_regex) 244 if (node_regex)
228 regfree (node_regex); 245 regfree (node_regex);
246 if (skip_node_regex)
247 regfree (skip_node_regex);
229 if (fstype_regex) 248 if (fstype_regex)
230 regfree (fstype_regex); 249 regfree (fstype_regex);
250 if (skip_fstype_regex)
251 regfree (skip_fstype_regex);
231 252
232 if (reverse)
233 rc_strlist_reverse (nodes); 253 rc_strlist_reverse (nodes);
234 254
235 result = EXIT_FAILURE; 255 result = EXIT_FAILURE;
236 STRLIST_FOREACH (nodes, node, i) { 256 STRLIST_FOREACH (nodes, node, i) {
257 if (point_regex && regexec (point_regex, node, 0, NULL, 0) != 0)
258 continue;
237 if (skip_regex && regexec (skip_regex, node, 0, NULL, 0) == 0) 259 if (skip_point_regex && regexec (skip_point_regex, node, 0, NULL, 0) == 0)
238 continue; 260 continue;
239 printf ("%s\n", node); 261 printf ("%s\n", node);
240 result = EXIT_SUCCESS; 262 result = EXIT_SUCCESS;
241 } 263 }
242 rc_strlist_free (nodes); 264 rc_strlist_free (nodes);
243 265
266 if (point_regex)
267 regfree (point_regex);
244 if (skip_regex) 268 if (skip_point_regex)
245 free (skip_regex); 269 regfree (skip_point_regex);
246 270
247 exit (result); 271 exit (result);
248} 272}

Legend:
Removed from v.2734  
changed lines
  Added in v.2771

  ViewVC Help
Powered by ViewVC 1.1.20