Mercurial > hg > toybox
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 |
rev | line source |
---|---|
458 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * ls.c - list files | |
4 * | |
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> | |
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 | 8 |
514 | 9 USE_LS(NEWTOY(ls, "AnRlF1a", TOYFLAG_BIN)) |
458 | 10 |
11 config LS | |
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 | 14 help |
514 | 15 usage: ls [-lFaA1] [directory...] |
458 | 16 list files |
17 | |
514 | 18 -1 list one file per line |
458 | 19 -a list all files |
514 | 20 -A list all files except . and .. |
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 | 23 */ |
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 | 33 #include "toys.h" |
34 | |
35 #define FLAG_a 1 | |
36 #define FLAG_1 2 | |
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 | 41 #define FLAG_A 64 |
458 | 42 |
43 static int dir_filter(const struct dirent *d) | |
44 { | |
514 | 45 /* Skip over all '.*' entries, unless -a is given */ |
46 if (!(toys.optflags & FLAG_a)) { | |
47 /* -A means show everything except the . & .. entries */ | |
48 if (toys.optflags & FLAG_A) { | |
49 if (strcmp(d->d_name, ".") == 0 || | |
50 strcmp(d->d_name, "..") == 0) | |
51 return 0; | |
52 } else if (d->d_name[0] == '.') | |
458 | 53 return 0; |
514 | 54 } |
458 | 55 return 1; |
56 } | |
57 | |
58 static void do_ls(int fd, char *name) | |
59 { | |
60 struct dirent **entries; | |
61 int nentries; | |
62 int i; | |
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 | 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 | 69 name = "."; |
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 | 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 | 89 |
90 | |
91 /* Determine the widest entry so we can flow them properly */ | |
92 if (!(toys.optflags & FLAG_1)) { | |
93 int columns; | |
94 char *columns_str; | |
95 | |
96 for (i = 0; i < nentries; i++) { | |
97 struct dirent *ent = entries[i]; | |
98 int width; | |
99 | |
100 width = strlen(ent->d_name); | |
101 if (width > maxwidth) | |
102 maxwidth = width; | |
103 } | |
104 /* We always want at least a single space for each entry */ | |
105 maxwidth++; | |
106 if (toys.optflags & FLAG_F) | |
107 maxwidth++; | |
108 | |
109 columns_str = getenv("COLUMNS"); | |
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 | 112 } |
113 | |
114 for (i = 0; i < nentries; i++) { | |
115 struct dirent *ent = entries[i]; | |
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 | 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 | 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 | 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 | 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 | 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 | 198 len++; |
199 } | |
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 | 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 | 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 | 208 } |
209 } | |
210 /* Make sure we put at a trailing new line in */ | |
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 | 227 } |
228 | |
229 void ls_main(void) | |
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 | 239 loopfiles(toys.optargs, do_ls); |
240 } |