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 ump 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)
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 * This function will convert a string to an unsigned long, printing
206 * an error message if it fails, and returning success or failure in err.
208 unsigned long parse_ulong(const char *str, const char *cmd,
209 const char *descr, int *err)
214 ret = strtoul(str, &tmp, 0);
220 com_err(cmd, 0, "Bad %s - %s", descr, str);
229 * This function will convert a string to a block number. It returns
230 * 0 on success, 1 on failure.
232 int strtoblk(const char *cmd, const char *str, blk_t *ret)
237 blk = parse_ulong(str, cmd, "block number", &err);
239 if (err == 0 && blk == 0) {
240 com_err(cmd, 0, "Invalid block number 0");
247 * This is a common helper function used by the command processing
250 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
251 const char *cmd, const char *usage, int flags)
253 if (argc < min_argc || argc > max_argc) {
254 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
257 if (flags & CHECK_FS_NOTOPEN) {
258 if (check_fs_not_open(argv[0]))
261 if (check_fs_open(argv[0]))
264 if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
266 if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
272 * This is a helper function used by do_stat, do_freei, do_seti, and
273 * do_testi, etc. Basically, any command which takes a single
274 * argument which is a file/inode number specifier.
276 int common_inode_args_process(int argc, char *argv[],
277 ext2_ino_t *inode, int flags)
279 if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
282 *inode = string_to_inode(argv[1]);
289 * This is a helper function used by do_freeb, do_setb, and do_testb
291 int common_block_args_process(int argc, char *argv[],
292 blk_t *block, int *count)
296 if (common_args_process(argc, argv, 2, 3, argv[0],
297 "<block> [count]", CHECK_FS_BITMAPS))
300 if (strtoblk(argv[0], argv[1], block))
303 *count = parse_ulong(argv[2], argv[0], "count", &err);
310 int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
311 const char *cmd, int bufsize)
315 retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
317 com_err(cmd, retval, "while reading inode %u", ino);
323 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
328 retval = ext2fs_read_inode(current_fs, ino, inode);
330 com_err(cmd, retval, "while reading inode %u", ino);
336 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
341 retval = ext2fs_write_inode(current_fs, ino, inode);
343 com_err(cmd, retval, "while writing inode %u", ino);