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

Diff of /trunk/src/mountinfo.c

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.20