blob: aa0113b8cd57070605eea8656f07feebd889251f [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
18// dynamic arrays
19typedef struct {
20 int count;
21 int capacity;
22 void** items;
23} dynarray_t;
24
25#define DYNARRAY_INITIALIZER { 0, 0, NULL }
26
27static void dynarray_init( dynarray_t *a )
28{
29 a->count = a->capacity = 0;
30 a->items = NULL;
31}
32
33static void dynarray_reserve_more( dynarray_t *a, int count )
34{
35 int old_cap = a->capacity;
36 int new_cap = old_cap;
37 const int max_cap = INT_MAX/sizeof(void*);
38 void** new_items;
39 int new_count = a->count + count;
40
41 if (count <= 0)
42 return;
43
44 if (count > max_cap - a->count)
45 abort();
46
47 new_count = a->count + count;
48
49 while (new_cap < new_count) {
50 old_cap = new_cap;
51 new_cap += (new_cap >> 2) + 4;
52 if (new_cap < old_cap || new_cap > max_cap) {
53 new_cap = max_cap;
54 }
55 }
56 new_items = realloc(a->items, new_cap*sizeof(void*));
57 if (new_items == NULL)
58 abort();
59
60 a->items = new_items;
61 a->capacity = new_cap;
62}
63
64static void dynarray_append( dynarray_t *a, void* item )
65{
66 if (a->count >= a->capacity)
67 dynarray_reserve_more(a, 1);
68
69 a->items[a->count++] = item;
70}
71
72static void dynarray_done( dynarray_t *a )
73{
74 free(a->items);
75 a->items = NULL;
76 a->count = a->capacity = 0;
77}
78
79#define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \
80 do { \
81 int _nn_##__LINE__ = 0; \
82 for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \
83 _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \
84 _stmnt; \
85 } \
86 } while (0)
87
88#define DYNARRAY_FOREACH(_array,_item,_stmnt) \
89 DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt)
90
91// string arrays
92
93typedef dynarray_t strlist_t;
94
95#define STRLIST_INITIALIZER DYNARRAY_INITIALIZER
96
97#define STRLIST_FOREACH(_list,_string,_stmnt) \
98 DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt)
99
100static void strlist_init( strlist_t *list )
101{
102 dynarray_init(list);
103}
104
105static void strlist_append_b( strlist_t *list, const void* str, size_t slen )
106{
107 char *copy = malloc(slen+1);
108 memcpy(copy, str, slen);
109 copy[slen] = '\0';
110 dynarray_append(list, copy);
111}
112
113static void strlist_append_dup( strlist_t *list, const char *str)
114{
115 strlist_append_b(list, str, strlen(str));
116}
117
118static void strlist_done( strlist_t *list )
119{
120 STRLIST_FOREACH(list, string, free(string));
121 dynarray_done(list);
122}
123
124static int strlist_compare_strings(const void* a, const void* b)
125{
126 const char *sa = *(const char **)a;
127 const char *sb = *(const char **)b;
128 return strcmp(sa, sb);
129}
130
131static void strlist_sort( strlist_t *list )
132{
133 if (list->count > 0) {
134 qsort(list->items,
135 (size_t)list->count,
136 sizeof(void*),
137 strlist_compare_strings);
138 }
139}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140
141// bits for flags argument
Andy McFaddenb33d3412009-04-08 19:25:35 -0700142#define LIST_LONG (1 << 0)
143#define LIST_ALL (1 << 1)
144#define LIST_RECURSIVE (1 << 2)
145#define LIST_DIRECTORIES (1 << 3)
Andy McFadden327e6962009-08-18 11:10:03 -0700146#define LIST_SIZE (1 << 4)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147
148// fwd
149static int listpath(const char *name, int flags);
150
151static char mode2kind(unsigned mode)
152{
153 switch(mode & S_IFMT){
154 case S_IFSOCK: return 's';
155 case S_IFLNK: return 'l';
156 case S_IFREG: return '-';
157 case S_IFDIR: return 'd';
158 case S_IFBLK: return 'b';
159 case S_IFCHR: return 'c';
160 case S_IFIFO: return 'p';
161 default: return '?';
162 }
163}
164
165static void mode2str(unsigned mode, char *out)
166{
167 *out++ = mode2kind(mode);
168
169 *out++ = (mode & 0400) ? 'r' : '-';
170 *out++ = (mode & 0200) ? 'w' : '-';
171 if(mode & 04000) {
172 *out++ = (mode & 0100) ? 's' : 'S';
173 } else {
174 *out++ = (mode & 0100) ? 'x' : '-';
175 }
176 *out++ = (mode & 040) ? 'r' : '-';
177 *out++ = (mode & 020) ? 'w' : '-';
178 if(mode & 02000) {
179 *out++ = (mode & 010) ? 's' : 'S';
180 } else {
181 *out++ = (mode & 010) ? 'x' : '-';
182 }
183 *out++ = (mode & 04) ? 'r' : '-';
184 *out++ = (mode & 02) ? 'w' : '-';
185 if(mode & 01000) {
186 *out++ = (mode & 01) ? 't' : 'T';
187 } else {
188 *out++ = (mode & 01) ? 'x' : '-';
189 }
190 *out = 0;
191}
192
193static void user2str(unsigned uid, char *out)
194{
195 struct passwd *pw = getpwuid(uid);
196 if(pw) {
197 strcpy(out, pw->pw_name);
198 } else {
199 sprintf(out, "%d", uid);
200 }
201}
202
203static void group2str(unsigned gid, char *out)
204{
205 struct group *gr = getgrgid(gid);
206 if(gr) {
207 strcpy(out, gr->gr_name);
208 } else {
209 sprintf(out, "%d", gid);
210 }
211}
212
Andy McFadden327e6962009-08-18 11:10:03 -0700213static int listfile_size(const char *path, int flags)
214{
215 struct stat s;
216
217 if (lstat(path, &s) < 0)
218 return -1;
219
220 /* blocks are 512 bytes, we want output to be KB */
221 printf("%lld %s\n", s.st_blocks / 2, path);
222 return 0;
223}
224
225static int listfile_long(const char *path, int flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226{
227 struct stat s;
228 char date[32];
229 char mode[16];
230 char user[16];
231 char group[16];
232 const char *name;
233
234 /* name is anything after the final '/', or the whole path if none*/
235 name = strrchr(path, '/');
236 if(name == 0) {
237 name = path;
238 } else {
239 name++;
240 }
241
242 if(lstat(path, &s) < 0) {
243 return -1;
244 }
245
246 mode2str(s.st_mode, mode);
247 user2str(s.st_uid, user);
248 group2str(s.st_gid, group);
249
250 strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s.st_mtime));
251 date[31] = 0;
252
253// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
254// MMMMMMMM UUUUUUUU GGGGGGGGG XXXXXXXX YYYY-MM-DD HH:MM NAME (->LINK)
255
256 switch(s.st_mode & S_IFMT) {
257 case S_IFBLK:
258 case S_IFCHR:
259 printf("%s %-8s %-8s %3d, %3d %s %s\n",
260 mode, user, group,
261 (int) MAJOR(s.st_rdev), (int) MINOR(s.st_rdev),
262 date, name);
263 break;
264 case S_IFREG:
265 printf("%s %-8s %-8s %8d %s %s\n",
266 mode, user, group, (int) s.st_size, date, name);
267 break;
268 case S_IFLNK: {
269 char linkto[256];
270 int len;
271
272 len = readlink(path, linkto, 256);
273 if(len < 0) return -1;
274
275 if(len > 255) {
276 linkto[252] = '.';
277 linkto[253] = '.';
278 linkto[254] = '.';
279 linkto[255] = 0;
280 } else {
281 linkto[len] = 0;
282 }
283
284 printf("%s %-8s %-8s %s %s -> %s\n",
285 mode, user, group, date, name, linkto);
286 break;
287 }
288 default:
289 printf("%s %-8s %-8s %s %s\n",
290 mode, user, group, date, name);
291
292 }
293 return 0;
294}
295
Andy McFadden327e6962009-08-18 11:10:03 -0700296static int listfile(const char *dirname, const char *filename, int flags)
297{
298 if ((flags & (LIST_LONG | LIST_SIZE)) == 0) {
299 printf("%s\n", filename);
300 return 0;
301 }
302
303 char tmp[4096];
304 const char* pathname = filename;
305
306 if (dirname != NULL) {
307 snprintf(tmp, sizeof(tmp), "%s/%s", dirname, filename);
308 pathname = tmp;
309 } else {
310 pathname = filename;
311 }
312
313 if ((flags & LIST_LONG) != 0) {
314 return listfile_long(pathname, flags);
315 } else /*((flags & LIST_SIZE) != 0)*/ {
316 return listfile_size(pathname, flags);
317 }
318}
319
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320static int listdir(const char *name, int flags)
321{
322 char tmp[4096];
323 DIR *d;
324 struct dirent *de;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700325 strlist_t files = STRLIST_INITIALIZER;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326
327 d = opendir(name);
328 if(d == 0) {
329 fprintf(stderr, "opendir failed, %s\n", strerror(errno));
330 return -1;
331 }
332
333 while((de = readdir(d)) != 0){
334 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
335 if(de->d_name[0] == '.' && (flags & LIST_ALL) == 0) continue;
Andy McFadden327e6962009-08-18 11:10:03 -0700336
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700337 strlist_append_dup(&files, de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 }
339
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700340 strlist_sort(&files);
341 STRLIST_FOREACH(&files, filename, listfile(name, filename, flags));
342 strlist_done(&files);
343
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 if (flags & LIST_RECURSIVE) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700345 strlist_t subdirs = STRLIST_INITIALIZER;
346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 rewinddir(d);
348
349 while ((de = readdir(d)) != 0) {
350 struct stat s;
351 int err;
352
353 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
354 continue;
355 if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0)
356 continue;
357
Andy McFadden327e6962009-08-18 11:10:03 -0700358 if (!strcmp(name, "/"))
359 snprintf(tmp, sizeof(tmp), "/%s", de->d_name);
360 else
361 snprintf(tmp, sizeof(tmp), "%s/%s", name, de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362
363 /*
364 * If the name ends in a '/', use stat() so we treat it like a
365 * directory even if it's a symlink.
366 */
367 if (tmp[strlen(tmp)-1] == '/')
368 err = stat(tmp, &s);
369 else
370 err = lstat(tmp, &s);
371
372 if (err < 0) {
373 perror(tmp);
374 closedir(d);
375 return -1;
376 }
377
378 if (S_ISDIR(s.st_mode)) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700379 strlist_append_dup(&subdirs, tmp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 }
381 }
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700382 strlist_sort(&subdirs);
383 STRLIST_FOREACH(&subdirs, path, {
384 printf("\n%s:\n", path);
385 listdir(path, flags);
386 });
387 strlist_done(&subdirs);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 }
389
390 closedir(d);
391 return 0;
392}
393
394static int listpath(const char *name, int flags)
395{
396 struct stat s;
397 int err;
398
399 /*
400 * If the name ends in a '/', use stat() so we treat it like a
401 * directory even if it's a symlink.
402 */
403 if (name[strlen(name)-1] == '/')
404 err = stat(name, &s);
405 else
406 err = lstat(name, &s);
407
408 if (err < 0) {
409 perror(name);
410 return -1;
411 }
412
Andy McFaddenb33d3412009-04-08 19:25:35 -0700413 if ((flags & LIST_DIRECTORIES) == 0 && S_ISDIR(s.st_mode)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 if (flags & LIST_RECURSIVE)
415 printf("\n%s:\n", name);
416 return listdir(name, flags);
417 } else {
Andy McFadden327e6962009-08-18 11:10:03 -0700418 /* yeah this calls stat() again*/
419 return listfile(NULL, name, flags);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 }
421}
422
423int ls_main(int argc, char **argv)
424{
425 int flags = 0;
426 int listed = 0;
427
428 if(argc > 1) {
429 int i;
430 int err = 0;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700431 strlist_t files = STRLIST_INITIALIZER;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432
433 for (i = 1; i < argc; i++) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700434 if (argv[i][0] == '-') {
435 /* an option ? */
436 const char *arg = argv[i]+1;
437 while (arg[0]) {
438 switch (arg[0]) {
439 case 'l': flags |= LIST_LONG; break;
440 case 's': flags |= LIST_SIZE; break;
441 case 'R': flags |= LIST_RECURSIVE; break;
442 case 'd': flags |= LIST_DIRECTORIES; break;
443 case 'a': flags |= LIST_ALL; break;
444 default:
445 fprintf(stderr, "%s: Unknown option '-%c'. Aborting.\n", "ls", arg[0]);
446 exit(1);
447 }
448 arg++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 }
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700450 } else {
451 /* not an option ? */
452 strlist_append_dup(&files, argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 }
454 }
455
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700456 if (files.count > 0) {
457 STRLIST_FOREACH(&files, path, {
458 if (listpath(path, flags) != 0) {
459 err = EXIT_FAILURE;
460 }
461 });
462 strlist_done(&files);
463 return err;
464 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 }
466
467 // list working directory if no files or directories were specified
468 return listpath(".", flags);
469}