blob: b08e37889ef0f54dc0024a4cd6efc81fc6e77837 [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)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
28// fwd
29static int listpath(const char *name, int flags);
30
31static char mode2kind(unsigned mode)
32{
33 switch(mode & S_IFMT){
34 case S_IFSOCK: return 's';
35 case S_IFLNK: return 'l';
36 case S_IFREG: return '-';
37 case S_IFDIR: return 'd';
38 case S_IFBLK: return 'b';
39 case S_IFCHR: return 'c';
40 case S_IFIFO: return 'p';
41 default: return '?';
42 }
43}
44
45static void mode2str(unsigned mode, char *out)
46{
47 *out++ = mode2kind(mode);
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +010048
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 *out++ = (mode & 0400) ? 'r' : '-';
50 *out++ = (mode & 0200) ? 'w' : '-';
51 if(mode & 04000) {
52 *out++ = (mode & 0100) ? 's' : 'S';
53 } else {
54 *out++ = (mode & 0100) ? 'x' : '-';
55 }
56 *out++ = (mode & 040) ? 'r' : '-';
57 *out++ = (mode & 020) ? 'w' : '-';
58 if(mode & 02000) {
59 *out++ = (mode & 010) ? 's' : 'S';
60 } else {
61 *out++ = (mode & 010) ? 'x' : '-';
62 }
63 *out++ = (mode & 04) ? 'r' : '-';
64 *out++ = (mode & 02) ? 'w' : '-';
65 if(mode & 01000) {
66 *out++ = (mode & 01) ? 't' : 'T';
67 } else {
68 *out++ = (mode & 01) ? 'x' : '-';
69 }
70 *out = 0;
71}
72
73static void user2str(unsigned uid, char *out)
74{
75 struct passwd *pw = getpwuid(uid);
76 if(pw) {
77 strcpy(out, pw->pw_name);
78 } else {
79 sprintf(out, "%d", uid);
80 }
81}
82
83static void group2str(unsigned gid, char *out)
84{
85 struct group *gr = getgrgid(gid);
86 if(gr) {
87 strcpy(out, gr->gr_name);
88 } else {
89 sprintf(out, "%d", gid);
90 }
91}
92
Andy McFadden9feee022009-09-27 15:13:56 -070093static int show_total_size(const char *dirname, DIR *d, int flags)
94{
95 struct dirent *de;
96 char tmp[1024];
97 struct stat s;
98 int sum = 0;
99
100 /* run through the directory and sum up the file block sizes */
101 while ((de = readdir(d)) != 0) {
102 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
103 continue;
104 if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0)
105 continue;
106
107 if (strcmp(dirname, "/") == 0)
108 snprintf(tmp, sizeof(tmp), "/%s", de->d_name);
109 else
110 snprintf(tmp, sizeof(tmp), "%s/%s", dirname, de->d_name);
111
112 if (lstat(tmp, &s) < 0) {
113 fprintf(stderr, "stat failed on %s: %s\n", tmp, strerror(errno));
114 rewinddir(d);
115 return -1;
116 }
117
118 sum += s.st_blocks / 2;
119 }
120
121 printf("total %d\n", sum);
122 rewinddir(d);
123 return 0;
124}
125
126static int listfile_size(const char *path, const char *filename, int flags)
Andy McFadden327e6962009-08-18 11:10:03 -0700127{
128 struct stat s;
129
Andy McFadden9feee022009-09-27 15:13:56 -0700130 if (lstat(path, &s) < 0) {
131 fprintf(stderr, "lstat '%s' failed: %s\n", path, strerror(errno));
Andy McFadden327e6962009-08-18 11:10:03 -0700132 return -1;
Andy McFadden9feee022009-09-27 15:13:56 -0700133 }
Andy McFadden327e6962009-08-18 11:10:03 -0700134
135 /* blocks are 512 bytes, we want output to be KB */
Andy McFadden9feee022009-09-27 15:13:56 -0700136 printf("%lld %s\n", s.st_blocks / 2, filename);
Andy McFadden327e6962009-08-18 11:10:03 -0700137 return 0;
138}
139
140static int listfile_long(const char *path, int flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141{
142 struct stat s;
143 char date[32];
144 char mode[16];
145 char user[16];
146 char group[16];
147 const char *name;
148
149 /* name is anything after the final '/', or the whole path if none*/
150 name = strrchr(path, '/');
151 if(name == 0) {
152 name = path;
153 } else {
154 name++;
155 }
156
157 if(lstat(path, &s) < 0) {
158 return -1;
159 }
160
161 mode2str(s.st_mode, mode);
Brad Fitzpatricke7fe5bf2010-10-25 13:29:53 -0700162 if (flags & LIST_LONG_NUMERIC) {
163 sprintf(user, "%ld", s.st_uid);
164 sprintf(group, "%ld", s.st_gid);
165 } else {
166 user2str(s.st_uid, user);
167 group2str(s.st_gid, group);
168 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
170 strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s.st_mtime));
171 date[31] = 0;
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100172
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
174// MMMMMMMM UUUUUUUU GGGGGGGGG XXXXXXXX YYYY-MM-DD HH:MM NAME (->LINK)
175
176 switch(s.st_mode & S_IFMT) {
177 case S_IFBLK:
178 case S_IFCHR:
179 printf("%s %-8s %-8s %3d, %3d %s %s\n",
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100180 mode, user, group,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 (int) MAJOR(s.st_rdev), (int) MINOR(s.st_rdev),
182 date, name);
183 break;
184 case S_IFREG:
Kenny Rooteb421702010-06-25 09:08:05 -0700185 printf("%s %-8s %-8s %8lld %s %s\n",
186 mode, user, group, s.st_size, date, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 break;
188 case S_IFLNK: {
189 char linkto[256];
190 int len;
191
192 len = readlink(path, linkto, 256);
193 if(len < 0) return -1;
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100194
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 if(len > 255) {
196 linkto[252] = '.';
197 linkto[253] = '.';
198 linkto[254] = '.';
199 linkto[255] = 0;
200 } else {
201 linkto[len] = 0;
202 }
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100203
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 printf("%s %-8s %-8s %s %s -> %s\n",
205 mode, user, group, date, name, linkto);
206 break;
207 }
208 default:
209 printf("%s %-8s %-8s %s %s\n",
210 mode, user, group, date, name);
211
212 }
213 return 0;
214}
215
Andy McFadden327e6962009-08-18 11:10:03 -0700216static int listfile(const char *dirname, const char *filename, int flags)
217{
218 if ((flags & (LIST_LONG | LIST_SIZE)) == 0) {
219 printf("%s\n", filename);
220 return 0;
221 }
222
223 char tmp[4096];
224 const char* pathname = filename;
225
226 if (dirname != NULL) {
227 snprintf(tmp, sizeof(tmp), "%s/%s", dirname, filename);
228 pathname = tmp;
229 } else {
230 pathname = filename;
231 }
232
233 if ((flags & LIST_LONG) != 0) {
234 return listfile_long(pathname, flags);
235 } else /*((flags & LIST_SIZE) != 0)*/ {
Andy McFadden9feee022009-09-27 15:13:56 -0700236 return listfile_size(pathname, filename, flags);
Andy McFadden327e6962009-08-18 11:10:03 -0700237 }
238}
239
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240static int listdir(const char *name, int flags)
241{
242 char tmp[4096];
243 DIR *d;
244 struct dirent *de;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700245 strlist_t files = STRLIST_INITIALIZER;
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100246
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 d = opendir(name);
248 if(d == 0) {
249 fprintf(stderr, "opendir failed, %s\n", strerror(errno));
250 return -1;
251 }
252
Andy McFadden9feee022009-09-27 15:13:56 -0700253 if ((flags & LIST_SIZE) != 0) {
254 show_total_size(name, d, flags);
255 }
256
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 while((de = readdir(d)) != 0){
258 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
259 if(de->d_name[0] == '.' && (flags & LIST_ALL) == 0) continue;
Andy McFadden327e6962009-08-18 11:10:03 -0700260
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700261 strlist_append_dup(&files, de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 }
263
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700264 strlist_sort(&files);
265 STRLIST_FOREACH(&files, filename, listfile(name, filename, flags));
266 strlist_done(&files);
267
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 if (flags & LIST_RECURSIVE) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700269 strlist_t subdirs = STRLIST_INITIALIZER;
270
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 rewinddir(d);
272
273 while ((de = readdir(d)) != 0) {
274 struct stat s;
275 int err;
276
277 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
278 continue;
279 if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0)
280 continue;
281
Andy McFadden327e6962009-08-18 11:10:03 -0700282 if (!strcmp(name, "/"))
283 snprintf(tmp, sizeof(tmp), "/%s", de->d_name);
284 else
285 snprintf(tmp, sizeof(tmp), "%s/%s", name, de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286
287 /*
288 * If the name ends in a '/', use stat() so we treat it like a
289 * directory even if it's a symlink.
290 */
291 if (tmp[strlen(tmp)-1] == '/')
292 err = stat(tmp, &s);
293 else
294 err = lstat(tmp, &s);
295
296 if (err < 0) {
297 perror(tmp);
298 closedir(d);
299 return -1;
300 }
301
302 if (S_ISDIR(s.st_mode)) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700303 strlist_append_dup(&subdirs, tmp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 }
305 }
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700306 strlist_sort(&subdirs);
307 STRLIST_FOREACH(&subdirs, path, {
308 printf("\n%s:\n", path);
309 listdir(path, flags);
310 });
311 strlist_done(&subdirs);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 }
313
314 closedir(d);
315 return 0;
316}
317
318static int listpath(const char *name, int flags)
319{
320 struct stat s;
321 int err;
322
323 /*
324 * If the name ends in a '/', use stat() so we treat it like a
325 * directory even if it's a symlink.
326 */
327 if (name[strlen(name)-1] == '/')
328 err = stat(name, &s);
329 else
330 err = lstat(name, &s);
331
332 if (err < 0) {
333 perror(name);
334 return -1;
335 }
336
Andy McFaddenb33d3412009-04-08 19:25:35 -0700337 if ((flags & LIST_DIRECTORIES) == 0 && S_ISDIR(s.st_mode)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 if (flags & LIST_RECURSIVE)
339 printf("\n%s:\n", name);
340 return listdir(name, flags);
341 } else {
Andy McFadden327e6962009-08-18 11:10:03 -0700342 /* yeah this calls stat() again*/
343 return listfile(NULL, name, flags);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 }
345}
346
347int ls_main(int argc, char **argv)
348{
349 int flags = 0;
350 int listed = 0;
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100351
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 if(argc > 1) {
353 int i;
354 int err = 0;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700355 strlist_t files = STRLIST_INITIALIZER;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356
357 for (i = 1; i < argc; i++) {
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700358 if (argv[i][0] == '-') {
359 /* an option ? */
360 const char *arg = argv[i]+1;
361 while (arg[0]) {
362 switch (arg[0]) {
363 case 'l': flags |= LIST_LONG; break;
Brad Fitzpatricke7fe5bf2010-10-25 13:29:53 -0700364 case 'n': flags |= LIST_LONG | LIST_LONG_NUMERIC; break;
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700365 case 's': flags |= LIST_SIZE; break;
366 case 'R': flags |= LIST_RECURSIVE; break;
367 case 'd': flags |= LIST_DIRECTORIES; break;
368 case 'a': flags |= LIST_ALL; break;
369 default:
370 fprintf(stderr, "%s: Unknown option '-%c'. Aborting.\n", "ls", arg[0]);
371 exit(1);
372 }
373 arg++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 }
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700375 } else {
376 /* not an option ? */
377 strlist_append_dup(&files, argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 }
379 }
380
David 'Digit' Turneraa2106b2010-06-04 10:44:56 -0700381 if (files.count > 0) {
382 STRLIST_FOREACH(&files, path, {
383 if (listpath(path, flags) != 0) {
384 err = EXIT_FAILURE;
385 }
386 });
387 strlist_done(&files);
388 return err;
389 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 }
David 'Digit' Turnera8d1afb2011-01-06 08:39:44 +0100391
392 // list working directory if no files or directories were specified
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 return listpath(".", flags);
394}