OSDN Git Service

Regularize command headers, update links to standards documents.
[android-x86/external-toybox.git] / toys / posix / ls.c
1 /* vi: set sw=4 ts=4:
2  *
3  * ls.c - list files
4  *
5  * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
6  * Copyright 2012 Rob Landley <rob@landley.net>
7  *
8  * See http://opengroup.org/onlinepubs/9699919799/utilities/ls.html
9
10 // "[-Cl]"
11 USE_LS(NEWTOY(ls, "goACFHLRSacdfiklmnpqrstux1", TOYFLAG_BIN))
12
13 config LS
14         bool "ls"
15         default y
16         help
17           usage: ls [-ACFHLRSacdfiklmnpqrstux1] [directory...]
18           list files
19
20           what to show:
21           -a    all files including .hidden
22           -c    use ctime for timestamps
23           -d    directory, not contents
24           -i    inode number
25           -k    block sizes in kilobytes
26           -p    put a '/' after directory names
27           -q    unprintable chars as '?'
28           -s    size (in blocks)
29           -u    use access time for timestamps
30           -A    list all files except . and ..
31           -H    follow command line symlinks
32           -L    follow symlinks
33           -R    recursively list files in subdirectories
34           -F    append file type indicator (/=dir, *=exe, @=symlink, |=FIFO)
35
36           output formats:
37           -1    list one file per line
38           -C    columns (sorted vertically)
39           -g    like -l but no owner
40           -l    long (show full details for each file)
41           -m    comma separated
42           -n    like -l but numeric uid/gid
43           -o    like -l but no group
44           -x    columns (sorted horizontally)
45
46           sorting (default is alphabetical):
47           -f    unsorted
48           -r    reverse
49           -t    timestamp
50           -S    size
51 */
52
53 #include "toys.h"
54
55 #define FLAG_1 (1<<0)
56 #define FLAG_x (1<<1)
57 #define FLAG_u (1<<2)
58 #define FLAG_t (1<<3)
59 #define FLAG_s (1<<4)
60 #define FLAG_r (1<<5)
61 #define FLAG_q (1<<6)
62 #define FLAG_p (1<<7)
63 #define FLAG_n (1<<8)
64 #define FLAG_m (1<<9)
65 #define FLAG_l (1<<10)
66 #define FLAG_k (1<<11)
67 #define FLAG_i (1<<12)
68 #define FLAG_f (1<<13)
69 #define FLAG_d (1<<14)
70 #define FLAG_c (1<<15)
71 #define FLAG_a (1<<16)
72 #define FLAG_S (1<<17)
73 #define FLAG_R (1<<18)
74 #define FLAG_L (1<<19)
75 #define FLAG_H (1<<20)
76 #define FLAG_F (1<<21)
77 #define FLAG_C (1<<22)
78 #define FLAG_A (1<<23)
79 #define FLAG_o (1<<24)
80 #define FLAG_g (1<<25)
81
82 // test sst output (suid/sticky in ls flaglist)
83
84 // ls -lR starts .: then ./subdir:
85
86 DEFINE_GLOBALS(
87     struct dirtree *files;
88
89     unsigned screen_width;
90     int nl_title;
91
92     // group and user can make overlapping use of the utoa() buf, so move it
93     char uid_buf[12];
94 )
95
96 #define TT this.ls
97
98 void dlist_to_dirtree(struct dirtree *parent)
99 {
100     // Turn double_list into dirtree
101     struct dirtree *dt = parent->child;
102     if (dt) {
103         dt->parent->next = NULL;
104         while (dt) {
105             dt->parent = parent;
106             dt = dt->next;
107         }
108     }
109 }
110
111 static char endtype(struct stat *st)
112 {
113     mode_t mode = st->st_mode;
114     if ((toys.optflags&(FLAG_F|FLAG_p)) && S_ISDIR(mode)) return '/';
115     if (toys.optflags & FLAG_F) {
116         if (S_ISLNK(mode)) return '@';
117         if (S_ISREG(mode) && (mode&0111)) return '*';
118         if (S_ISFIFO(mode)) return '|';
119         if (S_ISSOCK(mode)) return '=';
120     }
121     return 0;
122 }
123
124 static char *getusername(uid_t uid)
125 {
126     struct passwd *pw = getpwuid(uid);
127     utoa_to_buf(uid, TT.uid_buf, 12);
128     return pw ? pw->pw_name : TT.uid_buf;
129 }
130
131 static char *getgroupname(gid_t gid)
132 {
133     struct group *gr = getgrgid(gid);
134     return gr ? gr->gr_name : utoa(gid);
135 }
136
137 // Figure out size of printable entry fields for display indent/wrap
138
139 static void entrylen(struct dirtree *dt, unsigned *len)
140 {
141     struct stat *st = &(dt->st);
142     unsigned flags = toys.optflags;
143
144     *len = strlen(dt->name);
145     if (endtype(st)) ++*len;
146     if (flags & FLAG_m) ++*len;
147
148     if (flags & FLAG_i) *len += (len[1] = numlen(st->st_ino));
149     if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
150         unsigned fn = flags & FLAG_n;
151         len[2] = numlen(st->st_nlink);
152         len[3] = strlen(fn ? utoa(st->st_uid) : getusername(st->st_uid));
153         len[4] = strlen(fn ? utoa(st->st_gid) : getgroupname(st->st_gid));
154         len[5] = numlen(st->st_size);
155     }
156     if (flags & FLAG_s) *len += (len[6] = numlen(st->st_blocks));
157 }
158
159 static int compare(void *a, void *b)
160 {
161     struct dirtree *dta = *(struct dirtree **)a;
162     struct dirtree *dtb = *(struct dirtree **)b;
163     int ret = 0, reverse = (toys.optflags & FLAG_r) ? -1 : 1;
164
165     if (toys.optflags & FLAG_S) {
166         if (dta->st.st_size > dtb->st.st_size) ret = -1;
167         else if (dta->st.st_size < dtb->st.st_size) ret = 1;
168     }
169     if (toys.optflags & FLAG_t) {
170         if (dta->st.st_mtime > dtb->st.st_mtime) ret = -1;
171         else if (dta->st.st_mtime < dtb->st.st_mtime) ret = 1;
172     }
173     if (!ret) ret = strcmp(dta->name, dtb->name);
174     return ret * reverse;
175 }
176
177 // callback from dirtree_recurse() determining how to handle this entry.
178
179 static int filter(struct dirtree *new)
180 {
181     int flags = toys.optflags;
182
183     // Special case to handle enormous dirs without running out of memory.
184     if (flags == (FLAG_1|FLAG_f)) {
185         xprintf("%s\n", new->name);
186         return 0;
187     }
188
189     if (!(flags&FLAG_f)) { 
190         if (flags & FLAG_a) return 0;
191         if (!(flags & FLAG_A) && new->name[0]=='.') return 0;
192     }
193
194     if (flags & FLAG_u) new->st.st_mtime = new->st.st_atime;
195     if (flags & FLAG_c) new->st.st_mtime = new->st.st_ctime;
196     if (flags & FLAG_k) new->st.st_blocks = (new->st.st_blocks + 1) / 2;
197     return dirtree_notdotdot(new);
198 }
199
200 // For column view, calculate horizontal position (for padding) and return
201 // index of next entry to display.
202
203 static unsigned long next_column(unsigned long ul, unsigned long dtlen,
204     unsigned columns, unsigned *xpos)
205 {
206     unsigned long transition;
207     unsigned height, widecols;
208
209     // Horizontal sort is easy
210     if (!(toys.optflags & FLAG_C)) {
211         *xpos = ul % columns;
212         return ul;
213     }
214
215     // vertical sort
216
217     // For -x, calculate height of display, rounded up
218     height = (dtlen+columns-1)/columns;
219
220     // Sanity check: does wrapping render this column count impossible
221     // due to the right edge wrapping eating a whole row?
222     if (height*columns - dtlen >= height) {
223         *xpos = columns;
224         return 0;
225     }
226
227     // Uneven rounding goes along right edge
228     widecols = dtlen % height;
229     if (!widecols) widecols = height;
230     transition = widecols * columns;
231     if (ul < transition) {
232         *xpos =  ul % columns;
233         return (*xpos*height) + (ul/columns);
234     }
235
236     ul -= transition;
237     *xpos = ul % (columns-1);
238
239     return (*xpos*height) + widecols + (ul/(columns-1));
240 }
241
242 // Display a list of dirtree entries, according to current format
243 // Output types -1, -l, -C, or stream
244
245 static void listfiles(int dirfd, struct dirtree *indir)
246 {
247     struct dirtree *dt, **sort = 0;
248     unsigned long dtlen = 0, ul = 0;
249     unsigned width, flags = toys.optflags, totals[7], len[7],
250         *colsizes = (unsigned *)(toybuf+260), columns = (sizeof(toybuf)-260)/4;
251     
252     memset(totals, 0, sizeof(totals));
253
254     // Silently descend into single directory listed by itself on command line.
255     // In this case only show dirname/total header when given -R.
256     if (!indir->parent) {
257         if (!(dt = indir->child)) return;
258         if (S_ISDIR(dt->st.st_mode) && !dt->next && !(flags & FLAG_d)) {
259             dt->extra = 1;
260             listfiles(open(dt->name, 0), dt);
261             return;
262         }
263     } else {
264         // Read directory contents. We dup() the fd because this will close it.
265         indir->data = dup(dirfd);
266         dirtree_recurse(indir, filter, (flags&FLAG_L));
267     }
268
269     // Copy linked list to array and sort it. Directories go in array because
270     // we visit them in sorted order.
271
272     for (;;) {
273         for (dt = indir->child; dt; dt = dt->next) {
274             if (sort) sort[dtlen] = dt;
275             dtlen++;
276         }
277         if (sort) break;
278         sort = xmalloc(dtlen * sizeof(void *));
279         dtlen = 0;
280         continue;
281     }
282
283     // Label directory if not top of tree, or if -R
284     if (indir->parent && (!indir->extra || (flags & FLAG_R)))
285     {
286         char *path = dirtree_path(indir, 0);
287
288         if (TT.nl_title++) xputc('\n');
289         xprintf("%s:\n", path);
290         free(path);
291     }
292
293     if (!(flags & FLAG_f)) qsort(sort, dtlen, sizeof(void *), (void *)compare);
294
295     // Find largest entry in each field for display alignment
296     if (flags & (FLAG_C|FLAG_x)) {
297
298         // columns can't be more than toybuf can hold, or more than files,
299         // or > 1/2 screen width (one char filename, one space).
300         if (columns > TT.screen_width/2) columns = TT.screen_width/2;
301         if (columns > dtlen) columns = dtlen;
302
303         // Try to fit as many columns as we can, dropping down by one each time
304         for (;columns > 1; columns--) {
305             unsigned c, totlen = columns;
306
307             memset(colsizes, 0, columns*sizeof(unsigned));
308             for (ul=0; ul<dtlen; ul++) {
309                 entrylen(sort[next_column(ul, dtlen, columns, &c)], len);
310                 if (c == columns) break;
311                 // Does this put us over budget?
312                 if (*len > colsizes[c]) {
313                     totlen += *len-colsizes[c];
314                     colsizes[c] = *len;
315                     if (totlen > TT.screen_width) break;
316                 }
317             }
318             // If it fit, stop here
319             if (ul == dtlen) break;
320         }
321     } else if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g|FLAG_s)) {
322         unsigned long blocks = 0;
323
324         for (ul = 0; ul<dtlen; ul++)
325         {
326             entrylen(sort[ul], len);
327             for (width=0; width<6; width++)
328                 if (len[width] > totals[width]) totals[width] = len[width];
329             blocks += sort[ul]->st.st_blocks;
330         }
331
332         if (indir->parent) xprintf("total %lu\n", blocks);
333     }
334
335     // Loop through again to produce output.
336     memset(toybuf, ' ', 256);
337     width = 0;
338     for (ul = 0; ul<dtlen; ul++) {
339         unsigned curcol;
340         unsigned long next = next_column(ul, dtlen, columns, &curcol);
341         struct stat *st = &(sort[next]->st);
342         mode_t mode = st->st_mode;
343         char et = endtype(st);
344
345         // Skip directories at the top of the tree when -d isn't set
346         if (S_ISDIR(mode) && !indir->parent && !(flags & FLAG_d)) continue;
347         TT.nl_title=1;
348
349         // Handle padding and wrapping for display purposes
350         entrylen(sort[next], len);
351         if (ul) {
352             if (flags & FLAG_m) xputc(',');
353             if (flags & (FLAG_C|FLAG_x)) {
354                 if (!curcol) xputc('\n');
355             } else if ((flags & FLAG_1) || width+1+*len > TT.screen_width) {
356                 xputc('\n');
357                 width = 0;
358             } else {
359                 xputc(' ');
360                 width++;
361             }
362         }
363         width += *len;
364
365         if (flags & FLAG_i)
366             xprintf("% *lu ", len[1], (unsigned long)st->st_ino);
367         if (flags & FLAG_s)
368             xprintf("% *lu ", len[6], (unsigned long)st->st_blocks);
369
370         if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
371             struct tm *tm;
372             char perm[11], thyme[64], c, d, *usr, *upad, *grp, *grpad;
373             int i, bit;
374
375             perm[10]=0;
376             for (i=0; i<9; i++) {
377                 bit = mode & (1<<i);
378                 c = i%3;
379                 if (!c && (mode & (1<<((d=i/3)+9)))) {
380                     c = "tss"[d];
381                     if (!bit) c &= ~0x20;
382                 } else c = bit ? "xwr"[c] : '-';
383                 perm[9-i] = c;
384             }
385
386             if (S_ISDIR(mode)) c = 'd';
387             else if (S_ISBLK(mode)) c = 'b';
388             else if (S_ISCHR(mode)) c = 'c';
389             else if (S_ISLNK(mode)) c = 'l';
390             else if (S_ISFIFO(mode)) c = 'p';
391             else if (S_ISSOCK(mode)) c = 's';
392             else c = '-';
393             *perm = c;
394
395             tm = localtime(&(st->st_mtime));
396             strftime(thyme, sizeof(thyme), "%F %H:%M", tm);
397
398             if (flags&FLAG_o) grp = grpad = toybuf+256;
399             else {
400                 grp = (flags&FLAG_n) ? utoa(st->st_gid)
401                       : getgroupname(st->st_gid);
402                 grpad = toybuf+256-(totals[4]-len[4]);
403             }
404
405             if (flags&FLAG_g) usr = upad = toybuf+256;
406             else {
407                 upad = toybuf+255-(totals[3]-len[3]);
408                 if (flags&FLAG_n) {
409                     usr = TT.uid_buf;
410                     utoa_to_buf(st->st_uid, TT.uid_buf, 12);
411                 } else usr = getusername(st->st_uid);
412             }
413
414             // Coerce the st types into something we know we can print.
415             xprintf("%s% *ld %s%s%s%s% *"PRId64" %s ", perm, totals[2]+1,
416                 (long)st->st_nlink, usr, upad, grp, grpad, totals[5]+1,
417                 (int64_t)st->st_size, thyme);
418         }
419
420         if (flags & FLAG_q) {
421             char *p;
422             for (p=sort[next]->name; *p; p++) xputc(isprint(*p) ? *p : '?');
423         } else xprintf("%s", sort[next]->name);
424         if ((flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) && S_ISLNK(mode))
425             xprintf(" -> %s", sort[next]->symlink);
426
427         if (et) xputc(et);
428
429         // Pad columns
430         if (flags & (FLAG_C|FLAG_x)) {
431             curcol = colsizes[curcol] - *len;
432             if (curcol >= 0) xprintf("%s", toybuf+255-curcol);
433         }
434     }
435
436     if (width) xputc('\n');
437
438     // Free directory entries, recursing first if necessary.
439
440     for (ul = 0; ul<dtlen; free(sort[ul++])) {
441         if ((flags & FLAG_d) || !S_ISDIR(sort[ul]->st.st_mode)
442             || !dirtree_notdotdot(sort[ul])) continue;
443
444         // Recurse into dirs if at top of the tree or given -R
445         if (!indir->parent || (flags & FLAG_R))
446             listfiles(openat(dirfd, sort[ul]->name, 0), sort[ul]);
447     }
448     free(sort);
449     if (dirfd != AT_FDCWD) close(indir->data);
450 }
451
452 void ls_main(void)
453 {
454     char **s, *noargs[] = {".", 0};
455     struct dirtree *dt;
456
457     // Do we have an implied -1
458     if (!isatty(1) || (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g)))
459         toys.optflags |= FLAG_1;
460     else {
461         TT.screen_width = 80;
462         terminal_size(&TT.screen_width, NULL);
463     }
464     // The optflags parsing infrastructure should really do this for us,
465     // but currently it has "switch off when this is set", so "-dR" and "-Rd"
466     // behave differently
467     if (toys.optflags & FLAG_d) toys.optflags &= ~FLAG_R;
468
469     // Iterate through command line arguments, collecting directories and files.
470     // Non-absolute paths are relative to current directory.
471     TT.files = dirtree_add_node(0, 0, 0);
472     for (s = *toys.optargs ? toys.optargs : noargs; *s; s++) {
473         dt = dirtree_add_node(AT_FDCWD, *s,
474             (toys.optflags & (FLAG_L|FLAG_H|FLAG_l))^FLAG_l);
475
476         if (!dt) {
477             toys.exitval = 1;
478             continue;
479         }
480
481         // Typecast means double_list->prev temporarirly goes in dirtree->parent
482         dlist_add_nomalloc((struct double_list **)&TT.files->child,
483                            (struct double_list *)dt);
484     }
485
486     // Turn double_list into dirtree
487     dlist_to_dirtree(TT.files);
488
489     // Display the files we collected
490     listfiles(AT_FDCWD, TT.files);
491
492     if (CFG_TOYBOX_FREE) free(TT.files);
493 }