ls.c revision 544349270e4c74a6feb971123884a8cf5052a7ee
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
21extern int optind;
22extern 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
34struct list_dir_struct {
35	FILE	*f;
36	int	col;
37	int	options;
38};
39
40static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
41				"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
42
43static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
44			 int	entry,
45			 struct ext2_dir_entry *dirent,
46			 int	offset EXT2FS_ATTR((unused)),
47			 int	blocksize EXT2FS_ATTR((unused)),
48			 char	*buf EXT2FS_ATTR((unused)),
49			 void	*private)
50{
51	struct ext2_inode	inode;
52	ext2_ino_t		ino;
53	struct tm		*tm_p;
54	time_t			modtime;
55	char			name[EXT2_NAME_LEN];
56	char			tmp[EXT2_NAME_LEN + 16];
57	char			datestr[80];
58	char			lbr, rbr;
59	int			thislen;
60	struct list_dir_struct *ls = (struct list_dir_struct *) private;
61
62	thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
63		(dirent->name_len & 0xFF) : EXT2_NAME_LEN;
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 & LONG_OPT) {
76		if (ino) {
77			if (debugfs_read_inode(ino, &inode, name))
78				return 0;
79			modtime = inode.i_mtime;
80			tm_p = localtime(&modtime);
81			sprintf(datestr, "%2d-%s-%4d %02d:%02d",
82				tm_p->tm_mday, monstr[tm_p->tm_mon],
83				1900 + tm_p->tm_year, tm_p->tm_hour,
84				tm_p->tm_min);
85		} else {
86			strcpy(datestr, "                 ");
87			memset(&inode, 0, sizeof(struct ext2_inode));
88		}
89		fprintf(ls->f, "%c%6u%c %6o (%d)  %5d  %5d   ", lbr, ino, rbr,
90			inode.i_mode, dirent->name_len >> 8,
91			inode.i_uid, inode.i_gid);
92		if (LINUX_S_ISDIR(inode.i_mode))
93			fprintf(ls->f, "%5d", inode.i_size);
94		else
95			fprintf(ls->f, "%5lld", inode.i_size |
96				((__u64)inode.i_size_high << 32));
97		fprintf (ls->f, " %s %s\n", datestr, name);
98	} else {
99		sprintf(tmp, "%c%u%c (%d) %s   ", lbr, dirent->inode, rbr,
100			dirent->rec_len, name);
101		thislen = strlen(tmp);
102
103		if (ls->col + thislen > 80) {
104			fprintf(ls->f, "\n");
105			ls->col = 0;
106		}
107		fprintf(ls->f, "%s", tmp);
108		ls->col += thislen;
109	}
110	return 0;
111}
112
113void do_list_dir(int argc, char *argv[])
114{
115	ext2_ino_t	inode;
116	int		retval;
117	int		c;
118	int		flags;
119	struct list_dir_struct ls;
120
121	ls.options = 0;
122	if (check_fs_open(argv[0]))
123		return;
124
125	reset_getopt();
126	while ((c = getopt (argc, argv, "dl")) != EOF) {
127		switch (c) {
128		case 'l':
129			ls.options |= LONG_OPT;
130			break;
131		case 'd':
132			ls.options |= DELETED_OPT;
133			break;
134		}
135	}
136
137	if (argc > optind+1) {
138		com_err(0, 0, "Usage: ls [-l] [-d] file");
139		return;
140	}
141
142	if (argc == optind)
143		inode = cwd;
144	else
145		inode = string_to_inode(argv[optind]);
146	if (!inode)
147		return;
148
149	ls.f = open_pager();
150	ls.col = 0;
151	flags = DIRENT_FLAG_INCLUDE_EMPTY;
152	if (ls.options & DELETED_OPT)
153		flags |= DIRENT_FLAG_INCLUDE_REMOVED;
154
155	retval = ext2fs_dir_iterate2(current_fs, inode, flags,
156				    0, list_dir_proc, &ls);
157	fprintf(ls.f, "\n");
158	close_pager(ls.f);
159	if (retval)
160		com_err(argv[1], retval, "");
161
162	return;
163}
164
165
166