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

Diff of /trunk/src/fstabinfo.c

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

Revision 2930 Revision 2931
18 Okay, we could use getfsent but the man page says use getmntent instead 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. */ 19 AND we don't have getfsent on uclibc or dietlibc for some odd reason. */
20#ifdef __linux__ 20#ifdef __linux__
21#define HAVE_GETMNTENT 21#define HAVE_GETMNTENT
22#include <mntent.h> 22#include <mntent.h>
23#define START_ENT fp = setmntent ("/etc/fstab", "r");
23#define GET_ENT getmntent (fp) 24#define GET_ENT getmntent (fp)
24#define GET_ENT_FILE(_name) getmntfile (fp, _name) 25#define GET_ENT_FILE(_name) getmntfile (_name)
25#define END_ENT endmntent (fp) 26#define END_ENT endmntent (fp)
26#define ENT_DEVICE(_ent) ent->mnt_fsname 27#define ENT_DEVICE(_ent) ent->mnt_fsname
27#define ENT_FILE(_ent) ent->mnt_dir 28#define ENT_FILE(_ent) ent->mnt_dir
28#define ENT_TYPE(_ent) ent->mnt_type 29#define ENT_TYPE(_ent) ent->mnt_type
29#define ENT_OPTS(_ent) ent->mnt_opts 30#define ENT_OPTS(_ent) ent->mnt_opts
30#define ENT_PASS(_ent) ent->mnt_passno 31#define ENT_PASS(_ent) ent->mnt_passno
31#else 32#else
32#define HAVE_GETFSENT 33#define HAVE_GETFSENT
33#include <fstab.h> 34#include <fstab.h>
35#define START_ENT
34#define GET_ENT getfsent () 36#define GET_ENT getfsent ()
35#define GET_ENT_FILE(_name) getfsfile (_name) 37#define GET_ENT_FILE(_name) getfsfile (_name)
36#define END_ENT endfsent () 38#define END_ENT endfsent ()
37#define ENT_DEVICE(_ent) ent->fs_spec 39#define ENT_DEVICE(_ent) ent->fs_spec
38#define ENT_TYPE(_ent) ent->fs_vfstype 40#define ENT_TYPE(_ent) ent->fs_vfstype
41#define ENT_PASS(_ent) ent->fs_passno 43#define ENT_PASS(_ent) ent->fs_passno
42#endif 44#endif
43 45
44#include "builtins.h" 46#include "builtins.h"
45#include "einfo.h" 47#include "einfo.h"
48#include "rc.h"
49#include "strlist.h"
46 50
47#ifdef HAVE_GETMNTENT 51#ifdef HAVE_GETMNTENT
48static struct mntent *getmntfile (FILE *fp, const char *file) 52static struct mntent *getmntfile (const char *file)
49{ 53{
50 struct mntent *ent; 54 struct mntent *ent = NULL;
55 FILE *fp;
51 56
57 START_ENT;
52 while ((ent = getmntent (fp))) 58 while ((ent = getmntent (fp)))
53 if (strcmp (file, ent->mnt_dir) == 0) 59 if (strcmp (file, ent->mnt_dir) == 0)
60 break;
61 END_ENT;
62
54 return (ent); 63 return (ent);
55
56 return (NULL);
57} 64}
58#endif 65#endif
59 66
60#include "_usage.h" 67#include "_usage.h"
61#define getoptstring "m:o:p:t:" getoptstring_COMMON 68#define getoptstring "m:o:p:t:" getoptstring_COMMON
62static struct option longopts[] = { 69static struct option longopts[] = {
63 { "mountcmd", 1, NULL, 'm'}, 70 { "mountcmd", 0, NULL, 'm'},
64 { "options", 1, NULL, 'o'}, 71 { "options", 0, NULL, 'o'},
65 { "passno", 1, NULL, 'p'}, 72 { "passno", 0, NULL, 'p'},
66 { "fstype", 1, NULL, 't'}, 73 { "fstype", 1, NULL, 't'},
67 longopts_COMMON 74 longopts_COMMON
68 { NULL, 0, NULL, 0} 75 { NULL, 0, NULL, 0}
69}; 76};
70static const char * const longopts_help[] = { 77static const char * const longopts_help[] = {
74 "Extract the file system type", 81 "Extract the file system type",
75 longopts_help_COMMON 82 longopts_help_COMMON
76}; 83};
77#include "_usage.c" 84#include "_usage.c"
78 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
79int fstabinfo (int argc, char **argv) 91int fstabinfo (int argc, char **argv)
80{ 92{
81#ifdef HAVE_GETMNTENT 93#ifdef HAVE_GETMNTENT
82 FILE *fp; 94 FILE *fp;
83 struct mntent *ent; 95 struct mntent *ent;
84#else 96#else
85 struct fstab *ent; 97 struct fstab *ent;
86#endif 98#endif
87 int result = EXIT_FAILURE; 99 int result = EXIT_FAILURE;
88 char *token; 100 char *token;
89 int n = 0; 101 int i;
90 int opt; 102 int opt;
103 int output = OUTPUT_FILE;
104 char **files = NULL;
105 char *file;
106 bool filtered = false;
91 107
92 while ((opt = getopt_long (argc, argv, getoptstring, 108 while ((opt = getopt_long (argc, argv, getoptstring,
93 longopts, (int *) 0)) != -1) 109 longopts, (int *) 0)) != -1)
94 { 110 {
95#ifdef HAVE_GETMNTENT
96 fp = setmntent ("/etc/fstab", "r");
97#endif
98 switch (opt) { 111 switch (opt) {
99 case 'm': 112 case 'm':
100 if ((ent = GET_ENT_FILE (optarg))) { 113 output = OUTPUT_MOUNTCMD;
101 printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
102 ENT_DEVICE (ent), ENT_FILE (ent));
103 result = EXIT_SUCCESS;
104 }
105 break; 114 break;
106 115
107 case 'o': 116 case 'o':
108 if ((ent = GET_ENT_FILE (optarg))) { 117 output = OUTPUT_OPTIONS;
109 printf ("%s\n", ENT_OPTS (ent));
110 result = EXIT_SUCCESS;
111 }
112 break; 118 break;
113 119
114 case 'p': 120 case 'p':
115 switch (optarg[0]) { 121 switch (optarg[0]) {
116 case '=': 122 case '=':
117 case '<': 123 case '<':
118 case '>': 124 case '>':
119 if (sscanf (optarg + 1, "%d", &n) != 1) 125 if (sscanf (optarg + 1, "%d", &i) != 1)
120 eerrorx ("%s: invalid passno %s", argv[0], optarg + 1); 126 eerrorx ("%s: invalid passno %s", argv[0], optarg + 1);
121 127
128 filtered = true;
129 START_ENT;
122 while ((ent = GET_ENT)) { 130 while ((ent = GET_ENT)) {
123 if (((optarg[0] == '=' && n == ENT_PASS (ent)) || 131 if (((optarg[0] == '=' && i == ENT_PASS (ent)) ||
124 (optarg[0] == '<' && n > ENT_PASS (ent)) || 132 (optarg[0] == '<' && i > ENT_PASS (ent)) ||
125 (optarg[0] == '>' && n < ENT_PASS (ent))) && 133 (optarg[0] == '>' && i < ENT_PASS (ent))) &&
126 strcmp (ENT_FILE (ent), "none") != 0) 134 strcmp (ENT_FILE (ent), "none") != 0)
127 { 135 rc_strlist_add (&files, ENT_FILE (ent));
128 printf ("%s\n", ENT_FILE (ent));
129 result = EXIT_SUCCESS;
130 }
131 } 136 }
137 END_ENT;
132 break; 138 break;
133 139
134 default: 140 default:
135 if ((ent = GET_ENT_FILE (optarg))) { 141 rc_strlist_add (&files, optarg);
136 printf ("%d\n", ENT_PASS (ent)); 142 output = OUTPUT_PASSNO;
137 result = EXIT_SUCCESS;
138 }
139 break; 143 break;
140 } 144 }
141 break; 145 break;
142 146
143 case 't': 147 case 't':
148 filtered = true;
144 while ((token = strsep (&optarg, ","))) 149 while ((token = strsep (&optarg, ","))) {
150 START_ENT;
145 while ((ent = GET_ENT)) 151 while ((ent = GET_ENT))
146 if (strcmp (token, ENT_TYPE (ent)) == 0) 152 if (strcmp (token, ENT_TYPE (ent)) == 0)
147 printf ("%s\n", ENT_FILE (ent)); 153 rc_strlist_add (&files, ENT_FILE (ent));
148 result = EXIT_SUCCESS; 154 END_ENT;
155 }
149 break; 156 break;
150 157
151 case_RC_COMMON_GETOPT 158 case_RC_COMMON_GETOPT
152 } 159 }
160 }
153 161
162 while (optind < argc)
163 rc_strlist_add (&files, argv[optind++]);
164
165 if (! files) {
166 if (filtered) {
167 if (! rc_is_env ("RC_QUIET", "yes"))
168 eerror ("%s: no matches found", argv[0]);
169 exit (EXIT_FAILURE);
170 }
171
172 START_ENT;
173 while ((ent = GET_ENT))
174 rc_strlist_add (&files, ENT_FILE (ent));
154 END_ENT; 175 END_ENT;
155 176
156 if (result != EXIT_SUCCESS) 177 if (! files)
157 break; 178 eerrorx ("%s: emtpy fstab", argv[0]);
158 } 179 }
159 180
160 if (result != EXIT_SUCCESS && argc == optind) 181 /* Ensure we always display something */
161 eerrorx ("%s: no arguments specified", argv[0]); 182 START_ENT;
183 STRLIST_FOREACH (files, file, i) {
184 if (! (ent = GET_ENT_FILE (file))) {
185 if (! rc_is_env ("RC_QUIET", "yes"))
186 eerror ("%s: no such entry `%s'", argv[0], file);
187 result = EXIT_FAILURE;
188 continue;
189 }
162 190
191 switch (output) {
192 case OUTPUT_MOUNTCMD:
193 printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
194 ENT_DEVICE (ent), ENT_FILE (ent));
195 break;
196
197 case OUTPUT_OPTIONS:
198 printf ("%s\n", ENT_OPTS (ent));
199 break;
200
201 case OUTPUT_FILE:
202 printf ("%s\n", file);
203 break;
204
205 case OUTPUT_PASSNO:
206 printf ("%d\n", ENT_PASS (ent));
207 break;
208 }
209 }
210 END_ENT;
211
212 rc_strlist_free (files);
163 exit (result); 213 exit (result);
164} 214}

Legend:
Removed from v.2930  
changed lines
  Added in v.2931

  ViewVC Help
Powered by ViewVC 1.1.20