ls.c revision ed78c021c3b111d8ab9a51aef5d5156e3004083f
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 21extern int optind; 22extern char *optarg; 23#endif 24#ifdef HAVE_OPTRESET 25extern int optreset; /* defined by BSD, but not others */ 26#endif 27 28#include "debugfs.h" 29 30/* 31 * list directory 32 */ 33 34#define LONG_OPT 0x0001 35#define DELETED_OPT 0x0002 36 37struct list_dir_struct { 38 FILE *f; 39 int col; 40 int options; 41}; 42 43static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 44 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 45 46static int list_dir_proc(ext2_ino_t dir, 47 int entry, 48 struct ext2_dir_entry *dirent, 49 int offset, 50 int blocksize, 51 char *buf, 52 void *private) 53{ 54 struct ext2_inode inode; 55 ext2_ino_t ino; 56 struct tm *tm_p; 57 time_t modtime; 58 char name[EXT2_NAME_LEN]; 59 char tmp[EXT2_NAME_LEN + 16]; 60 char datestr[80]; 61 char lbr, rbr; 62 int thislen; 63 struct list_dir_struct *ls = (struct list_dir_struct *) private; 64 65 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ? 66 (dirent->name_len & 0xFF) : EXT2_NAME_LEN; 67 strncpy(name, dirent->name, thislen); 68 name[thislen] = '\0'; 69 ino = dirent->inode; 70 71 if (entry == DIRENT_DELETED_FILE) { 72 lbr = '<'; 73 rbr = '>'; 74 ino = 0; 75 } else { 76 lbr = rbr = ' '; 77 } 78 if (ls->options & LONG_OPT) { 79 if (ino) { 80 if (debugfs_read_inode(ino, &inode, name)) 81 return 0; 82 modtime = inode.i_mtime; 83 tm_p = localtime(&modtime); 84 sprintf(datestr, "%2d-%s-%4d %02d:%02d", 85 tm_p->tm_mday, monstr[tm_p->tm_mon], 86 1900 + tm_p->tm_year, tm_p->tm_hour, 87 tm_p->tm_min); 88 } else { 89 strcpy(datestr, " "); 90 memset(&inode, 0, sizeof(struct ext2_inode)); 91 } 92 fprintf(ls->f, "%c%6u%c %6o (%d) %5d %5d ", lbr, ino, rbr, 93 inode.i_mode, dirent->name_len >> 8, 94 inode.i_uid, inode.i_gid); 95 if (LINUX_S_ISDIR(inode.i_mode)) 96 fprintf(ls->f, "%5d", inode.i_size); 97 else 98 fprintf(ls->f, "%5lld", inode.i_size | 99 ((__u64)inode.i_size_high << 32)); 100 fprintf (ls->f, " %s %s\n", datestr, name); 101 } else { 102 sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr, 103 dirent->rec_len, name); 104 thislen = strlen(tmp); 105 106 if (ls->col + thislen > 80) { 107 fprintf(ls->f, "\n"); 108 ls->col = 0; 109 } 110 fprintf(ls->f, "%s", tmp); 111 ls->col += thislen; 112 } 113 return 0; 114} 115 116void do_list_dir(int argc, char *argv[]) 117{ 118 ext2_ino_t inode; 119 int retval; 120 int c; 121 int flags; 122 struct list_dir_struct ls; 123 124 ls.options = 0; 125 if (check_fs_open(argv[0])) 126 return; 127 128 optind = 1; 129#ifdef HAVE_OPTRESET 130 optreset = 1; /* Makes BSD getopt happy */ 131#endif 132 while ((c = getopt (argc, argv, "dl")) != EOF) { 133 switch (c) { 134 case 'l': 135 ls.options |= LONG_OPT; 136 break; 137 case 'd': 138 ls.options |= DELETED_OPT; 139 break; 140 } 141 } 142 143 if (argc > optind+1) { 144 com_err(0, 0, "Usage: ls [-l] [-d] file"); 145 return; 146 } 147 148 if (argc == optind) 149 inode = cwd; 150 else 151 inode = string_to_inode(argv[optind]); 152 if (!inode) 153 return; 154 155 ls.f = open_pager(); 156 ls.col = 0; 157 flags = DIRENT_FLAG_INCLUDE_EMPTY; 158 if (ls.options & DELETED_OPT) 159 flags |= DIRENT_FLAG_INCLUDE_REMOVED; 160 161 retval = ext2fs_dir_iterate2(current_fs, inode, flags, 162 0, list_dir_proc, &ls); 163 fprintf(ls.f, "\n"); 164 close_pager(ls.f); 165 if (retval) 166 com_err(argv[1], retval, ""); 167 168 return; 169} 170 171 172