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