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

Contents of /trunk/src/fstabinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2932 - (show annotations) (download) (as text)
Tue Sep 25 22:45:57 2007 UTC (6 years, 9 months ago) by uberlord
File MIME type: text/x-csrc
File size: 4637 byte(s)
fstabinto now behaves more like mountinfo with respect to --quiet
1 /*
2 fstabinfo.c
3 Gets information about /etc/fstab.
4
5 Copyright 2007 Gentoo Foundation
6 */
7
8 #define APPLET "fstabinfo"
9
10 #include <errno.h>
11 #include <getopt.h>
12 #include <libgen.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 /* Yay for linux and it's non liking of POSIX functions.
18 Okay, we could use getfsent but the man page says use getmntent instead
19 AND we don't have getfsent on uclibc or dietlibc for some odd reason. */
20 #ifdef __linux__
21 #define HAVE_GETMNTENT
22 #include <mntent.h>
23 #define START_ENT fp = setmntent ("/etc/fstab", "r");
24 #define GET_ENT getmntent (fp)
25 #define GET_ENT_FILE(_name) getmntfile (_name)
26 #define END_ENT endmntent (fp)
27 #define ENT_DEVICE(_ent) ent->mnt_fsname
28 #define ENT_FILE(_ent) ent->mnt_dir
29 #define ENT_TYPE(_ent) ent->mnt_type
30 #define ENT_OPTS(_ent) ent->mnt_opts
31 #define ENT_PASS(_ent) ent->mnt_passno
32 #else
33 #define HAVE_GETFSENT
34 #include <fstab.h>
35 #define START_ENT
36 #define GET_ENT getfsent ()
37 #define GET_ENT_FILE(_name) getfsfile (_name)
38 #define END_ENT endfsent ()
39 #define ENT_DEVICE(_ent) ent->fs_spec
40 #define ENT_TYPE(_ent) ent->fs_vfstype
41 #define ENT_FILE(_ent) ent->fs_file
42 #define ENT_OPTS(_ent) ent->fs_mntops
43 #define ENT_PASS(_ent) ent->fs_passno
44 #endif
45
46 #include "builtins.h"
47 #include "einfo.h"
48 #include "rc.h"
49 #include "strlist.h"
50
51 #ifdef HAVE_GETMNTENT
52 static struct mntent *getmntfile (const char *file)
53 {
54 struct mntent *ent = NULL;
55 FILE *fp;
56
57 START_ENT;
58 while ((ent = getmntent (fp)))
59 if (strcmp (file, ent->mnt_dir) == 0)
60 break;
61 END_ENT;
62
63 return (ent);
64 }
65 #endif
66
67 #include "_usage.h"
68 #define getoptstring "m:o:p:t:" getoptstring_COMMON
69 static struct option longopts[] = {
70 { "mountcmd", 0, NULL, 'm'},
71 { "options", 0, NULL, 'o'},
72 { "passno", 0, NULL, 'p'},
73 { "fstype", 1, NULL, 't'},
74 longopts_COMMON
75 { NULL, 0, NULL, 0}
76 };
77 static const char * const longopts_help[] = {
78 "Construct the arguments to give to mount",
79 "Extract the options field",
80 "Extract the pass number field",
81 "Extract the file system type",
82 longopts_help_COMMON
83 };
84 #include "_usage.c"
85
86 #define OUTPUT_FILE (1 << 1)
87 #define OUTPUT_MOUNTCMD (1 << 2)
88 #define OUTPUT_OPTIONS (1 << 3)
89 #define OUTPUT_PASSNO (1 << 4)
90
91 int fstabinfo (int argc, char **argv)
92 {
93 #ifdef HAVE_GETMNTENT
94 FILE *fp;
95 struct mntent *ent;
96 #else
97 struct fstab *ent;
98 #endif
99 int result = EXIT_SUCCESS;
100 char *token;
101 int i;
102 int opt;
103 int output = OUTPUT_FILE;
104 char **files = NULL;
105 char *file;
106 bool filtered = false;
107
108 while ((opt = getopt_long (argc, argv, getoptstring,
109 longopts, (int *) 0)) != -1)
110 {
111 switch (opt) {
112 case 'm':
113 output = OUTPUT_MOUNTCMD;
114 break;
115
116 case 'o':
117 output = OUTPUT_OPTIONS;
118 break;
119
120 case 'p':
121 switch (optarg[0]) {
122 case '=':
123 case '<':
124 case '>':
125 if (sscanf (optarg + 1, "%d", &i) != 1)
126 eerrorx ("%s: invalid passno %s", argv[0], optarg + 1);
127
128 filtered = true;
129 START_ENT;
130 while ((ent = GET_ENT)) {
131 if (((optarg[0] == '=' && i == ENT_PASS (ent)) ||
132 (optarg[0] == '<' && i > ENT_PASS (ent)) ||
133 (optarg[0] == '>' && i < ENT_PASS (ent))) &&
134 strcmp (ENT_FILE (ent), "none") != 0)
135 rc_strlist_add (&files, ENT_FILE (ent));
136 }
137 END_ENT;
138 break;
139
140 default:
141 rc_strlist_add (&files, optarg);
142 output = OUTPUT_PASSNO;
143 break;
144 }
145 break;
146
147 case 't':
148 filtered = true;
149 while ((token = strsep (&optarg, ","))) {
150 START_ENT;
151 while ((ent = GET_ENT))
152 if (strcmp (token, ENT_TYPE (ent)) == 0)
153 rc_strlist_add (&files, ENT_FILE (ent));
154 END_ENT;
155 }
156 break;
157
158 case_RC_COMMON_GETOPT
159 }
160 }
161
162 while (optind < argc)
163 rc_strlist_add (&files, argv[optind++]);
164
165 if (! files && ! filtered) {
166 START_ENT;
167 while ((ent = GET_ENT))
168 rc_strlist_add (&files, ENT_FILE (ent));
169 END_ENT;
170
171 if (! files)
172 eerrorx ("%s: emtpy fstab", argv[0]);
173 }
174
175 /* Ensure we always display something */
176 START_ENT;
177 STRLIST_FOREACH (files, file, i) {
178 if (! (ent = GET_ENT_FILE (file))) {
179 result = EXIT_FAILURE;
180 continue;
181 }
182
183 /* No point in outputting if quiet */
184 if (rc_is_env ("RC_QUIET", "yes"))
185 continue;
186
187 switch (output) {
188 case OUTPUT_MOUNTCMD:
189 printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
190 ENT_DEVICE (ent), ENT_FILE (ent));
191 break;
192
193 case OUTPUT_OPTIONS:
194 printf ("%s\n", ENT_OPTS (ent));
195 break;
196
197 case OUTPUT_FILE:
198 printf ("%s\n", file);
199 break;
200
201 case OUTPUT_PASSNO:
202 printf ("%d\n", ENT_PASS (ent));
203 break;
204 }
205 }
206 END_ENT;
207
208 rc_strlist_free (files);
209 exit (result);
210 }

  ViewVC Help
Powered by ViewVC 1.1.20