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#define _XOPEN_SOURCE /* needed for strptime */
10
11#include <stdio.h>
12#include <unistd.h>
13#include <stdlib.h>
14#include <ctype.h>
15#include <string.h>
16#include <time.h>
17#include <signal.h>
18#ifdef HAVE_GETOPT_H
19#include <getopt.h>
20#else
21extern int optind;
22extern char *optarg;
23#endif
24#ifdef HAVE_OPTRESET
25extern int optreset;		/* defined by BSD, but not others */
26#endif
27
28#include "debugfs.h"
29
30/*
31 * This function resets the libc getopt() function, which keeps
32 * internal state.  Bad design!  Stupid libc API designers!  No
33 * biscuit!
34 *
35 * BSD-derived getopt() functions require that optind be reset to 1 in
36 * order to reset getopt() state.  This used to be generally accepted
37 * way of resetting getopt().  However, glibc's getopt()
38 * has additional getopt() state beyond optind, and requires that
39 * optind be set zero to reset its state.  So the unfortunate state of
40 * affairs is that BSD-derived versions of getopt() misbehave if
41 * optind is set to 0 in order to reset getopt(), and glibc's getopt()
42 * will core dump if optind is set 1 in order to reset getopt().
43 *
44 * More modern versions of BSD require that optreset be set to 1 in
45 * order to reset getopt().   Sigh.  Standards, anyone?
46 *
47 * We hide the hair here.
48 */
49void reset_getopt(void)
50{
51#if defined(__GLIBC__) || defined(__linux__)
52	optind = 0;
53#else
54	optind = 1;
55#endif
56#ifdef HAVE_OPTRESET
57	optreset = 1;		/* Makes BSD getopt happy */
58#endif
59}
60
61static const char *pager_search_list[] = { "pager", "more", "less", 0 };
62static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 };
63
64static const char *find_pager(char *buf)
65{
66	const char **i, **j;
67
68	for (i = pager_search_list; *i; i++) {
69		for (j = pager_dir_list; *j; j++) {
70			sprintf(buf, "%s/%s", *j, *i);
71			if (access(buf, X_OK) == 0)
72				return(buf);
73		}
74	}
75	return 0;
76}
77
78FILE *open_pager(void)
79{
80	FILE *outfile = 0;
81	const char *pager = getenv("DEBUGFS_PAGER");
82	char buf[80];
83
84	signal(SIGPIPE, SIG_IGN);
85	if (!pager)
86		pager = getenv("PAGER");
87	if (!pager)
88		pager = find_pager(buf);
89	if (!pager ||
90	    (strcmp(pager, "__none__") == 0) ||
91	    ((outfile = popen(pager, "w")) == 0))
92		return stdout;
93	return outfile;
94}
95
96void close_pager(FILE *stream)
97{
98	if (stream && stream != stdout) pclose(stream);
99}
100
101/*
102 * This routine is used whenever a command needs to turn a string into
103 * an inode.
104 */
105ext2_ino_t string_to_inode(char *str)
106{
107	ext2_ino_t	ino;
108	int		len = strlen(str);
109	char		*end;
110	int		retval;
111
112	/*
113	 * If the string is of the form <ino>, then treat it as an
114	 * inode number.
115	 */
116	if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
117		ino = strtoul(str+1, &end, 0);
118		if (*end=='>')
119			return ino;
120	}
121
122	retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
123	if (retval) {
124		com_err(str, retval, "");
125		return 0;
126	}
127	return ino;
128}
129
130/*
131 * This routine returns 1 if the filesystem is not open, and prints an
132 * error message to that effect.
133 */
134int check_fs_open(char *name)
135{
136	if (!current_fs) {
137		com_err(name, 0, "Filesystem not open");
138		return 1;
139	}
140	return 0;
141}
142
143/*
144 * This routine returns 1 if a filesystem is open, and prints an
145 * error message to that effect.
146 */
147int check_fs_not_open(char *name)
148{
149	if (current_fs) {
150		com_err(name, 0,
151			"Filesystem %s is still open.  Close it first.\n",
152			current_fs->device_name);
153		return 1;
154	}
155	return 0;
156}
157
158/*
159 * This routine returns 1 if a filesystem is not opened read/write,
160 * and prints an error message to that effect.
161 */
162int check_fs_read_write(char *name)
163{
164	if (!(current_fs->flags & EXT2_FLAG_RW)) {
165		com_err(name, 0, "Filesystem opened read/only");
166		return 1;
167	}
168	return 0;
169}
170
171/*
172 * This routine returns 1 if a filesystem is doesn't have its inode
173 * and block bitmaps loaded, and prints an error message to that
174 * effect.
175 */
176int check_fs_bitmaps(char *name)
177{
178	if (!current_fs->block_map || !current_fs->inode_map) {
179		com_err(name, 0, "Filesystem bitmaps not loaded");
180		return 1;
181	}
182	return 0;
183}
184
185/*
186 * This function takes a __u32 time value and converts it to a string,
187 * using ctime
188 */
189char *time_to_string(__u32 cl)
190{
191	static int	do_gmt = -1;
192	time_t		t = (time_t) cl;
193	char *		tz;
194
195	if (do_gmt == -1) {
196		/* The diet libc doesn't respect the TZ environemnt variable */
197		tz = getenv("TZ");
198		if (!tz)
199			tz = "";
200		do_gmt = !strcmp(tz, "GMT");
201	}
202
203	return asctime((do_gmt) ? gmtime(&t) : localtime(&t));
204}
205
206/*
207 * Parse a string as a time.  Return ((time_t)-1) if the string
208 * doesn't appear to be a sane time.
209 */
210extern time_t string_to_time(const char *arg)
211{
212	struct	tm	ts;
213	time_t		ret;
214	char *tmp;
215
216	if (strcmp(arg, "now") == 0) {
217		return time(0);
218	}
219	memset(&ts, 0, sizeof(ts));
220#ifdef HAVE_STRPTIME
221	strptime(arg, "%Y%m%d%H%M%S", &ts);
222#else
223	sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
224	       &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
225	ts.tm_year -= 1900;
226	ts.tm_mon -= 1;
227	if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
228	    ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
229	    ts.tm_min > 59 || ts.tm_sec > 61)
230		ts.tm_mday = 0;
231#endif
232	ret = mktime(&ts);
233	if (ts.tm_mday == 0 || ret == ((time_t) -1)) {
234		/* Try it as an integer... */
235		ret = strtoul(arg, &tmp, 0);
236		if (*tmp)
237			return ((time_t) -1);
238	}
239	return ret;
240}
241
242/*
243 * This function will convert a string to an unsigned long, printing
244 * an error message if it fails, and returning success or failure in err.
245 */
246unsigned long parse_ulong(const char *str, const char *cmd,
247			  const char *descr, int *err)
248{
249	char		*tmp;
250	unsigned long	ret;
251
252	ret = strtoul(str, &tmp, 0);
253	if (*tmp == 0) {
254		if (err)
255			*err = 0;
256		return ret;
257	}
258	com_err(cmd, 0, "Bad %s - %s", descr, str);
259	if (err)
260		*err = 1;
261	else
262		exit(1);
263	return 0;
264}
265
266/*
267 * This function will convert a string to a block number.  It returns
268 * 0 on success, 1 on failure.
269 */
270int strtoblk(const char *cmd, const char *str, blk_t *ret)
271{
272	blk_t	blk;
273	int	err;
274
275	blk = parse_ulong(str, cmd, "block number", &err);
276	*ret = blk;
277	if (err == 0 && blk == 0) {
278		com_err(cmd, 0, "Invalid block number 0");
279		err = 1;
280	}
281	return err;
282}
283
284/*
285 * This is a common helper function used by the command processing
286 * routines
287 */
288int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
289			const char *cmd, const char *usage, int flags)
290{
291	if (argc < min_argc || argc > max_argc) {
292		com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
293		return 1;
294	}
295	if (flags & CHECK_FS_NOTOPEN) {
296		if (check_fs_not_open(argv[0]))
297			return 1;
298	} else {
299		if (check_fs_open(argv[0]))
300			return 1;
301	}
302	if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
303		return 1;
304	if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
305		return 1;
306	return 0;
307}
308
309/*
310 * This is a helper function used by do_stat, do_freei, do_seti, and
311 * do_testi, etc.  Basically, any command which takes a single
312 * argument which is a file/inode number specifier.
313 */
314int common_inode_args_process(int argc, char *argv[],
315			      ext2_ino_t *inode, int flags)
316{
317	if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
318		return 1;
319
320	*inode = string_to_inode(argv[1]);
321	if (!*inode)
322		return 1;
323	return 0;
324}
325
326/*
327 * This is a helper function used by do_freeb, do_setb, and do_testb
328 */
329int common_block_args_process(int argc, char *argv[],
330			      blk_t *block, blk_t *count)
331{
332	int	err;
333
334	if (common_args_process(argc, argv, 2, 3, argv[0],
335				"<block> [count]", CHECK_FS_BITMAPS))
336		return 1;
337
338	if (strtoblk(argv[0], argv[1], block))
339		return 1;
340	if (argc > 2) {
341		*count = parse_ulong(argv[2], argv[0], "count", &err);
342		if (err)
343			return 1;
344	}
345	return 0;
346}
347
348int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
349			const char *cmd, int bufsize)
350{
351	int retval;
352
353	retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
354	if (retval) {
355		com_err(cmd, retval, "while reading inode %u", ino);
356		return 1;
357	}
358	return 0;
359}
360
361int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
362			const char *cmd)
363{
364	int retval;
365
366	retval = ext2fs_read_inode(current_fs, ino, inode);
367	if (retval) {
368		com_err(cmd, retval, "while reading inode %u", ino);
369		return 1;
370	}
371	return 0;
372}
373
374int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
375			const char *cmd)
376{
377	int retval;
378
379	retval = ext2fs_write_inode(current_fs, ino, inode);
380	if (retval) {
381		com_err(cmd, retval, "while writing inode %u", ino);
382		return 1;
383	}
384	return 0;
385}
386
387int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode,
388			    const char *cmd)
389{
390	int retval;
391
392	retval = ext2fs_write_new_inode(current_fs, ino, inode);
393	if (retval) {
394		com_err(cmd, retval, "while creating inode %u", ino);
395		return 1;
396	}
397	return 0;
398}
399