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

Diff of /trunk/src/mountinfo.c

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

Revision 2577 Revision 2580
13#elif defined(__linux__) 13#elif defined(__linux__)
14#include <limits.h> 14#include <limits.h>
15#endif 15#endif
16 16
17#include <errno.h> 17#include <errno.h>
18#include <getopt.h>
18#include <limits.h> 19#include <limits.h>
19#include <regex.h> 20#include <regex.h>
20#include <stdio.h> 21#include <stdio.h>
21#include <stdlib.h> 22#include <stdlib.h>
22#include <string.h> 23#include <string.h>
23 24
24#include "einfo.h" 25#include "einfo.h"
25#include "rc.h" 26#include "rc.h"
26#include "rc-misc.h" 27#include "rc-misc.h"
27#include "strlist.h" 28#include "strlist.h"
29
28 30
29#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__) 31#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
30static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex, 32static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
31 char **mounts, bool list_nodes, bool list_fstype) 33 char **mounts, bool list_nodes, bool list_fstype)
32{ 34{
33 struct statfs *mnts; 35 struct statfs *mnts;
34 int nmnts; 36 int nmnts;
35 int i; 37 int i;
36 char **list = NULL; 38 char **list = NULL;
39 char c;
37 40
38 if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0) 41 if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
39 eerrorx ("getmntinfo: %s", strerror (errno)); 42 eerrorx ("getmntinfo: %s", strerror (errno));
40 43
41 for (i = 0; i < nmnts; i++) { 44 for (i = 0; i < nmnts; i++) {
138 char buffer[256]; 141 char buffer[256];
139 bool list_nodes = false; 142 bool list_nodes = false;
140 bool list_fstype = false; 143 bool list_fstype = false;
141 bool reverse = false; 144 bool reverse = false;
142 char **mounts = NULL; 145 char **mounts = NULL;
146 char c;
143 147
144 for (i = 1; i < argc; i++) { 148 static struct option longopts[] = {
145 if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc)) { 149 { "fstype-regex", 1, NULL, 'F'},
146 i++; 150 { "node-regex", 1, NULL, 'N'},
151 { "skip-regex", 1, NULL, 'S'},
152 { "fstype", 0, NULL, 'f'},
153 { "node", 0, NULL, 'n'},
154 { "reverse", 0, NULL, 'r'},
155 { NULL, 0, NULL, 0}
156 };
157
158 while ((c = getopt_long (argc, argv, "F:N:S;fnr",
159 longopts, (int *) 0)) != -1)
160 switch (c) {
161 case 'F':
147 if (fstype_regex) 162 if (fstype_regex)
148 free (fstype_regex); 163 free (fstype_regex);
149 fstype_regex = rc_xmalloc (sizeof (regex_t)); 164 fstype_regex = rc_xmalloc (sizeof (regex_t));
150 if ((result = regcomp (fstype_regex, argv[i], 165 if ((result = regcomp (fstype_regex, optarg,
151 REG_EXTENDED | REG_NOSUB)) != 0) 166 REG_EXTENDED | REG_NOSUB)) != 0)
152 { 167 {
153 regerror (result, fstype_regex, buffer, sizeof (buffer)); 168 regerror (result, fstype_regex, buffer, sizeof (buffer));
154 eerrorx ("%s: invalid regex `%s'", argv[0], buffer); 169 eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
155 } 170 }
156 continue; 171 break;
172
173 case 'N':
174 if (node_regex)
175 free (node_regex);
176 node_regex = rc_xmalloc (sizeof (regex_t));
177 if ((result = regcomp (node_regex, optarg,
178 REG_EXTENDED | REG_NOSUB)) != 0)
179 {
180 regerror (result, node_regex, buffer, sizeof (buffer));
181 eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
182 }
183 break;
184
185 case 'R':
186 if (skip_regex)
187 free (skip_regex);
188 skip_regex = rc_xmalloc (sizeof (regex_t));
189 if ((result = regcomp (skip_regex, optarg,
190 REG_EXTENDED | REG_NOSUB)) != 0)
191 {
192 regerror (result, skip_regex, buffer, sizeof (buffer));
193 eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
194 }
195 break;
196
197 case 'f':
198 list_fstype = true;
199 list_nodes = false;
200 break;
201
202 case 'n':
203 list_nodes = true;
204 list_fstype = false;
205 break;
206
207 case 'r':
208 reverse = true;
209 break;
210
211 default:
212 exit (EXIT_FAILURE);
157 } 213 }
158 214
159 if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) { 215 while (optind < argc) {
160 i++;
161 if (node_regex)
162 free (node_regex);
163 node_regex = rc_xmalloc (sizeof (regex_t));
164 if ((result = regcomp (node_regex, argv[i],
165 REG_EXTENDED | REG_NOSUB)) != 0)
166 {
167 regerror (result, node_regex, buffer, sizeof (buffer));
168 eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
169 }
170 continue;
171 }
172
173 if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) {
174 i++;
175 if (skip_regex)
176 free (skip_regex);
177 skip_regex = rc_xmalloc (sizeof (regex_t));
178 if ((result = regcomp (skip_regex, argv[i],
179 REG_EXTENDED | REG_NOSUB)) != 0)
180 {
181 regerror (result, skip_regex, buffer, sizeof (buffer));
182 eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
183 }
184 continue;
185 }
186
187 if (strcmp (argv[i], "--fstype") == 0) {
188 list_fstype = true;
189 continue;
190 }
191
192 if (strcmp (argv[i], "--node") == 0) {
193 list_nodes = true;
194 continue;
195 }
196 if (strcmp (argv[i], "--reverse") == 0) {
197 reverse = true;
198 continue;
199 }
200
201 if (argv[i][0] != '/') 216 if (argv[optind][0] != '/')
202 eerrorx ("%s: `%s' is not a mount point", argv[0], argv[i]); 217 eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]);
203
204 mounts = rc_strlist_add (mounts, argv[i]); 218 mounts = rc_strlist_add (mounts, argv[optind++]);
205 } 219 }
206
207 nodes = find_mounts (node_regex, fstype_regex, mounts, 220 nodes = find_mounts (node_regex, fstype_regex, mounts,
208 list_nodes, list_fstype); 221 list_nodes, list_fstype);
209 222
210 if (node_regex) 223 if (node_regex)
211 regfree (node_regex); 224 regfree (node_regex);

Legend:
Removed from v.2577  
changed lines
  Added in v.2580

  ViewVC Help
Powered by ViewVC 1.1.20