ls.c revision 1.30
1/* $NetBSD: ls.c,v 1.30 1998/07/28 05:31:25 mycroft 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.30 1998/07/28 05:31:25 mycroft 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 <stdio.h> 62#include <stdlib.h> 63#include <string.h> 64#include <unistd.h> 65#include <pwd.h> 66#include <grp.h> 67 68#include "ls.h" 69#include "extern.h" 70 71int main __P((int, char *[])); 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; 87 88/* flags */ 89int f_accesstime; /* use time of last access */ 90int f_column; /* columnated format */ 91int f_columnacross; /* columnated format, sorted across */ 92int f_flags; /* show flags associated with a file */ 93int f_inode; /* print inode */ 94int f_listdir; /* list actual directory, not contents */ 95int f_listdot; /* list files beginning with . */ 96int f_longform; /* long listing format */ 97int f_nonprint; /* show unprintables as ? */ 98int f_nosort; /* don't sort output */ 99int f_numericonly; /* don't convert uid/gid to name */ 100int f_recursive; /* ls subdirectories also */ 101int f_reversesort; /* reverse whatever sort is used */ 102int f_sectime; /* print the real time for all files */ 103int f_singlecol; /* use single column output */ 104int f_size; /* list size in short listing */ 105int f_statustime; /* use time of last mode change */ 106int f_type; /* add type character for non-regular files */ 107int f_whiteout; /* show whiteout entries */ 108 109int 110main(argc, argv) 111 int argc; 112 char *argv[]; 113{ 114 static char dot[] = ".", *dotav[] = { dot, NULL }; 115 struct winsize win; 116 int ch, fts_options, notused; 117 int kflag = 0; 118 const char *p; 119 120 /* Terminal defaults to -Cq, non-terminal defaults to -1. */ 121 if (isatty(STDOUT_FILENO)) { 122 if ((p = getenv("COLUMNS")) != NULL) 123 termwidth = atoi(p); 124 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && 125 win.ws_col > 0) 126 termwidth = win.ws_col; 127 f_column = f_nonprint = 1; 128 } else 129 f_singlecol = 1; 130 131 /* Root is -A automatically. */ 132 if (!getuid()) 133 f_listdot = 1; 134 135 fts_options = FTS_PHYSICAL; 136 while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgiklnoqrstux")) != -1) { 137 switch (ch) { 138 /* 139 * The -1, -C, -l and -x options all override each other so 140 * shell aliasing works correctly. 141 */ 142 case '1': 143 f_singlecol = 1; 144 f_column = f_columnacross = f_longform = 0; 145 break; 146 case 'C': 147 f_column = 1; 148 f_columnacross = f_longform = f_singlecol = 0; 149 break; 150 case 'l': 151 f_longform = 1; 152 f_column = f_columnacross = f_singlecol = 0; 153 break; 154 case 'x': 155 f_columnacross = 1; 156 f_column = f_longform = f_singlecol = 0; 157 break; 158 /* The -c and -u options override each other. */ 159 case 'c': 160 f_statustime = 1; 161 f_accesstime = 0; 162 break; 163 case 'u': 164 f_accesstime = 1; 165 f_statustime = 0; 166 break; 167 case 'F': 168 f_type = 1; 169 break; 170 case 'L': 171 fts_options &= ~FTS_PHYSICAL; 172 fts_options |= FTS_LOGICAL; 173 break; 174 case 'R': 175 f_recursive = 1; 176 break; 177 case 'a': 178 fts_options |= FTS_SEEDOT; 179 /* FALLTHROUGH */ 180 case 'A': 181 f_listdot = 1; 182 break; 183 /* The -d option turns off the -R option. */ 184 case 'd': 185 f_listdir = 1; 186 f_recursive = 0; 187 break; 188 case 'f': 189 f_nosort = 1; 190 break; 191 case 'g': /* Compatibility with 4.3BSD. */ 192 break; 193 case 'i': 194 f_inode = 1; 195 break; 196 case 'k': 197 blocksize = 1024; 198 kflag = 1; 199 break; 200 case 'n': 201 f_numericonly = 1; 202 break; 203 case 'o': 204 f_flags = 1; 205 break; 206 case 'q': 207 f_nonprint = 1; 208 break; 209 case 'r': 210 f_reversesort = 1; 211 break; 212 case 'S': 213 sortkey = BY_SIZE; 214 break; 215 case 's': 216 f_size = 1; 217 break; 218 case 'T': 219 f_sectime = 1; 220 break; 221 case 't': 222 sortkey = BY_TIME; 223 break; 224 case 'W': 225 f_whiteout = 1; 226 break; 227 default: 228 case '?': 229 usage(); 230 } 231 } 232 argc -= optind; 233 argv += optind; 234 235 /* 236 * If not -F, -i, -l, -S, -s or -t options, don't require stat 237 * information. 238 */ 239 if (!f_inode && !f_longform && !f_size && !f_type && 240 sortkey == BY_NAME) 241 fts_options |= FTS_NOSTAT; 242 243 /* 244 * If not -F, -d or -l options, follow any symbolic links listed on 245 * the command line. 246 */ 247 if (!f_longform && !f_listdir && !f_type) 248 fts_options |= FTS_COMFOLLOW; 249 250 /* 251 * If -W, show whiteout entries 252 */ 253#ifdef FTS_WHITEOUT 254 if (f_whiteout) 255 fts_options |= FTS_WHITEOUT; 256#endif 257 258 /* If -l or -s, figure out block size. */ 259 if (f_longform || f_size) { 260 if (!kflag) 261 (void)getbsize(¬used, &blocksize); 262 blocksize /= 512; 263 } 264 265 /* Select a sort function. */ 266 if (f_reversesort) { 267 switch (sortkey) { 268 case BY_NAME: 269 sortfcn = revnamecmp; 270 break; 271 case BY_SIZE: 272 sortfcn = revsizecmp; 273 break; 274 case BY_TIME: 275 if (f_accesstime) 276 sortfcn = revacccmp; 277 else if (f_statustime) 278 sortfcn = revstatcmp; 279 else /* Use modification time. */ 280 sortfcn = revmodcmp; 281 break; 282 } 283 } else { 284 switch (sortkey) { 285 case BY_NAME: 286 sortfcn = namecmp; 287 break; 288 case BY_SIZE: 289 sortfcn = sizecmp; 290 break; 291 case BY_TIME: 292 if (f_accesstime) 293 sortfcn = acccmp; 294 else if (f_statustime) 295 sortfcn = statcmp; 296 else /* Use modification time. */ 297 sortfcn = modcmp; 298 break; 299 } 300 } 301 302 /* Select a print function. */ 303 if (f_singlecol) 304 printfcn = printscol; 305 else if (f_columnacross) 306 printfcn = printacol; 307 else if (f_longform) 308 printfcn = printlong; 309 else 310 printfcn = printcol; 311 312 if (argc) 313 traverse(argc, argv, fts_options); 314 else 315 traverse(1, dotav, fts_options); 316 exit(0); 317 /* NOTREACHED */ 318} 319 320static int output; /* If anything output. */ 321 322/* 323 * Traverse() walks the logical directory structure specified by the argv list 324 * in the order specified by the mastercmp() comparison function. During the 325 * traversal it passes linked lists of structures to display() which represent 326 * a superset (may be exact set) of the files to be displayed. 327 */ 328static void 329traverse(argc, argv, options) 330 int argc, options; 331 char *argv[]; 332{ 333 FTS *ftsp; 334 FTSENT *p, *chp; 335 int ch_options; 336 337 if ((ftsp = 338 fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) 339 err(1, "%s", ""); 340 341 display(NULL, fts_children(ftsp, 0)); 342 if (f_listdir) 343 return; 344 345 /* 346 * If not recursing down this tree and don't need stat info, just get 347 * the names. 348 */ 349 ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0; 350 351 while ((p = fts_read(ftsp)) != NULL) 352 switch (p->fts_info) { 353 case FTS_DC: 354 warnx("%s: directory causes a cycle", p->fts_name); 355 break; 356 case FTS_DNR: 357 case FTS_ERR: 358 warnx("%s: %s", p->fts_name, strerror(p->fts_errno)); 359 break; 360 case FTS_D: 361 if (p->fts_level != FTS_ROOTLEVEL && 362 p->fts_name[0] == '.' && !f_listdot) 363 break; 364 365 /* 366 * If already output something, put out a newline as 367 * a separator. If multiple arguments, precede each 368 * directory with its name. 369 */ 370 if (output) 371 (void)printf("\n%s:\n", p->fts_path); 372 else if (argc > 1) { 373 (void)printf("%s:\n", p->fts_path); 374 output = 1; 375 } 376 377 chp = fts_children(ftsp, ch_options); 378 display(p, chp); 379 380 if (!f_recursive && chp != NULL) 381 (void)fts_set(ftsp, p, FTS_SKIP); 382 break; 383 } 384 if (errno) 385 err(1, "fts_read"); 386} 387 388/* 389 * Display() takes a linked list of FTSENT structures and passes the list 390 * along with any other necessary information to the print function. P 391 * points to the parent directory of the display list. 392 */ 393static void 394display(p, list) 395 FTSENT *p, *list; 396{ 397 struct stat *sp; 398 DISPLAY d; 399 FTSENT *cur; 400 NAMES *np; 401 u_int64_t btotal, maxblock, maxsize; 402 int maxinode, maxnlink, maxmajor, maxminor; 403 int bcfile, entries, flen, glen, ulen, maxflags, maxgroup, maxlen; 404 int maxuser, needstats; 405 const char *user, *group; 406 char buf[21]; /* 64 bits == 20 digits, +1 for NUL */ 407 char nuser[12], ngroup[12]; 408 char *flags = NULL; 409 410#ifdef __GNUC__ 411 /* This outrageous construct just to shut up a GCC warning. */ 412 (void) &maxsize; 413#endif 414 415 /* 416 * If list is NULL there are two possibilities: that the parent 417 * directory p has no children, or that fts_children() returned an 418 * error. We ignore the error case since it will be replicated 419 * on the next call to fts_read() on the post-order visit to the 420 * directory p, and will be signalled in traverse(). 421 */ 422 if (list == NULL) 423 return; 424 425 needstats = f_inode || f_longform || f_size; 426 flen = 0; 427 maxinode = maxnlink = 0; 428 bcfile = 0; 429 maxuser = maxgroup = maxflags = maxlen = 0; 430 btotal = maxblock = maxsize = 0; 431 maxmajor = maxminor = 0; 432 for (cur = list, entries = 0; cur; cur = cur->fts_link) { 433 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 434 warnx("%s: %s", 435 cur->fts_name, strerror(cur->fts_errno)); 436 cur->fts_number = NO_PRINT; 437 continue; 438 } 439 440 /* 441 * P is NULL if list is the argv list, to which different rules 442 * apply. 443 */ 444 if (p == NULL) { 445 /* Directories will be displayed later. */ 446 if (cur->fts_info == FTS_D && !f_listdir) { 447 cur->fts_number = NO_PRINT; 448 continue; 449 } 450 } else { 451 /* Only display dot file if -a/-A set. */ 452 if (cur->fts_name[0] == '.' && !f_listdot) { 453 cur->fts_number = NO_PRINT; 454 continue; 455 } 456 } 457 if (f_nonprint) 458 prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen); 459 if (cur->fts_namelen > maxlen) 460 maxlen = cur->fts_namelen; 461 if (needstats) { 462 sp = cur->fts_statp; 463 if (sp->st_blocks > maxblock) 464 maxblock = sp->st_blocks; 465 if (sp->st_ino > maxinode) 466 maxinode = sp->st_ino; 467 if (sp->st_nlink > maxnlink) 468 maxnlink = sp->st_nlink; 469 if (sp->st_size > maxsize) 470 maxsize = sp->st_size; 471 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) { 472 bcfile = 1; 473 if (major(sp->st_rdev) > maxmajor) 474 maxmajor = major(sp->st_rdev); 475 if (minor(sp->st_rdev) > maxminor) 476 maxminor = minor(sp->st_rdev); 477 } 478 479 btotal += sp->st_blocks; 480 if (f_longform) { 481 if (f_numericonly) { 482 (void)snprintf(nuser, sizeof(nuser), 483 "%u", sp->st_uid); 484 (void)snprintf(ngroup, sizeof(ngroup), 485 "%u", sp->st_gid); 486 user = nuser; 487 group = ngroup; 488 } else { 489 user = user_from_uid(sp->st_uid, 0); 490 group = group_from_gid(sp->st_gid, 0); 491 } 492 if ((ulen = strlen(user)) > maxuser) 493 maxuser = ulen; 494 if ((glen = strlen(group)) > maxgroup) 495 maxgroup = glen; 496 if (f_flags) { 497 flags = 498 flags_to_string(sp->st_flags, "-"); 499 if ((flen = strlen(flags)) > maxflags) 500 maxflags = flen; 501 } else 502 flen = 0; 503 504 if ((np = malloc(sizeof(NAMES) + 505 ulen + glen + flen + 3)) == NULL) 506 err(1, "%s", ""); 507 508 np->user = &np->data[0]; 509 (void)strcpy(np->user, user); 510 np->group = &np->data[ulen + 1]; 511 (void)strcpy(np->group, group); 512 513 if (f_flags) { 514 np->flags = &np->data[ulen + glen + 2]; 515 (void)strcpy(np->flags, flags); 516 } 517 cur->fts_pointer = np; 518 } 519 } 520 ++entries; 521 } 522 523 if (!entries) 524 return; 525 526 d.list = list; 527 d.entries = entries; 528 d.maxlen = maxlen; 529 if (needstats) { 530 d.btotal = btotal; 531 (void)snprintf(buf, sizeof(buf), "%qu", (long long)maxblock); 532 d.s_block = strlen(buf); 533 d.s_flags = maxflags; 534 d.s_group = maxgroup; 535 (void)snprintf(buf, sizeof(buf), "%u", maxinode); 536 d.s_inode = strlen(buf); 537 (void)snprintf(buf, sizeof(buf), "%u", maxnlink); 538 d.s_nlink = strlen(buf); 539 (void)snprintf(buf, sizeof(buf), "%qu", (long long)maxsize); 540 d.s_size = strlen(buf); 541 d.s_user = maxuser; 542 if (bcfile) { 543 (void)snprintf(buf, sizeof(buf), "%u", maxmajor); 544 d.s_major = strlen(buf); 545 (void)snprintf(buf, sizeof(buf), "%u", maxminor); 546 d.s_minor = strlen(buf); 547 if (d.s_major + d.s_minor + 2 > d.s_size) 548 d.s_size = d.s_major + d.s_minor + 2; 549 else if (d.s_size - d.s_minor - 2 > d.s_major) 550 d.s_major = d.s_size - d.s_minor - 2; 551 } else { 552 d.s_major = 0; 553 d.s_minor = 0; 554 } 555 } 556 557 printfcn(&d); 558 output = 1; 559 560 if (f_longform) 561 for (cur = list; cur; cur = cur->fts_link) 562 free(cur->fts_pointer); 563} 564 565/* 566 * Ordering for mastercmp: 567 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories 568 * as larger than directories. Within either group, use the sort function. 569 * All other levels use the sort function. Error entries remain unsorted. 570 */ 571static int 572mastercmp(a, b) 573 const FTSENT **a, **b; 574{ 575 int a_info, b_info; 576 577 a_info = (*a)->fts_info; 578 if (a_info == FTS_ERR) 579 return (0); 580 b_info = (*b)->fts_info; 581 if (b_info == FTS_ERR) 582 return (0); 583 584 if (a_info == FTS_NS || b_info == FTS_NS) 585 if (b_info != FTS_NS) 586 return (1); 587 else if (a_info != FTS_NS) 588 return (-1); 589 else 590 return (namecmp(*a, *b)); 591 592 if (a_info != b_info && !f_listdir && 593 (*a)->fts_level == FTS_ROOTLEVEL) { 594 if (a_info == FTS_D) 595 return (1); 596 else if (b_info == FTS_D) 597 return (-1); 598 } 599 return (sortfcn(*a, *b)); 600} 601