Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame^] | 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 | |
| 15 | #include "debugfs.h" |
| 16 | |
| 17 | FILE *open_pager(void) |
| 18 | { |
| 19 | FILE *outfile; |
| 20 | char *pager = getenv("PAGER"); |
| 21 | |
| 22 | if (!pager) |
| 23 | outfile = stdout; |
| 24 | else { |
| 25 | outfile = popen(pager, "w"); |
| 26 | if (!outfile) outfile = stdout; |
| 27 | } |
| 28 | return (outfile); |
| 29 | } |
| 30 | |
| 31 | void close_pager(FILE *stream) |
| 32 | { |
| 33 | if (stream && stream != stdout) fclose(stream); |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * This routine is used whenever a command needs to turn a string into |
| 38 | * an inode. |
| 39 | */ |
| 40 | ino_t string_to_inode(char *str) |
| 41 | { |
| 42 | ino_t ino; |
| 43 | int len = strlen(str); |
| 44 | int i; |
| 45 | int retval; |
| 46 | |
| 47 | /* |
| 48 | * If the string is of the form <ino>, then treat it as an |
| 49 | * inode number. |
| 50 | */ |
| 51 | if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) { |
| 52 | for (i = 1; i < len-1; i++) |
| 53 | if (!isdigit(str[i])) |
| 54 | break; |
| 55 | if (i == len-1) |
| 56 | return(atoi(str+1)); |
| 57 | } |
| 58 | |
| 59 | retval = ext2fs_namei(fs, root, cwd, str, &ino); |
| 60 | if (retval) { |
| 61 | com_err(str, retval, ""); |
| 62 | return 0; |
| 63 | } |
| 64 | return ino; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * This routine returns 1 if the filesystem is not open, and prints an |
| 69 | * error message to that effect. |
| 70 | */ |
| 71 | int check_fs_open(char *name) |
| 72 | { |
| 73 | if (!fs) { |
| 74 | com_err(name, 0, "Filesystem not open"); |
| 75 | return 1; |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * This routine returns 1 if a filesystem is open, and prints an |
| 82 | * error message to that effect. |
| 83 | */ |
| 84 | int check_fs_not_open(char *name) |
| 85 | { |
| 86 | if (fs) { |
| 87 | com_err(name, 0, |
| 88 | "Filesystem %s is still open. Close it first.\n", |
| 89 | fs->device_name); |
| 90 | return 1; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |