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