14b88c807SRodney W. Grimes /* 24b88c807SRodney W. Grimes * Copyright (c) 1989, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Michael Fischbein. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 174b88c807SRodney W. Grimes * must display the following acknowledgement: 184b88c807SRodney W. Grimes * This product includes software developed by the University of 194b88c807SRodney W. Grimes * California, Berkeley and its contributors. 204b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 214b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 224b88c807SRodney W. Grimes * without specific prior written permission. 234b88c807SRodney W. Grimes * 244b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 254b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 264b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 274b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 284b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 294b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 304b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 314b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 324b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 334b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 344b88c807SRodney W. Grimes * SUCH DAMAGE. 354b88c807SRodney W. Grimes */ 364b88c807SRodney W. Grimes 374b88c807SRodney W. Grimes #ifndef lint 38febad2fcSSteve Price #if 0 39febad2fcSSteve Price static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94"; 40febad2fcSSteve Price #else 41d46c1a60SSteve Price static const char rcsid[] = 422a456239SPeter Wemm "$FreeBSD$"; 43febad2fcSSteve Price #endif 444b88c807SRodney W. Grimes #endif /* not lint */ 454b88c807SRodney W. Grimes 464b88c807SRodney W. Grimes #include <sys/param.h> 474b88c807SRodney W. Grimes #include <sys/stat.h> 484b88c807SRodney W. Grimes 494b88c807SRodney W. Grimes #include <err.h> 504b88c807SRodney W. Grimes #include <errno.h> 514b88c807SRodney W. Grimes #include <fts.h> 524b88c807SRodney W. Grimes #include <grp.h> 534b88c807SRodney W. Grimes #include <pwd.h> 544b88c807SRodney W. Grimes #include <stdio.h> 554b88c807SRodney W. Grimes #include <stdlib.h> 564b88c807SRodney W. Grimes #include <string.h> 574b88c807SRodney W. Grimes #include <time.h> 584b88c807SRodney W. Grimes #include <unistd.h> 59faebfe2eSAndrey A. Chernov #ifdef COLORLS 60faebfe2eSAndrey A. Chernov #include <ctype.h> 61faebfe2eSAndrey A. Chernov #include <termcap.h> 62faebfe2eSAndrey A. Chernov #include <signal.h> 63faebfe2eSAndrey A. Chernov #endif 644b88c807SRodney W. Grimes 654b88c807SRodney W. Grimes #include "ls.h" 664b88c807SRodney W. Grimes #include "extern.h" 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes static int printaname __P((FTSENT *, u_long, u_long)); 694b88c807SRodney W. Grimes static void printlink __P((FTSENT *)); 704b88c807SRodney W. Grimes static void printtime __P((time_t)); 714b88c807SRodney W. Grimes static int printtype __P((u_int)); 7238782c25SAndrey A. Chernov #ifdef COLORLS 7338782c25SAndrey A. Chernov static void endcolor __P((int)); 7438782c25SAndrey A. Chernov static int colortype __P((mode_t)); 7538782c25SAndrey A. Chernov #endif 764b88c807SRodney W. Grimes 774b88c807SRodney W. Grimes #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 784b88c807SRodney W. Grimes 7974985094SJosef Karthauser #ifdef COLORLS 803885812cSJosef Karthauser /* Most of these are taken from <sys/stat.h> */ 813885812cSJosef Karthauser typedef enum Colors { 823885812cSJosef Karthauser C_DIR, /* directory */ 833885812cSJosef Karthauser C_LNK, /* symbolic link */ 843885812cSJosef Karthauser C_SOCK, /* socket */ 853885812cSJosef Karthauser C_FIFO, /* pipe */ 863885812cSJosef Karthauser C_EXEC, /* executable */ 873885812cSJosef Karthauser C_BLK, /* block special */ 883885812cSJosef Karthauser C_CHR, /* character special */ 893885812cSJosef Karthauser C_SUID, /* setuid executable */ 903885812cSJosef Karthauser C_SGID, /* setgid executable */ 913885812cSJosef Karthauser C_WSDIR, /* directory writeble to others, with sticky bit */ 923885812cSJosef Karthauser C_WDIR, /* directory writeble to others, without sticky bit */ 933885812cSJosef Karthauser C_NUMCOLORS /* just a place-holder */ 943885812cSJosef Karthauser } Colors ; 953885812cSJosef Karthauser 963885812cSJosef Karthauser char *defcolors = "4x5x2x3x1x464301060203"; 973885812cSJosef Karthauser 983885812cSJosef Karthauser static int colors[C_NUMCOLORS][2]; 9974985094SJosef Karthauser #endif 1003885812cSJosef Karthauser 1014b88c807SRodney W. Grimes void 1024b88c807SRodney W. Grimes printscol(dp) 1034b88c807SRodney W. Grimes DISPLAY *dp; 1044b88c807SRodney W. Grimes { 1054b88c807SRodney W. Grimes FTSENT *p; 1064b88c807SRodney W. Grimes 1074b88c807SRodney W. Grimes for (p = dp->list; p; p = p->fts_link) { 1084b88c807SRodney W. Grimes if (IS_NOPRINT(p)) 1094b88c807SRodney W. Grimes continue; 1104b88c807SRodney W. Grimes (void)printaname(p, dp->s_inode, dp->s_block); 1114b88c807SRodney W. Grimes (void)putchar('\n'); 1124b88c807SRodney W. Grimes } 1134b88c807SRodney W. Grimes } 1144b88c807SRodney W. Grimes 115ee579ffbSAssar Westerlund /* 116ee579ffbSAssar Westerlund * print name in current style 117ee579ffbSAssar Westerlund */ 118ee579ffbSAssar Westerlund static int 119ee579ffbSAssar Westerlund printname(name) 120ee579ffbSAssar Westerlund const char *name; 121ee579ffbSAssar Westerlund { 122ee579ffbSAssar Westerlund if (f_octal || f_octal_escape) 123ee579ffbSAssar Westerlund return prn_octal(name); 124ee579ffbSAssar Westerlund else if (f_nonprint) 125ee579ffbSAssar Westerlund return prn_printable(name); 126ee579ffbSAssar Westerlund else 127ee579ffbSAssar Westerlund return printf("%s", name); 128ee579ffbSAssar Westerlund } 129ee579ffbSAssar Westerlund 1304b88c807SRodney W. Grimes void 1314b88c807SRodney W. Grimes printlong(dp) 1324b88c807SRodney W. Grimes DISPLAY *dp; 1334b88c807SRodney W. Grimes { 1344b88c807SRodney W. Grimes struct stat *sp; 1354b88c807SRodney W. Grimes FTSENT *p; 1364b88c807SRodney W. Grimes NAMES *np; 1374b88c807SRodney W. Grimes char buf[20]; 13847bb6b11SAndrey A. Chernov #ifdef COLORLS 13947bb6b11SAndrey A. Chernov int color_printed = 0; 14047bb6b11SAndrey A. Chernov #endif 1414b88c807SRodney W. Grimes 1424b88c807SRodney W. Grimes if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 1434b88c807SRodney W. Grimes (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 1444b88c807SRodney W. Grimes 1454b88c807SRodney W. Grimes for (p = dp->list; p; p = p->fts_link) { 1464b88c807SRodney W. Grimes if (IS_NOPRINT(p)) 1474b88c807SRodney W. Grimes continue; 1484b88c807SRodney W. Grimes sp = p->fts_statp; 1494b88c807SRodney W. Grimes if (f_inode) 150fb5cb208SSteve Price (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino); 1514b88c807SRodney W. Grimes if (f_size) 1524b88c807SRodney W. Grimes (void)printf("%*qd ", 1534b88c807SRodney W. Grimes dp->s_block, howmany(sp->st_blocks, blocksize)); 1544b88c807SRodney W. Grimes (void)strmode(sp->st_mode, buf); 1554b88c807SRodney W. Grimes np = p->fts_pointer; 1564b88c807SRodney W. Grimes (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink, 1574b88c807SRodney W. Grimes sp->st_nlink, dp->s_user, np->user, dp->s_group, 1584b88c807SRodney W. Grimes np->group); 1594b88c807SRodney W. Grimes if (f_flags) 1604b88c807SRodney W. Grimes (void)printf("%-*s ", dp->s_flags, np->flags); 1614b88c807SRodney W. Grimes if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 162029b2bd0SBruce Evans if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0) 163df2fbf15SJoerg Wunsch (void)printf("%3d, 0x%08x ", 164029b2bd0SBruce Evans major(sp->st_rdev), 165029b2bd0SBruce Evans (u_int)minor(sp->st_rdev)); 166df2fbf15SJoerg Wunsch else 1674b88c807SRodney W. Grimes (void)printf("%3d, %3d ", 1684b88c807SRodney W. Grimes major(sp->st_rdev), minor(sp->st_rdev)); 1694b88c807SRodney W. Grimes else if (dp->bcfile) 1704b88c807SRodney W. Grimes (void)printf("%*s%*qd ", 1714b88c807SRodney W. Grimes 8 - dp->s_size, "", dp->s_size, sp->st_size); 1724b88c807SRodney W. Grimes else 1734b88c807SRodney W. Grimes (void)printf("%*qd ", dp->s_size, sp->st_size); 1744b88c807SRodney W. Grimes if (f_accesstime) 1754b88c807SRodney W. Grimes printtime(sp->st_atime); 1764b88c807SRodney W. Grimes else if (f_statustime) 1774b88c807SRodney W. Grimes printtime(sp->st_ctime); 1784b88c807SRodney W. Grimes else 1794b88c807SRodney W. Grimes printtime(sp->st_mtime); 18074985094SJosef Karthauser #ifdef COLORLS 1813885812cSJosef Karthauser if (f_color) 182cf0feaeeSAndrey A. Chernov color_printed = colortype(sp->st_mode); 18374985094SJosef Karthauser #endif 184ee579ffbSAssar Westerlund (void)printname(p->fts_name); 18574985094SJosef Karthauser #ifdef COLORLS 186cf0feaeeSAndrey A. Chernov if (f_color && color_printed) 18738782c25SAndrey A. Chernov endcolor(0); 18874985094SJosef Karthauser #endif 1894b88c807SRodney W. Grimes if (f_type) 1904b88c807SRodney W. Grimes (void)printtype(sp->st_mode); 1914b88c807SRodney W. Grimes if (S_ISLNK(sp->st_mode)) 1924b88c807SRodney W. Grimes printlink(p); 1934b88c807SRodney W. Grimes (void)putchar('\n'); 1944b88c807SRodney W. Grimes } 1954b88c807SRodney W. Grimes } 1964b88c807SRodney W. Grimes 1974b88c807SRodney W. Grimes void 1984b88c807SRodney W. Grimes printcol(dp) 1994b88c807SRodney W. Grimes DISPLAY *dp; 2004b88c807SRodney W. Grimes { 2014b88c807SRodney W. Grimes extern int termwidth; 2024b88c807SRodney W. Grimes static FTSENT **array; 2034b88c807SRodney W. Grimes static int lastentries = -1; 2044b88c807SRodney W. Grimes FTSENT *p; 2054b88c807SRodney W. Grimes int base, chcnt, cnt, col, colwidth, num; 2064b88c807SRodney W. Grimes int endcol, numcols, numrows, row; 207545f583cSTim Vanderhoek int tabwidth; 208545f583cSTim Vanderhoek 209545f583cSTim Vanderhoek if (f_notabs) 210545f583cSTim Vanderhoek tabwidth = 1; 211545f583cSTim Vanderhoek else 212545f583cSTim Vanderhoek tabwidth = 8; 2134b88c807SRodney W. Grimes 2144b88c807SRodney W. Grimes /* 2154b88c807SRodney W. Grimes * Have to do random access in the linked list -- build a table 2164b88c807SRodney W. Grimes * of pointers. 2174b88c807SRodney W. Grimes */ 2184b88c807SRodney W. Grimes if (dp->entries > lastentries) { 2194b88c807SRodney W. Grimes lastentries = dp->entries; 2204b88c807SRodney W. Grimes if ((array = 2214b88c807SRodney W. Grimes realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) { 2224b88c807SRodney W. Grimes warn(NULL); 2234b88c807SRodney W. Grimes printscol(dp); 2244b88c807SRodney W. Grimes } 2254b88c807SRodney W. Grimes } 2264b88c807SRodney W. Grimes for (p = dp->list, num = 0; p; p = p->fts_link) 2274b88c807SRodney W. Grimes if (p->fts_number != NO_PRINT) 2284b88c807SRodney W. Grimes array[num++] = p; 2294b88c807SRodney W. Grimes 2304b88c807SRodney W. Grimes colwidth = dp->maxlen; 2314b88c807SRodney W. Grimes if (f_inode) 2324b88c807SRodney W. Grimes colwidth += dp->s_inode + 1; 2334b88c807SRodney W. Grimes if (f_size) 2344b88c807SRodney W. Grimes colwidth += dp->s_block + 1; 2354b88c807SRodney W. Grimes if (f_type) 2364b88c807SRodney W. Grimes colwidth += 1; 2374b88c807SRodney W. Grimes 238545f583cSTim Vanderhoek colwidth = (colwidth + tabwidth) & ~(tabwidth - 1); 2394b88c807SRodney W. Grimes if (termwidth < 2 * colwidth) { 2404b88c807SRodney W. Grimes printscol(dp); 2414b88c807SRodney W. Grimes return; 2424b88c807SRodney W. Grimes } 2434b88c807SRodney W. Grimes 2444b88c807SRodney W. Grimes numcols = termwidth / colwidth; 2454b88c807SRodney W. Grimes numrows = num / numcols; 2464b88c807SRodney W. Grimes if (num % numcols) 2474b88c807SRodney W. Grimes ++numrows; 2484b88c807SRodney W. Grimes 2494b88c807SRodney W. Grimes if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 2504b88c807SRodney W. Grimes (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 2514b88c807SRodney W. Grimes for (row = 0; row < numrows; ++row) { 2524b88c807SRodney W. Grimes endcol = colwidth; 2534b88c807SRodney W. Grimes for (base = row, chcnt = col = 0; col < numcols; ++col) { 2544b88c807SRodney W. Grimes chcnt += printaname(array[base], dp->s_inode, 2554b88c807SRodney W. Grimes dp->s_block); 2564b88c807SRodney W. Grimes if ((base += numrows) >= num) 2574b88c807SRodney W. Grimes break; 258545f583cSTim Vanderhoek while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1))) 259545f583cSTim Vanderhoek <= endcol){ 260545f583cSTim Vanderhoek (void)putchar(f_notabs ? ' ' : '\t'); 2614b88c807SRodney W. Grimes chcnt = cnt; 2624b88c807SRodney W. Grimes } 2634b88c807SRodney W. Grimes endcol += colwidth; 2644b88c807SRodney W. Grimes } 2654b88c807SRodney W. Grimes (void)putchar('\n'); 2664b88c807SRodney W. Grimes } 2674b88c807SRodney W. Grimes } 2684b88c807SRodney W. Grimes 2694b88c807SRodney W. Grimes /* 2704b88c807SRodney W. Grimes * print [inode] [size] name 2714b88c807SRodney W. Grimes * return # of characters printed, no trailing characters. 2724b88c807SRodney W. Grimes */ 2734b88c807SRodney W. Grimes static int 2744b88c807SRodney W. Grimes printaname(p, inodefield, sizefield) 2754b88c807SRodney W. Grimes FTSENT *p; 2764b88c807SRodney W. Grimes u_long sizefield, inodefield; 2774b88c807SRodney W. Grimes { 2784b88c807SRodney W. Grimes struct stat *sp; 2794b88c807SRodney W. Grimes int chcnt; 28047bb6b11SAndrey A. Chernov #ifdef COLORLS 28147bb6b11SAndrey A. Chernov int color_printed = 0; 28247bb6b11SAndrey A. Chernov #endif 2834b88c807SRodney W. Grimes 2844b88c807SRodney W. Grimes sp = p->fts_statp; 2854b88c807SRodney W. Grimes chcnt = 0; 2864b88c807SRodney W. Grimes if (f_inode) 287fb5cb208SSteve Price chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino); 2884b88c807SRodney W. Grimes if (f_size) 2894b88c807SRodney W. Grimes chcnt += printf("%*qd ", 2904b88c807SRodney W. Grimes (int)sizefield, howmany(sp->st_blocks, blocksize)); 29174985094SJosef Karthauser #ifdef COLORLS 2923885812cSJosef Karthauser if (f_color) 293cf0feaeeSAndrey A. Chernov color_printed = colortype(sp->st_mode); 29474985094SJosef Karthauser #endif 295ee579ffbSAssar Westerlund chcnt += printname(p->fts_name); 29674985094SJosef Karthauser #ifdef COLORLS 297cf0feaeeSAndrey A. Chernov if (f_color && color_printed) 29838782c25SAndrey A. Chernov endcolor(0); 29974985094SJosef Karthauser #endif 3004b88c807SRodney W. Grimes if (f_type) 3014b88c807SRodney W. Grimes chcnt += printtype(sp->st_mode); 3024b88c807SRodney W. Grimes return (chcnt); 3034b88c807SRodney W. Grimes } 3044b88c807SRodney W. Grimes 3054b88c807SRodney W. Grimes static void 3064b88c807SRodney W. Grimes printtime(ftime) 3074b88c807SRodney W. Grimes time_t ftime; 3084b88c807SRodney W. Grimes { 309fc4a9bafSAndrey A. Chernov char longstring[80]; 310f173abd0SMike Pritchard static time_t now; 31197e4e97bSJosef Karthauser const char *format; 312f173abd0SMike Pritchard 313f173abd0SMike Pritchard if (now == 0) 314f173abd0SMike Pritchard now = time(NULL); 3154b88c807SRodney W. Grimes 316656dcd43SGarrett Wollman #define SIXMONTHS ((365 / 2) * 86400) 3171e715e34SJosef Karthauser /* "%Ef" is a FreeBSD strftime definition for "%e %b" or "%b %e". 3181e715e34SJosef Karthauser * Actually format is locale sensitive. 3191e715e34SJosef Karthauser */ 3204b88c807SRodney W. Grimes if (f_sectime) 3211e715e34SJosef Karthauser /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */ 3221e715e34SJosef Karthauser format = "%Ef %T %Y "; 323f173abd0SMike Pritchard else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS) 3241e715e34SJosef Karthauser /* mmm dd hh:mm || dd mmm hh:mm */ 3251e715e34SJosef Karthauser format = "%Ef %R "; 32697e4e97bSJosef Karthauser else 3271e715e34SJosef Karthauser /* mmm dd yyyy || dd mmm yyyy */ 3281e715e34SJosef Karthauser format = "%Ef %Y "; 32997e4e97bSJosef Karthauser strftime(longstring, sizeof(longstring), format, localtime(&ftime)); 33097e4e97bSJosef Karthauser fputs(longstring, stdout); 3314b88c807SRodney W. Grimes } 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes static int 3344b88c807SRodney W. Grimes printtype(mode) 3354b88c807SRodney W. Grimes u_int mode; 3364b88c807SRodney W. Grimes { 3374b88c807SRodney W. Grimes switch (mode & S_IFMT) { 3384b88c807SRodney W. Grimes case S_IFDIR: 3394b88c807SRodney W. Grimes (void)putchar('/'); 3404b88c807SRodney W. Grimes return (1); 3414b88c807SRodney W. Grimes case S_IFIFO: 3424b88c807SRodney W. Grimes (void)putchar('|'); 3434b88c807SRodney W. Grimes return (1); 3444b88c807SRodney W. Grimes case S_IFLNK: 3454b88c807SRodney W. Grimes (void)putchar('@'); 3464b88c807SRodney W. Grimes return (1); 3474b88c807SRodney W. Grimes case S_IFSOCK: 3484b88c807SRodney W. Grimes (void)putchar('='); 3494b88c807SRodney W. Grimes return (1); 350fb5cb208SSteve Price case S_IFWHT: 351fb5cb208SSteve Price (void)putchar('%'); 352fb5cb208SSteve Price return (1); 3534b88c807SRodney W. Grimes } 3544b88c807SRodney W. Grimes if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 3554b88c807SRodney W. Grimes (void)putchar('*'); 3564b88c807SRodney W. Grimes return (1); 3574b88c807SRodney W. Grimes } 3584b88c807SRodney W. Grimes return (0); 3594b88c807SRodney W. Grimes } 3604b88c807SRodney W. Grimes 36174985094SJosef Karthauser #ifdef COLORLS 3621bf1478aSAndrey A. Chernov static int 3631bf1478aSAndrey A. Chernov putch(c) 364cf0feaeeSAndrey A. Chernov int c; 365cf0feaeeSAndrey A. Chernov { 36638782c25SAndrey A. Chernov (void) putchar(c); 36738782c25SAndrey A. Chernov return 0; 368cf0feaeeSAndrey A. Chernov } 369cf0feaeeSAndrey A. Chernov 3701bf1478aSAndrey A. Chernov static int 3711bf1478aSAndrey A. Chernov writech(c) 37238782c25SAndrey A. Chernov int c; 37338782c25SAndrey A. Chernov { 37438782c25SAndrey A. Chernov char tmp = c; 37538782c25SAndrey A. Chernov 37638782c25SAndrey A. Chernov (void) write(STDOUT_FILENO, &tmp, 1); 37738782c25SAndrey A. Chernov return 0; 37838782c25SAndrey A. Chernov } 379cf0feaeeSAndrey A. Chernov 3801bf1478aSAndrey A. Chernov static void 3813885812cSJosef Karthauser printcolor(c) 3823885812cSJosef Karthauser Colors c; 3833885812cSJosef Karthauser { 38474985094SJosef Karthauser char *ansiseq; 38574985094SJosef Karthauser 3863885812cSJosef Karthauser if (colors[c][0] != -1) { 387c85e2c8eSAndrey A. Chernov ansiseq = tgoto(ansi_fgcol, 0, colors[c][0]); 38838782c25SAndrey A. Chernov if (ansiseq) 389cf0feaeeSAndrey A. Chernov tputs(ansiseq, 1, putch); 3903885812cSJosef Karthauser } 39174985094SJosef Karthauser 39274985094SJosef Karthauser if (colors[c][1] != -1) { 393c85e2c8eSAndrey A. Chernov ansiseq = tgoto(ansi_bgcol, 0, colors[c][1]); 39438782c25SAndrey A. Chernov if (ansiseq) 395cf0feaeeSAndrey A. Chernov tputs(ansiseq, 1, putch); 39674985094SJosef Karthauser } 39774985094SJosef Karthauser } 39874985094SJosef Karthauser 39938782c25SAndrey A. Chernov static void 40038782c25SAndrey A. Chernov endcolor(sig) 40138782c25SAndrey A. Chernov int sig; 40274985094SJosef Karthauser { 40338782c25SAndrey A. Chernov tputs(ansi_coloff, 1, sig ? writech : putch); 4043885812cSJosef Karthauser } 4053885812cSJosef Karthauser 40638782c25SAndrey A. Chernov static int 4073885812cSJosef Karthauser colortype(mode) 4083885812cSJosef Karthauser mode_t mode; 4093885812cSJosef Karthauser { 4103885812cSJosef Karthauser switch(mode & S_IFMT) { 4113885812cSJosef Karthauser case S_IFDIR: 4123885812cSJosef Karthauser if (mode & S_IWOTH) 4133885812cSJosef Karthauser if (mode & S_ISTXT) 4143885812cSJosef Karthauser printcolor(C_WSDIR); 4153885812cSJosef Karthauser else 4163885812cSJosef Karthauser printcolor(C_WDIR); 4173885812cSJosef Karthauser else 4183885812cSJosef Karthauser printcolor(C_DIR); 4193885812cSJosef Karthauser return(1); 4203885812cSJosef Karthauser case S_IFLNK: 4213885812cSJosef Karthauser printcolor(C_LNK); 4223885812cSJosef Karthauser return(1); 4233885812cSJosef Karthauser case S_IFSOCK: 4243885812cSJosef Karthauser printcolor(C_SOCK); 4253885812cSJosef Karthauser return(1); 4263885812cSJosef Karthauser case S_IFIFO: 4273885812cSJosef Karthauser printcolor(C_FIFO); 4283885812cSJosef Karthauser return(1); 4293885812cSJosef Karthauser case S_IFBLK: 4303885812cSJosef Karthauser printcolor(C_BLK); 4313885812cSJosef Karthauser return(1); 4323885812cSJosef Karthauser case S_IFCHR: 4333885812cSJosef Karthauser printcolor(C_CHR); 4343885812cSJosef Karthauser return(1); 4353885812cSJosef Karthauser } 4363885812cSJosef Karthauser if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 4373885812cSJosef Karthauser if (mode & S_ISUID) 4383885812cSJosef Karthauser printcolor(C_SUID); 4393885812cSJosef Karthauser else if (mode & S_ISGID) 4403885812cSJosef Karthauser printcolor(C_SGID); 4413885812cSJosef Karthauser else 4423885812cSJosef Karthauser printcolor(C_EXEC); 4433885812cSJosef Karthauser return(1); 4443885812cSJosef Karthauser } 4453885812cSJosef Karthauser return(0); 4463885812cSJosef Karthauser } 4473885812cSJosef Karthauser 4483885812cSJosef Karthauser void 4493885812cSJosef Karthauser parsecolors(cs) 4503885812cSJosef Karthauser char *cs; 4513885812cSJosef Karthauser { 4523885812cSJosef Karthauser int i, j, len; 4533885812cSJosef Karthauser char c[2]; 45438782c25SAndrey A. Chernov 4553885812cSJosef Karthauser if (cs == NULL) cs = ""; /* LSCOLORS not set */ 4563885812cSJosef Karthauser len = strlen(cs); 4573885812cSJosef Karthauser for (i = 0 ; i < C_NUMCOLORS ; i++) { 4583885812cSJosef Karthauser if (len <= 2*i) { 4593885812cSJosef Karthauser c[0] = defcolors[2*i]; 4603885812cSJosef Karthauser c[1] = defcolors[2*i+1]; 4613885812cSJosef Karthauser } 4623885812cSJosef Karthauser else { 4633885812cSJosef Karthauser c[0] = cs[2*i]; 4643885812cSJosef Karthauser c[1] = cs[2*i+1]; 4653885812cSJosef Karthauser } 4663885812cSJosef Karthauser for (j = 0 ; j < 2 ; j++) { 4673885812cSJosef Karthauser if ((c[j] < '0' || c[j] > '7') && 468cf0feaeeSAndrey A. Chernov tolower((unsigned char)c[j]) != 'x') { 4693885812cSJosef Karthauser fprintf(stderr, 4703885812cSJosef Karthauser "error: invalid character '%c' in LSCOLORS env var\n", 4713885812cSJosef Karthauser c[j]); 4723885812cSJosef Karthauser c[j] = defcolors[2*i+j]; 4733885812cSJosef Karthauser } 47438782c25SAndrey A. Chernov if (tolower((unsigned char)c[j]) == 'x') 4753885812cSJosef Karthauser colors[i][j] = -1; 4763885812cSJosef Karthauser else 4773885812cSJosef Karthauser colors[i][j] = c[j]-'0'; 4783885812cSJosef Karthauser } 4793885812cSJosef Karthauser } 4803885812cSJosef Karthauser } 481cf0feaeeSAndrey A. Chernov 4821bf1478aSAndrey A. Chernov void 4831bf1478aSAndrey A. Chernov colorquit(sig) 484cf0feaeeSAndrey A. Chernov int sig; 485cf0feaeeSAndrey A. Chernov { 48638782c25SAndrey A. Chernov endcolor(sig); 487faebfe2eSAndrey A. Chernov 488faebfe2eSAndrey A. Chernov (void) signal(sig, SIG_DFL); 489faebfe2eSAndrey A. Chernov (void) kill(getpid(), sig); 490cf0feaeeSAndrey A. Chernov } 49174985094SJosef Karthauser #endif /*COLORLS*/ 4923885812cSJosef Karthauser 4934b88c807SRodney W. Grimes static void 4944b88c807SRodney W. Grimes printlink(p) 4954b88c807SRodney W. Grimes FTSENT *p; 4964b88c807SRodney W. Grimes { 4974b88c807SRodney W. Grimes int lnklen; 4984b88c807SRodney W. Grimes char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; 4994b88c807SRodney W. Grimes 5004b88c807SRodney W. Grimes if (p->fts_level == FTS_ROOTLEVEL) 5014b88c807SRodney W. Grimes (void)snprintf(name, sizeof(name), "%s", p->fts_name); 5024b88c807SRodney W. Grimes else 5034b88c807SRodney W. Grimes (void)snprintf(name, sizeof(name), 5044b88c807SRodney W. Grimes "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 5054b88c807SRodney W. Grimes if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 5064b88c807SRodney W. Grimes (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 5074b88c807SRodney W. Grimes return; 5084b88c807SRodney W. Grimes } 5094b88c807SRodney W. Grimes path[lnklen] = '\0'; 5107ea30648SDag-Erling Smørgrav (void)printf(" -> "); 511ee579ffbSAssar Westerlund printname(path); 5124b88c807SRodney W. Grimes } 513