annotate toys/ls.c @ 514:25617169cf49

Add -A to ls
author Andre Renaud <andre@bluewatersys.com>
date Sat, 03 Mar 2012 18:17:49 -0600
parents 763d581badae
children 31215cc6c9f2
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>
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
7 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
9 USE_LS(NEWTOY(ls, "AnRlF1a", TOYFLAG_BIN))
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config LS
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "ls"
502
763d581badae The aboriginal linux build needs ls -ditc and probably some more unimplemented options yet...
Rob Landley <rob@landley.net>
parents: 491
diff changeset
13 default n
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
15 usage: ls [-lFaA1] [directory...]
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 list files
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
18 -1 list one file per line
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -a list all files
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
20 -A list all files except . and ..
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
21 -F append a character as a file type indicator
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
22 -l show full details for each file
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
25 /* So that we can do 64-bit stat etc... */
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
26 #define _FILE_OFFSET_BITS 64
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
27
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
28 #include <unistd.h>
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
29 #include <sys/types.h>
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
30 #include <grp.h>
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
31 #include <pwd.h>
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
32
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 #include "toys.h"
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #define FLAG_a 1
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 #define FLAG_1 2
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 #define FLAG_F 4
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
38 #define FLAG_l 8
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
39 #define FLAG_R 16
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
40 #define FLAG_n 32
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
41 #define FLAG_A 64
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
42
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 static int dir_filter(const struct dirent *d)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 {
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
45 /* Skip over all '.*' entries, unless -a is given */
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
46 if (!(toys.optflags & FLAG_a)) {
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
47 /* -A means show everything except the . & .. entries */
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
48 if (toys.optflags & FLAG_A) {
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
49 if (strcmp(d->d_name, ".") == 0 ||
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
50 strcmp(d->d_name, "..") == 0)
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
51 return 0;
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
52 } else if (d->d_name[0] == '.')
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 return 0;
514
25617169cf49 Add -A to ls
Andre Renaud <andre@bluewatersys.com>
parents: 502
diff changeset
54 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 return 1;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 static void do_ls(int fd, char *name)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 {
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 struct dirent **entries;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 int nentries;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 int i;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 int maxwidth = -1;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
64 int ncolumns = 1;
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
65 struct dirent file_dirent;
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
66 struct dirent *file_direntp;
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
67
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
68 if (!name || strcmp(name, "-") == 0)
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 name = ".";
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
70
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
71 if (toys.optflags & FLAG_R)
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
72 xprintf("\n%s:\n", name);
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
73
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
74 /* Get all the files in this directory */
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 nentries = scandir(name, &entries, dir_filter, alphasort);
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
76 if (nentries < 0) {
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
77 /* We've just selected a single file, so create a single-length list */
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
78 /* FIXME: This means that ls *.x results in a whole bunch of single
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 * listings, not one combined listing.
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 */
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
81 if (errno == ENOTDIR) {
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
82 nentries = 1;
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
83 strcpy(file_dirent.d_name, name);
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
84 file_direntp = &file_dirent;
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
85 entries = &file_direntp;
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
86 } else
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
87 perror_exit("ls: cannot access %s'", name);
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
88 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
89
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
90
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 /* Determine the widest entry so we can flow them properly */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 if (!(toys.optflags & FLAG_1)) {
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 int columns;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 char *columns_str;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
95
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 for (i = 0; i < nentries; i++) {
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 struct dirent *ent = entries[i];
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 int width;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
99
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 width = strlen(ent->d_name);
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 if (width > maxwidth)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 maxwidth = width;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 /* We always want at least a single space for each entry */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 maxwidth++;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 if (toys.optflags & FLAG_F)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 maxwidth++;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
108
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 columns_str = getenv("COLUMNS");
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 columns = columns_str ? atoi(columns_str) : 80;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
111 ncolumns = maxwidth ? columns / maxwidth : 1;
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
113
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 for (i = 0; i < nentries; i++) {
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 struct dirent *ent = entries[i];
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 int len = strlen(ent->d_name);
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
117 struct stat st;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
118 int stat_valid = 0;
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
119
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
120 sprintf(toybuf, "%s/%s", name, ent->d_name);
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
121
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
122 /* Provide the ls -l long output */
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
123 if (toys.optflags & FLAG_l) {
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
124 char type;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
125 char timestamp[64];
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
126 struct tm mtime;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
127
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
128 if (lstat(toybuf, &st))
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
129 perror_exit("Can't stat %s", toybuf);
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
130 stat_valid = 1;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
131 if (S_ISDIR(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
132 type = 'd';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
133 else if (S_ISCHR(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
134 type = 'c';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
135 else if (S_ISBLK(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
136 type = 'b';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
137 else if (S_ISLNK(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
138 type = 'l';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
139 else
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
140 type = '-';
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
141
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
142 xprintf("%c%c%c%c%c%c%c%c%c%c ", type,
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
143 (st.st_mode & S_IRUSR) ? 'r' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
144 (st.st_mode & S_IWUSR) ? 'w' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
145 (st.st_mode & S_IXUSR) ? 'x' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
146 (st.st_mode & S_IRGRP) ? 'r' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
147 (st.st_mode & S_IWGRP) ? 'w' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
148 (st.st_mode & S_IXGRP) ? 'x' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
149 (st.st_mode & S_IROTH) ? 'r' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
150 (st.st_mode & S_IWOTH) ? 'w' : '-',
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
151 (st.st_mode & S_IXOTH) ? 'x' : '-');
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
152
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
153 xprintf("%2d ", st.st_nlink);
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
154 if (toys.optflags & FLAG_n) {
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
155 xprintf("%4d ", st.st_uid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
156 xprintf("%4d ", st.st_gid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
157 } else {
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
158 struct passwd *pwd = getpwuid(st.st_uid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
159 struct group *grp = getgrgid(st.st_gid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
160 if (!pwd)
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
161 xprintf("%4d ", st.st_uid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
162 else
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
163 xprintf("%-10s ", pwd->pw_name);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
164 if (!grp)
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
165 xprintf("%4d ", st.st_gid);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
166 else
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
167 xprintf("%-10s ", grp->gr_name);
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
168 }
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
169 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
170 xprintf("%3d, %3d ", major(st.st_rdev), minor(st.st_rdev));
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
171 else
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
172 xprintf("%12lld ", st.st_size);
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
173
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
174 localtime_r(&st.st_mtime, &mtime);
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
175
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
176 strftime(timestamp, sizeof(timestamp), "%b %e %H:%M", &mtime);
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
177 xprintf("%s ", timestamp);
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
178 }
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
179
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
180 xprintf("%s", ent->d_name);
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
181
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
182 /* Append the file-type indicator character */
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 if (toys.optflags & FLAG_F) {
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
184 if (!stat_valid) {
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
185 if (lstat(toybuf, &st))
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
186 perror_exit("Can't stat %s", toybuf);
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
187 stat_valid = 1;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
188 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 if (S_ISDIR(st.st_mode)) {
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
190 xprintf("/");
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 len++;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
192 } else if (S_ISREG(st.st_mode) &&
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
194 xprintf("*");
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
195 len++;
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
196 } else if (S_ISLNK(st.st_mode)) {
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
197 xprintf("@");
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 len++;
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 }
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
201 if (toys.optflags & FLAG_1) {
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
202 xprintf("\n");
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
203 } else {
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 if (i % ncolumns == ncolumns - 1)
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
205 xprintf("\n");
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 else
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
207 xprintf("%*s", maxwidth - len, "");
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 /* Make sure we put at a trailing new line in */
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 if (!(toys.optflags & FLAG_1) && (i % ncolumns))
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
212 xprintf("\n");
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
213
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
214 if (toys.optflags & FLAG_R) {
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
215 for (i = 0; i < nentries; i++) {
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
216 struct dirent *ent = entries[i];
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
217 struct stat st;
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
218 char dirname[PATH_MAX];
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
219
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
220 sprintf(dirname, "%s/%s", name, ent->d_name);
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
221 if (lstat(dirname, &st))
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
222 perror_exit("Can't stat %s", dirname);
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
223 if (S_ISDIR(st.st_mode))
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
224 do_ls(0, dirname);
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
225 }
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
226 }
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 }
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
228
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 void ls_main(void)
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 {
491
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
231 /* If the output is not a TTY, then just do one-file per line
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
232 * This makes ls easier to use with other command line tools (grep/awk etc...)
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
233 */
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
234 if (!isatty(fileno(stdout)))
176667e320f0 Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
Andre Renaud <andre@bluewatersys.com>
parents: 476
diff changeset
235 toys.optflags |= FLAG_1;
459
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
236 /* Long output must be one-file per line */
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
237 if (toys.optflags & FLAG_l)
1dbe91079950 Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents: 458
diff changeset
238 toys.optflags |= FLAG_1;
458
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 loopfiles(toys.optargs, do_ls);
9786a697d5aa Add ls from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 }