aboutsummaryrefslogtreecommitdiff
path: root/toys/ls.c
blob: 87a399780363469860350887126848a367f6179f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* vi: set sw=4 ts=4:
 *
 * ls.c - list files
 *
 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
 * Copyright 2012 Rob Landley <rob@landley.net>
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html

USE_LS(NEWTOY(ls, "ACFHLRSacdfiklmnpqrstux1", TOYFLAG_BIN))

config LS
	bool "ls"
	default y
	help
	  usage: ls [-ACFHLRSacdfiklmnpqrstux1] [directory...]
	  list files

          -1    list one file per line
          -a    list all files
	  -A	list all files except . and ..
          -F    append a character as a file type indicator
          -l    show full details for each file
*/

#include "toys.h"

#define FLAG_1 (1<<0)
//#define FLAG_x (1<<1)
//#define FLAG_u (1<<2)
//#define FLAG_t (1<<3)
//#define FLAG_s (1<<4)
//#define FLAG_r (1<<5)
//#define FLAG_q (1<<6)
#define FLAG_p (1<<7)
//#define FLAG_n (1<<8)
#define FLAG_m (1<<9)
#define FLAG_l (1<<10)
//#define FLAG_k (1<<11)
#define FLAG_i (1<<12)
#define FLAG_f (1<<13)
#define FLAG_d (1<<14)
//#define FLAG_c (1<<15)
#define FLAG_a (1<<16)
//#define FLAG_S (1<<17)
#define FLAG_R (1<<18)
//#define FLAG_L (1<<19)
//#define FLAG_H (1<<20)
#define FLAG_F (1<<21)
//#define FLAG_C (1<<21)
#define FLAG_A (1<<22)

// test sst output (suid/sticky in ls flaglist)

// ls -lR starts .: then ./subdir:

DEFINE_GLOBALS(
  struct dirtree *files;

  unsigned width;
  int nl_title;
)

#define TT this.ls

void dlist_to_dirtree(struct dirtree *parent)
{
    // Turn double_list into dirtree
    struct dirtree *dt = parent->child;
    if (dt) {
        dt->parent->next = NULL;
        while (dt) {
            dt->parent = parent;
            dt = dt->next;
        }
    }
}

static char endtype(struct stat *st)
{
    mode_t mode = st->st_mode;
    if ((toys.optflags&(FLAG_F|FLAG_p)) && S_ISDIR(mode)) return '/';
    if (toys.optflags & FLAG_F) {
        if (S_ISLNK(mode) && !(toys.optflags & FLAG_F)) return '@';
        if (S_ISREG(mode) && (mode&0111)) return '*';
        if (S_ISFIFO(mode)) return '|';
        if (S_ISSOCK(mode)) return '=';
    }
    return 0;
}

static char *getusername(uid_t uid)
{
  struct passwd *pw = getpwuid(uid);
  return pw ? pw->pw_name : utoa(uid);
}

static char *getgroupname(gid_t gid)
{
  struct group *gr = getgrgid(gid);
  return gr ? gr->gr_name : utoa(gid);
}

// Figure out size of printable entry fields for display indent/wrap

static void entrylen(struct dirtree *dt, unsigned *len)
{
    struct stat *st = &(dt->st);
    unsigned flags = toys.optflags;

    *len = strlen(dt->name);
    if (endtype(st)) ++*len;
    if (flags & FLAG_m) ++*len;

    if (flags & FLAG_i) *len += (len[1] = numlen(st->st_ino));
    if (flags & FLAG_l) {
        len[2] = numlen(st->st_nlink);
        len[3] = strlen(getusername(st->st_uid));
        len[4] = strlen(getgroupname(st->st_gid));
        len[5] = numlen(st->st_size);
    }
}

static int compare(void *a, void *b)
{
    struct dirtree *dta = *(struct dirtree **)a;
    struct dirtree *dtb = *(struct dirtree **)b;

// TODO handle flags
    return strcmp(dta->name, dtb->name);
}

// callback from dirtree_recurse() determining how to handle this entry.

static int filter(struct dirtree *new)
{
    int flags = toys.optflags;

// TODO should -1f print here to handle enormous dirs without runing
// out of mem?

    if (flags & FLAG_a) return DIRTREE_NORECURSE;
    if (!(flags & FLAG_A) && new->name[0]=='.')
        return DIRTREE_NOSAVE|DIRTREE_NORECURSE;

    return dirtree_isdotdot(new)|DIRTREE_NORECURSE;
}

// Display a list of dirtree entries, according to current format
// Output types -1, -l, -C, or stream

