ls.c revision 61322
1213805Shselasky/* 2213805Shselasky * Copyright (c) 1989, 1993, 1994 3213805Shselasky * The Regents of the University of California. All rights reserved. 4213805Shselasky * 5213805Shselasky * This code is derived from software contributed to Berkeley by 6213805Shselasky * Michael Fischbein. 7213805Shselasky * 8213805Shselasky * Redistribution and use in source and binary forms, with or without 9213805Shselasky * modification, are permitted provided that the following conditions 10213805Shselasky * are met: 11213805Shselasky * 1. Redistributions of source code must retain the above copyright 12213805Shselasky * notice, this list of conditions and the following disclaimer. 13213805Shselasky * 2. Redistributions in binary form must reproduce the above copyright 14213805Shselasky * notice, this list of conditions and the following disclaimer in the 15213805Shselasky * documentation and/or other materials provided with the distribution. 16213805Shselasky * 3. All advertising materials mentioning features or use of this software 17213805Shselasky * must display the following acknowledgement: 18213805Shselasky * This product includes software developed by the University of 19213805Shselasky * California, Berkeley and its contributors. 20213805Shselasky * 4. Neither the name of the University nor the names of its contributors 21213805Shselasky * may be used to endorse or promote products derived from this software 22213805Shselasky * without specific prior written permission. 23213805Shselasky * 24213805Shselasky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25213805Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26213805Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27213805Shselasky * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28213805Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29213805Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30213805Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31213805Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32213805Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33213805Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34213805Shselasky * SUCH DAMAGE. 35213805Shselasky */ 36213805Shselasky 37213805Shselasky#ifndef lint 38213805Shselaskystatic const char copyright[] = 39213805Shselasky"@(#) Copyright (c) 1989, 1993, 1994\n\ 40213805Shselasky The Regents of the University of California. All rights reserved.\n"; 41213805Shselasky#endif /* not lint */ 42213805Shselasky 43213805Shselasky#ifndef lint 44213805Shselasky#if 0 45213805Shselaskystatic char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94"; 46213805Shselasky#else 47213805Shselaskystatic const char rcsid[] = 48213805Shselasky "$FreeBSD: head/bin/ls/ls.c 61322 2000-06-06 07:14:01Z ache $"; 49213805Shselasky#endif 50213805Shselasky#endif /* not lint */ 51213805Shselasky 52213805Shselasky#include <sys/types.h> 53213805Shselasky#include <sys/stat.h> 54213805Shselasky#include <sys/ioctl.h> 55213805Shselasky 56213805Shselasky#include <dirent.h> 57213805Shselasky#include <err.h> 58213805Shselasky#include <errno.h> 59213805Shselasky#include <fts.h> 60213805Shselasky#include <limits.h> 61213805Shselasky#include <locale.h> 62213805Shselasky#include <stdio.h> 63213805Shselasky#include <stdlib.h> 64213805Shselasky#include <string.h> 65213805Shselasky#include <unistd.h> 66213805Shselasky#ifdef COLORLS 67213805Shselasky#include <termcap.h> 68213805Shselasky#include <signal.h> 69213805Shselasky#endif 70213805Shselasky 71213805Shselasky#include "ls.h" 72213805Shselasky#include "extern.h" 73213805Shselasky 74213805Shselasky/* 75213805Shselasky * Upward approximation of the maximum number of characters needed to 76213805Shselasky * represent a value of integral type t as a string, excluding the 77213805Shselasky * NUL terminator, with provision for a sign. 78213805Shselasky */ 79213805Shselasky#define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1) 80213805Shselasky 81213805Shselaskychar *getflags __P((u_long, char *)); 82213805Shselasky 83213805Shselaskystatic void display __P((FTSENT *, FTSENT *)); 84213805Shselaskystatic u_quad_t makenines __P((u_long)); 85213805Shselaskystatic int mastercmp __P((const FTSENT **, const FTSENT **)); 86213805Shselaskystatic void traverse __P((int, char **, int)); 87213805Shselasky 88213805Shselaskystatic void (*printfcn) __P((DISPLAY *)); 89213805Shselaskystatic int (*sortfcn) __P((const FTSENT *, const FTSENT *)); 90213805Shselasky 91213805Shselaskylong blocksize; /* block size units */ 92213805Shselaskyint termwidth = 80; /* default terminal width */ 93213805Shselasky 94213805Shselasky/* flags */ 95213805Shselaskyint f_accesstime; /* use time of last access */ 96213805Shselaskyint f_column; /* columnated format */ 97213805Shselaskyint f_flags; /* show flags associated with a file */ 98213805Shselaskyint f_inode; /* print inode */ 99213805Shselaskyint f_kblocks; /* print size in kilobytes */ 100213805Shselaskyint f_listdir; /* list actual directory, not contents */ 101213805Shselaskyint f_listdot; /* list files beginning with . */ 102213805Shselaskyint f_longform; /* long listing format */ 103213805Shselaskyint f_nonprint; /* show unprintables as ? */ 104213805Shselaskyint f_nosort; /* don't sort output */ 105213805Shselaskyint f_notabs; /* don't use tab-separated multi-col output */ 106213805Shselaskyint f_numericonly; /* don't convert uid/gid to name */ 107213805Shselaskyint f_octal; /* show unprintables as \xxx */ 108213805Shselaskyint f_octal_escape; /* like f_octal but use C escapes if possible */ 109213805Shselaskyint f_recursive; /* ls subdirectories also */ 110213805Shselaskyint f_reversesort; /* reverse whatever sort is used */ 111213805Shselaskyint f_sectime; /* print the real time for all files */ 112213805Shselaskyint f_singlecol; /* use single column output */ 113213805Shselaskyint f_size; /* list size in short listing */ 114213805Shselaskyint f_statustime; /* use time of last mode change */ 115213805Shselaskyint f_timesort; /* sort by time vice name */ 116213805Shselaskyint f_type; /* add type character for non-regular files */ 117213805Shselaskyint f_whiteout; /* show whiteout entries */ 118213805Shselasky#ifdef COLORLS 119213805Shselaskyint f_color; /* add type in color for non-regular files */ 120213805Shselasky 121213805Shselaskychar *ansi_bgcol; /* ANSI sequence to set background colour */ 122213805Shselaskychar *ansi_fgcol; /* ANSI sequence to set foreground colour */ 123213805Shselaskychar *ansi_coloff; /* ANSI sequence to reset colours */ 124213805Shselasky#endif 125213805Shselasky 126213805Shselaskyint rval; 127213805Shselasky 128213805Shselaskyint 129213805Shselaskymain(argc, argv) 130213805Shselasky int argc; 131213805Shselasky char *argv[]; 132213805Shselasky{ 133213805Shselasky static char dot[] = ".", *dotav[] = { dot, NULL }; 134213805Shselasky struct winsize win; 135213805Shselasky int ch, fts_options, notused; 136213805Shselasky char *p; 137213805Shselasky 138213805Shselasky#ifdef COLORLS 139213805Shselasky char termcapbuf[1024]; /* termcap definition buffer */ 140213805Shselasky char tcapbuf[512]; /* capability buffer */ 141213805Shselasky char *bp = tcapbuf; 142213805Shselasky#endif 143213805Shselasky 144213805Shselasky (void) setlocale(LC_ALL, ""); 145213805Shselasky 146213805Shselasky /* Terminal defaults to -Cq, non-terminal defaults to -1. */ 147213805Shselasky if (isatty(STDOUT_FILENO)) { 148213805Shselasky if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 || 149213805Shselasky !win.ws_col) { 150213805Shselasky if ((p = getenv("COLUMNS")) != NULL) 151223486Shselasky termwidth = atoi(p); 152213805Shselasky } 153213805Shselasky else 154213805Shselasky termwidth = win.ws_col; 155213805Shselasky f_column = f_nonprint = 1; 156213805Shselasky } else { 157213805Shselasky f_singlecol = 1; 158213805Shselasky /* retrieve environment variable, in case of explicit -C */ 159213805Shselasky if ((p = getenv("COLUMNS"))) 160213805Shselasky termwidth = atoi(p); 161213805Shselasky } 162213805Shselasky 163213805Shselasky /* Root is -A automatically. */ 164213805Shselasky if (!getuid()) 165213805Shselasky f_listdot = 1; 166213805Shselasky 167213805Shselasky fts_options = FTS_PHYSICAL; 168213805Shselasky while ((ch = getopt(argc, argv, "1ABCFGHLPRTWabcdfgiklnoqrstu")) != -1) { 169213805Shselasky switch (ch) { 170213805Shselasky /* 171213805Shselasky * The -1, -C and -l options all override each other so shell 172213805Shselasky * aliasing works right. 173213805Shselasky */ 174213805Shselasky case '1': 175213805Shselasky f_singlecol = 1; 176213805Shselasky f_column = f_longform = 0; 177213805Shselasky break; 178213805Shselasky case 'B': 179213805Shselasky f_nonprint = 0; 180213805Shselasky f_octal = 1; 181213805Shselasky f_octal_escape = 0; 182213805Shselasky break; 183213805Shselasky case 'C': 184213805Shselasky f_column = 1; 185213805Shselasky f_longform = f_singlecol = 0; 186213805Shselasky break; 187213805Shselasky case 'l': 188213805Shselasky f_longform = 1; 189213805Shselasky f_column = f_singlecol = 0; 190213805Shselasky break; 191213805Shselasky /* The -c and -u options override each other. */ 192213805Shselasky case 'c': 193213805Shselasky f_statustime = 1; 194213805Shselasky f_accesstime = 0; 195213805Shselasky break; 196213805Shselasky case 'u': 197213805Shselasky f_accesstime = 1; 198213805Shselasky f_statustime = 0; 199213805Shselasky break; 200213805Shselasky case 'F': 201213805Shselasky f_type = 1; 202213805Shselasky break; 203213805Shselasky case 'H': 204213805Shselasky fts_options |= FTS_COMFOLLOW; 205213805Shselasky break; 206213805Shselasky case 'G': 207213805Shselasky if (isatty(STDOUT_FILENO)) 208213805Shselasky#ifdef COLORLS 209213805Shselasky if (tgetent(termcapbuf, getenv("TERM")) == 1) { 210213805Shselasky ansi_fgcol = tgetstr("AF", &bp); 211213805Shselasky ansi_bgcol = tgetstr("AB", &bp); 212213805Shselasky 213213805Shselasky /* To switch colours off use 'op' if 214213805Shselasky * available, otherwise use 'oc', or 215213805Shselasky * don't do colours at all. */ 216213805Shselasky ansi_coloff = tgetstr("op", &bp); 217213805Shselasky if (!ansi_coloff) 218213805Shselasky ansi_coloff = tgetstr("oc", &bp); 219213805Shselasky if (ansi_fgcol && ansi_bgcol && ansi_coloff) 220213805Shselasky f_color = 1; 221213805Shselasky } 222213805Shselasky#else 223213805Shselasky (void)fprintf(stderr, "Color support not compiled in.\n"); 224213805Shselasky#endif 225213805Shselasky break; 226213805Shselasky case 'L': 227213805Shselasky fts_options &= ~FTS_PHYSICAL; 228213805Shselasky fts_options |= FTS_LOGICAL; 229213805Shselasky break; 230213805Shselasky case 'P': 231213805Shselasky fts_options &= ~FTS_COMFOLLOW; 232213805Shselasky fts_options &= ~FTS_LOGICAL; 233213805Shselasky fts_options |= FTS_PHYSICAL; 234213805Shselasky break; 235213805Shselasky case 'R': 236213805Shselasky f_recursive = 1; 237213805Shselasky break; 238213805Shselasky case 'a': 239213805Shselasky fts_options |= FTS_SEEDOT; 240213805Shselasky /* FALLTHROUGH */ 241213805Shselasky case 'A': 242213805Shselasky f_listdot = 1; 243213805Shselasky break; 244213805Shselasky /* The -d option turns off the -R option. */ 245213805Shselasky case 'd': 246213805Shselasky f_listdir = 1; 247213805Shselasky f_recursive = 0; 248213805Shselasky break; 249213805Shselasky case 'f': 250213805Shselasky f_nosort = 1; 251213805Shselasky break; 252213805Shselasky case 'g': /* Compatibility with 4.3BSD. */ 253213805Shselasky break; 254213805Shselasky case 'i': 255213805Shselasky f_inode = 1; 256213805Shselasky break; 257213805Shselasky case 'k': 258213805Shselasky f_kblocks = 1; 259213805Shselasky break; 260213805Shselasky case 'n': 261213805Shselasky f_numericonly = 1; 262213805Shselasky break; 263213805Shselasky case 'o': 264213805Shselasky f_flags = 1; 265213805Shselasky break; 266213805Shselasky case 'q': 267213805Shselasky f_nonprint = 1; 268213805Shselasky f_octal = 0; 269213805Shselasky f_octal_escape = 0; 270213805Shselasky break; 271213805Shselasky case 'r': 272213805Shselasky f_reversesort = 1; 273213805Shselasky break; 274213805Shselasky case 's': 275213805Shselasky f_size = 1; 276213805Shselasky break; 277213805Shselasky case 'T': 278213805Shselasky f_sectime = 1; 279213805Shselasky break; 280213805Shselasky case 't': 281213805Shselasky f_timesort = 1; 282213805Shselasky break; 283213805Shselasky case 'W': 284213805Shselasky f_whiteout = 1; 285213805Shselasky break; 286213805Shselasky case 'b': 287213805Shselasky f_nonprint = 0; 288213805Shselasky f_octal = 0; 289213805Shselasky f_octal_escape = 1; 290213805Shselasky break; 291213805Shselasky default: 292213805Shselasky case '?': 293213805Shselasky usage(); 294213805Shselasky } 295213805Shselasky } 296213805Shselasky argc -= optind; 297213805Shselasky argv += optind; 298213805Shselasky 299213805Shselasky#ifdef COLORLS 300213805Shselasky if (f_color) { 301213805Shselasky (void) signal(SIGINT, colorquit); 302213805Shselasky (void) signal(SIGQUIT, colorquit); 303213805Shselasky parsecolors(getenv("LSCOLORS")); 304213805Shselasky } 305213805Shselasky#endif 306213805Shselasky 307213805Shselasky /* 308213805Shselasky * If not -F, -i, -l, -s or -t options, don't require stat 309213805Shselasky * information, unless in color mode in which case we do 310213805Shselasky * need this to determine which colors to display. 311213805Shselasky */ 312213805Shselasky if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type 313213805Shselasky#ifdef COLORLS 314213805Shselasky && !f_color 315213805Shselasky#endif 316213805Shselasky ) 317213805Shselasky fts_options |= FTS_NOSTAT; 318213805Shselasky 319213805Shselasky /* 320213805Shselasky * If not -F, -d or -l options, follow any symbolic links listed on 321213805Shselasky * the command line. 322213805Shselasky */ 323213805Shselasky if (!f_longform && !f_listdir && !f_type) 324213805Shselasky fts_options |= FTS_COMFOLLOW; 325213805Shselasky 326213805Shselasky /* 327213805Shselasky * If -W, show whiteout entries 328213805Shselasky */ 329213805Shselasky#ifdef FTS_WHITEOUT 330213805Shselasky if (f_whiteout) 331213805Shselasky fts_options |= FTS_WHITEOUT; 332213805Shselasky#endif 333213805Shselasky 334213805Shselasky /* If -l or -s, figure out block size. */ 335213805Shselasky if (f_longform || f_size) { 336213805Shselasky if (f_kblocks) 337213805Shselasky blocksize = 2; 338213805Shselasky else { 339213805Shselasky (void)getbsize(¬used, &blocksize); 340213805Shselasky blocksize /= 512; 341213805Shselasky } 342213805Shselasky } 343213805Shselasky 344213805Shselasky /* Select a sort function. */ 345213805Shselasky if (f_reversesort) { 346213805Shselasky if (!f_timesort) 347213805Shselasky sortfcn = revnamecmp; 348213805Shselasky else if (f_accesstime) 349213805Shselasky sortfcn = revacccmp; 350213805Shselasky else if (f_statustime) 351213805Shselasky sortfcn = revstatcmp; 352213805Shselasky else /* Use modification time. */ 353213805Shselasky sortfcn = revmodcmp; 354213805Shselasky } else { 355213805Shselasky if (!f_timesort) 356213805Shselasky sortfcn = namecmp; 357213805Shselasky else if (f_accesstime) 358213805Shselasky sortfcn = acccmp; 359213805Shselasky else if (f_statustime) 360213805Shselasky sortfcn = statcmp; 361213805Shselasky else /* Use modification time. */ 362213805Shselasky sortfcn = modcmp; 363213805Shselasky } 364213805Shselasky 365213805Shselasky /* Select a print function. */ 366213805Shselasky if (f_singlecol) 367213805Shselasky printfcn = printscol; 368213805Shselasky else if (f_longform) 369213805Shselasky printfcn = printlong; 370213805Shselasky else 371213805Shselasky printfcn = printcol; 372213805Shselasky 373213805Shselasky if (argc) 374213805Shselasky traverse(argc, argv, fts_options); 375213805Shselasky else 376213805Shselasky traverse(1, dotav, fts_options); 377213805Shselasky exit(rval); 378213805Shselasky} 379213805Shselasky 380213805Shselaskystatic int output; /* If anything output. */ 381213805Shselasky 382213805Shselasky/* 383213805Shselasky * Traverse() walks the logical directory structure specified by the argv list 384213805Shselasky * in the order specified by the mastercmp() comparison function. During the 385213805Shselasky * traversal it passes linked lists of structures to display() which represent 386213805Shselasky * a superset (may be exact set) of the files to be displayed. 387213805Shselasky */ 388213805Shselaskystatic void 389213805Shselaskytraverse(argc, argv, options) 390213805Shselasky int argc, options; 391213805Shselasky char *argv[]; 392213805Shselasky{ 393213805Shselasky FTS *ftsp; 394213805Shselasky FTSENT *p, *chp; 395213805Shselasky int ch_options; 396213805Shselasky 397213805Shselasky if ((ftsp = 398213805Shselasky fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) 399213805Shselasky err(1, NULL); 400213805Shselasky 401213805Shselasky display(NULL, fts_children(ftsp, 0)); 402213805Shselasky if (f_listdir) 403213805Shselasky return; 404213805Shselasky 405213805Shselasky /* 406213805Shselasky * If not recursing down this tree and don't need stat info, just get 407213805Shselasky * the names. 408213805Shselasky */ 409213805Shselasky ch_options = !f_recursive && options & FTS_NOSTAT ? FTS_NAMEONLY : 0; 410213805Shselasky 411213805Shselasky while ((p = fts_read(ftsp)) != NULL) 412213805Shselasky switch (p->fts_info) { 413213805Shselasky case FTS_DC: 414213805Shselasky warnx("%s: directory causes a cycle", p->fts_name); 415213805Shselasky break; 416213805Shselasky case FTS_DNR: 417213805Shselasky case FTS_ERR: 418213805Shselasky warnx("%s: %s", p->fts_name, strerror(p->fts_errno)); 419213805Shselasky rval = 1; 420213805Shselasky break; 421213805Shselasky case FTS_D: 422213805Shselasky if (p->fts_level != FTS_ROOTLEVEL && 423213805Shselasky p->fts_name[0] == '.' && !f_listdot) 424213805Shselasky break; 425213805Shselasky 426213805Shselasky /* 427213805Shselasky * If already output something, put out a newline as 428213805Shselasky * a separator. If multiple arguments, precede each 429213805Shselasky * directory with its name. 430213805Shselasky */ 431213805Shselasky if (output) 432213805Shselasky (void)printf("\n%s:\n", p->fts_path); 433213805Shselasky else if (argc > 1) { 434213805Shselasky (void)printf("%s:\n", p->fts_path); 435213805Shselasky output = 1; 436213805Shselasky } 437213805Shselasky 438213805Shselasky chp = fts_children(ftsp, ch_options); 439213805Shselasky display(p, chp); 440213805Shselasky 441213805Shselasky if (!f_recursive && chp != NULL) 442213805Shselasky (void)fts_set(ftsp, p, FTS_SKIP); 443213805Shselasky break; 444213805Shselasky } 445213805Shselasky if (errno) 446213805Shselasky err(1, "fts_read"); 447213805Shselasky} 448213805Shselasky 449213805Shselasky/* 450213805Shselasky * Display() takes a linked list of FTSENT structures and passes the list 451213805Shselasky * along with any other necessary information to the print function. P 452213805Shselasky * points to the parent directory of the display list. 453213805Shselasky */ 454213805Shselaskystatic void 455213805Shselaskydisplay(p, list) 456213805Shselasky FTSENT *p, *list; 457213805Shselasky{ 458213805Shselasky struct stat *sp; 459213805Shselasky DISPLAY d; 460213805Shselasky FTSENT *cur; 461213805Shselasky NAMES *np; 462213805Shselasky u_quad_t maxsize; 463213805Shselasky u_long btotal, maxblock, maxinode, maxlen, maxnlink; 464213805Shselasky int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser; 465213805Shselasky char *initmax; 466213805Shselasky int entries, needstats; 467213805Shselasky char *user, *group, *flags; 468213805Shselasky char buf[STRBUF_SIZEOF(u_quad_t) + 1]; 469213805Shselasky char ngroup[STRBUF_SIZEOF(uid_t) + 1]; 470213805Shselasky char nuser[STRBUF_SIZEOF(gid_t) + 1]; 471213805Shselasky 472213805Shselasky /* 473213805Shselasky * If list is NULL there are two possibilities: that the parent 474213805Shselasky * directory p has no children, or that fts_children() returned an 475213805Shselasky * error. We ignore the error case since it will be replicated 476213805Shselasky * on the next call to fts_read() on the post-order visit to the 477213805Shselasky * directory p, and will be signaled in traverse(). 478213805Shselasky */ 479213805Shselasky if (list == NULL) 480213805Shselasky return; 481213805Shselasky 482213805Shselasky needstats = f_inode || f_longform || f_size; 483213805Shselasky flen = 0; 484213805Shselasky btotal = 0; 485213805Shselasky initmax = getenv("LS_COLWIDTHS"); 486213805Shselasky /* Fields match -lios order. New ones should be added at the end. */ 487213805Shselasky if (initmax != NULL && *initmax != '\0') { 488213805Shselasky char *initmax2, *jinitmax; 489213805Shselasky int ninitmax; 490213805Shselasky 491213805Shselasky /* Fill-in "::" as "0:0:0" for the sake of scanf. */ 492213805Shselasky jinitmax = initmax2 = malloc(strlen(initmax) * 2 + 2); 493213805Shselasky if (jinitmax == NULL) 494213805Shselasky err(1, NULL); 495213805Shselasky if (*initmax == ':') 496213805Shselasky strcpy(initmax2, "0:"), initmax2 += 2; 497213805Shselasky else 498213805Shselasky *initmax2++ = *initmax, *initmax2 = '\0'; 499213805Shselasky for (initmax++; *initmax != '\0'; initmax++) { 500213805Shselasky if (initmax[-1] == ':' && initmax[0] == ':') { 501213805Shselasky *initmax2++ = '0'; 502213805Shselasky *initmax2++ = initmax[0]; 503213805Shselasky initmax2[1] = '\0'; 504213805Shselasky } else { 505213805Shselasky *initmax2++ = initmax[0]; 506213805Shselasky initmax2[1] = '\0'; 507213805Shselasky } 508213805Shselasky } 509213805Shselasky if (initmax2[-1] == ':') strcpy(initmax2, "0"); 510213805Shselasky 511213805Shselasky ninitmax = sscanf(jinitmax, 512213805Shselasky " %lu : %lu : %lu : %i : %i : %i : %qu : %lu ", 513213805Shselasky &maxinode, &maxblock, &maxnlink, &maxuser, 514213805Shselasky &maxgroup, &maxflags, &maxsize, &maxlen); 515213805Shselasky f_notabs = 1; 516213805Shselasky switch (ninitmax) { 517213805Shselasky case 0: maxinode = 0; 518213805Shselasky case 1: maxblock = 0; 519213805Shselasky case 2: maxnlink = 0; 520213805Shselasky case 3: maxuser = 0; 521213805Shselasky case 4: maxgroup = 0; 522213805Shselasky case 5: maxflags = 0; 523213805Shselasky case 6: maxsize = 0; 524213805Shselasky case 7: maxlen = 0, f_notabs = 0; 525213805Shselasky } 526213805Shselasky maxinode = makenines(maxinode); 527213805Shselasky maxblock = makenines(maxblock); 528 maxnlink = makenines(maxnlink); 529 maxsize = makenines(maxsize); 530 } else if (initmax == NULL || *initmax == '\0') 531 maxblock = maxinode = maxlen = maxnlink = 532 maxuser = maxgroup = maxflags = maxsize = 0; 533 bcfile = 0; 534 flags = NULL; 535 for (cur = list, entries = 0; cur; cur = cur->fts_link) { 536 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 537 warnx("%s: %s", 538 cur->fts_name, strerror(cur->fts_errno)); 539 cur->fts_number = NO_PRINT; 540 rval = 1; 541 continue; 542 } 543 544 /* 545 * P is NULL if list is the argv list, to which different rules 546 * apply. 547 */ 548 if (p == NULL) { 549 /* Directories will be displayed later. */ 550 if (cur->fts_info == FTS_D && !f_listdir) { 551 cur->fts_number = NO_PRINT; 552 continue; 553 } 554 } else { 555 /* Only display dot file if -a/-A set. */ 556 if (cur->fts_name[0] == '.' && !f_listdot) { 557 cur->fts_number = NO_PRINT; 558 continue; 559 } 560 } 561 if (f_nonprint) 562 prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen); 563 if (cur->fts_namelen > maxlen) 564 maxlen = cur->fts_namelen; 565 if (f_octal || f_octal_escape) { 566 int t = len_octal(cur->fts_name, cur->fts_namelen); 567 if (t > maxlen) maxlen = t; 568 } 569 if (needstats) { 570 sp = cur->fts_statp; 571 if (sp->st_blocks > maxblock) 572 maxblock = sp->st_blocks; 573 if (sp->st_ino > maxinode) 574 maxinode = sp->st_ino; 575 if (sp->st_nlink > maxnlink) 576 maxnlink = sp->st_nlink; 577 if (sp->st_size > maxsize) 578 maxsize = sp->st_size; 579 580 btotal += sp->st_blocks; 581 if (f_longform) { 582 if (f_numericonly) { 583 (void)snprintf(nuser, sizeof(nuser), 584 "%u", sp->st_uid); 585 (void)snprintf(ngroup, sizeof(ngroup), 586 "%u", sp->st_gid); 587 user = nuser; 588 group = ngroup; 589 } else { 590 user = user_from_uid(sp->st_uid, 0); 591 group = group_from_gid(sp->st_gid, 0); 592 } 593 if ((ulen = strlen(user)) > maxuser) 594 maxuser = ulen; 595 if ((glen = strlen(group)) > maxgroup) 596 maxgroup = glen; 597 if (f_flags) { 598 flags = getflags(sp->st_flags, "-"); 599 if ((flen = strlen(flags)) > maxflags) 600 maxflags = flen; 601 } else 602 flen = 0; 603 604 if ((np = malloc(sizeof(NAMES) + 605 ulen + glen + flen + 3)) == NULL) 606 err(1, NULL); 607 608 np->user = &np->data[0]; 609 (void)strcpy(np->user, user); 610 np->group = &np->data[ulen + 1]; 611 (void)strcpy(np->group, group); 612 613 if (S_ISCHR(sp->st_mode) || 614 S_ISBLK(sp->st_mode)) 615 bcfile = 1; 616 617 if (f_flags) { 618 np->flags = &np->data[ulen + glen + 2]; 619 (void)strcpy(np->flags, flags); 620 } 621 cur->fts_pointer = np; 622 } 623 } 624 ++entries; 625 } 626 627 if (!entries) 628 return; 629 630 d.list = list; 631 d.entries = entries; 632 d.maxlen = maxlen; 633 if (needstats) { 634 d.bcfile = bcfile; 635 d.btotal = btotal; 636 (void)snprintf(buf, sizeof(buf), "%lu", maxblock); 637 d.s_block = strlen(buf); 638 d.s_flags = maxflags; 639 d.s_group = maxgroup; 640 (void)snprintf(buf, sizeof(buf), "%lu", maxinode); 641 d.s_inode = strlen(buf); 642 (void)snprintf(buf, sizeof(buf), "%lu", maxnlink); 643 d.s_nlink = strlen(buf); 644 (void)snprintf(buf, sizeof(buf), "%qu", maxsize); 645 d.s_size = strlen(buf); 646 d.s_user = maxuser; 647 } 648 649 printfcn(&d); 650 output = 1; 651 652 if (f_longform) 653 for (cur = list; cur; cur = cur->fts_link) 654 free(cur->fts_pointer); 655} 656 657/* 658 * Ordering for mastercmp: 659 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories 660 * as larger than directories. Within either group, use the sort function. 661 * All other levels use the sort function. Error entries remain unsorted. 662 */ 663static int 664mastercmp(a, b) 665 const FTSENT **a, **b; 666{ 667 int a_info, b_info; 668 669 a_info = (*a)->fts_info; 670 if (a_info == FTS_ERR) 671 return (0); 672 b_info = (*b)->fts_info; 673 if (b_info == FTS_ERR) 674 return (0); 675 676 if (a_info == FTS_NS || b_info == FTS_NS) 677 return (namecmp(*a, *b)); 678 679 if (a_info != b_info && 680 (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) { 681 if (a_info == FTS_D) 682 return (1); 683 if (b_info == FTS_D) 684 return (-1); 685 } 686 return (sortfcn(*a, *b)); 687} 688 689/* 690 * Makenines() returns (10**n)-1. This is useful for converting a width 691 * into a number that wide in decimal. 692 */ 693static u_quad_t 694makenines(n) 695 u_long n; 696{ 697 u_long i; 698 u_quad_t reg; 699 700 reg = 1; 701 /* Use a loop instead of pow(), since all values of n are small. */ 702 for (i = 0; i < n; i++) 703 reg *= 10; 704 reg--; 705 706 return reg; 707} 708