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
33 #define PARSE_OPT 0x0004
35 struct list_dir_struct {
41 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
44 static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
46 struct ext2_dir_entry *dirent,
47 int offset EXT2FS_ATTR((unused)),
48 int blocksize EXT2FS_ATTR((unused)),
49 char *buf EXT2FS_ATTR((unused)),
52 struct ext2_inode inode;
56 char name[EXT2_NAME_LEN + 1];
57 char tmp[EXT2_NAME_LEN + 16];
61 struct list_dir_struct *ls = (struct list_dir_struct *) private;
63 thislen = dirent->name_len & 0xFF;
64 strncpy(name, dirent->name, thislen);
68 if (entry == DIRENT_DELETED_FILE) {
75 if (ls->options & PARSE_OPT) {
77 if (debugfs_read_inode(ino, &inode, name))
80 memset(&inode, 0, sizeof(struct ext2_inode));
81 fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name);
82 if (LINUX_S_ISDIR(inode.i_mode))
85 fprintf(ls->f, "%lld/", EXT2_I_SIZE(&inode));
87 } else if (ls->options & LONG_OPT) {
89 if (debugfs_read_inode(ino, &inode, name))
91 modtime = inode.i_mtime;
92 tm_p = localtime(&modtime);
93 sprintf(datestr, "%2d-%s-%4d %02d:%02d",
94 tm_p->tm_mday, monstr[tm_p->tm_mon],
95 1900 + tm_p->tm_year, tm_p->tm_hour,
99 memset(&inode, 0, sizeof(struct ext2_inode));
101 fprintf(ls->f, "%c%6u%c %6o (%d) %5d %5d ", lbr, ino, rbr,
102 inode.i_mode, dirent->name_len >> 8,
103 inode_uid(inode), inode_gid(inode));
104 if (LINUX_S_ISDIR(inode.i_mode))
105 fprintf(ls->f, "%5d", inode.i_size);
107 fprintf(ls->f, "%5llu", EXT2_I_SIZE(&inode));
108 fprintf (ls->f, " %s %s\n", datestr, name);
110 sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr,
111 dirent->rec_len, name);
112 thislen = strlen(tmp);
114 if (ls->col + thislen > 80) {
115 fprintf(ls->f, "\n");
118 fprintf(ls->f, "%s", tmp);
124 void do_list_dir(int argc, char *argv[])
130 struct list_dir_struct ls;
133 if (check_fs_open(argv[0]))
137 while ((c = getopt (argc, argv, "dlp")) != EOF) {
140 ls.options |= LONG_OPT;
143 ls.options |= DELETED_OPT;
146 ls.options |= PARSE_OPT;
153 if (argc > optind+1) {
155 com_err(0, 0, "Usage: ls [-l] [-d] [-p] file");
162 inode = string_to_inode(argv[optind]);
168 flags = DIRENT_FLAG_INCLUDE_EMPTY;
169 if (ls.options & DELETED_OPT)
170 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
172 retval = ext2fs_dir_iterate2(current_fs, inode, flags,
173 0, list_dir_proc, &ls);
177 com_err(argv[1], retval, 0);