annotate toys/ls.c @ 597:b2d0e8e572ba

Fix an embarassing bug causing ls -l to segfault on 32 bit targets.
author Rob Landley <rob@landley.net>
date Wed, 13 Jun 2012 23:58:21 -0500
parents fb582378a36a
children 92200901cfe1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * ls.c - list files
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
9
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
10 // "[-Cl]"
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
11 USE_LS(NEWTOY(ls, "goACFHLRSacdfiklmnpqrstux1", TOYFLAG_BIN))
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
12
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 config LS
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 list files
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
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
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
22 -c use ctime for timestamps
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
23 -d directory, not contents
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
24 -i inode number
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
25 -k block sizes in kilobytes
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
26 -p put a '/' after directory names
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
27 -q unprintable chars as '?'
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
28 -s size (in blocks)
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
29 -u use access time for timestamps
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
30 -A list all files except . and ..
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
31 -H follow command line symlinks
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
32 -L follow symlinks
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
33 -R recursively list files in subdirectories
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
34 -F append file type indicator (/=dir, *=exe, @=symlink, |=FIFO)
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
35
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
36 output formats:
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
37 -1 list one file per line
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
38 -C columns (sorted vertically)
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
39 -g like -l but no owner
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
40 -l long (show full details for each file)
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
41 -m comma separated
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
42 -n like -l but numeric uid/gid
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
43 -o like -l but no group
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
44 -x columns (sorted horizontally)
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
45
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
46 sorting (default is alphabetical):
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
47 -f unsorted
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
48 -r reverse
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
49 -t timestamp
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
50 -S size
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
52
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 #include "toys.h"
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
54
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
55 #define FLAG_1 (1<<0)
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
56 #define FLAG_x (1<<1)
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
57 #define FLAG_u (1<<2)
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
58 #define FLAG_t (1<<3)
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
59 #define FLAG_s (1<<4)
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
60 #define FLAG_r (1<<5)
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
61 #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
62 #define FLAG_p (1<<7)
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
63 #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
64 #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
65 #define FLAG_l (1<<10)
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
66 #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
67 #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
68 #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
69 #define FLAG_d (1<<14)
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
70 #define FLAG_c (1<<15)
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 FLAG_a (1<<16)
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
72 #define FLAG_S (1<<17)
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
73 #define FLAG_R (1<<18)
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
74 #define FLAG_L (1<<19)
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
75 #define FLAG_H (1<<20)
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 #define FLAG_F (1<<21)
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
77 #define FLAG_C (1<<22)
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
78 #define FLAG_A (1<<23)
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
79 #define FLAG_o (1<<24)
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
80 #define FLAG_g (1<<25)
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
81
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
82 // 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
83
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 // ls -lR starts .: then ./subdir:
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
85
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
86 DEFINE_GLOBALS(
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
87 struct dirtree *files;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
88
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
89 unsigned screen_width;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
90 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
91 )
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
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 #define TT this.ls
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
94
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
95 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
96 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 // 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
98 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
99 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
100 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
101 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
102 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
103 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
104 }
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
105 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
107
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 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
109 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
111 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
112 if (toys.optflags & FLAG_F) {
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
113 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
114 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
115 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
116 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
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 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
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 *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
122 {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
123 struct passwd *pw = getpwuid(uid);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
124 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
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 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
128 {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
129 struct group *gr = getgrgid(gid);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
130 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
131 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 // 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
134
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
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 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
138 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
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 *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
141 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
142 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
143
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 if (flags & FLAG_i) *len += (len[1] = numlen(st->st_ino));
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
145 if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
146 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
147 len[2] = numlen(st->st_nlink);
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
148 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
149 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
150 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
151 }
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
152 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
153 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
156 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
157 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
158 struct dirtree *dtb = *(struct dirtree **)b;
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
159 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
160
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
161 if (toys.optflags & FLAG_S) {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
162 if (dta->st.st_size > dtb->st.st_size) ret = -1;
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
163 else if (dta->st.st_size < dtb->st.st_size) ret = 1;
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
164 }
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
165 if (toys.optflags & FLAG_t) {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
166 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
167 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
168 }
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
169 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
170 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
171 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
172
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
173 // 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
174
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
175 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
176 {
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
177 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
178
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
179 // 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
180 if (flags == (FLAG_1|FLAG_f)) {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
181 xprintf("%s\n", new->name);
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
182 return 0;
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
183 }
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
184
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
185 if (!(flags&FLAG_f)) {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
186 if (flags & FLAG_a) return 0;
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
187 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
188 }
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
189
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
190 if (flags & FLAG_u) new->st.st_mtime = new->st.st_atime;
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
191 if (flags & FLAG_c) new->st.st_mtime = new->st.st_ctime;
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
192 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
193 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
194 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
195
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
196 // For column view, calculate horizontal position (for padding) and return
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
197 // index of next entry to display.
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
198
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
199 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
200 unsigned columns, unsigned *xpos)
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
201 {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
202 unsigned long transition;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
203 unsigned height, widecols;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
204
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
205 // Horizontal sort is easy
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
206 if (!(toys.optflags & FLAG_C)) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
207 *xpos = ul % columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
208 return ul;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
209 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
210
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
211 // vertical sort
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
212
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
213 // For -x, calculate height of display, rounded up
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
214 height = (dtlen+columns-1)/columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
215
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
216 // Sanity check: does wrapping render this column count impossible
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
217 // due to the right edge wrapping eating a whole row?
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
218 if (height*columns - dtlen >= height) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
219 *xpos = columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
220 return 0;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
221 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
222
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
223 // Uneven rounding goes along right edge
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
224 widecols = dtlen % height;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
225 if (!widecols) widecols = height;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
226 transition = widecols * columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
227 if (ul < transition) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
228 *xpos = ul % columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
229 return (*xpos*height) + (ul/columns);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
230 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
231
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
232 ul -= transition;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
233 *xpos = ul % (columns-1);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
234
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
235 return (*xpos*height) + widecols + (ul/(columns-1));
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
236 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
237
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 // 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
239 // 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
240
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
241 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
242 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
244 unsigned long dtlen = 0, ul = 0;
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
245 unsigned width, flags = toys.optflags, totals[7], len[7],
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
246 *colsizes = (unsigned *)(toybuf+260), columns = (sizeof(toybuf)-260)/4;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
247
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
248 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
249
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
250 // 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
251 // 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
252 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
253 if (!(dt = indir->child)) return;
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
254 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
255 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
256 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
257 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
258 }
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
259 } 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
260 // 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
261 indir->data = dup(dirfd);
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
262 dirtree_recurse(indir, filter, (flags&FLAG_L));
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
263 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 // 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
266 // 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
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 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
269 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
270 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
271 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
272 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
273 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
274 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
275 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
276 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
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
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
279 // Label directory if not top of tree, or if -R
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
280 if (indir->parent && (!indir->extra || (flags & FLAG_R)))
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
281 {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
282 char *path = dirtree_path(indir, 0);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
283
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
284 if (TT.nl_title++) xputc('\n');
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
285 xprintf("%s:\n", path);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
286 free(path);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
287 }
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
288
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
289 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
290
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
291 // Find largest entry in each field for display alignment
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
292 if (flags & (FLAG_C|FLAG_x)) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
293
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
294 // columns can't be more than toybuf can hold, or more than files,
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
295 // or > 1/2 screen width (one char filename, one space).
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
296 if (columns > TT.screen_width/2) columns = TT.screen_width/2;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
297 if (columns > dtlen) columns = dtlen;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
298
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
299 // Try to fit as many columns as we can, dropping down by one each time
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
300 for (;columns > 1; columns--) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
301 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
302
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
303 memset(colsizes, 0, columns*sizeof(unsigned));
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
304 for (ul=0; ul<dtlen; ul++) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
305 entrylen(sort[next_column(ul, dtlen, columns, &c)], len);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
306 if (c == columns) break;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
307 // Does this put us over budget?
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
308 if (*len > colsizes[c]) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
309 totlen += *len-colsizes[c];
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
310 colsizes[c] = *len;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
311 if (totlen > TT.screen_width) break;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
312 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
313 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
314 // If it fit, stop here
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
315 if (ul == dtlen) break;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
316 }
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
317 } else if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g|FLAG_s)) {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
318 unsigned long blocks = 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
319
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
320 for (ul = 0; ul<dtlen; ul++)
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
321 {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
322 entrylen(sort[ul], len);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
323 for (width=0; width<6; width++)
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
324 if (len[width] > totals[width]) totals[width] = len[width];
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
325 blocks += sort[ul]->st.st_blocks;
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
326 }
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
327
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
328 if (indir->parent) xprintf("total %lu\n", blocks);
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
329 }
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
330
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
331 // Loop through again to produce output.
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
332 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
333 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
334 for (ul = 0; ul<dtlen; ul++) {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
335 unsigned curcol;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
336 unsigned long next = next_column(ul, dtlen, columns, &curcol);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
337 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
338 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
339 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
340
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
341 // Skip directories at the top of the tree when -d isn't set
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
342 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
343 TT.nl_title=1;
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
344
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
345 // Handle padding and wrapping for display purposes
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
346 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
347 if (ul) {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
348 if (flags & FLAG_m) xputc(',');
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
349 if (flags & (FLAG_C|FLAG_x)) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
350 if (!curcol) xputc('\n');
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
351 } 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
352 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
353 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
354 } 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
355 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
356 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
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 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
360
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
362 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
363 if (flags & FLAG_s)
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
364 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
365
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
366 if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
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
367 struct tm *tm;
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
368 char perm[11], thyme[64], c, d, *usr, *upad, 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
369 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
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 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
372 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
373 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
374 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
375 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
376 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
377 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
378 } 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
379 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
380 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
381
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
382 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
383 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
384 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
385 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
386 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
387 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
388 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
389 *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
390
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
392 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
393
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
394 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
395 else {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
396 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
397 : getgroupname(st->st_gid);
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
398 grpad = toybuf+256-(totals[4]-len[4]);
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
399 }
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
400
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
401 if (flags&FLAG_g) usr = upad = toybuf+256;
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
402 else {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
403 upad = toybuf+255-(totals[3]-len[3]);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
404 if (flags&FLAG_n) {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
405 usr = buf;
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
406 utoa_to_buf(st->st_uid, buf, 12);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
407 } else usr = getusername(st->st_uid);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
408 }
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
409
597
b2d0e8e572ba Fix an embarassing bug causing ls -l to segfault on 32 bit targets.
Rob Landley <rob@landley.net>
parents: 593
diff changeset
410 // Coerce the st types into something we know we can print.
b2d0e8e572ba Fix an embarassing bug causing ls -l to segfault on 32 bit targets.
Rob Landley <rob@landley.net>
parents: 593
diff changeset
411 xprintf("%s% *ld %s%s%s%s% *"PRId64" %s ", perm, totals[2]+1,
b2d0e8e572ba Fix an embarassing bug causing ls -l to segfault on 32 bit targets.
Rob Landley <rob@landley.net>
parents: 593
diff changeset
412 (long)st->st_nlink, usr, upad, grp, grpad, totals[5]+1,
b2d0e8e572ba Fix an embarassing bug causing ls -l to segfault on 32 bit targets.
Rob Landley <rob@landley.net>
parents: 593
diff changeset
413 (int64_t)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
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
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
416 if (flags & FLAG_q) {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
417 char *p;
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
418 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
419 } else xprintf("%s", sort[next]->name);
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
420 if ((flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) && S_ISLNK(mode))
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
421 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
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 if (et) xputc(et);
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
424
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
425 // Pad columns
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
426 if (flags & (FLAG_C|FLAG_x)) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
427 curcol = colsizes[curcol] - *len;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
428 if (curcol >= 0) xprintf("%s", toybuf+255-curcol);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
429 }
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
430 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
433
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
434 // Free directory entries, recursing first if necessary.
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
435
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
436 for (ul = 0; ul<dtlen; free(sort[ul++])) {
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
437 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
438 || !dirtree_notdotdot(sort[ul])) continue;
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
439
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
440 // 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
441 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
442 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
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 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
445 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
446 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
447
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
448 void ls_main(void)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
449 {
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
450 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
451 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
452
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 // Do we have an implied -1
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
454 if (!isatty(1) || (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g)))
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
455 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
456 else {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
457 TT.screen_width = 80;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
458 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
459 }
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
460 // The optflags parsing infrastructure should really do this for us,
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
461 // but currently it has "switch off when this is set", so "-dR" and "-Rd"
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
462 // behave differently
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
463 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
464
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
465 // 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
466 // Non-absolute paths are relative to current directory.
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
467 TT.files = dirtree_add_node(0, 0, 0);
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
468 for (s = *toys.optargs ? toys.optargs : noargs; *s; s++) {
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
469 dt = dirtree_add_node(AT_FDCWD, *s,
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
470 (toys.optflags & (FLAG_L|FLAG_H|FLAG_l))^FLAG_l);
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
471
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
472 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
473 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
474 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
475 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
476
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
477 // 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
478 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
479 (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
480 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
481
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
482 // 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
483 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
484
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
485 // 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
486 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
487
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
488 if (CFG_TOYBOX_FREE) free(TT.files);
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
489 }