implement ping using new network stack
[barebox-mini2440.git] / commands / ls.c
bloba02ccfe7721e4bbb0e984de2e3cd4679092ca0b8
1 /*
2 * ls.c - list files and directories
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <common.h>
24 #include <command.h>
25 #include <fs.h>
26 #include <linux/stat.h>
27 #include <errno.h>
28 #include <malloc.h>
29 #include <getopt.h>
30 #include <stringlist.h>
32 static void ls_one(const char *path, struct stat *s)
34 char modestr[11];
35 unsigned long namelen = strlen(path);
37 mkmodestr(s->st_mode, modestr);
38 printf("%s %10u %*.*s\n", modestr, s->st_size, namelen, namelen, path);
41 int ls(const char *path, ulong flags)
43 DIR *dir;
44 struct dirent *d;
45 char tmp[PATH_MAX];
46 struct stat s;
47 struct string_list sl;
49 string_list_init(&sl);
51 if (stat(path, &s))
52 return errno;
54 if (flags & LS_SHOWARG && s.st_mode & S_IFDIR)
55 printf("%s:\n", path);
57 if (!(s.st_mode & S_IFDIR)) {
58 ls_one(path, &s);
59 return 0;
62 dir = opendir(path);
63 if (!dir)
64 return errno;
66 while ((d = readdir(dir))) {
67 sprintf(tmp, "%s/%s", path, d->d_name);
68 if (stat(tmp, &s))
69 goto out;
70 if (flags & LS_COLUMN)
71 string_list_add(&sl, d->d_name);
72 else
73 ls_one(d->d_name, &s);
76 closedir(dir);
78 if (flags & LS_COLUMN) {
79 string_list_print_by_column(&sl);
80 string_list_free(&sl);
83 if (!(flags & LS_RECURSIVE))
84 return 0;
86 dir = opendir(path);
87 if (!dir) {
88 errno = -ENOENT;
89 return -ENOENT;
92 while ((d = readdir(dir))) {
94 if (!strcmp(d->d_name, "."))
95 continue;
96 if (!strcmp(d->d_name, ".."))
97 continue;
98 sprintf(tmp, "%s/%s", path, d->d_name);
100 if (stat(tmp, &s))
101 goto out;
102 if (s.st_mode & S_IFDIR) {
103 char *norm = normalise_path(tmp);
104 ls(norm, flags);
105 free(norm);
108 out:
109 closedir(dir);
111 return 0;
114 static int do_ls(struct command *cmdtp, int argc, char *argv[])
116 int ret, opt, o;
117 struct stat s;
118 ulong flags = LS_COLUMN;
119 struct string_list sl;
121 while((opt = getopt(argc, argv, "RCl")) > 0) {
122 switch(opt) {
123 case 'R':
124 flags |= LS_RECURSIVE | LS_SHOWARG;
125 break;
126 case 'C':
127 flags |= LS_COLUMN;
128 break;
129 case 'l':
130 flags &= ~LS_COLUMN;
131 break;
135 if (argc - optind > 1)
136 flags |= LS_SHOWARG;
138 if (optind == argc) {
139 ret = ls(getcwd(), flags);
140 return ret ? 1 : 0;
143 string_list_init(&sl);
145 o = optind;
147 /* first pass: all files */
148 while (o < argc) {
149 ret = stat(argv[o], &s);
150 if (ret) {
151 printf("%s: %s: %s\n", argv[0],
152 argv[o], errno_str());
153 o++;
154 continue;
157 if (!(s.st_mode & S_IFDIR)) {
158 if (flags & LS_COLUMN)
159 string_list_add(&sl, argv[o]);
160 else
161 ls_one(argv[o], &s);
164 o++;
167 if (flags & LS_COLUMN)
168 string_list_print_by_column(&sl);
170 string_list_free(&sl);
172 o = optind;
174 /* second pass: directories */
175 while (o < argc) {
176 ret = stat(argv[o], &s);
177 if (ret) {
178 o++;
179 continue;
182 if (s.st_mode & S_IFDIR) {
183 ret = ls(argv[o], flags);
184 if (ret) {
185 perror("ls");
186 o++;
187 continue;
191 o++;
194 return 0;
197 static const __maybe_unused char cmd_ls_help[] =
198 "Usage: ls [OPTION]... [FILE]...\n"
199 "List information about the FILEs (the current directory by default).\n"
200 " -R list subdirectories recursively\n";
202 BAREBOX_CMD_START(ls)
203 .cmd = do_ls,
204 .usage = "list a file or directory",
205 BAREBOX_CMD_HELP(cmd_ls_help)
206 BAREBOX_CMD_END