blob: bee365c9da603361f77da826e005c33e6050917f [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/types.h>
5#include <dirent.h>
6#include <errno.h>
7
8#include <sys/stat.h>
9#include <unistd.h>
10#include <time.h>
11
12#include <pwd.h>
13#include <grp.h>
14
15#include <linux/kdev_t.h>
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -070016#include <limits.h>
17
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +010018#include "dynarray.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019
20// bits for flags argument
Andy McFaddenb33d3412009-04-08 19:25:35 -070021#define LIST_LONG (1 << 0)
22#define LIST_ALL (1 << 1)
23#define LIST_RECURSIVE (1 << 2)
24#define LIST_DIRECTORIES (1 << 3)
Andy McFadden327e6962009-08-18 11:10:03 -070025#define LIST_SIZE (1 << 4)
Brad Fitzpatricke7fe5bf2010-10-25 13:29:53 -070026#define LIST_LONG_NUMERIC (1 << 5)
Kenny Root40dac652011-07-13 09:14:33 -070027#define LIST_CLASSIFY (1 << 6)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
29// fwd
30static int listpath(const char *name, int flags);
31
32static char mode2kind(unsigned mode)
33{
34 switch(mode & S_IFMT){
35 case S_IFSOCK: return 's';
36 case S_IFLNK: return 'l';
37 case S_IFREG: return '-';
38 case S_IFDIR: return 'd';
39 case S_IFBLK: return 'b';
40 case S_IFCHR: return 'c';
41 case S_IFIFO: return 'p';
42 default: return '?';
43 }
44}
45
46static void mode2str(unsigned mode, char *out)
47{
48 *out++ = mode2kind(mode);
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +010049
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050 *out++ = (mode & 0400) ? 'r' : '-';
51 *out++ = (mode & 0200) ? 'w' : '-';
52 if(mode & 04000) {
53 *out++ = (mode & 0100) ? 's' : 'S';
54 } else {
55 *out++ = (mode & 0100) ? 'x' : '-';
56 }
57 *out++ = (mode & 040) ? 'r' : '-';
58 *out++ = (mode & 020) ? 'w' : '-';
59 if(mode & 02000) {
60 *out++ = (mode & 010) ? 's' : 'S';
61 } else {
62 *out++ = (mode & 010) ? 'x' : '-';
63 }
64 *out++ = (mode & 04) ? 'r' : '-';
65 *out++ = (mode & 02) ? 'w' : '-';
66 if(mode & 01000) {
67 *out++ = (mode & 01) ? 't' : 'T';
68 } else {
69 *out++ = (mode & 01) ? 'x' : '-';
70 }
71 *out = 0;
72}
73
74static void user2str(unsigned uid, char *out)
75{
76 struct passwd *pw = getpwuid(uid);
77 if(pw) {
78 strcpy(out, pw->pw_name);
79 } else {
80 sprintf(out, "%d", uid);
81 }
82}
83
84static void group2str(unsigned gid, char *out)
85{
86 struct group *gr = getgrgid(gid);
87 if(gr) {
88 strcpy(out, gr->gr_name);
89 } else {
90 sprintf(out, "%d", gid);
91 }
92}
93
Andy McFadden9feee022009-09-27 15:13:56 -070094static int show_total_size(const char *dirname, DIR *d, int flags)
95{
96 struct dirent *de;
97 char tmp[1024];
98 struct stat s;
99 int sum = 0;
100
101 /* run through the directory and sum up the file block sizes */
102 while ((de = readdir(d)) != 0) {
103 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
104 continue;
105 if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0)
106 continue;
107
108 if (strcmp(dirname, "/") == 0)
109 snprintf(tmp, sizeof(tmp), "/%s", de->d_name);
110 else
111 snprintf(tmp, sizeof(tmp), "%s/%s", dirname, de->d_name);
112
113 if (lstat(tmp, &s) < 0) {
114 fprintf(stderr, "stat failed on %s: %s\n", tmp, strerror(errno));
115 rewinddir(d);
116 return -1;
117 }
118
119 sum += s.st_blocks / 2;
120 }
121
122 printf("total %d\n", sum);
123 rewinddir(d);
124 return 0;
125}
126
127static int listfile_size(const char *path, const char *filename, int flags)
Andy McFadden327e6962009-08-18 11:10:03 -0700128{
129 struct stat s;
130
Andy McFadden9feee022009-09-27 15:13:56 -0700131 if (lstat(path, &s) < 0) {
132 fprintf(stderr, "lstat '%s' failed: %s\n", path, strerror(errno));
Andy McFadden327e6962009-08-18 11:10:03 -0700133 return -1;
Andy McFadden9feee022009-09-27 15:13:56 -0700134 }
Andy McFadden327e6962009-08-18 11:10:03 -0700135
136 /* blocks are 512 bytes, we want output to be KB */
Kenny Root40dac652011-07-13 09:14:33 -0700137 if ((flags & LIST_SIZE) != 0) {
138 printf("%lld ", s.st_blocks / 2);
139 }
140
141 if ((flags & LIST_CLASSIFY) != 0) {
142 char filetype = mode2kind(s.st_mode);
143 if (filetype != 'l') {
144 printf("%c ", filetype);
145 } else {
146 struct stat link_dest;
147 if (!stat(path, &link_dest)) {
148 printf("l%c ", mode2kind(link_dest.st_mode));
149 } else {
150 fprintf(stderr, "stat '%s' failed: %s\n", path, strerror(errno));
151 printf("l? ");
152 }
153 }
154 }
155
156 printf("%s\n", filename);
157
Andy McFadden327e6962009-08-18 11:10:03 -0700158 return 0;
159}
160
161static int listfile_long(const char *path, int flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162{
163 struct stat s;
164 char date[32];
165 char mode[16];
166 char user[16];
167 char group[16];
168 const char *name;
169
170 /* name is anything after the final '/', or the whole path if none*/
171 name = strrchr(path, '/');
172 if(name == 0) {
173 name = path;
174 } else {
175 name++;
176 }
177
178 if(lstat(path, &s) < 0) {
179 return -1;
180 }
181
182 mode2str(s.st_mode, mode);
Brad Fitzpatricke7fe5bf2010-10-25 13:29:53 -0700183 if (flags & LIST_LONG_NUMERIC) {
184 sprintf(user, "%ld", s.st_uid);
185 sprintf(group, "%ld", s.st_gid);
186 } else {
187 user2str(s.st_uid, user);
188 group2str(s.st_gid, group);
189 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190
191 strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s.st_mtime));
192 date[31] = 0;
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100193
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
195// MMMMMMMM UUUUUUUU GGGGGGGGG XXXXXXXX YYYY-MM-DD HH:MM NAME (->LINK)
196
197 switch(s.st_mode & S_IFMT) {
198 case S_IFBLK:
199 case S_IFCHR:
200 printf("%s %-8s %-8s %3d, %3d %s %s\n",
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100201 mode, user, group,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 (int) MAJOR(s.st_rdev), (int) MINOR(s.st_rdev),
203 date, name);
204 break;
205 case S_IFREG:
Kenny Rooteb421702010-06-25 09:08:05 -0700206 printf("%s %-8s %-8s %8lld %s %s\n",
207 mode, user, group, s.st_size, date, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 break;
209 case S_IFLNK: {
210 char linkto[256];
211 int len;
212
213 len = readlink(path, linkto, 256);
214 if(len < 0) return -1;
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100215
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 if(len > 255) {
217 linkto[252] = '.';
218 linkto[253] = '.';
219 linkto[254] = '.';
220 linkto[255] = 0;
221 } else {
222 linkto[len] = 0;
223 }
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100224
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 printf("%s %-8s %-8s %s %s -> %s\n",
226 mode, user, group, date, name, linkto);
227 break;
228 }
229 default:
230 printf("%s %-8s %-8s %s %s\n",
231 mode, user, group, date, name);
232
233 }
234 return 0;
235}
236
Andy McFadden327e6962009-08-18 11:10:03 -0700237static int listfile(const char *dirname, const char *filename, int flags)
238{
Kenny Root40dac652011-07-13 09:14:33 -0700239 if ((flags & (LIST_LONG | LIST_SIZE | LIST_CLASSIFY)) == 0) {
Andy McFadden327e6962009-08-18 11:10:03 -0700240 printf("%s\n", filename);
241 return 0;
242 }
243
244 char tmp[4096];
245 const char* pathname = filename;
246
247 if (dirname != NULL) {
248 snprintf(tmp, sizeof(tmp), "%s/%s", dirname, filename);
249 pathname = tmp;
250 } else {
251 pathname = filename;
252 }
253
254 if ((flags & LIST_LONG) != 0) {
255 return listfile_long(pathname, flags);
256 } else /*((flags & LIST_SIZE) != 0)*/ {
Andy McFadden9feee022009-09-27 15:13:56 -0700257 return listfile_size(pathname, filename, flags);
Andy McFadden327e6962009-08-18 11:10:03 -0700258 }
259}
260
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261static int listdir(const char *name, int flags)
262{
263 char tmp[4096];
264 DIR *d;
265 struct dirent *de;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700266 strlist_t files = STRLIST_INITIALIZER;
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100267
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 d = opendir(name);
269 if(d == 0) {
270 fprintf(stderr, "opendir failed, %s\n", strerror(errno));
271 return -1;
272 }
273
Andy McFadden9feee022009-09-27 15:13:56 -0700274 if ((flags & LIST_SIZE) != 0) {
275 show_total_size(name, d, flags);
276 }
277
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 while((de = readdir(d)) != 0){
279 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
280 if(de->d_name[0] == '.' && (flags & LIST_ALL) == 0) continue;
Andy McFadden327e6962009-08-18 11:10:03 -0700281
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700282 strlist_append_dup(&files, de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 }
284
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700285 strlist_sort(&files);
286 STRLIST_FOREACH(&files, filename, listfile(name, filename, flags));
287 strlist_done(&files);
288
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 if (flags & LIST_RECURSIVE) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700290 strlist_t subdirs = STRLIST_INITIALIZER;
291
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 rewinddir(d);
293
294 while ((de = readdir(d)) != 0) {
295 struct stat s;
296 int err;
297
298 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
299 continue;
300 if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0)
301 continue;
302
Andy McFadden327e6962009-08-18 11:10:03 -0700303 if (!strcmp(name, "/"))
304 snprintf(tmp, sizeof(tmp), "/%s", de->d_name);
305 else
306 snprintf(tmp, sizeof(tmp), "%s/%s", name, de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307
308 /*
309 * If the name ends in a '/', use stat() so we treat it like a
310 * directory even if it's a symlink.
311 */
312 if (tmp[strlen(tmp)-1] == '/')
313 err = stat(tmp, &s);
314 else
315 err = lstat(tmp, &s);
316
317 if (err < 0) {
318 perror(tmp);
319 closedir(d);
320 return -1;
321 }
322
323 if (S_ISDIR(s.st_mode)) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700324 strlist_append_dup(&subdirs, tmp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 }
326 }
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700327 strlist_sort(&subdirs);
328 STRLIST_FOREACH(&subdirs, path, {
329 printf("\n%s:\n", path);
330 listdir(path, flags);
331 });
332 strlist_done(&subdirs);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 }
334
335 closedir(d);
336 return 0;
337}
338
339static int listpath(const char *name, int flags)
340{
341 struct stat s;
342 int err;
343
344 /*
345 * If the name ends in a '/', use stat() so we treat it like a
346 * directory even if it's a symlink.
347 */
348 if (name[strlen(name)-1] == '/')
349 err = stat(name, &s);
350 else
351 err = lstat(name, &s);
352
353 if (err < 0) {
354 perror(name);
355 return -1;
356 }
357
Andy McFaddenb33d3412009-04-08 19:25:35 -0700358 if ((flags & LIST_DIRECTORIES) == 0 && S_ISDIR(s.st_mode)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 if (flags & LIST_RECURSIVE)
360 printf("\n%s:\n", name);
361 return listdir(name, flags);
362 } else {
Andy McFadden327e6962009-08-18 11:10:03 -0700363 /* yeah this calls stat() again*/
364 return listfile(NULL, name, flags);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 }
366}
367
368int ls_main(int argc, char **argv)
369{
370 int flags = 0;
371 int listed = 0;
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100372
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373 if(argc > 1) {
374 int i;
375 int err = 0;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700376 strlist_t files = STRLIST_INITIALIZER;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377
378 for (i = 1; i < argc; i++) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700379 if (argv[i][0] == '-') {
380 /* an option ? */
381 const char *arg = argv[i]+1;
382 while (arg[0]) {
383 switch (arg[0]) {
384 case 'l': flags |= LIST_LONG; break;
Brad Fitzpatricke7fe5bf2010-10-25 13:29:53 -0700385 case 'n': flags |= LIST_LONG | LIST_LONG_NUMERIC; break;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700386 case 's': flags |= LIST_SIZE; break;
387 case 'R': flags |= LIST_RECURSIVE; break;
388 case 'd': flags |= LIST_DIRECTORIES; break;
389 case 'a': flags |= LIST_ALL; break;
Kenny Root40dac652011-07-13 09:14:33 -0700390 case 'F': flags |= LIST_CLASSIFY; break;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700391 default:
392 fprintf(stderr, "%s: Unknown option '-%c'. Aborting.\n", "ls", arg[0]);
393 exit(1);
394 }
395 arg++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396 }
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700397 } else {
398 /* not an option ? */
399 strlist_append_dup(&files, argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 }
401 }
402
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700403 if (files.count > 0) {
404 STRLIST_FOREACH(&files, path, {
405 if (listpath(path, flags) != 0) {
406 err = EXIT_FAILURE;
407 }
408 });
409 strlist_done(&files);
410 return err;
411 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 }
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100413
414 // list working directory if no files or directories were specified
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 return listpath(".", flags);
416}