xref: /freebsd/bin/ls/print.c (revision 47bb6b11f3e1619ca95afc191d60aa01de629c8e)
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 
4974985094SJosef Karthauser #ifdef COLORLS
5074985094SJosef Karthauser #include <ctype.h>
51cf0feaeeSAndrey A. Chernov #include <termcap.h>
52cf0feaeeSAndrey A. Chernov #include <term.h>       /* for tparm */
5374985094SJosef Karthauser #endif
544b88c807SRodney W. Grimes #include <err.h>
554b88c807SRodney W. Grimes #include <errno.h>
564b88c807SRodney W. Grimes #include <fts.h>
574b88c807SRodney W. Grimes #include <grp.h>
584b88c807SRodney W. Grimes #include <pwd.h>
594b88c807SRodney W. Grimes #include <stdio.h>
604b88c807SRodney W. Grimes #include <stdlib.h>
614b88c807SRodney W. Grimes #include <string.h>
624b88c807SRodney W. Grimes #include <time.h>
634b88c807SRodney W. Grimes #include <unistd.h>
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));
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
744b88c807SRodney W. Grimes 
7574985094SJosef Karthauser #ifdef COLORLS
763885812cSJosef Karthauser /* Most of these are taken from <sys/stat.h> */
773885812cSJosef Karthauser typedef enum Colors {
783885812cSJosef Karthauser     C_DIR,     /* directory */
793885812cSJosef Karthauser     C_LNK,     /* symbolic link */
803885812cSJosef Karthauser     C_SOCK,    /* socket */
813885812cSJosef Karthauser     C_FIFO,    /* pipe */
823885812cSJosef Karthauser     C_EXEC,    /* executable */
833885812cSJosef Karthauser     C_BLK,     /* block special */
843885812cSJosef Karthauser     C_CHR,     /* character special */
853885812cSJosef Karthauser     C_SUID,    /* setuid executable */
863885812cSJosef Karthauser     C_SGID,    /* setgid executable */
873885812cSJosef Karthauser     C_WSDIR,   /* directory writeble to others, with sticky bit */
883885812cSJosef Karthauser     C_WDIR,    /* directory writeble to others, without sticky bit */
893885812cSJosef Karthauser     C_NUMCOLORS        /* just a place-holder */
903885812cSJosef Karthauser } Colors ;
913885812cSJosef Karthauser 
923885812cSJosef Karthauser char *defcolors = "4x5x2x3x1x464301060203";
933885812cSJosef Karthauser 
943885812cSJosef Karthauser static int colors[C_NUMCOLORS][2];
9574985094SJosef Karthauser #endif
963885812cSJosef Karthauser 
974b88c807SRodney W. Grimes void
984b88c807SRodney W. Grimes printscol(dp)
994b88c807SRodney W. Grimes 	DISPLAY *dp;
1004b88c807SRodney W. Grimes {
1014b88c807SRodney W. Grimes 	FTSENT *p;
1024b88c807SRodney W. Grimes 
1034b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1044b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1054b88c807SRodney W. Grimes 			continue;
1064b88c807SRodney W. Grimes 		(void)printaname(p, dp->s_inode, dp->s_block);
1074b88c807SRodney W. Grimes 		(void)putchar('\n');
1084b88c807SRodney W. Grimes 	}
1094b88c807SRodney W. Grimes }
1104b88c807SRodney W. Grimes 
1114b88c807SRodney W. Grimes void
1124b88c807SRodney W. Grimes printlong(dp)
1134b88c807SRodney W. Grimes 	DISPLAY *dp;
1144b88c807SRodney W. Grimes {
1154b88c807SRodney W. Grimes 	struct stat *sp;
1164b88c807SRodney W. Grimes 	FTSENT *p;
1174b88c807SRodney W. Grimes 	NAMES *np;
1184b88c807SRodney W. Grimes 	char buf[20];
11947bb6b11SAndrey A. Chernov #ifdef COLORLS
12047bb6b11SAndrey A. Chernov 	int color_printed = 0;
12147bb6b11SAndrey A. Chernov #endif
1224b88c807SRodney W. Grimes 
1234b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1244b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1254b88c807SRodney W. Grimes 
1264b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1274b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1284b88c807SRodney W. Grimes 			continue;
1294b88c807SRodney W. Grimes 		sp = p->fts_statp;
1304b88c807SRodney W. Grimes 		if (f_inode)
131fb5cb208SSteve Price 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1324b88c807SRodney W. Grimes 		if (f_size)
1334b88c807SRodney W. Grimes 			(void)printf("%*qd ",
1344b88c807SRodney W. Grimes 			    dp->s_block, howmany(sp->st_blocks, blocksize));
1354b88c807SRodney W. Grimes 		(void)strmode(sp->st_mode, buf);
1364b88c807SRodney W. Grimes 		np = p->fts_pointer;
1374b88c807SRodney W. Grimes 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1384b88c807SRodney W. Grimes 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1394b88c807SRodney W. Grimes 		    np->group);
1404b88c807SRodney W. Grimes 		if (f_flags)
1414b88c807SRodney W. Grimes 			(void)printf("%-*s ", dp->s_flags, np->flags);
1424b88c807SRodney W. Grimes 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
143029b2bd0SBruce Evans 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
144df2fbf15SJoerg Wunsch 				(void)printf("%3d, 0x%08x ",
145029b2bd0SBruce Evans 				    major(sp->st_rdev),
146029b2bd0SBruce Evans 				    (u_int)minor(sp->st_rdev));
147df2fbf15SJoerg Wunsch 			else
1484b88c807SRodney W. Grimes 				(void)printf("%3d, %3d ",
1494b88c807SRodney W. Grimes 				    major(sp->st_rdev), minor(sp->st_rdev));
1504b88c807SRodney W. Grimes 		else if (dp->bcfile)
1514b88c807SRodney W. Grimes 			(void)printf("%*s%*qd ",
1524b88c807SRodney W. Grimes 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1534b88c807SRodney W. Grimes 		else
1544b88c807SRodney W. Grimes 			(void)printf("%*qd ", dp->s_size, sp->st_size);
1554b88c807SRodney W. Grimes 		if (f_accesstime)
1564b88c807SRodney W. Grimes 			printtime(sp->st_atime);
1574b88c807SRodney W. Grimes 		else if (f_statustime)
1584b88c807SRodney W. Grimes 			printtime(sp->st_ctime);
1594b88c807SRodney W. Grimes 		else
1604b88c807SRodney W. Grimes 			printtime(sp->st_mtime);
16174985094SJosef Karthauser #ifdef COLORLS
1623885812cSJosef Karthauser 		if (f_color)
163cf0feaeeSAndrey A. Chernov 			color_printed = colortype(sp->st_mode);
16474985094SJosef Karthauser #endif
1650d86878cSDag-Erling Smørgrav 		if (f_octal || f_octal_escape) (void)prn_octal(p->fts_name);
1667ea30648SDag-Erling Smørgrav 		else (void)printf("%s", p->fts_name);
16774985094SJosef Karthauser #ifdef COLORLS
168cf0feaeeSAndrey A. Chernov 		if (f_color && color_printed)
16974985094SJosef Karthauser 			endcolor();
17074985094SJosef Karthauser #endif
1714b88c807SRodney W. Grimes 		if (f_type)
1724b88c807SRodney W. Grimes 			(void)printtype(sp->st_mode);
1734b88c807SRodney W. Grimes 		if (S_ISLNK(sp->st_mode))
1744b88c807SRodney W. Grimes 			printlink(p);
1754b88c807SRodney W. Grimes 		(void)putchar('\n');
1764b88c807SRodney W. Grimes 	}
1774b88c807SRodney W. Grimes }
1784b88c807SRodney W. Grimes 
1794b88c807SRodney W. Grimes void
1804b88c807SRodney W. Grimes printcol(dp)
1814b88c807SRodney W. Grimes 	DISPLAY *dp;
1824b88c807SRodney W. Grimes {
1834b88c807SRodney W. Grimes 	extern int termwidth;
1844b88c807SRodney W. Grimes 	static FTSENT **array;
1854b88c807SRodney W. Grimes 	static int lastentries = -1;
1864b88c807SRodney W. Grimes 	FTSENT *p;
1874b88c807SRodney W. Grimes 	int base, chcnt, cnt, col, colwidth, num;
1884b88c807SRodney W. Grimes 	int endcol, numcols, numrows, row;
189545f583cSTim Vanderhoek 	int tabwidth;
190545f583cSTim Vanderhoek 
191545f583cSTim Vanderhoek 	if (f_notabs)
192545f583cSTim Vanderhoek 		tabwidth = 1;
193545f583cSTim Vanderhoek 	else
194545f583cSTim Vanderhoek 		tabwidth = 8;
1954b88c807SRodney W. Grimes 
1964b88c807SRodney W. Grimes 	/*
1974b88c807SRodney W. Grimes 	 * Have to do random access in the linked list -- build a table
1984b88c807SRodney W. Grimes 	 * of pointers.
1994b88c807SRodney W. Grimes 	 */
2004b88c807SRodney W. Grimes 	if (dp->entries > lastentries) {
2014b88c807SRodney W. Grimes 		lastentries = dp->entries;
2024b88c807SRodney W. Grimes 		if ((array =
2034b88c807SRodney W. Grimes 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
2044b88c807SRodney W. Grimes 			warn(NULL);
2054b88c807SRodney W. Grimes 			printscol(dp);
2064b88c807SRodney W. Grimes 		}
2074b88c807SRodney W. Grimes 	}
2084b88c807SRodney W. Grimes 	for (p = dp->list, num = 0; p; p = p->fts_link)
2094b88c807SRodney W. Grimes 		if (p->fts_number != NO_PRINT)
2104b88c807SRodney W. Grimes 			array[num++] = p;
2114b88c807SRodney W. Grimes 
2124b88c807SRodney W. Grimes 	colwidth = dp->maxlen;
2134b88c807SRodney W. Grimes 	if (f_inode)
2144b88c807SRodney W. Grimes 		colwidth += dp->s_inode + 1;
2154b88c807SRodney W. Grimes 	if (f_size)
2164b88c807SRodney W. Grimes 		colwidth += dp->s_block + 1;
2174b88c807SRodney W. Grimes 	if (f_type)
2184b88c807SRodney W. Grimes 		colwidth += 1;
2194b88c807SRodney W. Grimes 
220545f583cSTim Vanderhoek 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
2214b88c807SRodney W. Grimes 	if (termwidth < 2 * colwidth) {
2224b88c807SRodney W. Grimes 		printscol(dp);
2234b88c807SRodney W. Grimes 		return;
2244b88c807SRodney W. Grimes 	}
2254b88c807SRodney W. Grimes 
2264b88c807SRodney W. Grimes 	numcols = termwidth / colwidth;
2274b88c807SRodney W. Grimes 	numrows = num / numcols;
2284b88c807SRodney W. Grimes 	if (num % numcols)
2294b88c807SRodney W. Grimes 		++numrows;
2304b88c807SRodney W. Grimes 
2314b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
2324b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
2334b88c807SRodney W. Grimes 	for (row = 0; row < numrows; ++row) {
2344b88c807SRodney W. Grimes 		endcol = colwidth;
2354b88c807SRodney W. Grimes 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
2364b88c807SRodney W. Grimes 			chcnt += printaname(array[base], dp->s_inode,
2374b88c807SRodney W. Grimes 			    dp->s_block);
2384b88c807SRodney W. Grimes 			if ((base += numrows) >= num)
2394b88c807SRodney W. Grimes 				break;
24074985094SJosef Karthauser #ifdef COLORLS
2413885812cSJosef Karthauser 			/*
2423885812cSJosef Karthauser 			 * some terminals get confused if we mix tabs
2433885812cSJosef Karthauser 			 * with color sequences
2443885812cSJosef Karthauser 			 */
2453885812cSJosef Karthauser 			if (f_color)
2463885812cSJosef Karthauser 				while ((cnt = (chcnt + 1)) <= endcol) {
2473885812cSJosef Karthauser 					(void)putchar(' ');
2483885812cSJosef Karthauser 					chcnt = cnt;
2493885812cSJosef Karthauser 				}
2503885812cSJosef Karthauser 			else
25174985094SJosef Karthauser #endif
252545f583cSTim Vanderhoek 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
253545f583cSTim Vanderhoek 			    <= endcol){
254545f583cSTim Vanderhoek 				(void)putchar(f_notabs ? ' ' : '\t');
2554b88c807SRodney W. Grimes 				chcnt = cnt;
2564b88c807SRodney W. Grimes 			}
2574b88c807SRodney W. Grimes 			endcol += colwidth;
2584b88c807SRodney W. Grimes 		}
2594b88c807SRodney W. Grimes 		(void)putchar('\n');
2604b88c807SRodney W. Grimes 	}
2614b88c807SRodney W. Grimes }
2624b88c807SRodney W. Grimes 
2634b88c807SRodney W. Grimes /*
2644b88c807SRodney W. Grimes  * print [inode] [size] name
2654b88c807SRodney W. Grimes  * return # of characters printed, no trailing characters.
2664b88c807SRodney W. Grimes  */
2674b88c807SRodney W. Grimes static int
2684b88c807SRodney W. Grimes printaname(p, inodefield, sizefield)
2694b88c807SRodney W. Grimes 	FTSENT *p;
2704b88c807SRodney W. Grimes 	u_long sizefield, inodefield;
2714b88c807SRodney W. Grimes {
2724b88c807SRodney W. Grimes 	struct stat *sp;
2734b88c807SRodney W. Grimes 	int chcnt;
27447bb6b11SAndrey A. Chernov #ifdef COLORLS
27547bb6b11SAndrey A. Chernov 	int color_printed = 0;
27647bb6b11SAndrey A. Chernov #endif
2774b88c807SRodney W. Grimes 
2784b88c807SRodney W. Grimes 	sp = p->fts_statp;
2794b88c807SRodney W. Grimes 	chcnt = 0;
2804b88c807SRodney W. Grimes 	if (f_inode)
281fb5cb208SSteve Price 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
2824b88c807SRodney W. Grimes 	if (f_size)
2834b88c807SRodney W. Grimes 		chcnt += printf("%*qd ",
2844b88c807SRodney W. Grimes 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
28574985094SJosef Karthauser #ifdef COLORLS
2863885812cSJosef Karthauser 	if (f_color)
287cf0feaeeSAndrey A. Chernov 		color_printed = colortype(sp->st_mode);
28874985094SJosef Karthauser #endif
2890d86878cSDag-Erling Smørgrav 	chcnt += (f_octal || f_octal_escape) ? prn_octal(p->fts_name)
2900d86878cSDag-Erling Smørgrav 	                                     : printf("%s", p->fts_name);
29174985094SJosef Karthauser #ifdef COLORLS
292cf0feaeeSAndrey A. Chernov 	if (f_color && color_printed)
29374985094SJosef Karthauser 		endcolor();
29474985094SJosef Karthauser #endif
2954b88c807SRodney W. Grimes 	if (f_type)
2964b88c807SRodney W. Grimes 		chcnt += printtype(sp->st_mode);
2974b88c807SRodney W. Grimes 	return (chcnt);
2984b88c807SRodney W. Grimes }
2994b88c807SRodney W. Grimes 
3004b88c807SRodney W. Grimes static void
3014b88c807SRodney W. Grimes printtime(ftime)
3024b88c807SRodney W. Grimes 	time_t ftime;
3034b88c807SRodney W. Grimes {
3044b88c807SRodney W. Grimes 	int i;
305fc4a9bafSAndrey A. Chernov 	char longstring[80];
306f173abd0SMike Pritchard 	static time_t now;
307f173abd0SMike Pritchard 
308f173abd0SMike Pritchard 	if (now == 0)
309f173abd0SMike Pritchard 		now = time(NULL);
3104b88c807SRodney W. Grimes 
311fc4a9bafSAndrey A. Chernov 	strftime(longstring, sizeof(longstring), "%c", localtime(&ftime));
3124b88c807SRodney W. Grimes 	for (i = 4; i < 11; ++i)
3134b88c807SRodney W. Grimes 		(void)putchar(longstring[i]);
3144b88c807SRodney W. Grimes 
315656dcd43SGarrett Wollman #define	SIXMONTHS	((365 / 2) * 86400)
3164b88c807SRodney W. Grimes 	if (f_sectime)
3174b88c807SRodney W. Grimes 		for (i = 11; i < 24; i++)
3184b88c807SRodney W. Grimes 			(void)putchar(longstring[i]);
319f173abd0SMike Pritchard 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
3204b88c807SRodney W. Grimes 		for (i = 11; i < 16; ++i)
3214b88c807SRodney W. Grimes 			(void)putchar(longstring[i]);
3224b88c807SRodney W. Grimes 	else {
3234b88c807SRodney W. Grimes 		(void)putchar(' ');
3244b88c807SRodney W. Grimes 		for (i = 20; i < 24; ++i)
3254b88c807SRodney W. Grimes 			(void)putchar(longstring[i]);
3264b88c807SRodney W. Grimes 	}
3274b88c807SRodney W. Grimes 	(void)putchar(' ');
3284b88c807SRodney W. Grimes }
3294b88c807SRodney W. Grimes 
3304b88c807SRodney W. Grimes static int
3314b88c807SRodney W. Grimes printtype(mode)
3324b88c807SRodney W. Grimes 	u_int mode;
3334b88c807SRodney W. Grimes {
3344b88c807SRodney W. Grimes 	switch (mode & S_IFMT) {
3354b88c807SRodney W. Grimes 	case S_IFDIR:
3364b88c807SRodney W. Grimes 		(void)putchar('/');
3374b88c807SRodney W. Grimes 		return (1);
3384b88c807SRodney W. Grimes 	case S_IFIFO:
3394b88c807SRodney W. Grimes 		(void)putchar('|');
3404b88c807SRodney W. Grimes 		return (1);
3414b88c807SRodney W. Grimes 	case S_IFLNK:
3424b88c807SRodney W. Grimes 		(void)putchar('@');
3434b88c807SRodney W. Grimes 		return (1);
3444b88c807SRodney W. Grimes 	case S_IFSOCK:
3454b88c807SRodney W. Grimes 		(void)putchar('=');
3464b88c807SRodney W. Grimes 		return (1);
347fb5cb208SSteve Price 	case S_IFWHT:
348fb5cb208SSteve Price 		(void)putchar('%');
349fb5cb208SSteve Price 		return (1);
3504b88c807SRodney W. Grimes 	}
3514b88c807SRodney W. Grimes 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
3524b88c807SRodney W. Grimes 		(void)putchar('*');
3534b88c807SRodney W. Grimes 		return (1);
3544b88c807SRodney W. Grimes 	}
3554b88c807SRodney W. Grimes 	return (0);
3564b88c807SRodney W. Grimes }
3574b88c807SRodney W. Grimes 
35874985094SJosef Karthauser #ifdef COLORLS
359cf0feaeeSAndrey A. Chernov int putch(c)
360cf0feaeeSAndrey A. Chernov 	int c;
361cf0feaeeSAndrey A. Chernov {
362cf0feaeeSAndrey A. Chernov 	return putc(c, stdout);
363cf0feaeeSAndrey A. Chernov }
364cf0feaeeSAndrey A. Chernov 
365cf0feaeeSAndrey A. Chernov 
3663885812cSJosef Karthauser void
3673885812cSJosef Karthauser printcolor(c)
3683885812cSJosef Karthauser        Colors c;
3693885812cSJosef Karthauser {
37074985094SJosef Karthauser 	char *ansiseq;
37174985094SJosef Karthauser 
3723885812cSJosef Karthauser 	if (colors[c][0] != -1) {
3735a890e22SJosef Karthauser 		ansiseq = tparm(ansi_fgcol, colors[c][0]);
37474985094SJosef Karthauser 		if (ansiseq)
375cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
3763885812cSJosef Karthauser 	}
37774985094SJosef Karthauser 
37874985094SJosef Karthauser 	if (colors[c][1] != -1) {
3795a890e22SJosef Karthauser 		ansiseq = tparm(ansi_bgcol, colors[c][1]);
38074985094SJosef Karthauser 		if (ansiseq)
381cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
38274985094SJosef Karthauser 	}
38374985094SJosef Karthauser }
38474985094SJosef Karthauser 
38574985094SJosef Karthauser void
38674985094SJosef Karthauser endcolor()
38774985094SJosef Karthauser {
388cf0feaeeSAndrey A. Chernov 	tputs(ansi_coloff, 1, putch);
3893885812cSJosef Karthauser }
3903885812cSJosef Karthauser 
3913885812cSJosef Karthauser int
3923885812cSJosef Karthauser colortype(mode)
3933885812cSJosef Karthauser        mode_t mode;
3943885812cSJosef Karthauser {
3953885812cSJosef Karthauser 	switch(mode & S_IFMT) {
3963885812cSJosef Karthauser 	      case S_IFDIR:
3973885812cSJosef Karthauser 		if (mode & S_IWOTH)
3983885812cSJosef Karthauser 		    if (mode & S_ISTXT)
3993885812cSJosef Karthauser 			printcolor(C_WSDIR);
4003885812cSJosef Karthauser 		    else
4013885812cSJosef Karthauser 			printcolor(C_WDIR);
4023885812cSJosef Karthauser 		else
4033885812cSJosef Karthauser 		    printcolor(C_DIR);
4043885812cSJosef Karthauser 		return(1);
4053885812cSJosef Karthauser 	      case S_IFLNK:
4063885812cSJosef Karthauser 		printcolor(C_LNK);
4073885812cSJosef Karthauser 		return(1);
4083885812cSJosef Karthauser 	      case S_IFSOCK:
4093885812cSJosef Karthauser 		printcolor(C_SOCK);
4103885812cSJosef Karthauser 		return(1);
4113885812cSJosef Karthauser 	      case S_IFIFO:
4123885812cSJosef Karthauser 		printcolor(C_FIFO);
4133885812cSJosef Karthauser 		return(1);
4143885812cSJosef Karthauser 	      case S_IFBLK:
4153885812cSJosef Karthauser 		printcolor(C_BLK);
4163885812cSJosef Karthauser 		return(1);
4173885812cSJosef Karthauser 	      case S_IFCHR:
4183885812cSJosef Karthauser 		printcolor(C_CHR);
4193885812cSJosef Karthauser 		return(1);
4203885812cSJosef Karthauser 	}
4213885812cSJosef Karthauser 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4223885812cSJosef Karthauser 		if (mode & S_ISUID)
4233885812cSJosef Karthauser 		    printcolor(C_SUID);
4243885812cSJosef Karthauser 		else if (mode & S_ISGID)
4253885812cSJosef Karthauser 		    printcolor(C_SGID);
4263885812cSJosef Karthauser 		else
4273885812cSJosef Karthauser 		    printcolor(C_EXEC);
4283885812cSJosef Karthauser 		return(1);
4293885812cSJosef Karthauser 	}
4303885812cSJosef Karthauser 	return(0);
4313885812cSJosef Karthauser }
4323885812cSJosef Karthauser 
4333885812cSJosef Karthauser void
4343885812cSJosef Karthauser parsecolors(cs)
4353885812cSJosef Karthauser char *cs;
4363885812cSJosef Karthauser {
4373885812cSJosef Karthauser 	int i, j, len;
4383885812cSJosef Karthauser 	char c[2];
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 			}
4583885812cSJosef Karthauser 			if (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 
466cf0feaeeSAndrey A. Chernov /* ARGSUSED */
467cf0feaeeSAndrey A. Chernov void colorquit(sig)
468cf0feaeeSAndrey A. Chernov 	int sig;
469cf0feaeeSAndrey A. Chernov {
470cf0feaeeSAndrey A. Chernov 	endcolor();
471cf0feaeeSAndrey A. Chernov 	exit(1);
472cf0feaeeSAndrey A. Chernov }
47374985094SJosef Karthauser #endif /*COLORLS*/
4743885812cSJosef Karthauser 
4754b88c807SRodney W. Grimes static void
4764b88c807SRodney W. Grimes printlink(p)
4774b88c807SRodney W. Grimes 	FTSENT *p;
4784b88c807SRodney W. Grimes {
4794b88c807SRodney W. Grimes 	int lnklen;
4804b88c807SRodney W. Grimes 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
4814b88c807SRodney W. Grimes 
4824b88c807SRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL)
4834b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
4844b88c807SRodney W. Grimes 	else
4854b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name),
4864b88c807SRodney W. Grimes 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
4874b88c807SRodney W. Grimes 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
4884b88c807SRodney W. Grimes 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
4894b88c807SRodney W. Grimes 		return;
4904b88c807SRodney W. Grimes 	}
4914b88c807SRodney W. Grimes 	path[lnklen] = '\0';
4920d86878cSDag-Erling Smørgrav 	if (f_octal || f_octal_escape) {
4937ea30648SDag-Erling Smørgrav 	        (void)printf(" -> ");
4947ea30648SDag-Erling Smørgrav 	        (void)prn_octal(path);
4957ea30648SDag-Erling Smørgrav 	}
4967ea30648SDag-Erling Smørgrav 	else (void)printf(" -> %s", path);
4974b88c807SRodney W. Grimes }
498