2 * ls.c --- list directories
4 * Copyright (C) 1997 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
17 #include <sys/types.h>
25 extern int optreset; /* defined by BSD, but not others */
34 #define LONG_OPT 0x0001
35 #define DELETED_OPT 0x0002
37 struct list_dir_struct {
43 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
44 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
46 static int list_dir_proc(ext2_ino_t dir,
48 struct ext2_dir_entry *dirent,
54 struct ext2_inode inode;
59 char name[EXT2_NAME_LEN];
60 char tmp[EXT2_NAME_LEN + 16];
64 struct list_dir_struct *ls = (struct list_dir_struct *) private;
66 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
67 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
68 strncpy(name, dirent->name, thislen);
72 if (entry == DIRENT_DELETED_FILE) {
79 if (ls->options & LONG_OPT) {
81 if (debugfs_read_inode(ino, &inode, name))
83 modtime = inode.i_mtime;
84 tm_p = localtime(&modtime);
85 sprintf(datestr, "%2d-%s-%4d %02d:%02d",
86 tm_p->tm_mday, monstr[tm_p->tm_mon],
87 1900 + tm_p->tm_year, tm_p->tm_hour,
91 memset(&inode, 0, sizeof(struct ext2_inode));
93 fprintf(ls->f, "%c%6u%c %6o %5d %5d ", lbr, ino, rbr,
94 inode.i_mode, inode.i_uid, inode.i_gid);
95 if (LINUX_S_ISDIR(inode.i_mode))
96 fprintf(ls->f, "%5d", inode.i_size);
98 fprintf(ls->f, "%5lld", inode.i_size |
99 ((__u64)inode.i_size_high << 32));
100 fprintf (ls->f, " %s %s\n", datestr, name);
102 sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr,
103 dirent->rec_len, name);
104 thislen = strlen(tmp);
106 if (ls->col + thislen > 80) {
107 fprintf(ls->f, "\n");
110 fprintf(ls->f, "%s", tmp);
116 void do_list_dir(int argc, char *argv[])
122 struct list_dir_struct ls;
125 if (check_fs_open(argv[0]))
130 optreset = 1; /* Makes BSD getopt happy */
132 while ((c = getopt (argc, argv, "dl")) != EOF) {
135 ls.options |= LONG_OPT;
138 ls.options |= DELETED_OPT;
143 if (argc > optind+1) {
144 com_err(0, 0, "Usage: ls [-l] [-d] file");
151 inode = string_to_inode(argv[optind]);
157 flags = DIRENT_FLAG_INCLUDE_EMPTY;
158 if (ls.options & DELETED_OPT)
159 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
161 retval = ext2fs_dir_iterate2(current_fs, inode, flags,
162 0, list_dir_proc, &ls);
166 com_err(argv[1], retval, "");