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