Whamcloud - gitweb
Many files:
[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
16 #include "debugfs.h"
17
18 FILE *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
32 void 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  */
41 ino_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
60         retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
61         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  */
72 int check_fs_open(char *name)
73 {
74         if (!current_fs) {
75                 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  */
85 int check_fs_not_open(char *name)
86 {
87         if (current_fs) {
88                 com_err(name, 0,
89                         "Filesystem %s is still open.  Close it first.\n",
90                         current_fs->device_name);
91                 return 1;
92         }
93         return 0;
94 }
95
96 /*
97  * This routine returns 1 if a filesystem is not opened read/write,
98  * and prints an error message to that effect.
99  */
100 int 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  */
113 char *time_to_string(__u32 cl)
114 {
115         time_t  t = (time_t) cl;
116
117         return ctime(&t);
118 }
119
120
121         
122