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 FILE *open_pager(void)
62 const char *pager = getenv("PAGER");
64 signal(SIGPIPE, SIG_IGN);
66 if (strcmp(pager, "__none__") == 0) {
72 outfile = popen(pager, "w");
74 return (outfile ? outfile : stdout);
77 void close_pager(FILE *stream)
79 if (stream && stream != stdout) pclose(stream);
83 * This routine is used whenever a command needs to turn a string into
86 ext2_ino_t string_to_inode(char *str)
89 int len = strlen(str);
94 * If the string is of the form <ino>, then treat it as an
97 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
98 ino = strtoul(str+1, &end, 0);
103 retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
105 com_err(str, retval, "");
112 * This routine returns 1 if the filesystem is not open, and prints an
113 * error message to that effect.
115 int check_fs_open(char *name)
118 com_err(name, 0, "Filesystem not open");
125 * This routine returns 1 if a filesystem is open, and prints an
126 * error message to that effect.
128 int check_fs_not_open(char *name)
132 "Filesystem %s is still open. Close it first.\n",
133 current_fs->device_name);
140 * This routine returns 1 if a filesystem is not opened read/write,
141 * and prints an error message to that effect.
143 int check_fs_read_write(char *name)
145 if (!(current_fs->flags & EXT2_FLAG_RW)) {
146 com_err(name, 0, "Filesystem opened read/only");
153 * This routine returns 1 if a filesystem is doesn't have its inode
154 * and block bitmaps loaded, and prints an error message to that
157 int check_fs_bitmaps(char *name)
159 if (!current_fs->block_map || !current_fs->inode_map) {
160 com_err(name, 0, "Filesystem bitmaps not loaded");
167 * This function takes a __u32 time value and converts it to a string,
170 char *time_to_string(__u32 cl)
172 time_t t = (time_t) cl;
178 * This function will convert a string to an unsigned long, printing
179 * an error message if it fails, and returning success or failure in err.
181 unsigned long parse_ulong(const char *str, const char *cmd,
182 const char *descr, int *err)
187 ret = strtoul(str, &tmp, 0);
193 com_err(cmd, 0, "Bad %s - %s", descr, str);
202 * This function will convert a string to a block number. It returns
203 * 0 on success, 1 on failure.
205 int strtoblk(const char *cmd, const char *str, blk_t *ret)
210 blk = parse_ulong(str, cmd, "block number", &err);
212 if (err == 0 && blk == 0) {
213 com_err(cmd, 0, "Invalid block number 0");
220 * This is a common helper function used by the command processing
223 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
224 const char *cmd, const char *usage, int flags)
226 if (argc < min_argc || argc > max_argc) {
227 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
230 if (flags & CHECK_FS_NOTOPEN) {
231 if (check_fs_not_open(argv[0]))
234 if (check_fs_open(argv[0]))
237 if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
239 if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
245 * This is a helper function used by do_stat, do_freei, do_seti, and
246 * do_testi, etc. Basically, any command which takes a single
247 * argument which is a file/inode number specifier.
249 int common_inode_args_process(int argc, char *argv[],
250 ext2_ino_t *inode, int flags)
252 if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
255 *inode = string_to_inode(argv[1]);
262 * This is a helper function used by do_freeb, do_setb, and do_testb
264 int common_block_args_process(int argc, char *argv[],
265 blk_t *block, int *count)
269 if (common_args_process(argc, argv, 2, 3, argv[0],
270 "<block> [count]", CHECK_FS_BITMAPS))
273 if (strtoblk(argv[0], argv[1], block))
276 *count = parse_ulong(argv[2], argv[0], "count", &err);
283 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
288 retval = ext2fs_read_inode(current_fs, ino, inode);
290 com_err(cmd, retval, "while reading inode %u", ino);
296 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
301 retval = ext2fs_write_inode(current_fs, ino, inode);
303 com_err(cmd, retval, "while writing inode %u", ino);