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