]>
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 | ||
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> | |
efc6f628 | 20 | #else |
e1018eea TT |
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 |
d056b991 | 33 | #define PARSE_OPT 0x0004 |
521e3685 TT |
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"}; | |
efc6f628 | 43 | |
54434927 | 44 | static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), |
e1018eea TT |
45 | int entry, |
46 | struct ext2_dir_entry *dirent, | |
54434927 TT |
47 | int offset EXT2FS_ATTR((unused)), |
48 | int blocksize EXT2FS_ATTR((unused)), | |
49 | char *buf EXT2FS_ATTR((unused)), | |
e1018eea | 50 | void *private) |
521e3685 TT |
51 | { |
52 | struct ext2_inode inode; | |
e1018eea | 53 | ext2_ino_t ino; |
521e3685 TT |
54 | struct tm *tm_p; |
55 | time_t modtime; | |
b772900b | 56 | char name[EXT2_NAME_LEN + 1]; |
e1018eea | 57 | char tmp[EXT2_NAME_LEN + 16]; |
521e3685 | 58 | char datestr[80]; |
e1018eea TT |
59 | char lbr, rbr; |
60 | int thislen; | |
521e3685 | 61 | struct list_dir_struct *ls = (struct list_dir_struct *) private; |
521e3685 | 62 | |
cce382b1 TT |
63 | thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ? |
64 | (dirent->name_len & 0xFF) : EXT2_NAME_LEN; | |
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 | |
82 | fprintf(ls->f, "%lld/", inode.i_size | ((__u64)inode.i_size_high << 32)); | |
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 | |
de8f3a76 AD |
105 | fprintf(ls->f, "%5llu", inode.i_size | |
106 | ((unsigned long long) inode.i_size_high << 32)); | |
e1018eea TT |
107 | fprintf (ls->f, " %s %s\n", datestr, name); |
108 | } else { | |
109 | sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr, | |
110 | dirent->rec_len, name); | |
111 | thislen = strlen(tmp); | |
112 | ||
113 | if (ls->col + thislen > 80) { | |
114 | fprintf(ls->f, "\n"); | |
115 | ls->col = 0; | |
116 | } | |
117 | fprintf(ls->f, "%s", tmp); | |
118 | ls->col += thislen; | |
119 | } | |
521e3685 TT |
120 | return 0; |
121 | } | |
122 | ||
123 | void do_list_dir(int argc, char *argv[]) | |
124 | { | |
b044c2e0 TT |
125 | ext2_ino_t inode; |
126 | int retval; | |
e1018eea TT |
127 | int c; |
128 | int flags; | |
521e3685 | 129 | struct list_dir_struct ls; |
efc6f628 | 130 | |
521e3685 TT |
131 | ls.options = 0; |
132 | if (check_fs_open(argv[0])) | |
133 | return; | |
134 | ||
88494bb6 | 135 | reset_getopt(); |
d056b991 | 136 | while ((c = getopt (argc, argv, "dlp")) != EOF) { |
e1018eea TT |
137 | switch (c) { |
138 | case 'l': | |
139 | ls.options |= LONG_OPT; | |
140 | break; | |
141 | case 'd': | |
142 | ls.options |= DELETED_OPT; | |
143 | break; | |
d056b991 TT |
144 | case 'p': |
145 | ls.options |= PARSE_OPT; | |
146 | break; | |
49c6b4e9 TT |
147 | default: |
148 | goto print_usage; | |
e1018eea TT |
149 | } |
150 | } | |
151 | ||
152 | if (argc > optind+1) { | |
49c6b4e9 | 153 | print_usage: |
d056b991 | 154 | com_err(0, 0, "Usage: ls [-l] [-d] [-p] file"); |
e1018eea | 155 | return; |
521e3685 TT |
156 | } |
157 | ||
e1018eea | 158 | if (argc == optind) |
521e3685 TT |
159 | inode = cwd; |
160 | else | |
e1018eea | 161 | inode = string_to_inode(argv[optind]); |
521e3685 TT |
162 | if (!inode) |
163 | return; | |
164 | ||
165 | ls.f = open_pager(); | |
166 | ls.col = 0; | |
e1018eea TT |
167 | flags = DIRENT_FLAG_INCLUDE_EMPTY; |
168 | if (ls.options & DELETED_OPT) | |
169 | flags |= DIRENT_FLAG_INCLUDE_REMOVED; | |
170 | ||
171 | retval = ext2fs_dir_iterate2(current_fs, inode, flags, | |
521e3685 TT |
172 | 0, list_dir_proc, &ls); |
173 | fprintf(ls.f, "\n"); | |
174 | close_pager(ls.f); | |
175 | if (retval) | |
9b9a780f | 176 | com_err(argv[1], retval, 0); |
521e3685 TT |
177 | |
178 | return; | |
179 | } | |
180 | ||
181 |