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

Diff of /trunk/src/mountinfo.c

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.20