Whamcloud - gitweb
util.c (open_pager): Use DEBUGFS_PAGER in preference to PAGER
[tools/e2fsprogs.git] / debugfs / util.c
1 /*
2  * util.c --- utilities for the debugfs program
3  * 
4  * Copyright (C) 1993, 1994 Theodore Ts'o.  This file may be
5  * redistributed under the terms of the GNU Public License.
6  *
7  */
8
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <time.h>
15 #include <signal.h>
16 #ifdef HAVE_GETOPT_H
17 #include <getopt.h>
18 #else 
19 extern int optind;
20 extern char *optarg;
21 #endif
22 #ifdef HAVE_OPTRESET
23 extern int optreset;            /* defined by BSD, but not others */
24 #endif
25
26 #include "debugfs.h"
27
28 /*
29  * This function resets the libc getopt() function, which keeps
30  * internal state.  Bad design!  Stupid libc API designers!  No
31  * biscuit!
32  *
33  * BSD-derived getopt() functions require that optind be reset to 1 in
34  * order to reset getopt() state.  This used to be generally accepted
35  * way of resetting getopt().  However, glibc's getopt()
36  * has additional getopt() state beyond optind, and requires that
37  * optind be set zero to reset its state.  So the unfortunate state of
38  * affairs is that BSD-derived versions of getopt() misbehave if
39  * optind is set to 0 in order to reset getopt(), and glibc's getopt()
40  * will core ump if optind is set 1 in order to reset getopt().
41  * 
42  * More modern versions of BSD require that optreset be set to 1 in
43  * order to reset getopt().   Sigh.  Standards, anyone?
44  *
45  * We hide the hair here.
46  */
47 void reset_getopt(void)
48 {
49 #ifdef __GLIBC__
50         optind = 0;
51 #else
52         optind = 1;
53 #endif
54 #ifdef HAVE_OPTRESET
55         optreset = 1;           /* Makes BSD getopt happy */
56 #endif
57 }
58
59 static const char *pager_search_list[] = { "pager", "more", "less", 0 };
60 static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 };
61
62 static const char *find_pager(char *buf)
63 {
64         const char **i, **j;
65
66         for (i = pager_search_list; *i; i++) {
67                 for (j = pager_dir_list; *j; j++) {
68                         sprintf(buf, "%s/%s", *j, *i);
69                         if (access(buf, X_OK) == 0)
70                                 return(buf);
71                 }
72         }
73         return 0;
74 }
75
76 FILE *open_pager(void)
77 {
78         FILE *outfile = 0;
79         const char *pager = getenv("DEBUGFS_PAGER");
80         char buf[80];
81
82         signal(SIGPIPE, SIG_IGN);
83         if (!pager)
84                 pager = getenv("PAGER");
85         if (!pager)
86                 pager = find_pager(buf);
87         if (!pager || 
88             (strcmp(pager, "__none__") == 0) ||
89             ((outfile = popen(pager, "w")) == 0))
90                 return stdout;
91         return outfile;
92 }
93
94 void close_pager(FILE *stream)
95 {
96         if (stream && stream != stdout) pclose(stream);
97 }
98
99 /*
100  * This routine is used whenever a command needs to turn a string into
101  * an inode.
102  */
103 ext2_ino_t string_to_inode(char *str)
104 {
105         ext2_ino_t      ino;
106         int             len = strlen(str);
107         char            *end;
108         int             retval;
109
110         /*
111          * If the string is of the form <ino>, then treat it as an
112          * inode number.
113          */
114         if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
115                 ino = strtoul(str+1, &end, 0);
116                 if (*end=='>')
117                         return ino;
118         }
119
120         retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
121         if (retval) {
122                 com_err(str, retval, "");
123                 return 0;
124         }
125         return ino;
126 }
127
128 /*
129  * This routine returns 1 if the filesystem is not open, and prints an
130  * error message to that effect.
131  */
132 int check_fs_open(char *name)
133 {
134         if (!current_fs) {
135                 com_err(name, 0, "Filesystem not open");
136                 return 1;
137         }
138         return 0;
139 }
140
141 /*
142  * This routine returns 1 if a filesystem is open, and prints an
143  * error message to that effect.
144  */
145 int check_fs_not_open(char *name)
146 {
147         if (current_fs) {
148                 com_err(name, 0,
149                         "Filesystem %s is still open.  Close it first.\n",
150                         current_fs->device_name);
151                 return 1;
152         }
153         return 0;
154 }
155
156 /*
157  * This routine returns 1 if a filesystem is not opened read/write,
158  * and prints an error message to that effect.
159  */
160 int check_fs_read_write(char *name)
161 {
162         if (!(current_fs->flags & EXT2_FLAG_RW)) {
163                 com_err(name, 0, "Filesystem opened read/only");
164                 return 1;
165         }
166         return 0;
167 }
168
169 /*
170  * This routine returns 1 if a filesystem is doesn't have its inode
171  * and block bitmaps loaded, and prints an error message to that
172  * effect.
173  */
174 int check_fs_bitmaps(char *name)
175 {
176         if (!current_fs->block_map || !current_fs->inode_map) {
177                 com_err(name, 0, "Filesystem bitmaps not loaded");
178                 return 1;
179         }
180         return 0;
181 }
182
183 /*
184  * This function takes a __u32 time value and converts it to a string,
185  * using ctime
186  */
187 char *time_to_string(__u32 cl)
188 {
189         time_t  t = (time_t) cl;
190
191         return ctime(&t);
192 }
193
194 /*
195  * This function will convert a string to an unsigned long, printing
196  * an error message if it fails, and returning success or failure in err.
197  */
198 unsigned long parse_ulong(const char *str, const char *cmd,
199                           const char *descr, int *err)
200 {
201         char            *tmp;
202         unsigned long   ret;
203         
204         ret = strtoul(str, &tmp, 0);
205         if (*tmp == 0) {
206                 if (err)
207                         *err = 0;
208                 return ret;
209         }
210         com_err(cmd, 0, "Bad %s - %s", descr, str);
211         if (err)
212                 *err = 1;
213         else
214                 exit(1);
215         return 0;
216 }
217
218 /*
219  * This function will convert a string to a block number.  It returns
220  * 0 on success, 1 on failure.
221  */
222 int strtoblk(const char *cmd, const char *str, blk_t *ret)
223 {
224         blk_t   blk;
225         int     err;
226
227         blk = parse_ulong(str, cmd, "block number", &err);
228         *ret = blk;
229         if (err == 0 && blk == 0) {
230                 com_err(cmd, 0, "Invalid block number 0");
231                 err = 1;
232         }
233         return err;
234 }
235
236 /*
237  * This is a common helper function used by the command processing
238  * routines
239  */
240 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
241                         const char *cmd, const char *usage, int flags)
242 {
243         if (argc < min_argc || argc > max_argc) {
244                 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
245                 return 1;
246         }
247         if (flags & CHECK_FS_NOTOPEN) {
248                 if (check_fs_not_open(argv[0]))
249                         return 1;
250         } else {
251                 if (check_fs_open(argv[0]))
252                         return 1;
253         }
254         if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
255                 return 1;
256         if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
257                 return 1;
258         return 0;
259 }
260
261 /*
262  * This is a helper function used by do_stat, do_freei, do_seti, and
263  * do_testi, etc.  Basically, any command which takes a single
264  * argument which is a file/inode number specifier.
265  */
266 int common_inode_args_process(int argc, char *argv[],
267                               ext2_ino_t *inode, int flags)
268 {
269         if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
270                 return 1;
271         
272         *inode = string_to_inode(argv[1]);
273         if (!*inode) 
274                 return 1;
275         return 0;
276 }
277
278 /*
279  * This is a helper function used by do_freeb, do_setb, and do_testb
280  */
281 int common_block_args_process(int argc, char *argv[],
282                               blk_t *block, int *count)
283 {
284         int     err;
285
286         if (common_args_process(argc, argv, 2, 3, argv[0],
287                                 "<block> [count]", CHECK_FS_BITMAPS))
288                 return 1;
289
290         if (strtoblk(argv[0], argv[1], block))
291                 return 1;
292         if (argc > 2) {
293                 *count = parse_ulong(argv[2], argv[0], "count", &err);
294                 if (err)
295                         return 1;
296         }
297         return 0;
298 }
299
300 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
301                         const char *cmd)
302 {
303         int retval;
304
305         retval = ext2fs_read_inode(current_fs, ino, inode);
306         if (retval) {
307                 com_err(cmd, retval, "while reading inode %u", ino);
308                 return 1;
309         }
310         return 0;
311 }
312
313 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
314                         const char *cmd)
315 {
316         int retval;
317
318         retval = ext2fs_write_inode(current_fs, ino, inode);
319         if (retval) {
320                 com_err(cmd, retval, "while writing inode %u", ino);
321                 return 1;
322         }
323         return 0;
324 }
325