]>
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> | |
18 | ||
19 | #include "debugfs.h" | |
20 | ||
21 | /* | |
22 | * list directory | |
23 | */ | |
24 | ||
25 | #define LONG_OPT 0x0001 | |
26 | ||
27 | struct list_dir_struct { | |
28 | FILE *f; | |
29 | int col; | |
30 | int options; | |
31 | }; | |
32 | ||
33 | static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
34 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | |
35 | ||
b044c2e0 | 36 | static void ls_l_file(struct list_dir_struct *ls, char *name, ext2_ino_t ino) |
521e3685 TT |
37 | { |
38 | struct ext2_inode inode; | |
39 | errcode_t retval; | |
40 | struct tm *tm_p; | |
41 | time_t modtime; | |
42 | char datestr[80]; | |
43 | ||
44 | retval = ext2fs_read_inode(current_fs, ino, &inode); | |
45 | if (retval) { | |
f1304811 | 46 | fprintf(ls->f, "%5ld --- error --- %s\n", retval, name); |
521e3685 TT |
47 | return; |
48 | } | |
49 | modtime = inode.i_mtime; | |
50 | tm_p = localtime(&modtime); | |
2e8d40d5 TT |
51 | sprintf(datestr, "%2d-%s-%4d %02d:%02d", |
52 | tm_p->tm_mday, monstr[tm_p->tm_mon], 1900 + tm_p->tm_year, | |
521e3685 | 53 | tm_p->tm_hour, tm_p->tm_min); |
b044c2e0 | 54 | fprintf(ls->f, "%6u %6o %5d %5d ", ino, inode.i_mode, |
36a43d67 TT |
55 | inode.i_uid, inode.i_gid); |
56 | if (LINUX_S_ISDIR(inode.i_mode)) | |
57 | fprintf(ls->f, "%5d", inode.i_size); | |
58 | else | |
59 | fprintf(ls->f, "%5lld", inode.i_size | | |
60 | ((__u64)inode.i_size_high << 32)); | |
61 | fprintf (ls->f, " %s %s\n", datestr, name); | |
521e3685 TT |
62 | } |
63 | ||
64 | static void ls_file(struct list_dir_struct *ls, char *name, | |
b044c2e0 | 65 | ext2_ino_t ino, int rec_len) |
521e3685 TT |
66 | { |
67 | char tmp[EXT2_NAME_LEN + 16]; | |
68 | int thislen; | |
69 | ||
b044c2e0 | 70 | sprintf(tmp, "%u (%d) %s ", ino, rec_len, name); |
521e3685 TT |
71 | thislen = strlen(tmp); |
72 | ||
73 | if (ls->col + thislen > 80) { | |
74 | fprintf(ls->f, "\n"); | |
75 | ls->col = 0; | |
76 | } | |
77 | fprintf(ls->f, "%s", tmp); | |
78 | ls->col += thislen; | |
79 | } | |
80 | ||
81 | ||
82 | static int list_dir_proc(struct ext2_dir_entry *dirent, | |
83 | int offset, | |
84 | int blocksize, | |
85 | char *buf, | |
86 | void *private) | |
87 | { | |
88 | char name[EXT2_NAME_LEN]; | |
521e3685 TT |
89 | |
90 | struct list_dir_struct *ls = (struct list_dir_struct *) private; | |
91 | int thislen; | |
92 | ||
cce382b1 TT |
93 | thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ? |
94 | (dirent->name_len & 0xFF) : EXT2_NAME_LEN; | |
521e3685 TT |
95 | strncpy(name, dirent->name, thislen); |
96 | name[thislen] = '\0'; | |
97 | ||
98 | if (ls->options & LONG_OPT) | |
99 | ls_l_file(ls, name, dirent->inode); | |
100 | else | |
101 | ls_file(ls, name, dirent->inode, dirent->rec_len); | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | void do_list_dir(int argc, char *argv[]) | |
107 | { | |
b044c2e0 TT |
108 | ext2_ino_t inode; |
109 | int retval; | |
521e3685 | 110 | struct list_dir_struct ls; |
b044c2e0 | 111 | int argptr = 1; |
521e3685 TT |
112 | |
113 | ls.options = 0; | |
114 | if (check_fs_open(argv[0])) | |
115 | return; | |
116 | ||
117 | if ((argc > argptr) && (argv[argptr][0] == '-')) { | |
118 | argptr++; | |
119 | ls.options = LONG_OPT; | |
120 | } | |
121 | ||
122 | if (argc <= argptr) | |
123 | inode = cwd; | |
124 | else | |
125 | inode = string_to_inode(argv[argptr]); | |
126 | if (!inode) | |
127 | return; | |
128 | ||
129 | ls.f = open_pager(); | |
130 | ls.col = 0; | |
131 | retval = ext2fs_dir_iterate(current_fs, inode, | |
132 | DIRENT_FLAG_INCLUDE_EMPTY, | |
133 | 0, list_dir_proc, &ls); | |
134 | fprintf(ls.f, "\n"); | |
135 | close_pager(ls.f); | |
136 | if (retval) | |
137 | com_err(argv[1], retval, ""); | |
138 | ||
139 | return; | |
140 | } | |
141 | ||
142 |