xref: /freebsd/bin/ls/print.c (revision b0bc91e955fec541d6566cfe03fce9df97fa4bc0)
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>
5328fd017aSAndrey A. Chernov #include <langinfo.h>
544b88c807SRodney W. Grimes #include <pwd.h>
554b88c807SRodney W. Grimes #include <stdio.h>
564b88c807SRodney W. Grimes #include <stdlib.h>
574b88c807SRodney W. Grimes #include <string.h>
584b88c807SRodney W. Grimes #include <time.h>
594b88c807SRodney W. Grimes #include <unistd.h>
60faebfe2eSAndrey A. Chernov #ifdef COLORLS
61faebfe2eSAndrey A. Chernov #include <ctype.h>
62faebfe2eSAndrey A. Chernov #include <termcap.h>
63faebfe2eSAndrey A. Chernov #include <signal.h>
64faebfe2eSAndrey A. Chernov #endif
654b88c807SRodney W. Grimes 
664b88c807SRodney W. Grimes #include "ls.h"
674b88c807SRodney W. Grimes #include "extern.h"
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes static int	printaname __P((FTSENT *, u_long, u_long));
704b88c807SRodney W. Grimes static void	printlink __P((FTSENT *));
714b88c807SRodney W. Grimes static void	printtime __P((time_t));
724b88c807SRodney W. Grimes static int	printtype __P((u_int));
7338782c25SAndrey A. Chernov #ifdef COLORLS
7438782c25SAndrey A. Chernov static void     endcolor __P((int));
7538782c25SAndrey A. Chernov static int      colortype __P((mode_t));
7638782c25SAndrey A. Chernov #endif
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
794b88c807SRodney W. Grimes 
8074985094SJosef Karthauser #ifdef COLORLS
813885812cSJosef Karthauser /* Most of these are taken from <sys/stat.h> */
823885812cSJosef Karthauser typedef enum Colors {
833885812cSJosef Karthauser 	C_DIR,		/* directory */
843885812cSJosef Karthauser 	C_LNK,		/* symbolic link */
853885812cSJosef Karthauser 	C_SOCK,		/* socket */
863885812cSJosef Karthauser 	C_FIFO,		/* pipe */
873885812cSJosef Karthauser 	C_EXEC,		/* executable */
883885812cSJosef Karthauser 	C_BLK,		/* block special */
893885812cSJosef Karthauser 	C_CHR,		/* character special */
903885812cSJosef Karthauser 	C_SUID,		/* setuid executable */
913885812cSJosef Karthauser 	C_SGID,		/* setgid executable */
923885812cSJosef Karthauser 	C_WSDIR,	/* directory writeble to others, with sticky bit */
933885812cSJosef Karthauser 	C_WDIR,		/* directory writeble to others, without sticky bit */
943885812cSJosef Karthauser 	C_NUMCOLORS	/* just a place-holder */
953885812cSJosef Karthauser } Colors;
963885812cSJosef Karthauser 
97c1499cf6SJosef Karthauser char *defcolors = "exfxcxdxbxegedabagacad";
983885812cSJosef Karthauser 
99c1499cf6SJosef Karthauser /* colors for file types */
100c1499cf6SJosef Karthauser static struct {
101c1499cf6SJosef Karthauser 	int	num[2];
102c1499cf6SJosef Karthauser 	int	bold;
103c1499cf6SJosef Karthauser } colors[C_NUMCOLORS];
104c1499cf6SJosef Karthauser 
10574985094SJosef Karthauser #endif
1063885812cSJosef Karthauser 
1074b88c807SRodney W. Grimes void
1084b88c807SRodney W. Grimes printscol(dp)
1094b88c807SRodney W. Grimes 	DISPLAY	*dp;
1104b88c807SRodney W. Grimes {
1114b88c807SRodney W. Grimes 	FTSENT	*p;
1124b88c807SRodney W. Grimes 
1134b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1144b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1154b88c807SRodney W. Grimes 			continue;
1164b88c807SRodney W. Grimes 		(void)printaname(p, dp->s_inode, dp->s_block);
1174b88c807SRodney W. Grimes 		(void)putchar('\n');
1184b88c807SRodney W. Grimes 	}
1194b88c807SRodney W. Grimes }
1204b88c807SRodney W. Grimes 
121ee579ffbSAssar Westerlund /*
122ee579ffbSAssar Westerlund  * print name in current style
123ee579ffbSAssar Westerlund  */
124ee579ffbSAssar Westerlund static int
125ee579ffbSAssar Westerlund printname(name)
126ee579ffbSAssar Westerlund 	const char *name;
127ee579ffbSAssar Westerlund {
128ee579ffbSAssar Westerlund 	if (f_octal || f_octal_escape)
129ee579ffbSAssar Westerlund 		return prn_octal(name);
130ee579ffbSAssar Westerlund 	else if (f_nonprint)
131ee579ffbSAssar Westerlund 		return prn_printable(name);
132ee579ffbSAssar Westerlund 	else
133ee579ffbSAssar Westerlund 		return printf("%s", name);
134ee579ffbSAssar Westerlund }
135ee579ffbSAssar Westerlund 
1364b88c807SRodney W. Grimes void
1374b88c807SRodney W. Grimes printlong(dp)
1384b88c807SRodney W. Grimes 	DISPLAY *dp;
1394b88c807SRodney W. Grimes {
1404b88c807SRodney W. Grimes 	struct stat *sp;
1414b88c807SRodney W. Grimes 	FTSENT	*p;
1424b88c807SRodney W. Grimes 	NAMES	*np;
1434b88c807SRodney W. Grimes 	char	buf[20];
14447bb6b11SAndrey A. Chernov #ifdef COLORLS
14547bb6b11SAndrey A. Chernov 	int	color_printed = 0;
14647bb6b11SAndrey A. Chernov #endif
1474b88c807SRodney W. Grimes 
1484b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1494b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1504b88c807SRodney W. Grimes 
1514b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1524b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1534b88c807SRodney W. Grimes 			continue;
1544b88c807SRodney W. Grimes 		sp = p->fts_statp;
1554b88c807SRodney W. Grimes 		if (f_inode)
156fb5cb208SSteve Price 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1574b88c807SRodney W. Grimes 		if (f_size)
1584b88c807SRodney W. Grimes 			(void)printf("%*qd ",
1594b88c807SRodney W. Grimes 			    dp->s_block, howmany(sp->st_blocks, blocksize));
1604b88c807SRodney W. Grimes 		(void)strmode(sp->st_mode, buf);
1614b88c807SRodney W. Grimes 		np = p->fts_pointer;
1624b88c807SRodney W. Grimes 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1634b88c807SRodney W. Grimes 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1644b88c807SRodney W. Grimes 		    np->group);
1654b88c807SRodney W. Grimes 		if (f_flags)
1664b88c807SRodney W. Grimes 			(void)printf("%-*s ", dp->s_flags, np->flags);
1677304f61fSBrian Feldman 		if (f_lomac)
1687304f61fSBrian Feldman 			(void)printf("%-*s ", dp->s_lattr, np->lattr);
1694b88c807SRodney W. Grimes 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
170029b2bd0SBruce Evans 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
171df2fbf15SJoerg Wunsch 				(void)printf("%3d, 0x%08x ",
172029b2bd0SBruce Evans 				    major(sp->st_rdev),
173029b2bd0SBruce Evans 				    (u_int)minor(sp->st_rdev));
174df2fbf15SJoerg Wunsch 			else
1754b88c807SRodney W. Grimes 				(void)printf("%3d, %3d ",
1764b88c807SRodney W. Grimes 				    major(sp->st_rdev), minor(sp->st_rdev));
1774b88c807SRodney W. Grimes 		else if (dp->bcfile)
1784b88c807SRodney W. Grimes 			(void)printf("%*s%*qd ",
1794b88c807SRodney W. Grimes 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1804b88c807SRodney W. Grimes 		else
1814b88c807SRodney W. Grimes 			(void)printf("%*qd ", dp->s_size, sp->st_size);
1824b88c807SRodney W. Grimes 		if (f_accesstime)
1834b88c807SRodney W. Grimes 			printtime(sp->st_atime);
1844b88c807SRodney W. Grimes 		else if (f_statustime)
1854b88c807SRodney W. Grimes 			printtime(sp->st_ctime);
1864b88c807SRodney W. Grimes 		else
1874b88c807SRodney W. Grimes 			printtime(sp->st_mtime);
18874985094SJosef Karthauser #ifdef COLORLS
1893885812cSJosef Karthauser 		if (f_color)
190cf0feaeeSAndrey A. Chernov 			color_printed = colortype(sp->st_mode);
19174985094SJosef Karthauser #endif
192ee579ffbSAssar Westerlund 		(void)printname(p->fts_name);
19374985094SJosef Karthauser #ifdef COLORLS
194cf0feaeeSAndrey A. Chernov 		if (f_color && color_printed)
19538782c25SAndrey A. Chernov 			endcolor(0);
19674985094SJosef Karthauser #endif
1974b88c807SRodney W. Grimes 		if (f_type)
1984b88c807SRodney W. Grimes 			(void)printtype(sp->st_mode);
1994b88c807SRodney W. Grimes 		if (S_ISLNK(sp->st_mode))
2004b88c807SRodney W. Grimes 			printlink(p);
2014b88c807SRodney W. Grimes 		(void)putchar('\n');
2024b88c807SRodney W. Grimes 	}
2034b88c807SRodney W. Grimes }
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes void
2064b88c807SRodney W. Grimes printcol(dp)
2074b88c807SRodney W. Grimes 	DISPLAY	*dp;
2084b88c807SRodney W. Grimes {
2094b88c807SRodney W. Grimes 	extern int termwidth;
2104b88c807SRodney W. Grimes 	static FTSENT **array;
2114b88c807SRodney W. Grimes 	static int lastentries = -1;
2124b88c807SRodney W. Grimes 	FTSENT	*p;
213b0bc91e9SJosef Karthauser 	int	base;
214b0bc91e9SJosef Karthauser 	int	chcnt;
215b0bc91e9SJosef Karthauser 	int	cnt;
216b0bc91e9SJosef Karthauser 	int	col;
217b0bc91e9SJosef Karthauser 	int	colwidth;
218b0bc91e9SJosef Karthauser 	int	endcol;
219b0bc91e9SJosef Karthauser 	int	num;
220b0bc91e9SJosef Karthauser 	int	numcols;
221b0bc91e9SJosef Karthauser 	int	numrows;
222b0bc91e9SJosef Karthauser 	int	row;
223545f583cSTim Vanderhoek 	int	tabwidth;
224545f583cSTim Vanderhoek 
225545f583cSTim Vanderhoek 	if (f_notabs)
226545f583cSTim Vanderhoek 		tabwidth = 1;
227545f583cSTim Vanderhoek 	else
228545f583cSTim Vanderhoek 		tabwidth = 8;
2294b88c807SRodney W. Grimes 
2304b88c807SRodney W. Grimes 	/*
2314b88c807SRodney W. Grimes 	 * Have to do random access in the linked list -- build a table
2324b88c807SRodney W. Grimes 	 * of pointers.
2334b88c807SRodney W. Grimes 	 */
2344b88c807SRodney W. Grimes 	if (dp->entries > lastentries) {
2354b88c807SRodney W. Grimes 		lastentries = dp->entries;
2364b88c807SRodney W. Grimes 		if ((array =
2374b88c807SRodney W. Grimes 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
2384b88c807SRodney W. Grimes 			warn(NULL);
2394b88c807SRodney W. Grimes 			printscol(dp);
2404b88c807SRodney W. Grimes 		}
2414b88c807SRodney W. Grimes 	}
2424b88c807SRodney W. Grimes 	for (p = dp->list, num = 0; p; p = p->fts_link)
2434b88c807SRodney W. Grimes 		if (p->fts_number != NO_PRINT)
2444b88c807SRodney W. Grimes 			array[num++] = p;
2454b88c807SRodney W. Grimes 
2464b88c807SRodney W. Grimes 	colwidth = dp->maxlen;
2474b88c807SRodney W. Grimes 	if (f_inode)
2484b88c807SRodney W. Grimes 		colwidth += dp->s_inode + 1;
2494b88c807SRodney W. Grimes 	if (f_size)
2504b88c807SRodney W. Grimes 		colwidth += dp->s_block + 1;
2514b88c807SRodney W. Grimes 	if (f_type)
2524b88c807SRodney W. Grimes 		colwidth += 1;
2534b88c807SRodney W. Grimes 
254545f583cSTim Vanderhoek 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
2554b88c807SRodney W. Grimes 	if (termwidth < 2 * colwidth) {
2564b88c807SRodney W. Grimes 		printscol(dp);
2574b88c807SRodney W. Grimes 		return;
2584b88c807SRodney W. Grimes 	}
2594b88c807SRodney W. Grimes 
2604b88c807SRodney W. Grimes 	numcols = termwidth / colwidth;
2614b88c807SRodney W. Grimes 	numrows = num / numcols;
2624b88c807SRodney W. Grimes 	if (num % numcols)
2634b88c807SRodney W. Grimes 		++numrows;
2644b88c807SRodney W. Grimes 
2654b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
2664b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
2674b88c807SRodney W. Grimes 	for (row = 0; row < numrows; ++row) {
2684b88c807SRodney W. Grimes 		endcol = colwidth;
2694b88c807SRodney W. Grimes 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
2704b88c807SRodney W. Grimes 			chcnt += printaname(array[base], dp->s_inode,
2714b88c807SRodney W. Grimes 			    dp->s_block);
2724b88c807SRodney W. Grimes 			if ((base += numrows) >= num)
2734b88c807SRodney W. Grimes 				break;
274545f583cSTim Vanderhoek 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
275545f583cSTim Vanderhoek 			    <= endcol){
276545f583cSTim Vanderhoek 				(void)putchar(f_notabs ? ' ' : '\t');
2774b88c807SRodney W. Grimes 				chcnt = cnt;
2784b88c807SRodney W. Grimes 			}
2794b88c807SRodney W. Grimes 			endcol += colwidth;
2804b88c807SRodney W. Grimes 		}
2814b88c807SRodney W. Grimes 		(void)putchar('\n');
2824b88c807SRodney W. Grimes 	}
2834b88c807SRodney W. Grimes }
2844b88c807SRodney W. Grimes 
2854b88c807SRodney W. Grimes /*
2864b88c807SRodney W. Grimes  * print [inode] [size] name
2874b88c807SRodney W. Grimes  * return # of characters printed, no trailing characters.
2884b88c807SRodney W. Grimes  */
2894b88c807SRodney W. Grimes static int
2904b88c807SRodney W. Grimes printaname(p, inodefield, sizefield)
2914b88c807SRodney W. Grimes 	FTSENT	*p;
292b0bc91e9SJosef Karthauser 	u_long	inodefield;
293b0bc91e9SJosef Karthauser 	u_long	sizefield;
2944b88c807SRodney W. Grimes {
2954b88c807SRodney W. Grimes 	struct stat *sp;
2964b88c807SRodney W. Grimes 	int	chcnt;
29747bb6b11SAndrey A. Chernov #ifdef COLORLS
29847bb6b11SAndrey A. Chernov 	int	color_printed = 0;
29947bb6b11SAndrey A. Chernov #endif
3004b88c807SRodney W. Grimes 
3014b88c807SRodney W. Grimes 	sp = p->fts_statp;
3024b88c807SRodney W. Grimes 	chcnt = 0;
3034b88c807SRodney W. Grimes 	if (f_inode)
304fb5cb208SSteve Price 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
3054b88c807SRodney W. Grimes 	if (f_size)
3064b88c807SRodney W. Grimes 		chcnt += printf("%*qd ",
3074b88c807SRodney W. Grimes 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
30874985094SJosef Karthauser #ifdef COLORLS
3093885812cSJosef Karthauser 	if (f_color)
310cf0feaeeSAndrey A. Chernov 		color_printed = colortype(sp->st_mode);
31174985094SJosef Karthauser #endif
312ee579ffbSAssar Westerlund 	chcnt += printname(p->fts_name);
31374985094SJosef Karthauser #ifdef COLORLS
314cf0feaeeSAndrey A. Chernov 	if (f_color && color_printed)
31538782c25SAndrey A. Chernov 		endcolor(0);
31674985094SJosef Karthauser #endif
3174b88c807SRodney W. Grimes 	if (f_type)
3184b88c807SRodney W. Grimes 		chcnt += printtype(sp->st_mode);
3194b88c807SRodney W. Grimes 	return (chcnt);
3204b88c807SRodney W. Grimes }
3214b88c807SRodney W. Grimes 
3224b88c807SRodney W. Grimes static void
3234b88c807SRodney W. Grimes printtime(ftime)
3244b88c807SRodney W. Grimes 	time_t	ftime;
3254b88c807SRodney W. Grimes {
326fc4a9bafSAndrey A. Chernov 	char		longstring[80];
327f173abd0SMike Pritchard 	static time_t	now;
32897e4e97bSJosef Karthauser 	const char	*format;
3298234eb25SAndrey A. Chernov 	static int	d_first = -1;
330f173abd0SMike Pritchard 
33128fd017aSAndrey A. Chernov 	if (d_first < 0)
33228fd017aSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
333f173abd0SMike Pritchard 	if (now == 0)
334f173abd0SMike Pritchard 		now = time(NULL);
3354b88c807SRodney W. Grimes 
336656dcd43SGarrett Wollman #define	SIXMONTHS	((365 / 2) * 86400)
3374b88c807SRodney W. Grimes 	if (f_sectime)
3381e715e34SJosef Karthauser 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
33928fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
340f173abd0SMike Pritchard 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
3411e715e34SJosef Karthauser 		/* mmm dd hh:mm || dd mmm hh:mm */
34228fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %R " : "%b %e %R ";
34397e4e97bSJosef Karthauser 	else
3441e715e34SJosef Karthauser 		/* mmm dd  yyyy || dd mmm  yyyy */
34528fd017aSAndrey A. Chernov 		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
34697e4e97bSJosef Karthauser 	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
34797e4e97bSJosef Karthauser 	fputs(longstring, stdout);
3484b88c807SRodney W. Grimes }
3494b88c807SRodney W. Grimes 
3504b88c807SRodney W. Grimes static int
3514b88c807SRodney W. Grimes printtype(mode)
3524b88c807SRodney W. Grimes 	u_int	mode;
3534b88c807SRodney W. Grimes {
3544b88c807SRodney W. Grimes 	switch (mode & S_IFMT) {
3554b88c807SRodney W. Grimes 	case S_IFDIR:
3564b88c807SRodney W. Grimes 		(void)putchar('/');
3574b88c807SRodney W. Grimes 		return (1);
3584b88c807SRodney W. Grimes 	case S_IFIFO:
3594b88c807SRodney W. Grimes 		(void)putchar('|');
3604b88c807SRodney W. Grimes 		return (1);
3614b88c807SRodney W. Grimes 	case S_IFLNK:
3624b88c807SRodney W. Grimes 		(void)putchar('@');
3634b88c807SRodney W. Grimes 		return (1);
3644b88c807SRodney W. Grimes 	case S_IFSOCK:
3654b88c807SRodney W. Grimes 		(void)putchar('=');
3664b88c807SRodney W. Grimes 		return (1);
367fb5cb208SSteve Price 	case S_IFWHT:
368fb5cb208SSteve Price 		(void)putchar('%');
369fb5cb208SSteve Price 		return (1);
3704b88c807SRodney W. Grimes 	}
3714b88c807SRodney W. Grimes 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
3724b88c807SRodney W. Grimes 		(void)putchar('*');
3734b88c807SRodney W. Grimes 		return (1);
3744b88c807SRodney W. Grimes 	}
3754b88c807SRodney W. Grimes 	return (0);
3764b88c807SRodney W. Grimes }
3774b88c807SRodney W. Grimes 
37874985094SJosef Karthauser #ifdef COLORLS
3791bf1478aSAndrey A. Chernov static int
3801bf1478aSAndrey A. Chernov putch(c)
381cf0feaeeSAndrey A. Chernov 	int	c;
382cf0feaeeSAndrey A. Chernov {
38338782c25SAndrey A. Chernov 	(void) putchar(c);
38438782c25SAndrey A. Chernov 	return 0;
385cf0feaeeSAndrey A. Chernov }
386cf0feaeeSAndrey A. Chernov 
3871bf1478aSAndrey A. Chernov static int
3881bf1478aSAndrey A. Chernov writech(c)
38938782c25SAndrey A. Chernov 	int	c;
39038782c25SAndrey A. Chernov {
39138782c25SAndrey A. Chernov 	char	tmp = c;
39238782c25SAndrey A. Chernov 
39338782c25SAndrey A. Chernov 	(void) write(STDOUT_FILENO, &tmp, 1);
39438782c25SAndrey A. Chernov 	return 0;
39538782c25SAndrey A. Chernov }
396cf0feaeeSAndrey A. Chernov 
3971bf1478aSAndrey A. Chernov static void
3983885812cSJosef Karthauser printcolor(c)
3993885812cSJosef Karthauser 	Colors	c;
4003885812cSJosef Karthauser {
40174985094SJosef Karthauser 	char	*ansiseq;
40274985094SJosef Karthauser 
403c1499cf6SJosef Karthauser 	if (colors[c].bold)
404c1499cf6SJosef Karthauser 		tputs(enter_bold, 1, putch);
405c1499cf6SJosef Karthauser 
406c1499cf6SJosef Karthauser 	if (colors[c].num[0] != -1) {
407c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
40838782c25SAndrey A. Chernov 		if (ansiseq)
409cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
4103885812cSJosef Karthauser 	}
41174985094SJosef Karthauser 
412c1499cf6SJosef Karthauser 	if (colors[c].num[1] != -1) {
413c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
41438782c25SAndrey A. Chernov 		if (ansiseq)
415cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
41674985094SJosef Karthauser 	}
41774985094SJosef Karthauser }
41874985094SJosef Karthauser 
41938782c25SAndrey A. Chernov static void
42038782c25SAndrey A. Chernov endcolor(sig)
42138782c25SAndrey A. Chernov 	int	sig;
42274985094SJosef Karthauser {
42338782c25SAndrey A. Chernov 	tputs(ansi_coloff, 1, sig ? writech : putch);
424c1499cf6SJosef Karthauser 	tputs(attrs_off, 1, sig ? writech : putch);
4253885812cSJosef Karthauser }
4263885812cSJosef Karthauser 
42738782c25SAndrey A. Chernov static int
4283885812cSJosef Karthauser colortype(mode)
4293885812cSJosef Karthauser 	mode_t	mode;
4303885812cSJosef Karthauser {
4313885812cSJosef Karthauser 	switch(mode & S_IFMT) {
4323885812cSJosef Karthauser 	      case S_IFDIR:
4333885812cSJosef Karthauser 		if (mode & S_IWOTH)
4343885812cSJosef Karthauser 		    if (mode & S_ISTXT)
4353885812cSJosef Karthauser 			printcolor(C_WSDIR);
4363885812cSJosef Karthauser 		    else
4373885812cSJosef Karthauser 			printcolor(C_WDIR);
4383885812cSJosef Karthauser 		else
4393885812cSJosef Karthauser 		    printcolor(C_DIR);
4403885812cSJosef Karthauser 		return(1);
4413885812cSJosef Karthauser 	      case S_IFLNK:
4423885812cSJosef Karthauser 		printcolor(C_LNK);
4433885812cSJosef Karthauser 		return(1);
4443885812cSJosef Karthauser 	      case S_IFSOCK:
4453885812cSJosef Karthauser 		printcolor(C_SOCK);
4463885812cSJosef Karthauser 		return(1);
4473885812cSJosef Karthauser 	      case S_IFIFO:
4483885812cSJosef Karthauser 		printcolor(C_FIFO);
4493885812cSJosef Karthauser 		return(1);
4503885812cSJosef Karthauser 	      case S_IFBLK:
4513885812cSJosef Karthauser 		printcolor(C_BLK);
4523885812cSJosef Karthauser 		return(1);
4533885812cSJosef Karthauser 	      case S_IFCHR:
4543885812cSJosef Karthauser 		printcolor(C_CHR);
4553885812cSJosef Karthauser 		return(1);
4563885812cSJosef Karthauser 	}
4573885812cSJosef Karthauser 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4583885812cSJosef Karthauser 		if (mode & S_ISUID)
4593885812cSJosef Karthauser 		    printcolor(C_SUID);
4603885812cSJosef Karthauser 		else if (mode & S_ISGID)
4613885812cSJosef Karthauser 		    printcolor(C_SGID);
4623885812cSJosef Karthauser 		else
4633885812cSJosef Karthauser 		    printcolor(C_EXEC);
4643885812cSJosef Karthauser 		return(1);
4653885812cSJosef Karthauser 	}
4663885812cSJosef Karthauser 	return(0);
4673885812cSJosef Karthauser }
4683885812cSJosef Karthauser 
4693885812cSJosef Karthauser void
4703885812cSJosef Karthauser parsecolors(cs)
4713885812cSJosef Karthauser 	char	*cs;
4723885812cSJosef Karthauser {
473b0bc91e9SJosef Karthauser 	int	i;
474b0bc91e9SJosef Karthauser 	int	j;
475b0bc91e9SJosef Karthauser 	int	len;
4763885812cSJosef Karthauser 	char	c[2];
477c1499cf6SJosef Karthauser 	short	legacy_warn = 0;
47838782c25SAndrey A. Chernov 
479b0bc91e9SJosef Karthauser 	if (cs == NULL)
480b0bc91e9SJosef Karthauser 		cs = ""; /* LSCOLORS not set */
4813885812cSJosef Karthauser 	len = strlen(cs);
4823885812cSJosef Karthauser 	for (i = 0 ; i < C_NUMCOLORS ; i++) {
483c1499cf6SJosef Karthauser 		colors[i].bold = 0;
484c1499cf6SJosef Karthauser 
4853885812cSJosef Karthauser 		if (len <= 2 * i) {
4863885812cSJosef Karthauser 			c[0] = defcolors[2 * i];
4873885812cSJosef Karthauser 			c[1] = defcolors[2 * i + 1];
4883885812cSJosef Karthauser 		}
4893885812cSJosef Karthauser 		else {
4903885812cSJosef Karthauser 			c[0] = cs[2 * i];
4913885812cSJosef Karthauser 			c[1] = cs[2 * i + 1];
4923885812cSJosef Karthauser 		}
4933885812cSJosef Karthauser 		for (j = 0 ; j < 2 ; j++) {
494c1499cf6SJosef Karthauser 			/* Legacy colours used 0-7 */
495c1499cf6SJosef Karthauser 			if (c[j] >= '0' && c[j] <= '7') {
496c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - '0';
497c1499cf6SJosef Karthauser 				if (!legacy_warn) {
4983885812cSJosef Karthauser 					fprintf(stderr,
499c1499cf6SJosef Karthauser 					    "warn: colors are now defined "
500c1499cf6SJosef Karthauser 					    "using a-h instead of 0-9. "
501c1499cf6SJosef Karthauser 					    "see manual page.\n");
5023885812cSJosef Karthauser 				}
503c1499cf6SJosef Karthauser 				legacy_warn = 1;
504c1499cf6SJosef Karthauser 			} else if (c[j] >= 'a' && c[j] <= 'h')
505c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'a';
506c1499cf6SJosef Karthauser 			else if (c[j] >= 'A' && c[j] <= 'H') {
507c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'A';
508c1499cf6SJosef Karthauser 				colors[i].bold = 1;
509c1499cf6SJosef Karthauser 			} else if (tolower((unsigned char)c[j] == 'x'))
510c1499cf6SJosef Karthauser 				colors[i].num[j] = -1;
511c1499cf6SJosef Karthauser 			else {
512c1499cf6SJosef Karthauser 				fprintf(stderr,
513c1499cf6SJosef Karthauser 				    "error: invalid character '%c' in LSCOLORS"
514c1499cf6SJosef Karthauser 				    " env var\n", c[j]);
5150d72516eSJosef Karthauser 				colors[i].num[j] = -1;
516c1499cf6SJosef Karthauser 			}
5173885812cSJosef Karthauser 		}
5183885812cSJosef Karthauser 	}
5193885812cSJosef Karthauser }
520cf0feaeeSAndrey A. Chernov 
5211bf1478aSAndrey A. Chernov void
5221bf1478aSAndrey A. Chernov colorquit(sig)
523cf0feaeeSAndrey A. Chernov 	int	sig;
524cf0feaeeSAndrey A. Chernov {
52538782c25SAndrey A. Chernov 	endcolor(sig);
526faebfe2eSAndrey A. Chernov 
527faebfe2eSAndrey A. Chernov 	(void) signal(sig, SIG_DFL);
528faebfe2eSAndrey A. Chernov 	(void) kill(getpid(), sig);
529cf0feaeeSAndrey A. Chernov }
53074985094SJosef Karthauser #endif /*COLORLS*/
5313885812cSJosef Karthauser 
5324b88c807SRodney W. Grimes static void
5334b88c807SRodney W. Grimes printlink(p)
5344b88c807SRodney W. Grimes 	FTSENT	*p;
5354b88c807SRodney W. Grimes {
5364b88c807SRodney W. Grimes 	int	lnklen;
537b0bc91e9SJosef Karthauser 	char	name[MAXPATHLEN + 1];
538b0bc91e9SJosef Karthauser 	char	path[MAXPATHLEN + 1];
5394b88c807SRodney W. Grimes 
5404b88c807SRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL)
5414b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
5424b88c807SRodney W. Grimes 	else
5434b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name),
5444b88c807SRodney W. Grimes 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
5454b88c807SRodney W. Grimes 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
5464b88c807SRodney W. Grimes 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
5474b88c807SRodney W. Grimes 		return;
5484b88c807SRodney W. Grimes 	}
5494b88c807SRodney W. Grimes 	path[lnklen] = '\0';
5507ea30648SDag-Erling Smørgrav 	(void)printf(" -> ");
551ee579ffbSAssar Westerlund 	printname(path);
5524b88c807SRodney W. Grimes }
553