Mercurial > hg > toybox
annotate toys/ls.c @ 503:3b9dea897dc0
Upgrade yesno() and make cp -i use it.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 27 Feb 2012 21:56:49 -0600 |
parents | 763d581badae |
children | 25617169cf49 |
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 |
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
|
9 USE_LS(NEWTOY(ls, "nRlF1a", 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 |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
15 usage: ls [-l] [-F] [-a] [-1] [directory...] |
458 | 16 list files |
17 | |
18 -F append a character as a file type indicator | |
19 -a list all files | |
20 -1 list one file per line | |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
21 -l show full details for each file |
458 | 22 */ |
23 | |
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
|
24 /* 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
|
25 #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
|
26 |
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 #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
|
28 #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
|
29 #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
|
30 #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
|
31 |
458 | 32 #include "toys.h" |
33 | |
34 #define FLAG_a 1 | |
35 #define FLAG_1 2 | |
36 #define FLAG_F 4 | |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
37 #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
|
38 #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
|
39 #define FLAG_n 32 |
458 | 40 |
41 static int dir_filter(const struct dirent *d) | |
42 { | |
43 /* Skip over the . & .. entries unless -a is given */ | |
44 if (!(toys.optflags & FLAG_a)) | |
45 if (d->d_name[0] == '.') | |
46 return 0; | |
47 return 1; | |
48 } | |
49 | |
50 static void do_ls(int fd, char *name) | |
51 { | |
52 struct dirent **entries; | |
53 int nentries; | |
54 int i; | |
55 int maxwidth = -1; | |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
56 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
|
57 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
|
58 struct dirent *file_direntp; |
458 | 59 |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
60 if (!name || strcmp(name, "-") == 0) |
458 | 61 name = "."; |
62 | |
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
|
63 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
|
64 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
|
65 |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
66 /* Get all the files in this directory */ |
458 | 67 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
|
68 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
|
69 /* 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
|
70 /* 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
|
71 * 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
|
72 */ |
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 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 } 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
|
79 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
|
80 } |
458 | 81 |
82 | |
83 /* Determine the widest entry so we can flow them properly */ | |
84 if (!(toys.optflags & FLAG_1)) { | |
85 int columns; | |
86 char *columns_str; | |
87 | |
88 for (i = 0; i < nentries; i++) { | |
89 struct dirent *ent = entries[i]; | |
90 int width; | |
91 | |
92 width = strlen(ent->d_name); | |
93 if (width > maxwidth) | |
94 maxwidth = width; | |
95 } | |
96 /* We always want at least a single space for each entry */ | |
97 maxwidth++; | |
98 if (toys.optflags & FLAG_F) | |
99 maxwidth++; | |
100 | |
101 columns_str = getenv("COLUMNS"); | |
102 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
|
103 ncolumns = maxwidth ? columns / maxwidth : 1; |
458 | 104 } |
105 | |
106 for (i = 0; i < nentries; i++) { | |
107 struct dirent *ent = entries[i]; | |
108 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
|
109 struct stat st; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
110 int stat_valid = 0; |
458 | 111 |
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
|
112 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
|
113 |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
114 /* Provide the ls -l long output */ |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
115 if (toys.optflags & FLAG_l) { |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
116 char type; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
117 char timestamp[64]; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
118 struct tm mtime; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
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 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
|
121 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
|
122 stat_valid = 1; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
123 if (S_ISDIR(st.st_mode)) |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
124 type = 'd'; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
125 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
|
126 type = 'c'; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
127 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
|
128 type = 'b'; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
129 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
|
130 type = 'l'; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
131 else |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
132 type = '-'; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
133 |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
134 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
|
135 (st.st_mode & S_IRUSR) ? 'r' : '-', |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
136 (st.st_mode & S_IWUSR) ? 'w' : '-', |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
137 (st.st_mode & S_IXUSR) ? 'x' : '-', |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
138 (st.st_mode & S_IRGRP) ? 'r' : '-', |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
139 (st.st_mode & S_IWGRP) ? 'w' : '-', |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
140 (st.st_mode & S_IXGRP) ? 'x' : '-', |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
141 (st.st_mode & S_IROTH) ? 'r' : '-', |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
142 (st.st_mode & S_IWOTH) ? 'w' : '-', |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
143 (st.st_mode & S_IXOTH) ? 'x' : '-'); |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
144 |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
145 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
|
146 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
|
147 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
|
148 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
|
149 } 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 } |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
161 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
|
162 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
|
163 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
|
164 xprintf("%12lld ", st.st_size); |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
165 |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
166 localtime_r(&st.st_mtime, &mtime); |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
167 |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
168 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
|
169 xprintf("%s ", timestamp); |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
170 } |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
171 |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
172 xprintf("%s", ent->d_name); |
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 /* Append the file-type indicator character */ |
458 | 175 if (toys.optflags & FLAG_F) { |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
176 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
|
177 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
|
178 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
|
179 stat_valid = 1; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
180 } |
458 | 181 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
|
182 xprintf("/"); |
458 | 183 len++; |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
184 } else if (S_ISREG(st.st_mode) && |
458 | 185 (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
|
186 xprintf("*"); |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
187 len++; |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
188 } 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
|
189 xprintf("@"); |
458 | 190 len++; |
191 } | |
192 } | |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
193 if (toys.optflags & FLAG_1) { |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
194 xprintf("\n"); |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
195 } else { |
458 | 196 if (i % ncolumns == ncolumns - 1) |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
197 xprintf("\n"); |
458 | 198 else |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
199 xprintf("%*s", maxwidth - len, ""); |
458 | 200 } |
201 } | |
202 /* Make sure we put at a trailing new line in */ | |
203 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
|
204 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
|
205 |
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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 |
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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 } |
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 } |
458 | 219 } |
220 | |
221 void ls_main(void) | |
222 { | |
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
|
223 /* 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
|
224 * 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
|
225 */ |
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
|
226 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
|
227 toys.optflags |= FLAG_1; |
459
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
228 /* 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
|
229 if (toys.optflags & FLAG_l) |
1dbe91079950
Second drop of ls from Andre, adds -l.
Rob Landley <rob@landley.net>
parents:
458
diff
changeset
|
230 toys.optflags |= FLAG_1; |
458 | 231 loopfiles(toys.optargs, do_ls); |
232 } |