1 /* 2 * ls.c --- list directories 3 * 4 * Copyright (C) 1997 Theodore Ts'o. This file may be redistributed 5 * under the terms of the GNU Public License. 6 */ 7 8 #include <stdio.h> 9 #include <unistd.h> 10 #include <stdlib.h> 11 #include <ctype.h> 12 #include <string.h> 13 #include <time.h> 14 #ifdef HAVE_ERRNO_H 15 #include <errno.h> 16 #endif 17 #include <sys/types.h> 18 #ifdef HAVE_GETOPT_H 19 #include <getopt.h> 20 #else 21 extern int optind; 22 extern char *optarg; 23 #endif 24 25 #include "debugfs.h" 26 27 /* 28 * list directory 29 */ 30 31 #define LONG_OPT 0x0001 32 #define DELETED_OPT 0x0002 33 #define PARSE_OPT 0x0004 34 35 struct list_dir_struct { 36 FILE *f; 37 int col; 38 int options; 39 }; 40 41 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 43 44 static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), 45 int entry, 46 struct ext2_dir_entry *dirent, 47 int offset EXT2FS_ATTR((unused)), 48 int blocksize EXT2FS_ATTR((unused)), 49 char *buf EXT2FS_ATTR((unused)), 50 void *private) 51 { 52 struct ext2_inode inode; 53 ext2_ino_t ino; 54 struct tm *tm_p; 55 time_t modtime; 56 char name[EXT2_NAME_LEN + 1]; 57 char tmp[EXT2_NAME_LEN + 16]; 58 char datestr[80]; 59 char lbr, rbr; 60 int thislen; 61 struct list_dir_struct *ls = (struct list_dir_struct *) private; 62 63 thislen = dirent->name_len & 0xFF; 64 strncpy(name, dirent->name, thislen); 65 name[thislen] = '\0'; 66 ino = dirent->inode; 67 68 if (entry == DIRENT_DELETED_FILE) { 69 lbr = '<'; 70 rbr = '>'; 71 ino = 0; 72 } else { 73 lbr = rbr = ' '; 74 } 75 if (ls->options & PARSE_OPT) { 76 if (ino) { 77 if (debugfs_read_inode(ino, &inode, name)) 78 return 0; 79 } else 80 memset(&inode, 0, sizeof(struct ext2_inode)); 81 fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name); 82 if (LINUX_S_ISDIR(inode.i_mode)) 83 fprintf(ls->f, "/"); 84 else 85 fprintf(ls->f, "%lld/", EXT2_I_SIZE(&inode)); 86 fprintf(ls->f, "\n"); 87 } else if (ls->options & LONG_OPT) { 88 if (ino) { 89 if (debugfs_read_inode(ino, &inode, name)) 90 return 0; 91 modtime = inode.i_mtime; 92 tm_p = localtime(&modtime); 93 sprintf(datestr, "%2d-%s-%4d %02d:%02d", 94 tm_p->tm_mday, monstr[tm_p->tm_mon], 95 1900 + tm_p->tm_year, tm_p->tm_hour, 96 tm_p->tm_min); 97 } else { 98 strcpy(datestr, " "); 99 memset(&inode, 0, sizeof(struct ext2_inode)); 100 } 101 fprintf(ls->f, "%c%6u%c %6o (%d) %5d %5d ", lbr, ino, rbr, 102 inode.i_mode, dirent->name_len >> 8, 103 inode_uid(inode), inode_gid(inode)); 104 if (LINUX_S_ISDIR(inode.i_mode)) 105 fprintf(ls->f, "%5d", inode.i_size); 106 else 107 fprintf(ls->f, "%5llu", EXT2_I_SIZE(&inode)); 108 fprintf (ls->f, " %s %s\n", datestr, name); 109 } else { 110 sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr, 111 dirent->rec_len, name); 112 thislen = strlen(tmp); 113 114 if (ls->col + thislen > 80) { 115 fprintf(ls->f, "\n"); 116 ls->col = 0; 117 } 118 fprintf(ls->f, "%s", tmp); 119 ls->col += thislen; 120 } 121 return 0; 122 } 123 124 void do_list_dir(int argc, char *argv[]) 125 { 126 ext2_ino_t inode; 127 int retval; 128 int c; 129 int flags; 130 struct list_dir_struct ls; 131 132 ls.options = 0; 133 if (check_fs_open(argv[0])) 134 return; 135 136 reset_getopt(); 137 while ((c = getopt (argc, argv, "dlp")) != EOF) { 138 switch (c) { 139 case 'l': 140 ls.options |= LONG_OPT; 141 break; 142 case 'd': 143 ls.options |= DELETED_OPT; 144 break; 145 case 'p': 146 ls.options |= PARSE_OPT; 147 break; 148 default: 149 goto print_usage; 150 } 151 } 152 153 if (argc > optind+1) { 154 print_usage: 155 com_err(0, 0, "Usage: ls [-l] [-d] [-p] file"); 156 return; 157 } 158 159 if (argc == optind) 160 inode = cwd; 161 else 162 inode = string_to_inode(argv[optind]); 163 if (!inode) 164 return; 165 166 ls.f = open_pager(); 167 ls.col = 0; 168 flags = DIRENT_FLAG_INCLUDE_EMPTY; 169 if (ls.options & DELETED_OPT) 170 flags |= DIRENT_FLAG_INCLUDE_REMOVED; 171 172 retval = ext2fs_dir_iterate2(current_fs, inode, flags, 173 0, list_dir_proc, &ls); 174 fprintf(ls.f, "\n"); 175 close_pager(ls.f); 176 if (retval) 177 com_err(argv[1], retval, 0); 178 179 return; 180 } 181 182 183