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 time_t t = (time_t) cl;
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.
198 unsigned long parse_ulong(const char *str, const char *cmd,
199 const char *descr, int *err)
204 ret = strtoul(str, &tmp, 0);
210 com_err(cmd, 0, "Bad %s - %s", descr, str);
219 * This function will convert a string to a block number. It returns
220 * 0 on success, 1 on failure.
222 int strtoblk(const char *cmd, const char *str, blk_t *ret)
227 blk = parse_ulong(str, cmd, "block number", &err);
229 if (err == 0 && blk == 0) {
230 com_err(cmd, 0, "Invalid block number 0");
237 * This is a common helper function used by the command processing
240 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
241 const char *cmd, const char *usage, int flags)
243 if (argc < min_argc || argc > max_argc) {
244 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
247 if (flags & CHECK_FS_NOTOPEN) {
248 if (check_fs_not_open(argv[0]))
251 if (check_fs_open(argv[0]))
254 if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
256 if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
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.
266 int common_inode_args_process(int argc, char *argv[],
267 ext2_ino_t *inode, int flags)
269 if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
272 *inode = string_to_inode(argv[1]);
279 * This is a helper function used by do_freeb, do_setb, and do_testb
281 int common_block_args_process(int argc, char *argv[],
282 blk_t *block, int *count)
286 if (common_args_process(argc, argv, 2, 3, argv[0],
287 "<block> [count]", CHECK_FS_BITMAPS))
290 if (strtoblk(argv[0], argv[1], block))
293 *count = parse_ulong(argv[2], argv[0], "count", &err);
300 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
305 retval = ext2fs_read_inode(current_fs, ino, inode);
307 com_err(cmd, retval, "while reading inode %u", ino);
313 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
318 retval = ext2fs_write_inode(current_fs, ino, inode);
320 com_err(cmd, retval, "while writing inode %u", ino);