xref: /freebsd/bin/ls/print.c (revision 97e4e97bd1d7e665a045fcb4dca3bed3602dd229)
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 
1154b88c807SRodney W. Grimes void
1164b88c807SRodney W. Grimes printlong(dp)
1174b88c807SRodney W. Grimes 	DISPLAY *dp;
1184b88c807SRodney W. Grimes {
1194b88c807SRodney W. Grimes 	struct stat *sp;
1204b88c807SRodney W. Grimes 	FTSENT *p;
1214b88c807SRodney W. Grimes 	NAMES *np;
1224b88c807SRodney W. Grimes 	char buf[20];
12347bb6b11SAndrey A. Chernov #ifdef COLORLS
12447bb6b11SAndrey A. Chernov 	int color_printed = 0;
12547bb6b11SAndrey A. Chernov #endif
1264b88c807SRodney W. Grimes 
1274b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1284b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1294b88c807SRodney W. Grimes 
1304b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1314b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1324b88c807SRodney W. Grimes 			continue;
1334b88c807SRodney W. Grimes 		sp = p->fts_statp;
1344b88c807SRodney W. Grimes 		if (f_inode)
135fb5cb208SSteve Price 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1364b88c807SRodney W. Grimes 		if (f_size)
1374b88c807SRodney W. Grimes 			(void)printf("%*qd ",
1384b88c807SRodney W. Grimes 			    dp->s_block, howmany(sp->st_blocks, blocksize));
1394b88c807SRodney W. Grimes 		(void)strmode(sp->st_mode, buf);
1404b88c807SRodney W. Grimes 		np = p->fts_pointer;
1414b88c807SRodney W. Grimes 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1424b88c807SRodney W. Grimes 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1434b88c807SRodney W. Grimes 		    np->group);
1444b88c807SRodney W. Grimes 		if (f_flags)
1454b88c807SRodney W. Grimes 			(void)printf("%-*s ", dp->s_flags, np->flags);
1464b88c807SRodney W. Grimes 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
147029b2bd0SBruce Evans 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
148df2fbf15SJoerg Wunsch 				(void)printf("%3d, 0x%08x ",
149029b2bd0SBruce Evans 				    major(sp->st_rdev),
150029b2bd0SBruce Evans 				    (u_int)minor(sp->st_rdev));
151df2fbf15SJoerg Wunsch 			else
1524b88c807SRodney W. Grimes 				(void)printf("%3d, %3d ",
1534b88c807SRodney W. Grimes 				    major(sp->st_rdev), minor(sp->st_rdev));
1544b88c807SRodney W. Grimes 		else if (dp->bcfile)
1554b88c807SRodney W. Grimes 			(void)printf("%*s%*qd ",
1564b88c807SRodney W. Grimes 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1574b88c807SRodney W. Grimes 		else
1584b88c807SRodney W. Grimes 			(void)printf("%*qd ", dp->s_size, sp->st_size);
1594b88c807SRodney W. Grimes 		if (f_accesstime)
1604b88c807SRodney W. Grimes 			printtime(sp->st_atime);
1614b88c807SRodney W. Grimes 		else if (f_statustime)
1624b88c807SRodney W. Grimes 			printtime(sp->st_ctime);
1634b88c807SRodney W. Grimes 		else
1644b88c807SRodney W. Grimes 			printtime(sp->st_mtime);
16574985094SJosef Karthauser #ifdef COLORLS
1663885812cSJosef Karthauser 		if (f_color)
167cf0feaeeSAndrey A. Chernov 			color_printed = colortype(sp->st_mode);
16874985094SJosef Karthauser #endif
1690d86878cSDag-Erling Smørgrav 		if (f_octal || f_octal_escape) (void)prn_octal(p->fts_name);
1707ea30648SDag-Erling Smørgrav 		else (void)printf("%s", p->fts_name);
17174985094SJosef Karthauser #ifdef COLORLS
172cf0feaeeSAndrey A. Chernov 		if (f_color && color_printed)
17338782c25SAndrey A. Chernov 			endcolor(0);
17474985094SJosef Karthauser #endif
1754b88c807SRodney W. Grimes 		if (f_type)
1764b88c807SRodney W. Grimes 			(void)printtype(sp->st_mode);
1774b88c807SRodney W. Grimes 		if (S_ISLNK(sp->st_mode))
1784b88c807SRodney W. Grimes 			printlink(p);
1794b88c807SRodney W. Grimes 		(void)putchar('\n');
1804b88c807SRodney W. Grimes 	}
1814b88c807SRodney W. Grimes }
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes void
1844b88c807SRodney W. Grimes printcol(dp)
1854b88c807SRodney W. Grimes 	DISPLAY *dp;
1864b88c807SRodney W. Grimes {
1874b88c807SRodney W. Grimes 	extern int termwidth;
1884b88c807SRodney W. Grimes 	static FTSENT **array;
1894b88c807SRodney W. Grimes 	static int lastentries = -1;
1904b88c807SRodney W. Grimes 	FTSENT *p;
1914b88c807SRodney W. Grimes 	int base, chcnt, cnt, col, colwidth, num;
1924b88c807SRodney W. Grimes 	int endcol, numcols, numrows, row;
193545f583cSTim Vanderhoek 	int tabwidth;
194545f583cSTim Vanderhoek 
195545f583cSTim Vanderhoek 	if (f_notabs)
196545f583cSTim Vanderhoek 		tabwidth = 1;
197545f583cSTim Vanderhoek 	else
198545f583cSTim Vanderhoek 		tabwidth = 8;
1994b88c807SRodney W. Grimes 
2004b88c807SRodney W. Grimes 	/*
2014b88c807SRodney W. Grimes 	 * Have to do random access in the linked list -- build a table
2024b88c807SRodney W. Grimes 	 * of pointers.
2034b88c807SRodney W. Grimes 	 */
2044b88c807SRodney W. Grimes 	if (dp->entries > lastentries) {
2054b88c807SRodney W. Grimes 		lastentries = dp->entries;
2064b88c807SRodney W. Grimes 		if ((array =
2074b88c807SRodney W. Grimes 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
2084b88c807SRodney W. Grimes 			warn(NULL);
2094b88c807SRodney W. Grimes 			printscol(dp);
2104b88c807SRodney W. Grimes 		}
2114b88c807SRodney W. Grimes 	}
2124b88c807SRodney W. Grimes 	for (p = dp->list, num = 0; p; p = p->fts_link)
2134b88c807SRodney W. Grimes 		if (p->fts_number != NO_PRINT)
2144b88c807SRodney W. Grimes 			array[num++] = p;
2154b88c807SRodney W. Grimes 
2164b88c807SRodney W. Grimes 	colwidth = dp->maxlen;
2174b88c807SRodney W. Grimes 	if (f_inode)
2184b88c807SRodney W. Grimes 		colwidth += dp->s_inode + 1;
2194b88c807SRodney W. Grimes 	if (f_size)
2204b88c807SRodney W. Grimes 		colwidth += dp->s_block + 1;
2214b88c807SRodney W. Grimes 	if (f_type)
2224b88c807SRodney W. Grimes 		colwidth += 1;
2234b88c807SRodney W. Grimes 
224545f583cSTim Vanderhoek 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
2254b88c807SRodney W. Grimes 	if (termwidth < 2 * colwidth) {
2264b88c807SRodney W. Grimes 		printscol(dp);
2274b88c807SRodney W. Grimes 		return;
2284b88c807SRodney W. Grimes 	}
2294b88c807SRodney W. Grimes 
2304b88c807SRodney W. Grimes 	numcols = termwidth / colwidth;
2314b88c807SRodney W. Grimes 	numrows = num / numcols;
2324b88c807SRodney W. Grimes 	if (num % numcols)
2334b88c807SRodney W. Grimes 		++numrows;
2344b88c807SRodney W. Grimes 
2354b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
2364b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
2374b88c807SRodney W. Grimes 	for (row = 0; row < numrows; ++row) {
2384b88c807SRodney W. Grimes 		endcol = colwidth;
2394b88c807SRodney W. Grimes 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
2404b88c807SRodney W. Grimes 			chcnt += printaname(array[base], dp->s_inode,
2414b88c807SRodney W. Grimes 			    dp->s_block);
2424b88c807SRodney W. Grimes 			if ((base += numrows) >= num)
2434b88c807SRodney W. Grimes 				break;
244545f583cSTim Vanderhoek 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
245545f583cSTim Vanderhoek 			    <= endcol){
246545f583cSTim Vanderhoek 				(void)putchar(f_notabs ? ' ' : '\t');
2474b88c807SRodney W. Grimes 				chcnt = cnt;
2484b88c807SRodney W. Grimes 			}
2494b88c807SRodney W. Grimes 			endcol += colwidth;
2504b88c807SRodney W. Grimes 		}
2514b88c807SRodney W. Grimes 		(void)putchar('\n');
2524b88c807SRodney W. Grimes 	}
2534b88c807SRodney W. Grimes }
2544b88c807SRodney W. Grimes 
2554b88c807SRodney W. Grimes /*
2564b88c807SRodney W. Grimes  * print [inode] [size] name
2574b88c807SRodney W. Grimes  * return # of characters printed, no trailing characters.
2584b88c807SRodney W. Grimes  */
2594b88c807SRodney W. Grimes static int
2604b88c807SRodney W. Grimes printaname(p, inodefield, sizefield)
2614b88c807SRodney W. Grimes 	FTSENT *p;
2624b88c807SRodney W. Grimes 	u_long sizefield, inodefield;
2634b88c807SRodney W. Grimes {
2644b88c807SRodney W. Grimes 	struct stat *sp;
2654b88c807SRodney W. Grimes 	int chcnt;
26647bb6b11SAndrey A. Chernov #ifdef COLORLS
26747bb6b11SAndrey A. Chernov 	int color_printed = 0;
26847bb6b11SAndrey A. Chernov #endif
2694b88c807SRodney W. Grimes 
2704b88c807SRodney W. Grimes 	sp = p->fts_statp;
2714b88c807SRodney W. Grimes 	chcnt = 0;
2724b88c807SRodney W. Grimes 	if (f_inode)
273fb5cb208SSteve Price 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
2744b88c807SRodney W. Grimes 	if (f_size)
2754b88c807SRodney W. Grimes 		chcnt += printf("%*qd ",
2764b88c807SRodney W. Grimes 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
27774985094SJosef Karthauser #ifdef COLORLS
2783885812cSJosef Karthauser 	if (f_color)
279cf0feaeeSAndrey A. Chernov 		color_printed = colortype(sp->st_mode);
28074985094SJosef Karthauser #endif
2810d86878cSDag-Erling Smørgrav 	chcnt += (f_octal || f_octal_escape) ? prn_octal(p->fts_name)
2820d86878cSDag-Erling Smørgrav 	                                     : printf("%s", p->fts_name);
28374985094SJosef Karthauser #ifdef COLORLS
284cf0feaeeSAndrey A. Chernov 	if (f_color && color_printed)
28538782c25SAndrey A. Chernov 		endcolor(0);
28674985094SJosef Karthauser #endif
2874b88c807SRodney W. Grimes 	if (f_type)
2884b88c807SRodney W. Grimes 		chcnt += printtype(sp->st_mode);
2894b88c807SRodney W. Grimes 	return (chcnt);
2904b88c807SRodney W. Grimes }
2914b88c807SRodney W. Grimes 
2924b88c807SRodney W. Grimes static void
2934b88c807SRodney W. Grimes printtime(ftime)
2944b88c807SRodney W. Grimes 	time_t ftime;
2954b88c807SRodney W. Grimes {
296fc4a9bafSAndrey A. Chernov 	char longstring[80];
297f173abd0SMike Pritchard 	static time_t now;
29897e4e97bSJosef Karthauser 	const char *format;
299f173abd0SMike Pritchard 
300f173abd0SMike Pritchard 	if (now == 0)
301f173abd0SMike Pritchard 		now = time(NULL);
3024b88c807SRodney W. Grimes 
303656dcd43SGarrett Wollman #define	SIXMONTHS	((365 / 2) * 86400)
3044b88c807SRodney W. Grimes 	if (f_sectime)
30597e4e97bSJosef Karthauser 		/* Mmm dd hh:mm:ss yyyy */
30697e4e97bSJosef Karthauser 		format = "%b %e %T %Y ";
307f173abd0SMike Pritchard 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
30897e4e97bSJosef Karthauser 		/* Mmm dd hh:mm */
30997e4e97bSJosef Karthauser 		format = "%b %e %R ";
31097e4e97bSJosef Karthauser 	else
31197e4e97bSJosef Karthauser 		/* Mmm dd yyyy */
31297e4e97bSJosef Karthauser 		format = "%b %e %Y ";
31397e4e97bSJosef Karthauser 	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
31497e4e97bSJosef Karthauser 	fputs(longstring, stdout);
3154b88c807SRodney W. Grimes }
3164b88c807SRodney W. Grimes 
3174b88c807SRodney W. Grimes static int
3184b88c807SRodney W. Grimes printtype(mode)
3194b88c807SRodney W. Grimes 	u_int mode;
3204b88c807SRodney W. Grimes {
3214b88c807SRodney W. Grimes 	switch (mode & S_IFMT) {
3224b88c807SRodney W. Grimes 	case S_IFDIR:
3234b88c807SRodney W. Grimes 		(void)putchar('/');
3244b88c807SRodney W. Grimes 		return (1);
3254b88c807SRodney W. Grimes 	case S_IFIFO:
3264b88c807SRodney W. Grimes 		(void)putchar('|');
3274b88c807SRodney W. Grimes 		return (1);
3284b88c807SRodney W. Grimes 	case S_IFLNK:
3294b88c807SRodney W. Grimes 		(void)putchar('@');
3304b88c807SRodney W. Grimes 		return (1);
3314b88c807SRodney W. Grimes 	case S_IFSOCK:
3324b88c807SRodney W. Grimes 		(void)putchar('=');
3334b88c807SRodney W. Grimes 		return (1);
334fb5cb208SSteve Price 	case S_IFWHT:
335fb5cb208SSteve Price 		(void)putchar('%');
336fb5cb208SSteve Price 		return (1);
3374b88c807SRodney W. Grimes 	}
3384b88c807SRodney W. Grimes 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
3394b88c807SRodney W. Grimes 		(void)putchar('*');
3404b88c807SRodney W. Grimes 		return (1);
3414b88c807SRodney W. Grimes 	}
3424b88c807SRodney W. Grimes 	return (0);
3434b88c807SRodney W. Grimes }
3444b88c807SRodney W. Grimes 
34574985094SJosef Karthauser #ifdef COLORLS
3461bf1478aSAndrey A. Chernov static int
3471bf1478aSAndrey A. Chernov putch(c)
348cf0feaeeSAndrey A. Chernov 	int c;
349cf0feaeeSAndrey A. Chernov {
35038782c25SAndrey A. Chernov 	(void) putchar(c);
35138782c25SAndrey A. Chernov 	return 0;
352cf0feaeeSAndrey A. Chernov }
353cf0feaeeSAndrey A. Chernov 
3541bf1478aSAndrey A. Chernov static int
3551bf1478aSAndrey A. Chernov writech(c)
35638782c25SAndrey A. Chernov 	int c;
35738782c25SAndrey A. Chernov {
35838782c25SAndrey A. Chernov 	char tmp = c;
35938782c25SAndrey A. Chernov 
36038782c25SAndrey A. Chernov 	(void) write(STDOUT_FILENO, &tmp, 1);
36138782c25SAndrey A. Chernov 	return 0;
36238782c25SAndrey A. Chernov }
363cf0feaeeSAndrey A. Chernov 
3641bf1478aSAndrey A. Chernov static void
3653885812cSJosef Karthauser printcolor(c)
3663885812cSJosef Karthauser        Colors c;
3673885812cSJosef Karthauser {
36874985094SJosef Karthauser 	char *ansiseq;
36974985094SJosef Karthauser 
3703885812cSJosef Karthauser 	if (colors[c][0] != -1) {
371c85e2c8eSAndrey A. Chernov 		ansiseq = tgoto(ansi_fgcol, 0, colors[c][0]);
37238782c25SAndrey A. Chernov 		if (ansiseq)
373cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
3743885812cSJosef Karthauser 	}
37574985094SJosef Karthauser 
37674985094SJosef Karthauser 	if (colors[c][1] != -1) {
377c85e2c8eSAndrey A. Chernov 		ansiseq = tgoto(ansi_bgcol, 0, colors[c][1]);
37838782c25SAndrey A. Chernov 		if (ansiseq)
379cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
38074985094SJosef Karthauser 	}
38174985094SJosef Karthauser }
38274985094SJosef Karthauser 
38338782c25SAndrey A. Chernov static void
38438782c25SAndrey A. Chernov endcolor(sig)
38538782c25SAndrey A. Chernov 	int sig;
38674985094SJosef Karthauser {
38738782c25SAndrey A. Chernov 	tputs(ansi_coloff, 1, sig ? writech : putch);
3883885812cSJosef Karthauser }
3893885812cSJosef Karthauser 
39038782c25SAndrey A. Chernov static int
3913885812cSJosef Karthauser colortype(mode)
3923885812cSJosef Karthauser        mode_t mode;
3933885812cSJosef Karthauser {
3943885812cSJosef Karthauser 	switch(mode & S_IFMT) {
3953885812cSJosef Karthauser 	      case S_IFDIR:
3963885812cSJosef Karthauser 		if (mode & S_IWOTH)
3973885812cSJosef Karthauser 		    if (mode & S_ISTXT)
3983885812cSJosef Karthauser 			printcolor(C_WSDIR);
3993885812cSJosef Karthauser 		    else
4003885812cSJosef Karthauser 			printcolor(C_WDIR);
4013885812cSJosef Karthauser 		else
4023885812cSJosef Karthauser 		    printcolor(C_DIR);
4033885812cSJosef Karthauser 		return(1);
4043885812cSJosef Karthauser 	      case S_IFLNK:
4053885812cSJosef Karthauser 		printcolor(C_LNK);
4063885812cSJosef Karthauser 		return(1);
4073885812cSJosef Karthauser 	      case S_IFSOCK:
4083885812cSJosef Karthauser 		printcolor(C_SOCK);
4093885812cSJosef Karthauser 		return(1);
4103885812cSJosef Karthauser 	      case S_IFIFO:
4113885812cSJosef Karthauser 		printcolor(C_FIFO);
4123885812cSJosef Karthauser 		return(1);
4133885812cSJosef Karthauser 	      case S_IFBLK:
4143885812cSJosef Karthauser 		printcolor(C_BLK);
4153885812cSJosef Karthauser 		return(1);
4163885812cSJosef Karthauser 	      case S_IFCHR:
4173885812cSJosef Karthauser 		printcolor(C_CHR);
4183885812cSJosef Karthauser 		return(1);
4193885812cSJosef Karthauser 	}
4203885812cSJosef Karthauser 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4213885812cSJosef Karthauser 		if (mode & S_ISUID)
4223885812cSJosef Karthauser 		    printcolor(C_SUID);
4233885812cSJosef Karthauser 		else if (mode & S_ISGID)
4243885812cSJosef Karthauser 		    printcolor(C_SGID);
4253885812cSJosef Karthauser 		else
4263885812cSJosef Karthauser 		    printcolor(C_EXEC);
4273885812cSJosef Karthauser 		return(1);
4283885812cSJosef Karthauser 	}
4293885812cSJosef Karthauser 	return(0);
4303885812cSJosef Karthauser }
4313885812cSJosef Karthauser 
4323885812cSJosef Karthauser void
4333885812cSJosef Karthauser parsecolors(cs)
4343885812cSJosef Karthauser char *cs;
4353885812cSJosef Karthauser {
4363885812cSJosef Karthauser 	int i, j, len;
4373885812cSJosef Karthauser 	char c[2];
43838782c25SAndrey A. Chernov 
4393885812cSJosef Karthauser 	if (cs == NULL)    cs = ""; /* LSCOLORS not set */
4403885812cSJosef Karthauser 	len = strlen(cs);
4413885812cSJosef Karthauser 	for (i = 0 ; i < C_NUMCOLORS ; i++) {
4423885812cSJosef Karthauser 		if (len <= 2*i) {
4433885812cSJosef Karthauser 			c[0] = defcolors[2*i];
4443885812cSJosef Karthauser 			c[1] = defcolors[2*i+1];
4453885812cSJosef Karthauser 		}
4463885812cSJosef Karthauser 		else {
4473885812cSJosef Karthauser 			c[0] = cs[2*i];
4483885812cSJosef Karthauser 			c[1] = cs[2*i+1];
4493885812cSJosef Karthauser 		}
4503885812cSJosef Karthauser 		for (j = 0 ; j < 2 ; j++) {
4513885812cSJosef Karthauser 			if ((c[j] < '0' || c[j] > '7') &&
452cf0feaeeSAndrey A. Chernov 			    tolower((unsigned char)c[j]) != 'x') {
4533885812cSJosef Karthauser 				fprintf(stderr,
4543885812cSJosef Karthauser 					"error: invalid character '%c' in LSCOLORS env var\n",
4553885812cSJosef Karthauser 					c[j]);
4563885812cSJosef Karthauser 				c[j] = defcolors[2*i+j];
4573885812cSJosef Karthauser 			}
45838782c25SAndrey A. Chernov 			if (tolower((unsigned char)c[j]) == 'x')
4593885812cSJosef Karthauser 			    colors[i][j] = -1;
4603885812cSJosef Karthauser 			else
4613885812cSJosef Karthauser 			    colors[i][j] = c[j]-'0';
4623885812cSJosef Karthauser 		}
4633885812cSJosef Karthauser 	}
4643885812cSJosef Karthauser }
465cf0feaeeSAndrey A. Chernov 
4661bf1478aSAndrey A. Chernov void
4671bf1478aSAndrey A. Chernov colorquit(sig)
468cf0feaeeSAndrey A. Chernov 	int sig;
469cf0feaeeSAndrey A. Chernov {
47038782c25SAndrey A. Chernov 	endcolor(sig);
471faebfe2eSAndrey A. Chernov 
472faebfe2eSAndrey A. Chernov 	(void) signal(sig, SIG_DFL);
473faebfe2eSAndrey A. Chernov 	(void) kill(getpid(), sig);
474cf0feaeeSAndrey A. Chernov }
47574985094SJosef Karthauser #endif /*COLORLS*/
4763885812cSJosef Karthauser 
4774b88c807SRodney W. Grimes static void
4784b88c807SRodney W. Grimes printlink(p)
4794b88c807SRodney W. Grimes 	FTSENT *p;
4804b88c807SRodney W. Grimes {
4814b88c807SRodney W. Grimes 	int lnklen;
4824b88c807SRodney W. Grimes 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
4834b88c807SRodney W. Grimes 
4844b88c807SRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL)
4854b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
4864b88c807SRodney W. Grimes 	else
4874b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name),
4884b88c807SRodney W. Grimes 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
4894b88c807SRodney W. Grimes 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
4904b88c807SRodney W. Grimes 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
4914b88c807SRodney W. Grimes 		return;
4924b88c807SRodney W. Grimes 	}
4934b88c807SRodney W. Grimes 	path[lnklen] = '\0';
4940d86878cSDag-Erling Smørgrav 	if (f_octal || f_octal_escape) {
4957ea30648SDag-Erling Smørgrav 	        (void)printf(" -> ");
4967ea30648SDag-Erling Smørgrav 	        (void)prn_octal(path);
4977ea30648SDag-Erling Smørgrav 	}
4987ea30648SDag-Erling Smørgrav 	else (void)printf(" -> %s", path);
4994b88c807SRodney W. Grimes }
500