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.
18 #include <sys/types.h>
32 #define LONG_OPT 0x0001
33 #define DELETED_OPT 0x0002
34 #define PARSE_OPT 0x0004
36 struct list_dir_struct {
42 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
43 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
45 static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
47 struct ext2_dir_entry *dirent,
48 int offset EXT2FS_ATTR((unused)),
49 int blocksize EXT2FS_ATTR((unused)),
50 char *buf EXT2FS_ATTR((unused)),
53 struct ext2_inode inode;
57 char name[EXT2_NAME_LEN + 1];
58 char tmp[EXT2_NAME_LEN + 16];
62 struct list_dir_struct *ls = (struct list_dir_struct *) private;
64 thislen = dirent->name_len & 0xFF;
65 strncpy(name, dirent->name, thislen);
69 if (entry == DIRENT_DELETED_FILE) {
76 if (ls->options & PARSE_OPT) {
78 if (debugfs_read_inode(ino, &inode, name))
81 memset(&inode, 0, sizeof(struct ext2_inode));
82 fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name);
83 if (LINUX_S_ISDIR(inode.i_mode))
86 fprintf(ls->f, "%lld/", EXT2_I_SIZE(&inode));
88 } else if (ls->options & LONG_OPT) {
90 if (debugfs_read_inode(ino, &inode, name))
92 modtime = inode.i_mtime;
93 tm_p = localtime(&modtime);
94 sprintf(datestr, "%2d-%s-%4d %02d:%02d",
95 tm_p->tm_mday, monstr[tm_p->tm_mon],
96 1900 + tm_p->tm_year, tm_p->tm_hour,
100 memset(&inode, 0, sizeof(struct ext2_inode));
102 fprintf(ls->f, "%c%6u%c %6o (%d) %5d %5d ", lbr, ino, rbr,
103 inode.i_mode, dirent->name_len >> 8,
104 inode_uid(inode), inode_gid(inode));
105 if (LINUX_S_ISDIR(inode.i_mode))
106 fprintf(ls->f, "%5d", inode.i_size);
108 fprintf(ls->f, "%5llu", EXT2_I_SIZE(&inode));
109 fprintf (ls->f, " %s %s\n", datestr, name);
111 sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr,
112 dirent->rec_len, name);
113 thislen = strlen(tmp);
115 if (ls->col + thislen > 80) {
116 fprintf(ls->f, "\n");
119 fprintf(ls->f, "%s", tmp);
125 void do_list_dir(int argc, char *argv[])
131 struct list_dir_struct ls;
134 if (check_fs_open(argv[0]))
138 while ((c = getopt (argc, argv, "dlp")) != EOF) {
141 ls.options |= LONG_OPT;
144 ls.options |= DELETED_OPT;
147 ls.options |= PARSE_OPT;
154 if (argc > optind+1) {
156 com_err(0, 0, "Usage: ls [-l] [-d] [-p] file");
163 inode = string_to_inode(argv[optind]);
169 flags = DIRENT_FLAG_INCLUDE_EMPTY;
170 if (ls.options & DELETED_OPT)
171 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
173 retval = ext2fs_dir_iterate2(current_fs, inode, flags,
174 0, list_dir_proc, &ls);
178 com_err(argv[1], retval, 0);