blob: 4c96db46e64315e3ce8c10c61d7708dc3e213368 [file] [log] [blame]
Theodore Ts'o521e3681997-04-29 17:48:10 +00001/*
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
19#include "debugfs.h"
20
21/*
22 * list directory
23 */
24
25#define LONG_OPT 0x0001
26
27struct list_dir_struct {
28 FILE *f;
29 int col;
30 int options;
31};
32
33static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
34 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
35
36static void ls_l_file(struct list_dir_struct *ls, char *name, ino_t ino)
37{
38 struct ext2_inode inode;
39 errcode_t retval;
40 struct tm *tm_p;
41 time_t modtime;
42 char datestr[80];
43
44 retval = ext2fs_read_inode(current_fs, ino, &inode);
45 if (retval) {
Theodore Ts'of1304811997-10-25 03:51:53 +000046 fprintf(ls->f, "%5ld --- error --- %s\n", retval, name);
Theodore Ts'o521e3681997-04-29 17:48:10 +000047 return;
48 }
49 modtime = inode.i_mtime;
50 tm_p = localtime(&modtime);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000051 sprintf(datestr, "%2d-%s-%4d %02d:%02d",
52 tm_p->tm_mday, monstr[tm_p->tm_mon], 1900 + tm_p->tm_year,
Theodore Ts'o521e3681997-04-29 17:48:10 +000053 tm_p->tm_hour, tm_p->tm_min);
Theodore Ts'o36a43d61998-03-24 16:17:51 +000054 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);
58 else
59 fprintf(ls->f, "%5lld", inode.i_size |
60 ((__u64)inode.i_size_high << 32));
61 fprintf (ls->f, " %s %s\n", datestr, name);
Theodore Ts'o521e3681997-04-29 17:48:10 +000062}
63
64static void ls_file(struct list_dir_struct *ls, char *name,
65 ino_t ino, int rec_len)
66{
67 char tmp[EXT2_NAME_LEN + 16];
68 int thislen;
69
Theodore Ts'of1304811997-10-25 03:51:53 +000070 sprintf(tmp, "%ld (%d) %s ", ino, rec_len, name);
Theodore Ts'o521e3681997-04-29 17:48:10 +000071 thislen = strlen(tmp);
72
73 if (ls->col + thislen > 80) {
74 fprintf(ls->f, "\n");
75 ls->col = 0;
76 }
77 fprintf(ls->f, "%s", tmp);
78 ls->col += thislen;
79}
80
81
82static int list_dir_proc(struct ext2_dir_entry *dirent,
83 int offset,
84 int blocksize,
85 char *buf,
86 void *private)
87{
88 char name[EXT2_NAME_LEN];
Theodore Ts'o521e3681997-04-29 17:48:10 +000089
90 struct list_dir_struct *ls = (struct list_dir_struct *) private;
91 int thislen;
92
Theodore Ts'occe382b1998-03-09 13:07:09 +000093 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
94 (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
Theodore Ts'o521e3681997-04-29 17:48:10 +000095 strncpy(name, dirent->name, thislen);
96 name[thislen] = '\0';
97
98 if (ls->options & LONG_OPT)
99 ls_l_file(ls, name, dirent->inode);
100 else
101 ls_file(ls, name, dirent->inode, dirent->rec_len);
102
103 return 0;
104}
105
106void do_list_dir(int argc, char *argv[])
107{
108 ino_t inode;
109 int retval;
110 struct list_dir_struct ls;
111 int argptr = 1;
112
113 ls.options = 0;
114 if (check_fs_open(argv[0]))
115 return;
116
117 if ((argc > argptr) && (argv[argptr][0] == '-')) {
118 argptr++;
119 ls.options = LONG_OPT;
120 }
121
122 if (argc <= argptr)
123 inode = cwd;
124 else
125 inode = string_to_inode(argv[argptr]);
126 if (!inode)
127 return;
128
129 ls.f = open_pager();
130 ls.col = 0;
131 retval = ext2fs_dir_iterate(current_fs, inode,
132 DIRENT_FLAG_INCLUDE_EMPTY,
133 0, list_dir_proc, &ls);
134 fprintf(ls.f, "\n");
135 close_pager(ls.f);
136 if (retval)
137 com_err(argv[1], retval, "");
138
139 return;
140}
141
142