Commit | Line | Data |
---|---|---|
bcf1365c | 1 | /* |
cf0a6ef1 KM |
2 | * Copyright (c) 1989, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
9ffe97fe KB |
4 | * |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Michael Fischbein. | |
7 | * | |
ae14fb92 | 8 | * %sccs.include.redist.c% |
bcf1365c | 9 | */ |
9ffe97fe KB |
10 | |
11 | #ifndef lint | |
3952e42c | 12 | static char copyright[] = |
cf0a6ef1 KM |
13 | "@(#) Copyright (c) 1989, 1993\n\ |
14 | The Regents of the University of California. All rights reserved.\n"; | |
9ffe97fe KB |
15 | #endif /* not lint */ |
16 | ||
17 | #ifndef lint | |
cf0a6ef1 | 18 | static char sccsid[] = "@(#)ls.c 8.1 (Berkeley) %G%"; |
9ffe97fe KB |
19 | #endif /* not lint */ |
20 | ||
abe0683d | 21 | #include <sys/types.h> |
9ffe97fe | 22 | #include <sys/stat.h> |
4847d483 | 23 | #include <sys/ioctl.h> |
c3346b7f | 24 | |
f59807e9 | 25 | #include <dirent.h> |
c3346b7f KB |
26 | #include <err.h> |
27 | #include <errno.h> | |
f0a884e9 | 28 | #include <fts.h> |
c3346b7f | 29 | #include <stdio.h> |
0ce0ae04 | 30 | #include <stdlib.h> |
6ebcb998 | 31 | #include <string.h> |
c3346b7f KB |
32 | #include <unistd.h> |
33 | ||
b3f77fd1 | 34 | #include "ls.h" |
0ce0ae04 | 35 | #include "extern.h" |
2fd18a0a | 36 | |
abe0683d | 37 | static void display __P((FTSENT *, FTSENT *)); |
abe0683d EA |
38 | static int mastercmp __P((const FTSENT **, const FTSENT **)); |
39 | static void traverse __P((int, char **, int)); | |
40 | ||
41 | static void (*printfcn) __P((DISPLAY *)); | |
f0a884e9 | 42 | static int (*sortfcn) __P((const FTSENT *, const FTSENT *)); |
bcf1365c | 43 | |
abe0683d | 44 | long blocksize; /* block size units */ |
fb29be9b KB |
45 | int termwidth = 80; /* default terminal width */ |
46 | ||
9ffe97fe | 47 | /* flags */ |
9ffe97fe | 48 | int f_accesstime; /* use time of last access */ |
4845c19f | 49 | int f_column; /* columnated format */ |
32980675 | 50 | int f_flags; /* show flags associated with a file */ |
9ffe97fe | 51 | int f_inode; /* print inode */ |
38eeee6d KB |
52 | int f_listdir; /* list actual directory, not contents */ |
53 | int f_listdot; /* list files beginning with . */ | |
9ffe97fe | 54 | int f_longform; /* long listing format */ |
4dfe4db2 | 55 | int f_newline; /* if precede with newline */ |
9ffe97fe | 56 | int f_nonprint; /* show unprintables as ? */ |
9d5fbfa8 | 57 | int f_nosort; /* don't sort output */ |
9ffe97fe | 58 | int f_recursive; /* ls subdirectories also */ |
38eeee6d | 59 | int f_reversesort; /* reverse whatever sort is used */ |
926d5d7d | 60 | int f_sectime; /* print the real time for all files */ |
38eeee6d | 61 | int f_singlecol; /* use single column output */ |
9ffe97fe | 62 | int f_size; /* list size in short listing */ |
38eeee6d | 63 | int f_statustime; /* use time of last mode change */ |
4dfe4db2 | 64 | int f_dirname; /* if precede with directory name */ |
9ffe97fe | 65 | int f_timesort; /* sort by time vice name */ |
38eeee6d | 66 | int f_type; /* add type character for non-regular files */ |
2fd18a0a | 67 | |
4db4a32d | 68 | int |
9ffe97fe KB |
69 | main(argc, argv) |
70 | int argc; | |
f0a884e9 | 71 | char *argv[]; |
547b0031 | 72 | { |
f59807e9 | 73 | static char dot[] = ".", *dotav[] = { dot, NULL }; |
4847d483 | 74 | struct winsize win; |
4933888a | 75 | int ch, fts_options, notused; |
0ce0ae04 | 76 | char *p; |
2fd18a0a | 77 | |
f0a884e9 | 78 | /* Terminal defaults to -Cq, non-terminal defaults to -1. */ |
f59807e9 KB |
79 | if (isatty(STDOUT_FILENO)) { |
80 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 || | |
81 | !win.ws_col) { | |
fb29be9b KB |
82 | if (p = getenv("COLUMNS")) |
83 | termwidth = atoi(p); | |
84 | } | |
4847d483 KB |
85 | else |
86 | termwidth = win.ws_col; | |
f59807e9 | 87 | f_column = f_nonprint = 1; |
9ffe97fe KB |
88 | } else |
89 | f_singlecol = 1; | |
2fd18a0a | 90 | |
f0a884e9 | 91 | /* Root is -A automatically. */ |
9ffe97fe | 92 | if (!getuid()) |
b3f77fd1 | 93 | f_listdot = 1; |
9ffe97fe | 94 | |
4db4a32d | 95 | fts_options = FTS_PHYSICAL; |
c58b1817 | 96 | while ((ch = getopt(argc, argv, "1ACFLRTacdfgiloqrstu")) != EOF) { |
9ffe97fe | 97 | switch (ch) { |
38eeee6d | 98 | /* |
f0a884e9 EA |
99 | * The -1, -C and -l options all override each other so shell |
100 | * aliasing works right. | |
38eeee6d | 101 | */ |
9ffe97fe KB |
102 | case '1': |
103 | f_singlecol = 1; | |
4845c19f | 104 | f_column = f_longform = 0; |
2fd18a0a | 105 | break; |
9ffe97fe | 106 | case 'C': |
4845c19f | 107 | f_column = 1; |
38eeee6d KB |
108 | f_longform = f_singlecol = 0; |
109 | break; | |
110 | case 'l': | |
111 | f_longform = 1; | |
4845c19f | 112 | f_column = f_singlecol = 0; |
9ffe97fe | 113 | break; |
f59807e9 | 114 | /* The -c and -u options override each other. */ |
38eeee6d KB |
115 | case 'c': |
116 | f_statustime = 1; | |
117 | f_accesstime = 0; | |
118 | break; | |
119 | case 'u': | |
120 | f_accesstime = 1; | |
121 | f_statustime = 0; | |
122 | break; | |
9ffe97fe KB |
123 | case 'F': |
124 | f_type = 1; | |
125 | break; | |
126 | case 'L': | |
4db4a32d KB |
127 | fts_options &= ~FTS_PHYSICAL; |
128 | fts_options |= FTS_LOGICAL; | |
9ffe97fe KB |
129 | break; |
130 | case 'R': | |
131 | f_recursive = 1; | |
9ffe97fe KB |
132 | break; |
133 | case 'a': | |
f0a884e9 | 134 | fts_options |= FTS_SEEDOT; |
9ffe97fe | 135 | /* FALLTHROUGH */ |
2fd18a0a | 136 | case 'A': |
b3f77fd1 | 137 | f_listdot = 1; |
2fd18a0a | 138 | break; |
f59807e9 | 139 | /* The -d option turns off the -R option. */ |
1c52c7b1 JB |
140 | case 'S': |
141 | Sflg++; /* fall into... */ | |
547b0031 | 142 | case 'd': |
9ffe97fe | 143 | f_listdir = 1; |
f59807e9 | 144 | f_recursive = 0; |
2fd18a0a | 145 | break; |
9d5fbfa8 KB |
146 | case 'f': |
147 | f_nosort = 1; | |
148 | break; | |
abe0683d | 149 | case 'g': /* Compatibility with 4.3BSD. */ |
2fd18a0a | 150 | break; |
ec0c22c1 | 151 | case 'i': |
9ffe97fe | 152 | f_inode = 1; |
2fd18a0a | 153 | break; |
32980675 KM |
154 | case 'o': |
155 | f_flags = 1; | |
156 | break; | |
ec0c22c1 | 157 | case 'q': |
9ffe97fe | 158 | f_nonprint = 1; |
2fd18a0a | 159 | break; |
547b0031 | 160 | case 'r': |
9ffe97fe | 161 | f_reversesort = 1; |
2fd18a0a | 162 | break; |
ec0c22c1 | 163 | case 's': |
9ffe97fe | 164 | f_size = 1; |
2fd18a0a | 165 | break; |
926d5d7d KB |
166 | case 'T': |
167 | f_sectime = 1; | |
168 | break; | |
547b0031 | 169 | case 't': |
9ffe97fe | 170 | f_timesort = 1; |
2fd18a0a | 171 | break; |
ec0c22c1 | 172 | default: |
2fd18a0a | 173 | case '?': |
9ffe97fe | 174 | usage(); |
2955376b | 175 | } |
9ffe97fe | 176 | } |
b3f77fd1 KB |
177 | argc -= optind; |
178 | argv += optind; | |
9ffe97fe | 179 | |
65c9f129 KB |
180 | /* |
181 | * If not -F, -i, -l, -s or -t options, don't require stat | |
a86fb882 EA |
182 | * information. |
183 | */ | |
65c9f129 | 184 | if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type) |
f0a884e9 | 185 | fts_options |= FTS_NOSTAT; |
b3f77fd1 | 186 | |
91d39f6b KB |
187 | /* |
188 | * If not -F, -d or -l options, follow any symbolic links listed on | |
189 | * the command line. | |
190 | */ | |
191 | if (!f_longform && !f_listdir && !f_type) | |
192 | fts_options |= FTS_COMFOLLOW; | |
193 | ||
4933888a KB |
194 | /* If -l or -s, figure out block size. */ |
195 | if (f_longform || f_size) { | |
de637784 | 196 | (void)getbsize(¬used, &blocksize); |
4933888a KB |
197 | blocksize /= 512; |
198 | } | |
199 | ||
f0a884e9 | 200 | /* Select a sort function. */ |
9ffe97fe KB |
201 | if (f_reversesort) { |
202 | if (!f_timesort) | |
203 | sortfcn = revnamecmp; | |
204 | else if (f_accesstime) | |
205 | sortfcn = revacccmp; | |
9ffe97fe KB |
206 | else if (f_statustime) |
207 | sortfcn = revstatcmp; | |
f0a884e9 | 208 | else /* Use modification time. */ |
38eeee6d | 209 | sortfcn = revmodcmp; |
2fd18a0a | 210 | } else { |
9ffe97fe KB |
211 | if (!f_timesort) |
212 | sortfcn = namecmp; | |
213 | else if (f_accesstime) | |
214 | sortfcn = acccmp; | |
9ffe97fe KB |
215 | else if (f_statustime) |
216 | sortfcn = statcmp; | |
f0a884e9 | 217 | else /* Use modification time. */ |
38eeee6d | 218 | sortfcn = modcmp; |
9ffe97fe KB |
219 | } |
220 | ||
f0a884e9 | 221 | /* Select a print function. */ |
4847d483 KB |
222 | if (f_singlecol) |
223 | printfcn = printscol; | |
224 | else if (f_longform) | |
225 | printfcn = printlong; | |
b3f77fd1 | 226 | else |
4e9923b0 | 227 | printfcn = printcol; |
9ffe97fe | 228 | |
0a3501e4 | 229 | if (argc) |
f0a884e9 | 230 | traverse(argc, argv, fts_options); |
f59807e9 | 231 | else |
f0a884e9 | 232 | traverse(1, dotav, fts_options); |
4e9923b0 | 233 | exit(0); |
2fd18a0a KB |
234 | } |
235 | ||
f59807e9 KB |
236 | static int output; /* If anything output. */ |
237 | ||
f0a884e9 EA |
238 | /* |
239 | * Traverse() walks the logical directory structure specified by the argv list | |
240 | * in the order specified by the mastercmp() comparison function. During the | |
241 | * traversal it passes linked lists of structures to display() which represent | |
242 | * a superset (may be exact set) of the files to be displayed. | |
243 | */ | |
abe0683d | 244 | static void |
f0a884e9 EA |
245 | traverse(argc, argv, options) |
246 | int argc, options; | |
247 | char *argv[]; | |
248 | { | |
249 | register FTS *ftsp; | |
f5eca7a8 | 250 | register FTSENT *p, *chp; |
f59807e9 | 251 | int ch_options; |
abe0683d | 252 | |
f0a884e9 EA |
253 | if ((ftsp = |
254 | fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) | |
c3346b7f | 255 | err(1, NULL); |
91d39f6b | 256 | |
f59807e9 | 257 | display(NULL, fts_children(ftsp, 0)); |
f0a884e9 EA |
258 | if (f_listdir) |
259 | return; | |
f59807e9 | 260 | |
db658853 KB |
261 | /* |
262 | * If not recursing down this tree and don't need stat info, just get | |
263 | * the names. | |
264 | */ | |
f59807e9 | 265 | ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0; |
db658853 | 266 | |
f0a884e9 | 267 | while (p = fts_read(ftsp)) |
a6bebda8 | 268 | switch (p->fts_info) { |
f0a884e9 | 269 | case FTS_DC: |
c3346b7f | 270 | warnx("%s: directory causes a cycle", p->fts_name); |
f0a884e9 EA |
271 | break; |
272 | case FTS_DNR: | |
273 | case FTS_ERR: | |
a6bebda8 | 274 | errno = p->fts_errno; |
c3346b7f | 275 | warn("%s", p->fts_name); |
f0a884e9 EA |
276 | break; |
277 | case FTS_D: | |
4db4a32d KB |
278 | if (p->fts_level != FTS_ROOTLEVEL && |
279 | p->fts_name[0] == '.' && !f_listdot) | |
280 | break; | |
f59807e9 KB |
281 | |
282 | /* | |
283 | * If already output something, put out a newline as | |
284 | * a separator. If multiple arguments, precede each | |
285 | * directory with its name. | |
286 | */ | |
287 | if (output) | |
288 | (void)printf("\n%s:\n", p->fts_path); | |
289 | else if (argc > 1) { | |
290 | (void)printf("%s:\n", p->fts_path); | |
291 | output = 1; | |
292 | } | |
293 | ||
f5eca7a8 EA |
294 | chp = fts_children(ftsp, ch_options); |
295 | display(p, chp); | |
f59807e9 | 296 | |
f5eca7a8 | 297 | if (!f_recursive && chp != NULL) |
4db4a32d | 298 | (void)fts_set(ftsp, p, FTS_SKIP); |
f0a884e9 EA |
299 | break; |
300 | } | |
301 | (void)fts_close(ftsp); | |
302 | } | |
be4f2604 | 303 | |
f0a884e9 | 304 | /* |
09cf81e2 KB |
305 | * Display() takes a linked list of FTSENT structures and passes the list |
306 | * along with any other necessary information to the print function. P | |
307 | * points to the parent directory of the display list. | |
f0a884e9 | 308 | */ |
abe0683d | 309 | static void |
f59807e9 | 310 | display(p, list) |
f0a884e9 EA |
311 | register FTSENT *p; |
312 | FTSENT *list; | |
2fd18a0a | 313 | { |
f0a884e9 | 314 | register FTSENT *cur; |
abe0683d EA |
315 | struct stat *sp; |
316 | DISPLAY d; | |
317 | NAMES *np; | |
0e3001c9 KB |
318 | u_long btotal, maxblock, maxinode, maxlen, maxnlink; |
319 | u_quad_t maxsize; | |
09cf81e2 | 320 | int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser; |
abe0683d EA |
321 | int entries, needstats; |
322 | char *user, *group, *flags, buf[20]; /* 32 bits == 10 digits */ | |
323 | ||
f0a884e9 EA |
324 | /* |
325 | * If list is NULL there are two possibilities: that the parent | |
326 | * directory p has no children, or that fts_children() returned an | |
91d39f6b | 327 | * error. We ignore the error case since it will be replicated |
f0a884e9 EA |
328 | * on the next call to fts_read() on the post-order visit to the |
329 | * directory p, and will be signalled in traverse(). | |
330 | */ | |
f59807e9 | 331 | if (list == NULL) |
f0a884e9 | 332 | return; |
b3f77fd1 | 333 | |
abe0683d EA |
334 | needstats = f_inode || f_longform || f_size; |
335 | flen = 0; | |
0e3001c9 | 336 | btotal = maxblock = maxinode = maxlen = maxnlink = 0; |
09cf81e2 | 337 | bcfile = 0; |
abe0683d | 338 | maxuser = maxgroup = maxflags = 0; |
0e3001c9 | 339 | maxsize = 0; |
f59807e9 KB |
340 | for (cur = list, entries = 0; cur; cur = cur->fts_link) { |
341 | if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { | |
a6bebda8 | 342 | errno = cur->fts_errno; |
c3346b7f | 343 | warn("%s", cur->fts_name); |
f59807e9 KB |
344 | cur->fts_number = NO_PRINT; |
345 | continue; | |
9ffe97fe | 346 | } |
f59807e9 KB |
347 | |
348 | /* | |
abe0683d EA |
349 | * P is NULL if list is the argv list, to which different rules |
350 | * apply. | |
f59807e9 KB |
351 | */ |
352 | if (p == NULL) { | |
353 | /* Directories will be displayed later. */ | |
354 | if (cur->fts_info == FTS_D && !f_listdir) { | |
f0a884e9 | 355 | cur->fts_number = NO_PRINT; |
b3f77fd1 | 356 | continue; |
f0a884e9 | 357 | } |
f59807e9 KB |
358 | } else { |
359 | /* Only display dot file if -a/-A set. */ | |
f0a884e9 EA |
360 | if (cur->fts_name[0] == '.' && !f_listdot) { |
361 | cur->fts_number = NO_PRINT; | |
b3f77fd1 | 362 | continue; |
f0a884e9 | 363 | } |
b3f77fd1 | 364 | } |
f59807e9 KB |
365 | if (f_nonprint) |
366 | prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen); | |
abe0683d | 367 | if (cur->fts_namelen > maxlen) |
f59807e9 | 368 | maxlen = cur->fts_namelen; |
abe0683d EA |
369 | if (needstats) { |
370 | sp = cur->fts_statp; | |
371 | if (sp->st_blocks > maxblock) | |
372 | maxblock = sp->st_blocks; | |
373 | if (sp->st_ino > maxinode) | |
374 | maxinode = sp->st_ino; | |
375 | if (sp->st_nlink > maxnlink) | |
376 | maxnlink = sp->st_nlink; | |
377 | if (sp->st_size > maxsize) | |
378 | maxsize = sp->st_size; | |
379 | ||
380 | btotal += sp->st_blocks; | |
381 | if (f_longform) { | |
382 | user = user_from_uid(sp->st_uid, 0); | |
383 | if ((ulen = strlen(user)) > maxuser) | |
384 | maxuser = ulen; | |
385 | group = group_from_gid(sp->st_gid, 0); | |
386 | if ((glen = strlen(group)) > maxgroup) | |
387 | maxgroup = glen; | |
388 | if (f_flags) { | |
9cc3702b KB |
389 | flags = |
390 | flags_to_string(sp->st_flags, "-"); | |
abe0683d EA |
391 | if ((flen = strlen(flags)) > maxflags) |
392 | maxflags = flen; | |
393 | } else | |
394 | flen = 0; | |
395 | ||
396 | if ((np = malloc(sizeof(NAMES) + | |
397 | ulen + glen + flen + 3)) == NULL) | |
c3346b7f | 398 | err(1, NULL); |
abe0683d EA |
399 | |
400 | np->user = &np->data[0]; | |
401 | (void)strcpy(np->user, user); | |
402 | np->group = &np->data[ulen + 1]; | |
403 | (void)strcpy(np->group, group); | |
404 | ||
09cf81e2 KB |
405 | if (S_ISCHR(sp->st_mode) || |
406 | S_ISBLK(sp->st_mode)) | |
407 | bcfile = 1; | |
408 | ||
abe0683d EA |
409 | if (f_flags) { |
410 | np->flags = &np->data[ulen + glen + 2]; | |
411 | (void)strcpy(np->flags, flags); | |
412 | } | |
413 | cur->fts_pointer = np; | |
414 | } | |
415 | } | |
f59807e9 | 416 | ++entries; |
be4f2604 | 417 | } |
547b0031 | 418 | |
abe0683d EA |
419 | if (!entries) |
420 | return; | |
421 | ||
422 | d.list = list; | |
423 | d.entries = entries; | |
424 | d.maxlen = maxlen; | |
425 | if (needstats) { | |
09cf81e2 | 426 | d.bcfile = bcfile; |
abe0683d | 427 | d.btotal = btotal; |
2dbdf5a9 KB |
428 | (void)snprintf(buf, sizeof(buf), "%lu", maxblock); |
429 | d.s_block = strlen(buf); | |
abe0683d EA |
430 | d.s_flags = maxflags; |
431 | d.s_group = maxgroup; | |
2dbdf5a9 KB |
432 | (void)snprintf(buf, sizeof(buf), "%lu", maxinode); |
433 | d.s_inode = strlen(buf); | |
434 | (void)snprintf(buf, sizeof(buf), "%lu", maxnlink); | |
435 | d.s_nlink = strlen(buf); | |
436 | (void)snprintf(buf, sizeof(buf), "%qu", maxsize); | |
437 | d.s_size = strlen(buf); | |
abe0683d | 438 | d.s_user = maxuser; |
f59807e9 | 439 | } |
abe0683d EA |
440 | |
441 | printfcn(&d); | |
442 | output = 1; | |
443 | ||
444 | if (f_longform) | |
445 | for (cur = list; cur; cur = cur->fts_link) | |
446 | free(cur->fts_pointer); | |
547b0031 BJ |
447 | } |
448 | ||
f0a884e9 EA |
449 | /* |
450 | * Ordering for mastercmp: | |
451 | * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories | |
452 | * as larger than directories. Within either group, use the sort function. | |
453 | * All other levels use the sort function. Error entries remain unsorted. | |
454 | */ | |
abe0683d | 455 | static int |
f0a884e9 EA |
456 | mastercmp(a, b) |
457 | const FTSENT **a, **b; | |
547b0031 | 458 | { |
f0a884e9 | 459 | register int a_info, b_info; |
547b0031 | 460 | |
f0a884e9 EA |
461 | a_info = (*a)->fts_info; |
462 | if (a_info == FTS_ERR) | |
0ce0ae04 | 463 | return (0); |
f0a884e9 EA |
464 | b_info = (*b)->fts_info; |
465 | if (b_info == FTS_ERR) | |
466 | return (0); | |
4db4a32d KB |
467 | |
468 | if (a_info == FTS_NS || b_info == FTS_NS) | |
f0a884e9 | 469 | return (namecmp(*a, *b)); |
4845c19f | 470 | |
abe0683d | 471 | if (a_info == b_info) |
f0a884e9 | 472 | return (sortfcn(*a, *b)); |
c7f12bd6 | 473 | |
f0a884e9 | 474 | if ((*a)->fts_level == FTS_ROOTLEVEL) |
abe0683d | 475 | if (a_info == FTS_D) |
f0a884e9 EA |
476 | return (1); |
477 | else if (b_info == FTS_D) | |
478 | return (-1); | |
479 | else | |
480 | return (sortfcn(*a, *b)); | |
481 | else | |
482 | return (sortfcn(*a, *b)); | |
547b0031 | 483 | } |