Whamcloud - gitweb
e01fd2040786fc3f821423f99c6d7c7c0179b77c
[tools/e2fsprogs.git] / debugfs / ls.c
1 /*
2  * ls.c --- list directories
3  *
4  * Copyright (C) 1997 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <time.h>
14 #ifdef HAVE_ERRNO_H
15 #include <errno.h>
16 #endif
17 #include <sys/types.h>
18 #ifdef HAVE_GETOPT_H
19 #include <getopt.h>
20 #else
21 extern int optind;
22 extern char *optarg;
23 #endif
24
25 #include "debugfs.h"
26
27 /*
28  * list directory
29  */
30
31 #define LONG_OPT        0x0001
32 #define DELETED_OPT     0x0002
33 #define PARSE_OPT       0x0004
34
35 struct list_dir_struct {
36         FILE    *f;
37         int     col;
38         int     options;
39 };
40
41 static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42                                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
43
44 static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
45                          int    entry,
46                          struct ext2_dir_entry *dirent,
47                          int    offset EXT2FS_ATTR((unused)),
48                          int    blocksize EXT2FS_ATTR((unused)),
49                          char   *buf EXT2FS_ATTR((unused)),
50                          void   *private)
51 {
52         struct ext2_inode       inode;
53         ext2_ino_t              ino;
54         struct tm               *tm_p;
55         time_t                  modtime;
56         char                    name[EXT2_NAME_LEN + 1];
57         char                    tmp[EXT2_NAME_LEN + 16];
58         char                    datestr[80];
59         char                    lbr, rbr;
60         int                     thislen;
61         struct list_dir_struct *ls = (struct list_dir_struct *) private;
62
63         thislen = dirent->name_len & 0xFF;
64         strncpy(name, dirent->name, thislen);
65         name[thislen] = '\0';
66         ino = dirent->inode;
67
68         if (entry == DIRENT_DELETED_FILE) {
69                 lbr = '<';
70                 rbr = '>';
71                 ino = 0;
72         } else {
73                 lbr = rbr = ' ';
74         }
75         if (ls->options & PARSE_OPT) {
76                 if (ino && debugfs_read_inode(ino, &inode, name)) return 0;
77                 fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name);
78                 if (LINUX_S_ISDIR(inode.i_mode))
79                         fprintf(ls->f, "/");
80                 else
81                         fprintf(ls->f, "%lld/", EXT2_I_SIZE(&inode));
82                 fprintf(ls->f, "\n");
83         }
84         else if (ls->options & LONG_OPT) {
85                 if (ino) {
86                         if (debugfs_read_inode(ino, &inode, name))
87                                 return 0;
88                         modtime = inode.i_mtime;
89                         tm_p = localtime(&modtime);
90                         sprintf(datestr, "%2d-%s-%4d %02d:%02d",
91                                 tm_p->tm_mday, monstr[tm_p->tm_mon],
92                                 1900 + tm_p->tm_year, tm_p->tm_hour,
93                                 tm_p->tm_min);
94                 } else {
95                         strcpy(datestr, "                 ");
96                         memset(&inode, 0, sizeof(struct ext2_inode));
97                 }
98                 fprintf(ls->f, "%c%6u%c %6o (%d)  %5d  %5d   ", lbr, ino, rbr,
99                         inode.i_mode, dirent->name_len >> 8,
100                         inode_uid(inode), inode_gid(inode));
101                 if (LINUX_S_ISDIR(inode.i_mode))
102                         fprintf(ls->f, "%5d", inode.i_size);
103                 else
104                         fprintf(ls->f, "%5llu", EXT2_I_SIZE(&inode));
105                 fprintf (ls->f, " %s %s\n", datestr, name);
106         } else {
107                 sprintf(tmp, "%c%u%c (%d) %s   ", lbr, dirent->inode, rbr,
108                         dirent->rec_len, name);
109                 thislen = strlen(tmp);
110
111                 if (ls->col + thislen > 80) {
112                         fprintf(ls->f, "\n");
113                         ls->col = 0;
114                 }
115                 fprintf(ls->f, "%s", tmp);
116                 ls->col += thislen;
117         }
118         return 0;
119 }
120
121 void do_list_dir(int argc, char *argv[])
122 {
123         ext2_ino_t      inode;
124         int             retval;
125         int             c;
126         int             flags;
127         struct list_dir_struct ls;
128
129         ls.options = 0;
130         if (check_fs_open(argv[0]))
131                 return;
132
133         reset_getopt();
134         while ((c = getopt (argc, argv, "dlp")) != EOF) {
135                 switch (c) {
136                 case 'l':
137                         ls.options |= LONG_OPT;
138                         break;
139                 case 'd':
140                         ls.options |= DELETED_OPT;
141                         break;
142                 case 'p':
143                         ls.options |= PARSE_OPT;
144                         break;
145                 default:
146                         goto print_usage;
147                 }
148         }
149
150         if (argc > optind+1) {
151         print_usage:
152                 com_err(0, 0, "Usage: ls [-l] [-d] [-p] file");
153                 return;
154         }
155
156         if (argc == optind)
157                 inode = cwd;
158         else
159                 inode = string_to_inode(argv[optind]);
160         if (!inode)
161                 return;
162
163         ls.f = open_pager();
164         ls.col = 0;
165         flags = DIRENT_FLAG_INCLUDE_EMPTY;
166         if (ls.options & DELETED_OPT)
167                 flags |= DIRENT_FLAG_INCLUDE_REMOVED;
168
169         retval = ext2fs_dir_iterate2(current_fs, inode, flags,
170                                     0, list_dir_proc, &ls);
171         fprintf(ls.f, "\n");
172         close_pager(ls.f);
173         if (retval)
174                 com_err(argv[1], retval, 0);
175
176         return;
177 }
178
179