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