ls.c revision 1.41
1/* $NetBSD: ls.c,v 1.41 2000/03/06 11:03:45 enami Exp $ */ 2 3/* 4 * Copyright (c) 1989, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Michael Fischbein. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39#include <sys/cdefs.h> 40#ifndef lint 41__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\ 42 The Regents of the University of California. All rights reserved.\n"); 43#endif /* not lint */ 44 45#ifndef lint 46#if 0 47static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94"; 48#else 49__RCSID("$NetBSD: ls.c,v 1.41 2000/03/06 11:03:45 enami Exp $"); 50#endif 51#endif /* not lint */ 52 53#include <sys/types.h> 54#include <sys/stat.h> 55#include <sys/ioctl.h> 56 57#include <dirent.h> 58#include <err.h> 59#include <errno.h> 60#include <fts.h> 61#include <locale.h> 62#include <stdio.h> 63#include <stdlib.h> 64#include <string.h> 65#include <unistd.h> 66#include <termios.h> 67#include <pwd.h> 68#include <grp.h> 69 70#include "ls.h" 71#include "extern.h" 72 73static void display __P((FTSENT *, FTSENT *)); 74static int mastercmp __P((const FTSENT **, const FTSENT **)); 75static void traverse __P((int, char **, int)); 76 77static void (*printfcn) __P((DISPLAY *)); 78static int (*sortfcn) __P((const FTSENT *, const FTSENT *)); 79 80#define BY_NAME 0 81#define BY_SIZE 1 82#define BY_TIME 2 83 84long blocksize; /* block size units */ 85int termwidth = 80; /* default terminal width */ 86int sortkey = BY_NAME; 87int rval = EXIT_SUCCESS; /* exit value - set if error encountered */ 88 89/* flags */ 90int f_accesstime; /* use time of last access */ 91int f_column; /* columnated format */ 92int f_columnacross; /* columnated format, sorted across */ 93int f_flags; /* show flags associated with a file */ 94int f_inode; /* print inode */ 95int f_listdir; /* list actual directory, not contents */ 96int f_listdot; /* list files beginning with . */ 97int f_longform; /* long listing format */ 98int f_nonprint; /* show unprintables as ? */ 99int f_nosort; /* don't sort output */ 100int f_numericonly; /* don't convert uid/gid to name */ 101int f_recursive; /* ls subdirectories also */ 102int f_reversesort; /* reverse whatever sort is used */ 103int f_sectime; /* print the real time for all files */ 104int f_singlecol; /* use single column output */ 105int f_size; /* list size in short listing */ 106int f_statustime; /* use time of last mode change */ 107int f_stream; /* stream format */ 108int f_type; /* add type character for non-regular files */ 109int f_typedir; /* add type character for directories */ 110int f_whiteout; /* show whiteout entries */ 111 112int 113ls_main(argc, argv) 114 int argc; 115 char *argv[]; 116{ 117 static char dot[] = ".", *dotav[] = { dot, NULL }; 118 struct winsize win; 119 int ch, fts_options, notused; 120 int kflag = 0; 121 const char *p; 122 123 setlocale(LC_ALL, ""); 124 125 /* Terminal defaults to -Cq, non-terminal defaults to -1. */ 126 if (isatty(STDOUT_FILENO)) { 127 if ((p = getenv("COLUMNS")) != NULL) 128 termwidth = atoi(p); 129 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && 130 win.ws_col > 0) 131 termwidth = win.ws_col; 132 f_column = f_nonprint = 1; 133 } else 134 f_singlecol = 1; 135 136 /* Root is -A automatically. */ 137 if (!getuid()) 138 f_listdot = 1; 139 140 fts_options = FTS_PHYSICAL; 141 while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgiklmnopqrstux")) != -1) { 142 switch (ch) { 143 /* 144 * The -1, -C, -l, -m and -x options all override each other so 145 * shell aliasing works correctly. 146 */ 147 case '1': 148 f_singlecol = 1; 149 f_column = f_columnacross = f_longform = f_stream = 0; 150 break; 151 case 'C': 152 f_column = 1; 153 f_columnacross = f_longform = f_singlecol = f_stream = 154 0; 155 break; 156 case 'l': 157 f_longform = 1; 158 f_column = f_columnacross = f_singlecol = f_stream = 0; 159 break; 160 case 'm': 161 f_stream = 1; 162 f_column = f_columnacross = f_longform = f_singlecol = 163 0; 164 break; 165 case 'x': 166 f_columnacross = 1; 167 f_column = f_longform = f_singlecol = f_stream = 0; 168 break; 169 /* The -c and -u options override each other. */ 170 case 'c': 171 f_statustime = 1; 172 f_accesstime = 0; 173 break; 174 case 'u': 175 f_accesstime = 1; 176 f_statustime = 0; 177 break; 178 case 'F': 179 f_type = 1; 180 break; 181 case 'L': 182 fts_options &= ~FTS_PHYSICAL; 183 fts_options |= FTS_LOGICAL; 184 break; 185 case 'R': 186 f_recursive = 1; 187 break; 188 case 'a': 189 fts_options |= FTS_SEEDOT; 190 /* FALLTHROUGH */ 191 case 'A': 192 f_listdot = 1; 193 break; 194 /* The -d option turns off the -R option. */ 195 case 'd': 196 f_listdir = 1; 197 f_recursive = 0; 198 break; 199 case 'f': 200 f_nosort = 1; 201 break; 202 case 'g': /* Compatibility with 4.3BSD. */ 203 break; 204 case 'i': 205 f_inode = 1; 206 break; 207 case 'k': 208 blocksize = 1024; 209 kflag = 1; 210 break; 211 case 'n': 212 f_numericonly = 1; 213 break; 214 case 'o': 215 f_flags = 1; 216 break; 217 case 'p': 218 f_typedir = 1; 219 break; 220 case 'q': 221 f_nonprint = 1; 222 break; 223 case 'r': 224 f_reversesort = 1; 225 break; 226 case 'S': 227 sortkey = BY_SIZE; 228 break; 229 case 's': 230 f_size = 1; 231 break; 232 case 'T': 233 f_sectime = 1; 234 break; 235 case 't': 236 sortkey = BY_TIME; 237 break; 238 case 'W': 239 f_whiteout = 1; 240 break; 241 default: 242 case '?': 243 usage(); 244 } 245 } 246 argc -= optind; 247 argv += optind; 248 249 /* 250 * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat 251 * information. 252 */ 253 if (!f_inode && !f_longform && !f_size && !f_type && !f_typedir && 254 sortkey == BY_NAME) 255 fts_options |= FTS_NOSTAT; 256 257 /* 258 * If not -F, -d or -l options, follow any symbolic links listed on 259 * the command line. 260 */ 261 if (!f_longform && !f_listdir && !f_type) 262 fts_options |= FTS_COMFOLLOW; 263 264 /* 265 * If -W, show whiteout entries 266 */ 267#ifdef FTS_WHITEOUT 268 if (f_whiteout) 269 fts_options |= FTS_WHITEOUT; 270#endif 271 272 /* If -l or -s, figure out block size. */ 273 if (f_longform || f_size) { 274 if (!kflag) 275 (void)getbsize(¬used, &blocksize); 276 blocksize /= 512; 277 } 278 279 /* Select a sort function. */ 280 if (f_reversesort) { 281 switch (sortkey) { 282 case BY_NAME: 283 sortfcn = revnamecmp; 284 break; 285 case BY_SIZE: 286 sortfcn = revsizecmp; 287 break; 288 case BY_TIME: 289 if (f_accesstime) 290 sortfcn = revacccmp; 291 else if (f_statustime) 292 sortfcn = revstatcmp; 293 else /* Use modification time. */ 294 sortfcn = revmodcmp; 295 break; 296 } 297 } else { 298 switch (sortkey) { 299 case BY_NAME: 300 sortfcn = namecmp; 301 break; 302 case BY_SIZE: 303 sortfcn = sizecmp; 304 break; 305 case BY_TIME: 306 if (f_accesstime) 307 sortfcn = acccmp; 308 else if (f_statustime) 309 sortfcn = statcmp; 310 else /* Use modification time. */ 311 sortfcn = modcmp; 312 break; 313 } 314 } 315 316 /* Select a print function. */ 317 if (f_singlecol) 318 printfcn = printscol; 319 else if (f_columnacross) 320 printfcn = printacol; 321 else if (f_longform) 322 printfcn = printlong; 323 else if (f_stream) 324 printfcn = printstream; 325 else 326 printfcn = printcol; 327 328 if (argc) 329 traverse(argc, argv, fts_options); 330 else 331 traverse(1, dotav, fts_options); 332 exit(rval); 333 /* NOTREACHED */ 334} 335 336static int output; /* If anything output. */ 337 338/* 339 * Traverse() walks the logical directory structure specified by the argv list 340 * in the order specified by the mastercmp() comparison function. During the 341 * traversal it passes linked lists of structures to display() which represent 342 * a superset (may be exact set) of the files to be displayed. 343 */ 344static void 345traverse(argc, argv, options) 346 int argc, options; 347 char *argv[]; 348{ 349 FTS *ftsp; 350 FTSENT *p, *chp; 351 int ch_options; 352 353 if ((ftsp = 354 fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) 355 err(EXIT_FAILURE, NULL); 356 357 display(NULL, fts_children(ftsp, 0)); 358 if (f_listdir) 359 return; 360 361 /* 362 * If not recursing down this tree and don't need stat info, just get 363 * the names. 364 */ 365 ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0; 366 367 while ((p = fts_read(ftsp)) != NULL) 368 switch (p->fts_info) { 369 case FTS_DC: 370 warnx("%s: directory causes a cycle", p->fts_name); 371 break; 372 case FTS_DNR: 373 case FTS_ERR: 374 warnx("%s: %s", p->fts_name, strerror(p->fts_errno)); 375 rval = EXIT_FAILURE; 376 break; 377 case FTS_D: 378 if (p->fts_level != FTS_ROOTLEVEL && 379 p->fts_name[0] == '.' && !f_listdot) 380 break; 381 382 /* 383 * If already output something, put out a newline as 384 * a separator. If multiple arguments, precede each 385 * directory with its name. 386 */ 387 if (output) 388 (void)printf("\n%s:\n", p->fts_path); 389 else if (argc > 1) { 390 (void)printf("%s:\n", p->fts_path); 391 output = 1; 392 } 393 394 chp = fts_children(ftsp, ch_options); 395 display(p, chp); 396 397 if (!f_recursive && chp != NULL) 398 (void)fts_set(ftsp, p, FTS_SKIP); 399 break; 400 } 401 if (errno) 402 err(EXIT_FAILURE, "fts_read"); 403} 404 405/* 406 * Display() takes a linked list of FTSENT structures and passes the list 407 * along with any other necessary information to the print function. P 408 * points to the parent directory of the display list. 409 */ 410static void 411display(p, list) 412 FTSENT *p, *list; 413{ 414 struct stat *sp; 415 DISPLAY d; 416 FTSENT *cur; 417 NAMES *np; 418 u_int64_t btotal, maxblock, maxsize; 419 int maxinode, maxnlink, maxmajor, maxminor; 420 int bcfile, entries, flen, glen, ulen, maxflags, maxgroup, maxlen; 421 int maxuser, needstats; 422 const char *user, *group; 423 char buf[21]; /* 64 bits == 20 digits, +1 for NUL */ 424 char nuser[12], ngroup[12]; 425 char *flags = NULL; 426 427#ifdef __GNUC__ 428 /* This outrageous construct just to shut up a GCC warning. */ 429 (void) &maxsize; 430#endif 431 432 /* 433 * If list is NULL there are two possibilities: that the parent 434 * directory p has no children, or that fts_children() returned an 435 * error. We ignore the error case since it will be replicated 436 * on the next call to fts_read() on the post-order visit to the 437 * directory p, and will be signalled in traverse(). 438 */ 439 if (list == NULL) 440 return; 441 442 needstats = f_inode || f_longform || f_size; 443 flen = 0; 444 maxinode = maxnlink = 0; 445 bcfile = 0; 446 maxuser = maxgroup = maxflags = maxlen = 0; 447 btotal = maxblock = maxsize = 0; 448 maxmajor = maxminor = 0; 449 for (cur = list, entries = 0; cur; cur = cur->fts_link) { 450 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 451 warnx("%s: %s", 452 cur->fts_name, strerror(cur->fts_errno)); 453 cur->fts_number = NO_PRINT; 454 rval = EXIT_FAILURE; 455 continue; 456 } 457 458 /* 459 * P is NULL if list is the argv list, to which different rules 460 * apply. 461 */ 462 if (p == NULL) { 463 /* Directories will be displayed later. */ 464 if (cur->fts_info == FTS_D && !f_listdir) { 465 cur->fts_number = NO_PRINT; 466 continue; 467 } 468 } else { 469 /* Only display dot file if -a/-A set. */ 470 if (cur->fts_name[0] == '.' && !f_listdot) { 471 cur->fts_number = NO_PRINT; 472 continue; 473 } 474 } 475 if (f_nonprint) 476 prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen); 477 if (cur->fts_namelen > maxlen) 478 maxlen = cur->fts_namelen; 479 if (needstats) { 480 sp = cur->fts_statp; 481 if (sp->st_blocks > maxblock) 482 maxblock = sp->st_blocks; 483 if (sp->st_ino > maxinode) 484 maxinode = sp->st_ino; 485 if (sp->st_nlink > maxnlink) 486 maxnlink = sp->st_nlink; 487 if (sp->st_size > maxsize) 488 maxsize = sp->st_size; 489 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) { 490 bcfile = 1; 491 if (major(sp->st_rdev) > maxmajor) 492 maxmajor = major(sp->st_rdev); 493 if (minor(sp->st_rdev) > maxminor) 494 maxminor = minor(sp->st_rdev); 495 } 496 497 btotal += sp->st_blocks; 498 if (f_longform) { 499 if (f_numericonly || 500 (user = user_from_uid(sp->st_uid, 0)) == 501 NULL) { 502 (void)snprintf(nuser, sizeof(nuser), 503 "%u", sp->st_uid); 504 user = nuser; 505 } 506 if (f_numericonly || 507 (group = group_from_gid(sp->st_gid, 0)) == 508 NULL) { 509 (void)snprintf(ngroup, sizeof(ngroup), 510 "%u", sp->st_gid); 511 group = ngroup; 512 } 513 if ((ulen = strlen(user)) > maxuser) 514 maxuser = ulen; 515 if ((glen = strlen(group)) > maxgroup) 516 maxgroup = glen; 517 if (f_flags) { 518 flags = 519 flags_to_string(sp->st_flags, "-"); 520 if ((flen = strlen(flags)) > maxflags) 521 maxflags = flen; 522 } else 523 flen = 0; 524 525 if ((np = malloc(sizeof(NAMES) + 526 ulen + glen + flen + 3)) == NULL) 527 err(EXIT_FAILURE, NULL); 528 529 np->user = &np->data[0]; 530 (void)strcpy(np->user, user); 531 np->group = &np->data[ulen + 1]; 532 (void)strcpy(np->group, group); 533 534 if (f_flags) { 535 np->flags = &np->data[ulen + glen + 2]; 536 (void)strcpy(np->flags, flags); 537 } 538 cur->fts_pointer = np; 539 } 540 } 541 ++entries; 542 } 543 544 if (!entries) 545 return; 546 547 d.list = list; 548 d.entries = entries; 549 d.maxlen = maxlen; 550 if (needstats) { 551 d.btotal = btotal; 552 (void)snprintf(buf, sizeof(buf), "%llu", (long long)maxblock); 553 d.s_block = strlen(buf); 554 d.s_flags = maxflags; 555 d.s_group = maxgroup; 556 (void)snprintf(buf, sizeof(buf), "%u", maxinode); 557 d.s_inode = strlen(buf); 558 (void)snprintf(buf, sizeof(buf), "%u", maxnlink); 559 d.s_nlink = strlen(buf); 560 (void)snprintf(buf, sizeof(buf), "%llu", (long long)maxsize); 561 d.s_size = strlen(buf); 562 d.s_user = maxuser; 563 if (bcfile) { 564 (void)snprintf(buf, sizeof(buf), "%u", maxmajor); 565 d.s_major = strlen(buf); 566 (void)snprintf(buf, sizeof(buf), "%u", maxminor); 567 d.s_minor = strlen(buf); 568 if (d.s_major + d.s_minor + 2 > d.s_size) 569 d.s_size = d.s_major + d.s_minor + 2; 570 else if (d.s_size - d.s_minor - 2 > d.s_major) 571 d.s_major = d.s_size - d.s_minor - 2; 572 } else { 573 d.s_major = 0; 574 d.s_minor = 0; 575 } 576 } 577 578 printfcn(&d); 579 output = 1; 580 581 if (f_longform) 582 for (cur = list; cur; cur = cur->fts_link) 583 free(cur->fts_pointer); 584} 585 586/* 587 * Ordering for mastercmp: 588 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories 589 * as larger than directories. Within either group, use the sort function. 590 * All other levels use the sort function. Error entries remain unsorted. 591 */ 592static int 593mastercmp(a, b) 594 const FTSENT **a, **b; 595{ 596 int a_info, b_info; 597 598 a_info = (*a)->fts_info; 599 if (a_info == FTS_ERR) 600 return (0); 601 b_info = (*b)->fts_info; 602 if (b_info == FTS_ERR) 603 return (0); 604 605 if (a_info == FTS_NS || b_info == FTS_NS) { 606 if (b_info != FTS_NS) 607 return (1); 608 else if (a_info != FTS_NS) 609 return (-1); 610 else 611 return (namecmp(*a, *b)); 612 } 613 614 if (a_info != b_info && !f_listdir && 615 (*a)->fts_level == FTS_ROOTLEVEL) { 616 if (a_info == FTS_D) 617 return (1); 618 else if (b_info == FTS_D) 619 return (-1); 620 } 621 return (sortfcn(*a, *b)); 622} 623