Mercurial > hg > toybox
annotate toys/ls.c @ 591:7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 09 Jun 2012 19:06:49 -0500 |
parents | ca6875170b9a |
children | fb582378a36a |
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]" |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
11 USE_LS(NEWTOY(ls, "oACFHLRSacdfiklmnpqrstux1", 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: |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
21 -a all files including .hidden |
584 | 22 -d directory, not contents |
23 -i inode number | |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
24 -k block sizes in kilobytes |
584 | 25 -p put a '/' after directory names |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
26 -q unprintable chars as '?' |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
27 -s size (in blocks) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
28 -u use access time for timestamps |
514 | 29 -A list all files except . and .. |
584 | 30 -R recursively list files in subdirectories |
31 -F append file type indicator (/=dir, *=exe, @=symlink, |=FIFO) | |
32 | |
33 output formats: | |
34 -1 list one file per line | |
35 -C columns (sorted vertically) | |
36 -x columns (sorted horizontally) | |
37 -l long (show full details for each file) | |
38 -m comma separated | |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
39 -n like -l but numeric uid/gid |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
40 -o like -l but no group |
584 | 41 |
42 sorting: | |
43 -f unsorted | |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
44 -r reverse |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
45 -t by timestamp |
458 | 46 */ |
47 | |
48 #include "toys.h" | |
49 | |
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
|
50 #define FLAG_1 (1<<0) |
584 | 51 #define FLAG_x (1<<1) |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
52 #define FLAG_u (1<<2) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
53 #define FLAG_t (1<<3) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
54 #define FLAG_s (1<<4) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
55 #define FLAG_r (1<<5) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
56 #define FLAG_q (1<<6) |
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
|
57 #define FLAG_p (1<<7) |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
58 #define FLAG_n (1<<8) |
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
|
59 #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
|
60 #define FLAG_l (1<<10) |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
61 #define FLAG_k (1<<11) |
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
|
62 #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
|
63 #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
|
64 #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
|
65 //#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
|
66 #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
|
67 //#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
|
68 #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
|
69 //#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
|
70 //#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
|
71 #define FLAG_F (1<<21) |
584 | 72 #define FLAG_C (1<<22) |
73 #define FLAG_A (1<<23) | |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
74 #define FLAG_o (1<<24) |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
75 |
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 // 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
|
77 |
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 // ls -lR starts .: then ./subdir: |
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 DEFINE_GLOBALS( |
584 | 81 struct dirtree *files; |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
82 |
584 | 83 unsigned screen_width; |
84 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
|
85 ) |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
86 |
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
|
87 #define TT this.ls |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
88 |
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
|
89 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
|
90 { |
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
|
91 // 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 } |
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
|
99 } |
458 | 100 } |
101 | |
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
|
102 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
|
103 { |
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 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
|
105 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
|
106 if (toys.optflags & FLAG_F) { |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
107 if (S_ISLNK(mode)) return '@'; |
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
|
108 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
|
109 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
|
110 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
|
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 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
|
113 } |
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
|
114 |
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
|
115 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
|
116 { |
584 | 117 struct passwd *pw = getpwuid(uid); |
118 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
|
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 |
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 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
|
122 { |
584 | 123 struct group *gr = getgrgid(gid); |
124 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
|
125 } |
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 |
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 // 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
|
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 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
|
130 { |
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 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
|
132 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
|
133 |
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 = 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
|
135 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
|
136 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
|
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 if (flags & FLAG_i) *len += (len[1] = numlen(st->st_ino)); |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
139 if (flags & (FLAG_l|FLAG_o|FLAG_n)) { |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
140 unsigned fn = flags & FLAG_n; |
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
|
141 len[2] = numlen(st->st_nlink); |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
142 len[3] = strlen(fn ? utoa(st->st_uid) : getusername(st->st_uid)); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
143 len[4] = strlen(fn ? utoa(st->st_gid) : getgroupname(st->st_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
|
144 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
|
145 } |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
146 if (flags & FLAG_s) *len += (len[6] = numlen(st->st_blocks)); |
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
|
147 } |
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
|
148 |
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 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
|
150 { |
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
|
151 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
|
152 struct dirtree *dtb = *(struct dirtree **)b; |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
153 int ret = 0, reverse = (toys.optflags & FLAG_r) ? -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
|
154 |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
155 if (toys.optflags & FLAG_t) { |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
156 if (dta->st.st_mtime > dtb->st.st_mtime) ret = -1; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
157 else if (dta->st.st_mtime < dtb->st.st_mtime) ret = 1; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
158 } |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
159 if (!ret) ret = strcmp(dta->name, dtb->name); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
160 return ret * reverse; |
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
|
161 } |
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
|
162 |
569
2e0367cb9585
More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
163 // 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
|
164 |
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
|
165 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
|
166 { |
569
2e0367cb9585
More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents:
565
diff
changeset
|
167 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
|
168 |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
169 // Special case to handle enormous dirs without running out of memory. |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
170 if (flags == (FLAG_1|FLAG_f)) { |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
171 xprintf("%s\n", new->name); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
172 return 0; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
173 } |
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
|
174 |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
175 if (!(flags&FLAG_f)) { |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
176 if (flags & FLAG_a) return 0; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
177 if (!(flags & FLAG_A) && new->name[0]=='.') return 0; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
178 } |
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
|
179 |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
180 if (flags & FLAG_u) new->st.st_mtime = new->st.st_atime; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
181 if (flags & FLAG_k) new->st.st_blocks = (new->st.st_blocks + 1) / 2; |
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
|
182 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
|
183 } |
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
|
184 |
584 | 185 // For column view, calculate horizontal position (for padding) and return |
186 // index of next entry to display. | |
187 | |
188 static unsigned long next_column(unsigned long ul, unsigned long dtlen, | |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
189 unsigned columns, unsigned *xpos) |
584 | 190 { |
191 unsigned long transition; | |
192 unsigned height, widecols; | |
193 | |
194 // Horizontal sort is easy | |
195 if (!(toys.optflags & FLAG_C)) { | |
196 *xpos = ul % columns; | |
197 return ul; | |
198 } | |
199 | |
200 // vertical sort | |
201 | |
202 // For -x, calculate height of display, rounded up | |
203 height = (dtlen+columns-1)/columns; | |
204 | |
205 // Sanity check: does wrapping render this column count impossible | |
206 // due to the right edge wrapping eating a whole row? | |
207 if (height*columns - dtlen >= height) { | |
208 *xpos = columns; | |
209 return 0; | |
210 } | |
211 | |
212 // Uneven rounding goes along right edge | |
213 widecols = dtlen % height; | |
214 if (!widecols) widecols = height; | |
215 transition = widecols * columns; | |
216 if (ul < transition) { | |
217 *xpos = ul % columns; | |
218 return (*xpos*height) + (ul/columns); | |
219 } | |
220 | |
221 ul -= transition; | |
222 *xpos = ul % (columns-1); | |
223 | |
224 return (*xpos*height) + widecols + (ul/(columns-1)); | |
225 } | |
226 | |
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
|
227 // 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
|
228 // 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
|
229 |
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
|
230 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
|
231 { |
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 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
|
233 unsigned long dtlen = 0, ul = 0; |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
234 unsigned width, flags = toys.optflags, totals[7], len[7], |
584 | 235 *colsizes = (unsigned *)(toybuf+260), columns = (sizeof(toybuf)-260)/4; |
236 | |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
237 memset(totals, 0, sizeof(totals)); |
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
|
238 |
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
|
239 // 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
|
240 // 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
|
241 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
|
242 if (!(dt = indir->child)) return; |
584 | 243 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
|
244 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
|
245 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
|
246 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
|
247 } |
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
|
248 } 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
|
249 // 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
|
250 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
|
251 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
|
252 } |
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
|
253 |
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
|
254 // 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
|
255 // 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
|
256 |
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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 } |
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
|
262 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
|
263 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
|
264 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
|
265 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
|
266 } |
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
|
267 |
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
|
268 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
|
269 |
584 | 270 // Find largest entry in each field for display alignment |
271 if (flags & (FLAG_C|FLAG_x)) { | |
272 | |
273 // columns can't be more than toybuf can hold, or more than files, | |
274 // or > 1/2 screen width (one char filename, one space). | |
275 if (columns > TT.screen_width/2) columns = TT.screen_width/2; | |
276 if (columns > dtlen) columns = dtlen; | |
277 | |
278 // Try to fit as many columns as we can, dropping down by one each time | |
279 for (;columns > 1; columns--) { | |
280 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
|
281 |
584 | 282 memset(colsizes, 0, columns*sizeof(unsigned)); |
283 for (ul=0; ul<dtlen; ul++) { | |
284 entrylen(sort[next_column(ul, dtlen, columns, &c)], len); | |
285 if (c == columns) break; | |
286 // Does this put us over budget? | |
287 if (*len > colsizes[c]) { | |
288 totlen += *len-colsizes[c]; | |
289 colsizes[c] = *len; | |
290 if (totlen > TT.screen_width) break; | |
291 } | |
292 } | |
293 // If it fit, stop here | |
294 if (ul == dtlen) break; | |
295 } | |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
296 } else if (flags & (FLAG_l|FLAG_o|FLAG_n)) for (ul = 0; ul<dtlen; ul++) { |
571 | 297 entrylen(sort[ul], len); |
584 | 298 for (width=0; width<6; width++) |
299 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
|
300 } |
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 |
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
|
302 // Label directory if not top of tree, or if -R |
584 | 303 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
|
304 { |
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
|
305 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
|
306 |
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
|
307 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
|
308 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
|
309 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
|
310 } |
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
|
311 |
571 | 312 // This is wrong, should be blocks used not file count. |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
313 if (indir->parent && (flags & (FLAG_l|FLAG_o|FLAG_n))) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
314 xprintf("total %lu\n", dtlen); |
571 | 315 |
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
|
316 // Loop through again to produce output. |
584 | 317 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
|
318 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
|
319 for (ul = 0; ul<dtlen; ul++) { |
584 | 320 unsigned curcol; |
321 unsigned long next = next_column(ul, dtlen, columns, &curcol); | |
322 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
|
323 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
|
324 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
|
325 |
571 | 326 // Skip directories at the top of the tree when -d isn't set |
327 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
|
328 TT.nl_title=1; |
571 | 329 |
584 | 330 // Handle padding and wrapping for display purposes |
331 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
|
332 if (ul) { |
584 | 333 if (flags & FLAG_m) xputc(','); |
334 if (flags & (FLAG_C|FLAG_x)) { | |
335 if (!curcol) xputc('\n'); | |
336 } 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
|
337 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
|
338 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
|
339 } 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
|
340 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
|
341 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
|
342 } |
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 } |
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 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
|
345 |
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 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
|
347 xprintf("% *lu ", len[1], (unsigned long)st->st_ino); |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
348 if (flags & FLAG_s) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
349 xprintf("% *lu ", len[6], (unsigned long)st->st_blocks); |
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
|
350 |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
351 if (flags & (FLAG_l|FLAG_o|FLAG_n)) { |
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
|
352 struct tm *tm; |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
353 char perm[11], thyme[64], c, d, *usr, buf[12], *grp, *grpad; |
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
|
354 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
|
355 |
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 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
|
357 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
|
358 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
|
359 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
|
360 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
|
361 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
|
362 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
|
363 } 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
|
364 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
|
365 } |
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
|
366 |
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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 *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
|
375 |
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 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
|
377 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
|
378 |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
379 if (flags&FLAG_n) { |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
380 usr = buf; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
381 utoa_to_buf(st->st_uid, buf, 12); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
382 } else usr = getusername(st->st_uid); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
383 if (flags&FLAG_o) grp = grpad = toybuf+256; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
384 else { |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
385 grp = (flags&FLAG_n) ? utoa(st->st_gid) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
386 : getgroupname(st->st_gid); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
387 grpad = toybuf+256-(totals[4]-len[4]); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
388 } |
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
|
389 xprintf("%s% *d %s%s%s%s% *d %s ", perm, totals[2]+1, st->st_nlink, |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
390 usr, toybuf+255-(totals[3]-len[3]), |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
391 grp, grpad, totals[5]+1, st->st_size, thyme); |
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
|
392 } |
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 |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
394 if (flags & FLAG_q) { |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
395 char *p; |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
396 for (p=sort[next]->name; *p; p++) xputc(isprint(*p) ? *p : '?'); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
397 } else xprintf("%s", sort[next]->name); |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
398 if ((flags & (FLAG_l|FLAG_o|FLAG_n)) && S_ISLNK(mode)) |
584 | 399 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
|
400 |
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
|
401 if (et) xputc(et); |
584 | 402 |
403 // Pad columns | |
404 if (flags & (FLAG_C|FLAG_x)) { | |
405 curcol = colsizes[curcol] - *len; | |
406 if (curcol >= 0) xprintf("%s", toybuf+255-curcol); | |
407 } | |
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
|
408 } |
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
|
409 |
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 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
|
411 |
571 | 412 // Free directory entries, recursing first if necessary. |
413 | |
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
|
414 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
|
415 // TODO follow symlinks when? |
571 | 416 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
|
417 || !dirtree_notdotdot(sort[ul])) continue; |
571 | 418 |
419 // 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
|
420 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
|
421 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
|
422 } |
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 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
|
424 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
|
425 } |
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
|
426 |
458 | 427 void ls_main(void) |
428 { | |
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
|
429 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
|
430 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
|
431 |
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
|
432 // Do we have an implied -1 |
591
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
433 if (!isatty(1) || (toys.optflags&(FLAG_l|FLAG_o|FLAG_n))) |
7c4ca3f0536b
Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents:
584
diff
changeset
|
434 toys.optflags |= FLAG_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
|
435 else { |
584 | 436 TT.screen_width = 80; |
437 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
|
438 } |
571 | 439 // The optflags parsing infrastructure should really do this for us, |
440 // but currently it has "switch off when this is set", so "-dR" and "-Rd" | |
441 // behave differently | |
442 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
|
443 |
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
|
444 // 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
|
445 // 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
|
446 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
|
447 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
|
448 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
|
449 |
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
|
450 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
|
451 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
|
452 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
|
453 } |
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
|
454 |
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
|
455 // 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
|
456 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
|
457 (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
|
458 } |
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
|
459 |
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
|
460 // 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
|
461 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
|
462 |
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
|
463 // 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
|
464 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
|
465 |
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
|
466 if (CFG_TOYBOX_FREE) free(TT.files); |
458 | 467 } |