blob: c8eeae171ded0f44dd01acb6fa92d30b331e9221 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <ctype.h>
13#include <string.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000014#include <time.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000015
16#include "debugfs.h"
17
18FILE *open_pager(void)
19{
20 FILE *outfile;
21 char *pager = getenv("PAGER");
22
23 if (!pager)
24 outfile = stdout;
25 else {
26 outfile = popen(pager, "w");
27 if (!outfile) outfile = stdout;
28 }
29 return (outfile);
30}
31
32void close_pager(FILE *stream)
33{
34 if (stream && stream != stdout) fclose(stream);
35}
36
37/*
38 * This routine is used whenever a command needs to turn a string into
39 * an inode.
40 */
41ino_t string_to_inode(char *str)
42{
43 ino_t ino;
44 int len = strlen(str);
45 int i;
46 int retval;
47
48 /*
49 * If the string is of the form <ino>, then treat it as an
50 * inode number.
51 */
52 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
53 for (i = 1; i < len-1; i++)
54 if (!isdigit(str[i]))
55 break;
56 if (i == len-1)
57 return(atoi(str+1));
58 }
59
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000060 retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000061 if (retval) {
62 com_err(str, retval, "");
63 return 0;
64 }
65 return ino;
66}
67
68/*
69 * This routine returns 1 if the filesystem is not open, and prints an
70 * error message to that effect.
71 */
72int check_fs_open(char *name)
73{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000074 if (!current_fs) {
Theodore Ts'o3839e651997-04-26 13:21:57 +000075 com_err(name, 0, "Filesystem not open");
76 return 1;
77 }
78 return 0;
79}
80
81/*
82 * This routine returns 1 if a filesystem is open, and prints an
83 * error message to that effect.
84 */
85int check_fs_not_open(char *name)
86{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000087 if (current_fs) {
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 com_err(name, 0,
89 "Filesystem %s is still open. Close it first.\n",
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000090 current_fs->device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 return 1;
92 }
93 return 0;
94}
95
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000096/*
97 * This routine returns 1 if a filesystem is not opened read/write,
98 * and prints an error message to that effect.
99 */
100int check_fs_read_write(char *name)
101{
102 if (!(current_fs->flags & EXT2_FLAG_RW)) {
103 com_err(name, 0, "Filesystem opened read/only");
104 return 1;
105 }
106 return 0;
107}
108
109/*
110 * This function takes a __u32 time value and converts it to a string,
111 * using ctime
112 */
113char *time_to_string(__u32 cl)
114{
115 time_t t = (time_t) cl;
116
117 return ctime(&t);
118}
119
120
121
122