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

Contents of /trunk/src/mountinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (hide annotations) (download) (as text)
Thu Jun 28 15:44:14 2007 UTC (7 years ago) by uberlord
File MIME type: text/x-csrc
File size: 5500 byte(s)
Add --nocolor to more programs
1 uberlord 2547 /*
2     mountinfo.c
3     Obtains information about mounted filesystems.
4    
5     Copyright 2007 Gentoo Foundation
6     */
7    
8 vapier 2618 #define APPLET "mountinfo"
9    
10 uberlord 2547 #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 uberlord 2580 #include <getopt.h>
21 uberlord 2547 #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 uberlord 2580
33 uberlord 2547 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
34     static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
35 uberlord 2577 char **mounts, bool list_nodes, bool list_fstype)
36 uberlord 2547 {
37 uberlord 2577 struct statfs *mnts;
38     int nmnts;
39     int i;
40     char **list = NULL;
41 uberlord 2547
42 uberlord 2577 if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
43     eerrorx ("getmntinfo: %s", strerror (errno));
44 uberlord 2547
45 uberlord 2577 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 uberlord 2547
53 uberlord 2577 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 uberlord 2547
66 uberlord 2577 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 uberlord 2547
72 uberlord 2577 return (list);
73 uberlord 2547 }
74    
75     #elif defined (__linux__)
76     static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
77 uberlord 2577 char **mounts, bool list_nodes, bool list_fstype)
78 uberlord 2547 {
79 uberlord 2577 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 uberlord 2569
87 uberlord 2577 if ((fp = fopen ("/proc/mounts", "r")) == NULL)
88     eerrorx ("getmntinfo: %s", strerror (errno));
89 uberlord 2547
90 uberlord 2577 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 uberlord 2569
97 uberlord 2577 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 uberlord 2547
106 uberlord 2577 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 uberlord 2547
119 uberlord 2577 list = rc_strlist_addsortc (list,
120     list_nodes ?
121     list_fstype ? fstype :
122     from : to);
123     }
124     fclose (fp);
125 uberlord 2547
126 uberlord 2577 return (list);
127 uberlord 2547 }
128    
129     #else
130     # error "Operating system not supported!"
131     #endif
132    
133 uberlord 2734 #include "_usage.h"
134     #define getoptstring "F:N:S:fnrV" getoptstring_COMMON
135 vapier 2603 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 uberlord 2734 longopts_COMMON
143 vapier 2603 { NULL, 0, NULL, 0}
144     };
145 vapier 2618 #include "_usage.c"
146 vapier 2603
147 uberlord 2547 int main (int argc, char **argv)
148     {
149 uberlord 2577 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 uberlord 2698 int opt;
162 uberlord 2547
163 uberlord 2698 while ((opt = getopt_long (argc, argv, getoptstring,
164     longopts, (int *) 0)) != -1)
165     switch (opt) {
166 uberlord 2580 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 uberlord 2547
178 uberlord 2580 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 uberlord 2547
190 uberlord 2581 case 'S':
191 uberlord 2580 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 uberlord 2547
202 uberlord 2580 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 vapier 2618 case_RC_COMMON_GETOPT
217 uberlord 2577 }
218 uberlord 2547
219 uberlord 2580 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 uberlord 2577 }
224     nodes = find_mounts (node_regex, fstype_regex, mounts,
225     list_nodes, list_fstype);
226 uberlord 2547
227 uberlord 2577 if (node_regex)
228     regfree (node_regex);
229     if (fstype_regex)
230     regfree (fstype_regex);
231 uberlord 2547
232 uberlord 2577 if (reverse)
233     rc_strlist_reverse (nodes);
234 uberlord 2547
235 uberlord 2577 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 uberlord 2547
244 uberlord 2577 if (skip_regex)
245     free (skip_regex);
246 uberlord 2547
247 uberlord 2577 exit (result);
248 uberlord 2547 }

  ViewVC Help
Powered by ViewVC 1.1.20