print.c (55514) | print.c (61178) |
---|---|
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 --- 25 unchanged lines hidden (view full) --- 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[] = | 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 --- 25 unchanged lines hidden (view full) --- 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 55514 2000-01-06 14:40:10Z bde $"; | 42 "$FreeBSD: head/bin/ls/print.c 61178 2000-06-02 14:53:42Z joe $"; |
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> | 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#include <ctype.h> |
|
59 60#include "ls.h" 61#include "extern.h" 62 63static int printaname __P((FTSENT *, u_long, u_long)); 64static void printlink __P((FTSENT *)); 65static void printtime __P((time_t)); 66static int printtype __P((u_int)); 67 68#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 69 | 60 61#include "ls.h" 62#include "extern.h" 63 64static int printaname __P((FTSENT *, u_long, u_long)); 65static void printlink __P((FTSENT *)); 66static void printtime __P((time_t)); 67static int printtype __P((u_int)); 68 69#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 70 |
71/* Most of these are taken from <sys/stat.h> */ 72typedef enum Colors { 73 C_DIR, /* directory */ 74 C_LNK, /* symbolic link */ 75 C_SOCK, /* socket */ 76 C_FIFO, /* pipe */ 77 C_EXEC, /* executable */ 78 C_BLK, /* block special */ 79 C_CHR, /* character special */ 80 C_SUID, /* setuid executable */ 81 C_SGID, /* setgid executable */ 82 C_WSDIR, /* directory writeble to others, with sticky bit */ 83 C_WDIR, /* directory writeble to others, without sticky bit */ 84 C_NUMCOLORS /* just a place-holder */ 85} Colors ; 86 87char *defcolors = "4x5x2x3x1x464301060203"; 88 89static int colors[C_NUMCOLORS][2]; 90 |
|
70void 71printscol(dp) 72 DISPLAY *dp; 73{ 74 FTSENT *p; 75 76 for (p = dp->list; p; p = p->fts_link) { 77 if (IS_NOPRINT(p)) --- 45 unchanged lines hidden (view full) --- 123 else 124 (void)printf("%*qd ", dp->s_size, sp->st_size); 125 if (f_accesstime) 126 printtime(sp->st_atime); 127 else if (f_statustime) 128 printtime(sp->st_ctime); 129 else 130 printtime(sp->st_mtime); | 91void 92printscol(dp) 93 DISPLAY *dp; 94{ 95 FTSENT *p; 96 97 for (p = dp->list; p; p = p->fts_link) { 98 if (IS_NOPRINT(p)) --- 45 unchanged lines hidden (view full) --- 144 else 145 (void)printf("%*qd ", dp->s_size, sp->st_size); 146 if (f_accesstime) 147 printtime(sp->st_atime); 148 else if (f_statustime) 149 printtime(sp->st_ctime); 150 else 151 printtime(sp->st_mtime); |
152 if (f_color) 153 (void)colortype(sp->st_mode); |
|
131 if (f_octal || f_octal_escape) (void)prn_octal(p->fts_name); 132 else (void)printf("%s", p->fts_name); | 154 if (f_octal || f_octal_escape) (void)prn_octal(p->fts_name); 155 else (void)printf("%s", p->fts_name); |
156 if (f_color) 157 (void)printf("\033[m"); |
|
133 if (f_type) 134 (void)printtype(sp->st_mode); 135 if (S_ISLNK(sp->st_mode)) 136 printlink(p); 137 (void)putchar('\n'); 138 } 139} 140 --- 53 unchanged lines hidden (view full) --- 194 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 195 for (row = 0; row < numrows; ++row) { 196 endcol = colwidth; 197 for (base = row, chcnt = col = 0; col < numcols; ++col) { 198 chcnt += printaname(array[base], dp->s_inode, 199 dp->s_block); 200 if ((base += numrows) >= num) 201 break; | 158 if (f_type) 159 (void)printtype(sp->st_mode); 160 if (S_ISLNK(sp->st_mode)) 161 printlink(p); 162 (void)putchar('\n'); 163 } 164} 165 --- 53 unchanged lines hidden (view full) --- 219 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 220 for (row = 0; row < numrows; ++row) { 221 endcol = colwidth; 222 for (base = row, chcnt = col = 0; col < numcols; ++col) { 223 chcnt += printaname(array[base], dp->s_inode, 224 dp->s_block); 225 if ((base += numrows) >= num) 226 break; |
227 /* 228 * some terminals get confused if we mix tabs 229 * with color sequences 230 */ 231 if (f_color) 232 while ((cnt = (chcnt + 1)) <= endcol) { 233 (void)putchar(' '); 234 chcnt = cnt; 235 } 236 else |
|
202 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1))) 203 <= endcol){ 204 (void)putchar(f_notabs ? ' ' : '\t'); 205 chcnt = cnt; 206 } 207 endcol += colwidth; 208 } 209 (void)putchar('\n'); --- 14 unchanged lines hidden (view full) --- 224 225 sp = p->fts_statp; 226 chcnt = 0; 227 if (f_inode) 228 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino); 229 if (f_size) 230 chcnt += printf("%*qd ", 231 (int)sizefield, howmany(sp->st_blocks, blocksize)); | 237 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1))) 238 <= endcol){ 239 (void)putchar(f_notabs ? ' ' : '\t'); 240 chcnt = cnt; 241 } 242 endcol += colwidth; 243 } 244 (void)putchar('\n'); --- 14 unchanged lines hidden (view full) --- 259 260 sp = p->fts_statp; 261 chcnt = 0; 262 if (f_inode) 263 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino); 264 if (f_size) 265 chcnt += printf("%*qd ", 266 (int)sizefield, howmany(sp->st_blocks, blocksize)); |
267 if (f_color) 268 (void)colortype(sp->st_mode); |
|
232 chcnt += (f_octal || f_octal_escape) ? prn_octal(p->fts_name) 233 : printf("%s", p->fts_name); | 269 chcnt += (f_octal || f_octal_escape) ? prn_octal(p->fts_name) 270 : printf("%s", p->fts_name); |
271 if (f_color) 272 printf("\033[m"); |
|
234 if (f_type) 235 chcnt += printtype(sp->st_mode); 236 return (chcnt); 237} 238 239static void 240printtime(ftime) 241 time_t ftime; --- 47 unchanged lines hidden (view full) --- 289 } 290 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 291 (void)putchar('*'); 292 return (1); 293 } 294 return (0); 295} 296 | 273 if (f_type) 274 chcnt += printtype(sp->st_mode); 275 return (chcnt); 276} 277 278static void 279printtime(ftime) 280 time_t ftime; --- 47 unchanged lines hidden (view full) --- 328 } 329 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 330 (void)putchar('*'); 331 return (1); 332 } 333 return (0); 334} 335 |
336void 337printcolor(c) 338 Colors c; 339{ 340 printf("\033["); 341 if (colors[c][0] != -1) { 342 printf("3%d", colors[c][0]); 343 if (colors[c][1] != -1) 344 printf(";"); 345 } 346 if (colors[c][1] != -1) 347 printf("4%d", colors[c][1]); 348 printf("m"); 349} 350 351int 352colortype(mode) 353 mode_t mode; 354{ 355 switch(mode & S_IFMT) { 356 case S_IFDIR: 357 if (mode & S_IWOTH) 358 if (mode & S_ISTXT) 359 printcolor(C_WSDIR); 360 else 361 printcolor(C_WDIR); 362 else 363 printcolor(C_DIR); 364 return(1); 365 case S_IFLNK: 366 printcolor(C_LNK); 367 return(1); 368 case S_IFSOCK: 369 printcolor(C_SOCK); 370 return(1); 371 case S_IFIFO: 372 printcolor(C_FIFO); 373 return(1); 374 case S_IFBLK: 375 printcolor(C_BLK); 376 return(1); 377 case S_IFCHR: 378 printcolor(C_CHR); 379 return(1); 380 } 381 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 382 if (mode & S_ISUID) 383 printcolor(C_SUID); 384 else if (mode & S_ISGID) 385 printcolor(C_SGID); 386 else 387 printcolor(C_EXEC); 388 return(1); 389 } 390 return(0); 391} 392 393void 394parsecolors(cs) 395char *cs; 396{ 397 int i, j, len; 398 char c[2]; 399 if (cs == NULL) cs = ""; /* LSCOLORS not set */ 400 len = strlen(cs); 401 for (i = 0 ; i < C_NUMCOLORS ; i++) { 402 if (len <= 2*i) { 403 c[0] = defcolors[2*i]; 404 c[1] = defcolors[2*i+1]; 405 } 406 else { 407 c[0] = cs[2*i]; 408 c[1] = cs[2*i+1]; 409 } 410 for (j = 0 ; j < 2 ; j++) { 411 if ((c[j] < '0' || c[j] > '7') && 412 tolower(c[j]) != 'x') { 413 fprintf(stderr, 414 "error: invalid character '%c' in LSCOLORS env var\n", 415 c[j]); 416 c[j] = defcolors[2*i+j]; 417 } 418 if (c[j] == 'x') 419 colors[i][j] = -1; 420 else 421 colors[i][j] = c[j]-'0'; 422 } 423 } 424} 425 |
|
297static void 298printlink(p) 299 FTSENT *p; 300{ 301 int lnklen; 302 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; 303 304 if (p->fts_level == FTS_ROOTLEVEL) --- 15 unchanged lines hidden --- | 426static void 427printlink(p) 428 FTSENT *p; 429{ 430 int lnklen; 431 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; 432 433 if (p->fts_level == FTS_ROOTLEVEL) --- 15 unchanged lines hidden --- |