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>
31 #define LONG_OPT 0x0001
32 #define DELETED_OPT 0x0002
34 struct list_dir_struct {
40 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
41 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
43 static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
45 struct ext2_dir_entry *dirent,
46 int offset EXT2FS_ATTR((unused)),
47 int blocksize EXT2FS_ATTR((unused)),
48 char *buf EXT2FS_ATTR((unused)),
51 struct ext2_inode inode;
55 char name[EXT2_NAME_LEN + 1];
56 char tmp[EXT2_NAME_LEN + 16];
60 struct list_dir_struct *ls = (struct list_dir_struct *) private;
62 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
63 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
64 strncpy(name, dirent->name, thislen);
68 if (entry == DIRENT_DELETED_FILE) {
75 if (ls->options & LONG_OPT) {
77 if (debugfs_read_inode(ino, &inode, name))
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,
87 memset(&inode, 0, sizeof(struct ext2_inode));
89 fprintf(ls->f, "%c%6u%c %6o (%d) %5d %5d ", lbr, ino, rbr,
90 inode.i_mode, dirent->name_len >> 8,
91 inode_uid(inode), inode_gid(inode));
92 if (LINUX_S_ISDIR(inode.i_mode))
93 fprintf(ls->f, "%5d", inode.i_size);
95 fprintf(ls->f, "%5llu", inode.i_size |
96 ((unsigned long long) inode.i_size_high << 32));
97 fprintf (ls->f, " %s %s\n", datestr, name);
99 sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr,
100 dirent->rec_len, name);
101 thislen = strlen(tmp);
103 if (ls->col + thislen > 80) {
104 fprintf(ls->f, "\n");
107 fprintf(ls->f, "%s", tmp);
113 void do_list_dir(int argc, char *argv[])
119 struct list_dir_struct ls;
122 if (check_fs_open(argv[0]))
126 while ((c = getopt (argc, argv, "dl")) != EOF) {
129 ls.options |= LONG_OPT;
132 ls.options |= DELETED_OPT;
139 if (argc > optind+1) {
141 com_err(0, 0, "Usage: ls [-l] [-d] file");
148 inode = string_to_inode(argv[optind]);
154 flags = DIRENT_FLAG_INCLUDE_EMPTY;
155 if (ls.options & DELETED_OPT)
156 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
158 retval = ext2fs_dir_iterate2(current_fs, inode, flags,
159 0, list_dir_proc, &ls);
163 com_err(argv[1], retval, 0);