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 #define LONG_OPT 0x0001
27 struct list_dir_struct {
33 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
34 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
36 static void ls_l_file(struct list_dir_struct *ls, char *name, ino_t ino)
38 struct ext2_inode inode;
44 retval = ext2fs_read_inode(current_fs, ino, &inode);
46 fprintf(ls->f, "%5ld --- error --- %s\n", retval, name);
49 modtime = inode.i_mtime;
50 tm_p = localtime(&modtime);
51 sprintf(datestr, "%2d-%s-%4d %02d:%02d",
52 tm_p->tm_mday, monstr[tm_p->tm_mon], 1900 + tm_p->tm_year,
53 tm_p->tm_hour, tm_p->tm_min);
54 fprintf(ls->f, "%6ld %6o %5d %5d ", ino, inode.i_mode,
55 inode.i_uid, inode.i_gid);
56 if (LINUX_S_ISDIR(inode.i_mode))
57 fprintf(ls->f, "%5d", inode.i_size);
59 fprintf(ls->f, "%5lld", inode.i_size |
60 ((__u64)inode.i_size_high << 32));
61 fprintf (ls->f, " %s %s\n", datestr, name);
64 static void ls_file(struct list_dir_struct *ls, char *name,
65 ino_t ino, int rec_len)
67 char tmp[EXT2_NAME_LEN + 16];
70 sprintf(tmp, "%ld (%d) %s ", ino, rec_len, name);
71 thislen = strlen(tmp);
73 if (ls->col + thislen > 80) {
77 fprintf(ls->f, "%s", tmp);
82 static int list_dir_proc(struct ext2_dir_entry *dirent,
88 char name[EXT2_NAME_LEN];
90 struct list_dir_struct *ls = (struct list_dir_struct *) private;
93 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
94 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
95 strncpy(name, dirent->name, thislen);
98 if (ls->options & LONG_OPT)
99 ls_l_file(ls, name, dirent->inode);
101 ls_file(ls, name, dirent->inode, dirent->rec_len);
106 void do_list_dir(int argc, char *argv[])
110 struct list_dir_struct ls;
114 if (check_fs_open(argv[0]))
117 if ((argc > argptr) && (argv[argptr][0] == '-')) {
119 ls.options = LONG_OPT;
125 inode = string_to_inode(argv[argptr]);
131 retval = ext2fs_dir_iterate(current_fs, inode,
132 DIRENT_FLAG_INCLUDE_EMPTY,
133 0, list_dir_proc, &ls);
137 com_err(argv[1], retval, "");