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