comparison toys/ls.c @ 491:176667e320f0

Add in the -n support, and the 64-bit defines. Force ls to act as ls -1 if used in a pipe.
author Andre Renaud <andre@bluewatersys.com>
date Tue, 21 Feb 2012 20:48:52 -0600
parents d10b58563cff
children 763d581badae
comparison
equal deleted inserted replaced
490:96a5e66a7dae 491:176667e320f0
4 * 4 *
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> 5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
6 * 6 *
7 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html 7 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html
8 8
9 USE_LS(NEWTOY(ls, "RlF1a", TOYFLAG_BIN)) 9 USE_LS(NEWTOY(ls, "nRlF1a", TOYFLAG_BIN))
10 10
11 config LS 11 config LS
12 bool "ls" 12 bool "ls"
13 default y 13 default y
14 help 14 help
19 -a list all files 19 -a list all files
20 -1 list one file per line 20 -1 list one file per line
21 -l show full details for each file 21 -l show full details for each file
22 */ 22 */
23 23
24 /* So that we can do 64-bit stat etc... */
25 #define _FILE_OFFSET_BITS 64
26
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <grp.h>
30 #include <pwd.h>
31
24 #include "toys.h" 32 #include "toys.h"
25 33
26 #define FLAG_a 1 34 #define FLAG_a 1
27 #define FLAG_1 2 35 #define FLAG_1 2
28 #define FLAG_F 4 36 #define FLAG_F 4
29 #define FLAG_l 8 37 #define FLAG_l 8
30 #define FLAG_R 16 38 #define FLAG_R 16
39 #define FLAG_n 32
31 40
32 static int dir_filter(const struct dirent *d) 41 static int dir_filter(const struct dirent *d)
33 { 42 {
34 /* Skip over the . & .. entries unless -a is given */ 43 /* Skip over the . & .. entries unless -a is given */
35 if (!(toys.optflags & FLAG_a)) 44 if (!(toys.optflags & FLAG_a))
132 (st.st_mode & S_IROTH) ? 'r' : '-', 141 (st.st_mode & S_IROTH) ? 'r' : '-',
133 (st.st_mode & S_IWOTH) ? 'w' : '-', 142 (st.st_mode & S_IWOTH) ? 'w' : '-',
134 (st.st_mode & S_IXOTH) ? 'x' : '-'); 143 (st.st_mode & S_IXOTH) ? 'x' : '-');
135 144
136 xprintf("%2d ", st.st_nlink); 145 xprintf("%2d ", st.st_nlink);
137 /* FIXME: We're never looking up uid/gid numbers */ 146 if (toys.optflags & FLAG_n) {
138 xprintf("%4d ", st.st_uid); 147 xprintf("%4d ", st.st_uid);
139 xprintf("%4d ", st.st_gid); 148 xprintf("%4d ", st.st_gid);
149 } else {
150 struct passwd *pwd = getpwuid(st.st_uid);
151 struct group *grp = getgrgid(st.st_gid);
152 if (!pwd)
153 xprintf("%4d ", st.st_uid);
154 else
155 xprintf("%-10s ", pwd->pw_name);
156 if (!grp)
157 xprintf("%4d ", st.st_gid);
158 else
159 xprintf("%-10s ", grp->gr_name);
160 }
140 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) 161 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
141 xprintf("%3d, %3d ", major(st.st_rdev), minor(st.st_rdev)); 162 xprintf("%3d, %3d ", major(st.st_rdev), minor(st.st_rdev));
142 else 163 else
143 xprintf("%8ld ", st.st_size); 164 xprintf("%12lld ", st.st_size);
144 165
145 localtime_r(&st.st_mtime, &mtime); 166 localtime_r(&st.st_mtime, &mtime);
146 167
147 strftime(timestamp, sizeof(timestamp), "%b %e %H:%M", &mtime); 168 strftime(timestamp, sizeof(timestamp), "%b %e %H:%M", &mtime);
148 xprintf("%s ", timestamp); 169 xprintf("%s ", timestamp);
197 } 218 }
198 } 219 }
199 220
200 void ls_main(void) 221 void ls_main(void)
201 { 222 {
223 /* If the output is not a TTY, then just do one-file per line
224 * This makes ls easier to use with other command line tools (grep/awk etc...)
225 */
226 if (!isatty(fileno(stdout)))
227 toys.optflags |= FLAG_1;
202 /* Long output must be one-file per line */ 228 /* Long output must be one-file per line */
203 if (toys.optflags & FLAG_l) 229 if (toys.optflags & FLAG_l)
204 toys.optflags |= FLAG_1; 230 toys.optflags |= FLAG_1;
205 loopfiles(toys.optargs, do_ls); 231 loopfiles(toys.optargs, do_ls);
206 } 232 }