print.c revision 61296
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 38#if 0 39static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94"; 40#else 41static const char rcsid[] = 42 "$FreeBSD: head/bin/ls/print.c 61296 2000-06-05 20:54:46Z ache $"; 43#endif 44#endif /* not lint */ 45 46#include <sys/param.h> 47#include <sys/stat.h> 48 49#include <err.h> 50#include <errno.h> 51#include <fts.h> 52#include <grp.h> 53#include <pwd.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <string.h> 57#include <time.h> 58#include <unistd.h> 59#ifdef COLORLS 60#include <ctype.h> 61#include <termcap.h> 62#include <signal.h> 63#endif 64 65#include "ls.h" 66#include "extern.h" 67 68static int printaname __P((FTSENT *, u_long, u_long)); 69static void printlink __P((FTSENT *)); 70static void printtime __P((time_t)); 71static int printtype __P((u_int)); 72 73#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 74 75#ifdef COLORLS 76/* Most of these are taken from <sys/stat.h> */ 77typedef enum Colors { 78 C_DIR, /* directory */ 79 C_LNK, /* symbolic link */ 80 C_SOCK, /* socket */ 81 C_FIFO, /* pipe */ 82 C_EXEC, /* executable */ 83 C_BLK, /* block special */ 84 C_CHR, /* character special */ 85 C_SUID, /* setuid executable */ 86 C_SGID, /* setgid executable */ 87 C_WSDIR, /* directory writeble to others, with sticky bit */ 88 C_WDIR, /* directory writeble to others, without sticky bit */ 89 C_NUMCOLORS /* just a place-holder */ 90} Colors ; 91 92char *defcolors = "4x5x2x3x1x464301060203"; 93 94static int colors[C_NUMCOLORS][2]; 95#endif 96 97void 98printscol(dp) 99 DISPLAY *dp; 100{ 101 FTSENT *p; 102 103 for (p = dp->list; p; p = p->fts_link) { 104 if (IS_NOPRINT(p)) 105 continue; 106 (void)printaname(p, dp->s_inode, dp->s_block); 107 (void)putchar('\n'); 108 } 109} 110 111void 112printlong(dp) 113 DISPLAY *dp; 114{ 115 struct stat *sp; 116 FTSENT *p; 117 NAMES *np; 118 char buf[20]; 119#ifdef COLORLS 120 int color_printed = 0; 121#endif 122 123 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 124 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 125 126 for (p = dp->list; p; p = p->fts_link) { 127 if (IS_NOPRINT(p)) 128 continue; 129 sp = p->fts_statp; 130 if (f_inode) 131 (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino); 132 if (f_size) 133 (void)printf("%*qd ", 134 dp->s_block, howmany(sp->st_blocks, blocksize)); 135 (void)strmode(sp->st_mode, buf); 136 np = p->fts_pointer; 137 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink, 138 sp->st_nlink, dp->s_user, np->user, dp->s_group, 139 np->group); 140 if (f_flags) 141 (void)printf("%-*s ", dp->s_flags, np->flags); 142 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 143 if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0) 144 (void)printf("%3d, 0x%08x ", 145 major(sp->st_rdev), 146 (u_int)minor(sp->st_rdev)); 147 else 148 (void)printf("%3d, %3d ", 149 major(sp->st_rdev), minor(sp->st_rdev)); 150 else if (dp->bcfile) 151 (void)printf("%*s%*qd ", 152 8 - dp->s_size, "", dp->s_size, sp->st_size); 153 else 154 (void)printf("%*qd ", dp->s_size, sp->st_size); 155 if (f_accesstime) 156 printtime(sp->st_atime); 157 else if (f_statustime) 158 printtime(sp->st_ctime); 159 else 160 printtime(sp->st_mtime); 161#ifdef COLORLS 162 if (f_color) 163 color_printed = colortype(sp->st_mode); 164#endif 165 if (f_octal || f_octal_escape) (void)prn_octal(p->fts_name); 166 else (void)printf("%s", p->fts_name); 167#ifdef COLORLS 168 if (f_color && color_printed) 169 endcolor(); 170#endif 171 if (f_type) 172 (void)printtype(sp->st_mode); 173 if (S_ISLNK(sp->st_mode)) 174 printlink(p); 175 (void)putchar('\n'); 176 } 177} 178 179void 180printcol(dp) 181 DISPLAY *dp; 182{ 183 extern int termwidth; 184 static FTSENT **array; 185 static int lastentries = -1; 186 FTSENT *p; 187 int base, chcnt, cnt, col, colwidth, num; 188 int endcol, numcols, numrows, row; 189 int tabwidth; 190 191 if (f_notabs) 192 tabwidth = 1; 193 else 194 tabwidth = 8; 195 196 /* 197 * Have to do random access in the linked list -- build a table 198 * of pointers. 199 */ 200 if (dp->entries > lastentries) { 201 lastentries = dp->entries; 202 if ((array = 203 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) { 204 warn(NULL); 205 printscol(dp); 206 } 207 } 208 for (p = dp->list, num = 0; p; p = p->fts_link) 209 if (p->fts_number != NO_PRINT) 210 array[num++] = p; 211 212 colwidth = dp->maxlen; 213 if (f_inode) 214 colwidth += dp->s_inode + 1; 215 if (f_size) 216 colwidth += dp->s_block + 1; 217 if (f_type) 218 colwidth += 1; 219 220 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1); 221 if (termwidth < 2 * colwidth) { 222 printscol(dp); 223 return; 224 } 225 226 numcols = termwidth / colwidth; 227 numrows = num / numcols; 228 if (num % numcols) 229 ++numrows; 230 231 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 232 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 233 for (row = 0; row < numrows; ++row) { 234 endcol = colwidth; 235 for (base = row, chcnt = col = 0; col < numcols; ++col) { 236 chcnt += printaname(array[base], dp->s_inode, 237 dp->s_block); 238 if ((base += numrows) >= num) 239 break; 240#ifdef COLORLS 241 /* 242 * some terminals get confused if we mix tabs 243 * with color sequences 244 */ 245 if (f_color) 246 while ((cnt = (chcnt + 1)) <= endcol) { 247 (void)putchar(' '); 248 chcnt = cnt; 249 } 250 else 251#endif 252 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1))) 253 <= endcol){ 254 (void)putchar(f_notabs ? ' ' : '\t'); 255 chcnt = cnt; 256 } 257 endcol += colwidth; 258 } 259 (void)putchar('\n'); 260 } 261} 262 263/* 264 * print [inode] [size] name 265 * return # of characters printed, no trailing characters. 266 */ 267static int 268printaname(p, inodefield, sizefield) 269 FTSENT *p; 270 u_long sizefield, inodefield; 271{ 272 struct stat *sp; 273 int chcnt; 274#ifdef COLORLS 275 int color_printed = 0; 276#endif 277 278 sp = p->fts_statp; 279 chcnt = 0; 280 if (f_inode) 281 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino); 282 if (f_size) 283 chcnt += printf("%*qd ", 284 (int)sizefield, howmany(sp->st_blocks, blocksize)); 285#ifdef COLORLS 286 if (f_color) 287 color_printed = colortype(sp->st_mode); 288#endif 289 chcnt += (f_octal || f_octal_escape) ? prn_octal(p->fts_name) 290 : printf("%s", p->fts_name); 291#ifdef COLORLS 292 if (f_color && color_printed) 293 endcolor(); 294#endif 295 if (f_type) 296 chcnt += printtype(sp->st_mode); 297 return (chcnt); 298} 299 300static void 301printtime(ftime) 302 time_t ftime; 303{ 304 int i; 305 char longstring[80]; 306 static time_t now; 307 308 if (now == 0) 309 now = time(NULL); 310 311 strftime(longstring, sizeof(longstring), "%c", localtime(&ftime)); 312 for (i = 4; i < 11; ++i) 313 (void)putchar(longstring[i]); 314 315#define SIXMONTHS ((365 / 2) * 86400) 316 if (f_sectime) 317 for (i = 11; i < 24; i++) 318 (void)putchar(longstring[i]); 319 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS) 320 for (i = 11; i < 16; ++i) 321 (void)putchar(longstring[i]); 322 else { 323 (void)putchar(' '); 324 for (i = 20; i < 24; ++i) 325 (void)putchar(longstring[i]); 326 } 327 (void)putchar(' '); 328} 329 330static int 331printtype(mode) 332 u_int mode; 333{ 334 switch (mode & S_IFMT) { 335 case S_IFDIR: 336 (void)putchar('/'); 337 return (1); 338 case S_IFIFO: 339 (void)putchar('|'); 340 return (1); 341 case S_IFLNK: 342 (void)putchar('@'); 343 return (1); 344 case S_IFSOCK: 345 (void)putchar('='); 346 return (1); 347 case S_IFWHT: 348 (void)putchar('%'); 349 return (1); 350 } 351 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 352 (void)putchar('*'); 353 return (1); 354 } 355 return (0); 356} 357 358#ifdef COLORLS 359int putch(c) 360 int c; 361{ 362 return putc(c, stdout); 363} 364 365 366void 367printcolor(c) 368 Colors c; 369{ 370 char *ansiseq; 371 372 if (colors[c][0] != -1) { 373 ansiseq = tgoto(ansi_fgcol, 0, colors[c][0]); 374 if (ansiseq && *ansiseq != 'O') /* "OOPS" */ 375 tputs(ansiseq, 1, putch); 376 } 377 378 if (colors[c][1] != -1) { 379 ansiseq = tgoto(ansi_bgcol, 0, colors[c][1]); 380 if (ansiseq && *ansiseq != 'O') /* "OOPS" */ 381 tputs(ansiseq, 1, putch); 382 } 383} 384 385void 386endcolor() 387{ 388 tputs(ansi_coloff, 1, putch); 389} 390 391int 392colortype(mode) 393 mode_t mode; 394{ 395 switch(mode & S_IFMT) { 396 case S_IFDIR: 397 if (mode & S_IWOTH) 398 if (mode & S_ISTXT) 399 printcolor(C_WSDIR); 400 else 401 printcolor(C_WDIR); 402 else 403 printcolor(C_DIR); 404 return(1); 405 case S_IFLNK: 406 printcolor(C_LNK); 407 return(1); 408 case S_IFSOCK: 409 printcolor(C_SOCK); 410 return(1); 411 case S_IFIFO: 412 printcolor(C_FIFO); 413 return(1); 414 case S_IFBLK: 415 printcolor(C_BLK); 416 return(1); 417 case S_IFCHR: 418 printcolor(C_CHR); 419 return(1); 420 } 421 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 422 if (mode & S_ISUID) 423 printcolor(C_SUID); 424 else if (mode & S_ISGID) 425 printcolor(C_SGID); 426 else 427 printcolor(C_EXEC); 428 return(1); 429 } 430 return(0); 431} 432 433void 434parsecolors(cs) 435char *cs; 436{ 437 int i, j, len; 438 char c[2]; 439 if (cs == NULL) cs = ""; /* LSCOLORS not set */ 440 len = strlen(cs); 441 for (i = 0 ; i < C_NUMCOLORS ; i++) { 442 if (len <= 2*i) { 443 c[0] = defcolors[2*i]; 444 c[1] = defcolors[2*i+1]; 445 } 446 else { 447 c[0] = cs[2*i]; 448 c[1] = cs[2*i+1]; 449 } 450 for (j = 0 ; j < 2 ; j++) { 451 if ((c[j] < '0' || c[j] > '7') && 452 tolower((unsigned char)c[j]) != 'x') { 453 fprintf(stderr, 454 "error: invalid character '%c' in LSCOLORS env var\n", 455 c[j]); 456 c[j] = defcolors[2*i+j]; 457 } 458 if (c[j] == 'x') 459 colors[i][j] = -1; 460 else 461 colors[i][j] = c[j]-'0'; 462 } 463 } 464} 465 466void colorquit(sig) 467 int sig; 468{ 469 endcolor(); 470 fflush(stdout); 471 472 (void) signal(sig, SIG_DFL); 473 (void) kill(getpid(), sig); 474} 475#endif /*COLORLS*/ 476 477static void 478printlink(p) 479 FTSENT *p; 480{ 481 int lnklen; 482 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; 483 484 if (p->fts_level == FTS_ROOTLEVEL) 485 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 486 else 487 (void)snprintf(name, sizeof(name), 488 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 489 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 490 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 491 return; 492 } 493 path[lnklen] = '\0'; 494 if (f_octal || f_octal_escape) { 495 (void)printf(" -> "); 496 (void)prn_octal(path); 497 } 498 else (void)printf(" -> %s", path); 499} 500