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