annotate toys/posix/ls.c @ 674:7e846e281e38

New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author Rob Landley <rob@landley.net>
date Mon, 08 Oct 2012 00:02:30 -0500
parents 60cbc87c4314
children 786841fdb1e0
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 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
8 * See http://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
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 663
diff changeset
53 #define FOR_ls
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 #include "toys.h"
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
55
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
56 // 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
57
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
58 // ls -lR starts .: then ./subdir:
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
59
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 663
diff changeset
60 GLOBALS(
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
61 struct dirtree *files;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
62
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
63 unsigned screen_width;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
64 int nl_title;
650
dd82e0b28eda Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
Rob Landley <rob@landley.net>
parents: 638
diff changeset
65
dd82e0b28eda Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
Rob Landley <rob@landley.net>
parents: 638
diff changeset
66 // group and user can make overlapping use of the utoa() buf, so move it
dd82e0b28eda Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
Rob Landley <rob@landley.net>
parents: 638
diff changeset
67 char uid_buf[12];
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
68 )
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
69
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
70 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
71 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
72 // 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
73 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
74 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
75 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
76 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
77 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
78 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
79 }
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
80 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
82
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
83 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
84 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
85 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
86 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
87 if (toys.optflags & FLAG_F) {
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
88 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
89 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
90 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
91 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
92 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
94 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
95
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
97 {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
98 struct passwd *pw = getpwuid(uid);
650
dd82e0b28eda Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
Rob Landley <rob@landley.net>
parents: 638
diff changeset
99 utoa_to_buf(uid, TT.uid_buf, 12);
dd82e0b28eda Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
Rob Landley <rob@landley.net>
parents: 638
diff changeset
100 return pw ? pw->pw_name : TT.uid_buf;
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
101 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
102
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
103 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
104 {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
105 struct group *gr = getgrgid(gid);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
106 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
107 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 // 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
110
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
111 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
112 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
113 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
114 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
115
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 *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
117 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
118 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
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 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
121 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
122 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
123 len[2] = numlen(st->st_nlink);
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
124 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
125 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
126 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
127 }
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
128 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
129 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
130
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
131 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
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 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
134 struct dirtree *dtb = *(struct dirtree **)b;
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
135 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
136
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
137 if (toys.optflags & FLAG_S) {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
138 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
139 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
140 }
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
141 if (toys.optflags & FLAG_t) {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
142 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
143 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
144 }
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
145 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
146 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
147 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
148
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
149 // 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
150
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
151 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
152 {
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
153 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
154
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
155 // 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
156 if (flags == (FLAG_1|FLAG_f)) {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
157 xprintf("%s\n", new->name);
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
158 return 0;
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
159 }
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
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
161 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
162 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
163 if (flags & FLAG_k) new->st.st_blocks = (new->st.st_blocks + 1) / 2;
663
60cbc87c4314 I screwed up ls in commit 580. Oops. Fix it.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
164
60cbc87c4314 I screwed up ls in commit 580. Oops. Fix it.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
165 if (flags & (FLAG_a|FLAG_f)) return DIRTREE_SAVE;
60cbc87c4314 I screwed up ls in commit 580. Oops. Fix it.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
166 if (!(flags & FLAG_A) && new->name[0]=='.') return 0;
60cbc87c4314 I screwed up ls in commit 580. Oops. Fix it.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
167
60cbc87c4314 I screwed up ls in commit 580. Oops. Fix it.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
168 return dirtree_notdotdot(new) & DIRTREE_SAVE;
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
169 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
170
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
171 // 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
172 // index of next entry to display.
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
173
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
174 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
175 unsigned columns, unsigned *xpos)
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
176 {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
177 unsigned long transition;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
178 unsigned height, widecols;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
179
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
180 // Horizontal sort is easy
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
181 if (!(toys.optflags & FLAG_C)) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
182 *xpos = ul % columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
183 return ul;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
184 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
185
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
186 // vertical sort
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
187
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
188 // For -x, calculate height of display, rounded up
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
189 height = (dtlen+columns-1)/columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
190
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
191 // 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
192 // 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
193 if (height*columns - dtlen >= height) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
194 *xpos = columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
195 return 0;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
196 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
197
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
198 // Uneven rounding goes along right edge
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
199 widecols = dtlen % height;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
200 if (!widecols) widecols = height;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
201 transition = widecols * columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
202 if (ul < transition) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
203 *xpos = ul % columns;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
204 return (*xpos*height) + (ul/columns);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
205 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
206
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
207 ul -= transition;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
208 *xpos = ul % (columns-1);
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 return (*xpos*height) + widecols + (ul/(columns-1));
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
211 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
212
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
213 // 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
214 // 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
215
573
c42ed3601b35 Simplify/unify listfiles recursion: populate directory node (and detect top of tree) at start of function rather than end (and redundantly in main). Move title printing down next to total printing.
Rob Landley <rob@landley.net>
parents: 571
diff changeset
216 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
217 {
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
218 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
219 unsigned long dtlen = 0, ul = 0;
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
220 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
221 *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
222
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
223 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
224
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
225 // 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
226 // 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
227 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
228 if (!(dt = indir->child)) return;
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
229 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
230 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
231 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
232 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
233 }
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
234 } 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
235 // 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
236 indir->data = dup(dirfd);
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
237 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
238 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
239
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 // 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
241 // 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
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 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
244 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
245 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
246 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
247 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
248 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
249 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
250 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
251 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
252 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
253
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
254 // 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
255 if (indir->parent && (!indir->extra || (flags & FLAG_R)))
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
256 {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
257 char *path = dirtree_path(indir, 0);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
258
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
259 if (TT.nl_title++) xputc('\n');
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
260 xprintf("%s:\n", path);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
261 free(path);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
262 }
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
263
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
264 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
265
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
266 // 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
267 if (flags & (FLAG_C|FLAG_x)) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
268
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
269 // 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
270 // 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
271 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
272 if (columns > dtlen) columns = dtlen;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
273
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
274 // 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
275 for (;columns > 1; columns--) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
276 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
277
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
278 memset(colsizes, 0, columns*sizeof(unsigned));
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
279 for (ul=0; ul<dtlen; ul++) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
280 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
281 if (c == columns) break;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
282 // Does this put us over budget?
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
283 if (*len > colsizes[c]) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
284 totlen += *len-colsizes[c];
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
285 colsizes[c] = *len;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
286 if (totlen > TT.screen_width) break;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
287 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
288 }
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
289 // If it fit, stop here
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
290 if (ul == dtlen) break;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
291 }
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
292 } 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
293 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
294
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
295 for (ul = 0; ul<dtlen; ul++)
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
296 {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
297 entrylen(sort[ul], len);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
298 for (width=0; width<6; width++)
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
299 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
300 blocks += sort[ul]->st.st_blocks;
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
301 }
573
c42ed3601b35 Simplify/unify listfiles recursion: populate directory node (and detect top of tree) at start of function rather than end (and redundantly in main). Move title printing down next to total printing.
Rob Landley <rob@landley.net>
parents: 571
diff changeset
302
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
303 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
304 }
c42ed3601b35 Simplify/unify listfiles recursion: populate directory node (and detect top of tree) at start of function rather than end (and redundantly in main). Move title printing down next to total printing.
Rob Landley <rob@landley.net>
parents: 571
diff changeset
305
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
306 // Loop through again to produce output.
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
307 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
308 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
309 for (ul = 0; ul<dtlen; ul++) {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
310 unsigned curcol;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
311 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
312 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
313 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
314 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
315
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
316 // 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
317 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
318 TT.nl_title=1;
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
319
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
320 // Handle padding and wrapping for display purposes
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
321 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
322 if (ul) {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
323 if (flags & FLAG_m) xputc(',');
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
324 if (flags & (FLAG_C|FLAG_x)) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
325 if (!curcol) xputc('\n');
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
326 } 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
327 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
328 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
329 } 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
330 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
331 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
332 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
335
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
336 if (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
337 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
338 if (flags & FLAG_s)
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
339 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
340
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
341 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
342 struct tm *tm;
650
dd82e0b28eda Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
Rob Landley <rob@landley.net>
parents: 638
diff changeset
343 char perm[11], thyme[64], c, d, *usr, *upad, *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
344 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
345
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
346 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
347 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
348 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
349 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
350 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
351 c = "tss"[d];
638
92200901cfe1 Make chmod +w respect umask, implement +s and +t, fix ls to show suid/sgid/stid without x bit.
Rob Landley <rob@landley.net>
parents: 597
diff changeset
352 if (!bit) c &= ~0x20;
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
353 } 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
354 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
355 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
356
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 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
358 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
359 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
360 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
361 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
362 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
363 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
364 *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
365
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
366 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
367 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
368
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
369 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
370 else {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
371 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
372 : getgroupname(st->st_gid);
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
373 grpad = toybuf+256-(totals[4]-len[4]);
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
374 }
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
375
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
376 if (flags&FLAG_g) usr = upad = toybuf+256;
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
377 else {
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
378 upad = toybuf+255-(totals[3]-len[3]);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
379 if (flags&FLAG_n) {
650
dd82e0b28eda Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
Rob Landley <rob@landley.net>
parents: 638
diff changeset
380 usr = TT.uid_buf;
dd82e0b28eda Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
Rob Landley <rob@landley.net>
parents: 638
diff changeset
381 utoa_to_buf(st->st_uid, TT.uid_buf, 12);
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
382 } else usr = getusername(st->st_uid);
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
383 }
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
384
597
b2d0e8e572ba Fix an embarassing bug causing ls -l to segfault on 32 bit targets.
Rob Landley <rob@landley.net>
parents: 593
diff changeset
385 // 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
386 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
387 (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
388 (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
389 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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
591
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
391 if (flags & FLAG_q) {
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
392 char *p;
7c4ca3f0536b Add ls -kqsunort, and fix -F @symlink.
Rob Landley <rob@landley.net>
parents: 584
diff changeset
393 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
394 } else xprintf("%s", sort[next]->name);
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
395 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
396 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
397
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
398 if (et) xputc(et);
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
399
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
400 // Pad columns
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
401 if (flags & (FLAG_C|FLAG_x)) {
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
402 curcol = colsizes[curcol] - *len;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
403 if (curcol >= 0) xprintf("%s", toybuf+255-curcol);
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
404 }
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
405 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
406
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
407 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
408
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
409 // Free directory entries, recursing first if necessary.
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
410
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
411 for (ul = 0; ul<dtlen; free(sort[ul++])) {
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
412 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
413 || !dirtree_notdotdot(sort[ul])) continue;
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
414
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
415 // 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
416 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
417 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
418 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
419 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
420 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
421 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 void ls_main(void)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
424 {
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
425 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
426 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
427
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
428 // Do we have an implied -1
593
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
429 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
430 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
431 else {
584
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
432 TT.screen_width = 80;
ca6875170b9a Implement -C and -x for ls.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
433 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
434 }
571
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
435 // 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
436 // 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
437 // behave differently
1a06fcaa1775 Make -d work in ls.
Rob Landley <rob@landley.net>
parents: 570
diff changeset
438 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
439
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
440 // 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
441 // 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
442 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
443 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
444 dt = dirtree_add_node(AT_FDCWD, *s,
fb582378a36a Implement DIRTREE_SYMFOLLOW and ls -cSHL.
Rob Landley <rob@landley.net>
parents: 591
diff changeset
445 (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
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 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
448 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
449 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
450 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
451
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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 // 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
453 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
454 (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
455 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (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
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 533
diff changeset
457 // 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
458 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
459
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
460 // 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
461 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
462
573
c42ed3601b35 Simplify/unify listfiles recursion: populate directory node (and detect top of tree) at start of function rather than end (and redundantly in main). Move title printing down next to total printing.
Rob Landley <rob@landley.net>
parents: 571
diff changeset
463 if (CFG_TOYBOX_FREE) free(TT.files);
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
464 }