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) < EXT2_NAME_LEN) ?
64 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
65 strncpy(name, dirent->name, thislen);
69 if (entry == DIRENT_DELETED_FILE) {
76 if (ls->options & PARSE_OPT) {
77 if (ino && debugfs_read_inode(ino, &inode, name)) return 0;
78 fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name);
79 if (LINUX_S_ISDIR(inode.i_mode))
82 fprintf(ls->f, "%lld/", inode.i_size | ((__u64)inode.i_size_high << 32));
85 else if (ls->options & LONG_OPT) {
87 if (debugfs_read_inode(ino, &inode, name))
89 modtime = inode.i_mtime;
90 tm_p = localtime(&modtime);
91 sprintf(datestr, "%2d-%s-%4d %02d:%02d",
92 tm_p->tm_mday, monstr[tm_p->tm_mon],
93 1900 + tm_p->tm_year, tm_p->tm_hour,
97 memset(&inode, 0, sizeof(struct ext2_inode));
99 fprintf(ls->f, "%c%6u%c %6o (%d) %5d %5d ", lbr, ino, rbr,
100 inode.i_mode, dirent->name_len >> 8,
101 inode_uid(inode), inode_gid(inode));
102 if (LINUX_S_ISDIR(inode.i_mode))
103 fprintf(ls->f, "%5d", inode.i_size);
105 fprintf(ls->f, "%5llu", inode.i_size |
106 ((unsigned long long) inode.i_size_high << 32));
107 fprintf (ls->f, " %s %s\n", datestr, name);
109 sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr,
110 dirent->rec_len, name);
111 thislen = strlen(tmp);
113 if (ls->col + thislen > 80) {
114 fprintf(ls->f, "\n");
117 fprintf(ls->f, "%s", tmp);
123 void do_list_dir(int argc, char *argv[])
129 struct list_dir_struct ls;
132 if (check_fs_open(argv[0]))
136 while ((c = getopt (argc, argv, "dlp")) != EOF) {
139 ls.options |= LONG_OPT;
142 ls.options |= DELETED_OPT;
145 ls.options |= PARSE_OPT;
152 if (argc > optind+1) {
154 com_err(0, 0, "Usage: ls [-l] [-d] [-p] file");
161 inode = string_to_inode(argv[optind]);
167 flags = DIRENT_FLAG_INCLUDE_EMPTY;
168 if (ls.options & DELETED_OPT)
169 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
171 retval = ext2fs_dir_iterate2(current_fs, inode, flags,
172 0, list_dir_proc, &ls);
176 com_err(argv[1], retval, 0);