Whamcloud - gitweb
debugfs.c:
[tools/e2fsprogs.git] / debugfs / util.c
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 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <time.h>
15 #include <signal.h>
16
17 #include "debugfs.h"
18
19 FILE *open_pager(void)
20 {
21         FILE *outfile;
22         char *pager = getenv("PAGER");
23
24         signal(SIGPIPE, SIG_IGN);
25         if (!pager)
26                 pager = "more";
27
28         outfile = popen(pager, "w");
29         if (!outfile)
30                 outfile = stdout;
31
32         return (outfile);
33 }
34
35 void close_pager(FILE *stream)
36 {
37         if (stream && stream != stdout) fclose(stream);
38 }
39
40 /*
41  * This routine is used whenever a command needs to turn a string into
42  * an inode.
43  */
44 ino_t string_to_inode(char *str)
45 {
46         ino_t   ino;
47         int     len = strlen(str);
48         int     i;
49         int     retval;
50
51         /*
52          * If the string is of the form <ino>, then treat it as an
53          * inode number.
54          */
55         if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
56                 for (i = 1; i < len-1; i++)
57                         if (!isdigit(str[i]))
58                                 break;
59                 if (i == len-1)
60                         return(atoi(str+1));
61         }
62
63         retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
64         if (retval) {
65                 com_err(str, retval, "");
66                 return 0;
67         }
68         return ino;
69 }
70
71 /*
72  * This routine returns 1 if the filesystem is not open, and prints an
73  * error message to that effect.
74  */
75 int check_fs_open(char *name)
76 {
77         if (!current_fs) {
78                 com_err(name, 0, "Filesystem not open");
79                 return 1;
80         }
81         return 0;
82 }
83
84 /*
85  * This routine returns 1 if a filesystem is open, and prints an
86  * error message to that effect.
87  */
88 int check_fs_not_open(char *name)
89 {
90         if (current_fs) {
91                 com_err(name, 0,
92                         "Filesystem %s is still open.  Close it first.\n",
93                         current_fs->device_name);
94                 return 1;
95         }
96         return 0;
97 }
98
99 /*
100  * This routine returns 1 if a filesystem is not opened read/write,
101  * and prints an error message to that effect.
102  */
103 int check_fs_read_write(char *name)
104 {
105         if (!(current_fs->flags & EXT2_FLAG_RW)) {
106                 com_err(name, 0, "Filesystem opened read/only");
107                 return 1;
108         }
109         return 0;
110 }
111
112 /*
113  * This routine returns 1 if a filesystem is doesn't have its inode
114  * and block bitmaps loaded, and prints an error message to that
115  * effect.
116  */
117 int check_fs_bitmaps(char *name)
118 {
119         if (!current_fs->block_map || !current_fs->inode_map) {
120                 com_err(name, 0, "Filesystem bitmaps not loaded");
121                 return 1;
122         }
123         return 0;
124 }
125
126 /*
127  * This function takes a __u32 time value and converts it to a string,
128  * using ctime
129  */
130 char *time_to_string(__u32 cl)
131 {
132         time_t  t = (time_t) cl;
133
134         return ctime(&t);
135 }
136
137
138         
139