static void listfiles(int dirfd, struct dirtree *indir)
{
    struct dirtree *dt, **sort = 0;
    unsigned long dtlen = 0, ul = 0;
    unsigned width, flags = toys.optflags, totals[6], len[6];

    
    // Silently descend into single directory listed by itself on command line.
    // In this case only show dirname/total header when given -R.
    if (!indir->parent) {
        if (!(dt = indir->child)) return;
        if (S_ISDIR(dt->st.st_mode) && !dt->next && !(toys.optflags&FLAG_d)) {
            dt->extra = 1;
            listfiles(open(dt->name, 0), dt);
            return;
        }
    } else {
        // Read directory contents. We dup() the fd because this will close it.
        indir->data = dup(dirfd);
        dirtree_recurse(indir, filter);
    }

    // Copy linked list to array and sort it. Directories go in array because
    // we visit them in sorted order.

    for (;;) {
        for (dt = indir->child; dt; dt = dt->next) {
            if (sort) sort[dtlen] = dt;
            dtlen++;
        }
        if (sort) break;
        sort = xmalloc(dtlen * sizeof(void *));
        dtlen = 0;
        continue;
    }

    if (!(flags & FLAG_f)) qsort(sort, dtlen, sizeof(void *), (void *)compare);

    // Find largest entry in each field

    memset(totals, 0, 6*sizeof(unsigned));
    for (ul = 0; ul<dtlen; ul++) {
        entrylen(sort[ul], len);
        if (flags & FLAG_l) {
            for (width=0; width<6; width++)
                if (len[width] > totals[width]) totals[width] = len[width];
//TODO      } else if (flags & FLAG_C) {
        } else if (*len > *totals) *totals = *len;
    }

    // Label directory if not top of tree, or if -R
    if (indir->parent && (!indir->extra || (flags&FLAG_R)))
    {
        char *path = dirtree_path(indir, 0);

        if (TT.nl_title++) xputc('\n');
        xprintf("%s:\n", path);
        free(path);
    }

    // This is wrong, should be blocks used not file count.
    if (indir->parent && (flags & FLAG_l)) xprintf("total %lu\n", dtlen);

    // Loop through again to produce output.
    width = 0;
    memset(toybuf, ' ', 256);
    for (ul = 0; ul<dtlen; ul++) {
        struct stat *st = &(sort[ul]->st);
        mode_t mode = st->st_mode;
        char et = endtype(st);

        // Skip directories at the top of the tree when -d isn't set
        if (S_ISDIR(mode) && !indir->parent && !(flags & FLAG_d)) continue;
        TT.nl_title=1;

        // Do we need to wrap at right edge of screen?
        entrylen(sort[ul], len);
        if (ul) {
            if (toys.optflags & FLAG_m) xputc(',');
            if ((flags & FLAG_1) || width+1+*len > TT.width) {
                xputc('\n');
                width = 0;
            } else {
                xputc(' ');
                width++;
            }
        }
        width += *len;

        if (flags & FLAG_i)
            xprintf("% *lu ", len[1], (unsigned long)st->st_ino);

        if (flags & FLAG_l) {
            struct tm *tm;
            char perm[11], thyme[64], c, d;
            int i, bit;

            perm[10]=0;
            for (i=0; i<9; i++) {
                bit = mode & (1<<i);
                c = i%3;
                if (!c && (mode & (1<<((d=i/3)+9)))) {
                    c = "tss"[d];
                    if (!bit) c &= 0x20;
                } else c = bit ? "xwr"[c] : '-';
                perm[9-i] = c;
            }

            if (S_ISDIR(mode)) c = 'd';
            else if (S_ISBLK(mode)) c = 'b';
            else if (S_ISCHR(mode)) c = 'c';
            else if (S_ISLNK(mode)) c = 'l';
            else if (S_ISFIFO(mode)) c = 'p';
            else if (S_ISSOCK(mode)) c = 's';
            else c = '-';
            *perm = c;

            tm = localtime(&(st->st_mtime));
            strftime(thyme, sizeof(thyme), "%F %H:%M", tm);

            xprintf("%s% *d %s%s%s%s% *d %s ", perm, totals[2]+1, st->st_nlink,
                    getusername(st->st_uid), toybuf+255-(totals[3]-len[3]),
                    getgroupname(st->st_gid), toybuf+256-(totals[4]-len[4]),
                    totals[5]+1, st->st_size, thyme);
        }

        xprintf("%s", sort[ul]->name);
        if ((flags & FLAG_l) && S_ISLNK(mode))
            xprintf(" -> %s", sort[ul]->symlink);

        if (et) xputc(et);
    }

    if (width) xputc('\n');

    // Free directory entries, recursing first if necessary.

    for (ul = 0; ul<dtlen; free(sort[ul++])) {
// TODO follow symlinks when?
        if ((flags & FLAG_d) || !S_ISDIR(sort[ul]->st.st_mode)
            || dirtree_isdotdot(sort[ul])) continue;

        // Recurse into dirs if at top of the tree or given -R
        if (!indir->parent || (flags & FLAG_R))
            listfiles(openat(dirfd, sort[ul]->name, 0), sort[ul]);
    }
    free(sort);
    if (dirfd != AT_FDCWD) close(indir->data);
}

void ls_main(void)
{
    char **s, *noargs[] = {".", 0};
    struct dirtree *dt;

    // Do we have an implied -1
    if (!isatty(1) || (toys.optflags&FLAG_l)) toys.optflags |= FLAG_1;
    else {
        TT.width = 80;
        terminal_size(&TT.width, NULL);
    }
    // The optflags parsing infrastructure should really do this for us,
    // but currently it has "switch off when this is set", so "-dR" and "-Rd"
    // behave differently
    if (toys.optflags & FLAG_d) toys.optflags &= ~FLAG_R;

    // Iterate through command line arguments, collecting directories and files.
    // Non-absolute paths are relative to current directory.
    TT.files = dirtree_add_node(0, 0);
    for (s = *toys.optargs ? toys.optargs : noargs; *s; s++) {
        dt = dirtree_add_node(AT_FDCWD, *s);

        if (!dt) {
            toys.exitval = 1;
            continue;
        }

        // Typecast means double_list->prev temporarirly goes in dirtree->parent
        dlist_add_nomalloc((struct double_list **)&TT.files->child,
                           (struct double_list *)dt);
    }

    // Turn double_list into dirtree
    dlist_to_dirtree(TT.files);

    // Display the files we collected
    listfiles(AT_FDCWD, TT.files);

    if (CFG_TOYBOX_FREE) free(TT.files);
}