2 * util.c --- utilities for the debugfs program
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
23 extern int optreset; /* defined by BSD, but not others */
29 * This function resets the libc getopt() function, which keeps
30 * internal state. Bad design! Stupid libc API designers! No
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 dump if optind is set 1 in order to reset getopt().
42 * More modern versions of BSD require that optreset be set to 1 in
43 * order to reset getopt(). Sigh. Standards, anyone?
45 * We hide the hair here.
47 void reset_getopt(void)
49 #if defined(__GLIBC__) || defined(__linux__)
55 optreset = 1; /* Makes BSD getopt happy */
59 static const char *pager_search_list[] = { "pager", "more", "less", 0 };
60 static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 };
62 static const char *find_pager(char *buf)
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)
76 FILE *open_pager(void)
79 const char *pager = getenv("DEBUGFS_PAGER");
82 signal(SIGPIPE, SIG_IGN);
84 pager = getenv("PAGER");
86 pager = find_pager(buf);
88 (strcmp(pager, "__none__") == 0) ||
89 ((outfile = popen(pager, "w")) == 0))
94 void close_pager(FILE *stream)
96 if (stream && stream != stdout) pclose(stream);
100 * This routine is used whenever a command needs to turn a string into
103 ext2_ino_t string_to_inode(char *str)
106 int len = strlen(str);
111 * If the string is of the form <ino>, then treat it as an
114 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
115 ino = strtoul(str+1, &end, 0);
120 retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
122 com_err(str, retval, "");
129 * This routine returns 1 if the filesystem is not open, and prints an
130 * error message to that effect.
132 int check_fs_open(char *name)
135 com_err(name, 0, "Filesystem not open");
142 * This routine returns 1 if a filesystem is open, and prints an
143 * error message to that effect.
145 int check_fs_not_open(char *name)
149 "Filesystem %s is still open. Close it first.\n",
150 current_fs->device_name);
157 * This routine returns 1 if a filesystem is not opened read/write,
158 * and prints an error message to that effect.
160 int check_fs_read_write(char *name)
162 if (!(current_fs->flags & EXT2_FLAG_RW)) {
163 com_err(name, 0, "Filesystem opened read/only");
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
174 int check_fs_bitmaps(char *name)
176 if (!current_fs->block_map || !current_fs->inode_map) {
177 com_err(name, 0, "Filesystem bitmaps not loaded");
184 * This function takes a __u32 time value and converts it to a string,
187 char *time_to_string(__u32 cl)
189 static int do_gmt = -1;
190 time_t t = (time_t) cl;
194 /* The diet libc doesn't respect the TZ environemnt variable */
198 do_gmt = !strcmp(tz, "GMT");
201 return asctime((do_gmt) ? gmtime(&t) : localtime(&t));
205 * Parse a string as a time. Return ((time_t)-1) if the string
206 * doesn't appear to be a sane time.
208 extern time_t string_to_time(const char *arg)
214 if (strcmp(arg, "now") == 0) {
217 memset(&ts, 0, sizeof(ts));
219 strptime(arg, "%Y%m%d%H%M%S", &ts);
221 sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
222 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
225 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
226 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
227 ts.tm_min > 59 || ts.tm_sec > 61)
231 if (ts.tm_mday == 0 || ret == ((time_t) -1)) {
232 /* Try it as an integer... */
233 ret = strtoul(arg, &tmp, 0);
235 return ((time_t) -1);
241 * This function will convert a string to an unsigned long, printing
242 * an error message if it fails, and returning success or failure in err.
244 unsigned long parse_ulong(const char *str, const char *cmd,
245 const char *descr, int *err)
250 ret = strtoul(str, &tmp, 0);
256 com_err(cmd, 0, "Bad %s - %s", descr, str);
265 * This function will convert a string to a block number. It returns
266 * 0 on success, 1 on failure.
268 int strtoblk(const char *cmd, const char *str, blk_t *ret)
273 blk = parse_ulong(str, cmd, "block number", &err);
275 if (err == 0 && blk == 0) {
276 com_err(cmd, 0, "Invalid block number 0");
283 * This is a common helper function used by the command processing
286 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
287 const char *cmd, const char *usage, int flags)
289 if (argc < min_argc || argc > max_argc) {
290 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
293 if (flags & CHECK_FS_NOTOPEN) {
294 if (check_fs_not_open(argv[0]))
297 if (check_fs_open(argv[0]))
300 if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
302 if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
308 * This is a helper function used by do_stat, do_freei, do_seti, and
309 * do_testi, etc. Basically, any command which takes a single
310 * argument which is a file/inode number specifier.
312 int common_inode_args_process(int argc, char *argv[],
313 ext2_ino_t *inode, int flags)
315 if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
318 *inode = string_to_inode(argv[1]);
325 * This is a helper function used by do_freeb, do_setb, and do_testb
327 int common_block_args_process(int argc, char *argv[],
328 blk_t *block, blk_t *count)
332 if (common_args_process(argc, argv, 2, 3, argv[0],
333 "<block> [count]", CHECK_FS_BITMAPS))
336 if (strtoblk(argv[0], argv[1], block))
339 *count = parse_ulong(argv[2], argv[0], "count", &err);
346 int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
347 const char *cmd, int bufsize)
351 retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
353 com_err(cmd, retval, "while reading inode %u", ino);
359 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
364 retval = ext2fs_read_inode(current_fs, ino, inode);
366 com_err(cmd, retval, "while reading inode %u", ino);
372 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
377 retval = ext2fs_write_inode(current_fs, ino, inode);
379 com_err(cmd, retval, "while writing inode %u", ino);
385 int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode,
390 retval = ext2fs_write_new_inode(current_fs, ino, inode);
392 com_err(cmd, retval, "while creating inode %u", ino);