xref: /freebsd/bin/ls/print.c (revision f7b8687a938048b590cc0b4689847e28a3c0d97c)
19ddb49cbSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
33febad2fcSSteve Price #if 0
34c73d77ceSMark Murray #ifndef lint
35febad2fcSSteve Price static char sccsid[] = "@(#)print.c	8.4 (Berkeley) 4/17/94";
364b88c807SRodney W. Grimes #endif /* not lint */
37c73d77ceSMark Murray #endif
385eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
395eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
414b88c807SRodney W. Grimes #include <sys/param.h>
424b88c807SRodney W. Grimes #include <sys/stat.h>
43dd9aaeb0STim J. Robbins #include <sys/acl.h>
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes #include <err.h>
464b88c807SRodney W. Grimes #include <errno.h>
474b88c807SRodney W. Grimes #include <fts.h>
4828fd017aSAndrey A. Chernov #include <langinfo.h>
49478aa805SPawel Jakub Dawidek #include <libutil.h>
504b88c807SRodney W. Grimes #include <stdio.h>
514b88c807SRodney W. Grimes #include <stdlib.h>
524b88c807SRodney W. Grimes #include <string.h>
53434b6ea4SBruce Evans #include <time.h>
544b88c807SRodney W. Grimes #include <unistd.h>
55faebfe2eSAndrey A. Chernov #ifdef COLORLS
56faebfe2eSAndrey A. Chernov #include <ctype.h>
57faebfe2eSAndrey A. Chernov #include <termcap.h>
58faebfe2eSAndrey A. Chernov #include <signal.h>
59faebfe2eSAndrey A. Chernov #endif
604b88c807SRodney W. Grimes 
614b88c807SRodney W. Grimes #include "ls.h"
624b88c807SRodney W. Grimes #include "extern.h"
634b88c807SRodney W. Grimes 
6440feca3aSMark Murray static int	printaname(const FTSENT *, u_long, u_long);
65ca2993fbSMark Murray static void	printlink(const FTSENT *);
6646251ddeSWarner Losh static void	printtime(time_t);
6746251ddeSWarner Losh static int	printtype(u_int);
6846251ddeSWarner Losh static void	printsize(size_t, off_t);
6938782c25SAndrey A. Chernov #ifdef COLORLS
7046251ddeSWarner Losh static void	endcolor(int);
7146251ddeSWarner Losh static int	colortype(mode_t);
7238782c25SAndrey A. Chernov #endif
73f7b8687aSEdward Tomasz Napierala static void	aclmode(char *, const FTSENT *);
744b88c807SRodney W. Grimes 
754b88c807SRodney W. Grimes #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
760e8d1551SJosef Karthauser 
7774985094SJosef Karthauser #ifdef COLORLS
783885812cSJosef Karthauser /* Most of these are taken from <sys/stat.h> */
793885812cSJosef Karthauser typedef enum Colors {
803885812cSJosef Karthauser 	C_DIR,			/* directory */
813885812cSJosef Karthauser 	C_LNK,			/* symbolic link */
823885812cSJosef Karthauser 	C_SOCK,			/* socket */
833885812cSJosef Karthauser 	C_FIFO,			/* pipe */
843885812cSJosef Karthauser 	C_EXEC,			/* executable */
853885812cSJosef Karthauser 	C_BLK,			/* block special */
863885812cSJosef Karthauser 	C_CHR,			/* character special */
873885812cSJosef Karthauser 	C_SUID,			/* setuid executable */
883885812cSJosef Karthauser 	C_SGID,			/* setgid executable */
895dda5d0dSJosef Karthauser 	C_WSDIR,		/* directory writeble to others, with sticky
905dda5d0dSJosef Karthauser 				 * bit */
915dda5d0dSJosef Karthauser 	C_WDIR,			/* directory writeble to others, without
925dda5d0dSJosef Karthauser 				 * sticky bit */
933885812cSJosef Karthauser 	C_NUMCOLORS		/* just a place-holder */
943885812cSJosef Karthauser } Colors;
953885812cSJosef Karthauser 
969052855aSMark Murray static const char *defcolors = "exfxcxdxbxegedabagacad";
973885812cSJosef Karthauser 
98c1499cf6SJosef Karthauser /* colors for file types */
99c1499cf6SJosef Karthauser static struct {
100c1499cf6SJosef Karthauser 	int	num[2];
101c1499cf6SJosef Karthauser 	int	bold;
102c1499cf6SJosef Karthauser } colors[C_NUMCOLORS];
10374985094SJosef Karthauser #endif
1043885812cSJosef Karthauser 
1054b88c807SRodney W. Grimes void
10640feca3aSMark Murray printscol(const DISPLAY *dp)
1074b88c807SRodney W. Grimes {
1084b88c807SRodney W. Grimes 	FTSENT *p;
1094b88c807SRodney W. Grimes 
1104b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1114b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1124b88c807SRodney W. Grimes 			continue;
1134b88c807SRodney W. Grimes 		(void)printaname(p, dp->s_inode, dp->s_block);
1144b88c807SRodney W. Grimes 		(void)putchar('\n');
1154b88c807SRodney W. Grimes 	}
1164b88c807SRodney W. Grimes }
1174b88c807SRodney W. Grimes 
118ee579ffbSAssar Westerlund /*
119ee579ffbSAssar Westerlund  * print name in current style
120ee579ffbSAssar Westerlund  */
1211656f850STim J. Robbins int
12246251ddeSWarner Losh printname(const char *name)
123ee579ffbSAssar Westerlund {
124ee579ffbSAssar Westerlund 	if (f_octal || f_octal_escape)
125ee579ffbSAssar Westerlund 		return prn_octal(name);
126ee579ffbSAssar Westerlund 	else if (f_nonprint)
127ee579ffbSAssar Westerlund 		return prn_printable(name);
128ee579ffbSAssar Westerlund 	else
129107409f4STim J. Robbins 		return prn_normal(name);
130ee579ffbSAssar Westerlund }
131ee579ffbSAssar Westerlund 
1324b88c807SRodney W. Grimes void
13340feca3aSMark Murray printlong(const DISPLAY *dp)
1344b88c807SRodney W. Grimes {
1354b88c807SRodney W. Grimes 	struct stat *sp;
1364b88c807SRodney W. Grimes 	FTSENT *p;
1374b88c807SRodney W. Grimes 	NAMES *np;
1384b88c807SRodney W. Grimes 	char buf[20];
13947bb6b11SAndrey A. Chernov #ifdef COLORLS
14047bb6b11SAndrey A. Chernov 	int color_printed = 0;
14147bb6b11SAndrey A. Chernov #endif
1424b88c807SRodney W. Grimes 
14348a91b69SDavid Schultz 	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
14448a91b69SDavid Schultz 	    (f_longform || f_size)) {
1454b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
14648a91b69SDavid Schultz 	}
1474b88c807SRodney W. Grimes 
1484b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1494b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1504b88c807SRodney W. Grimes 			continue;
1514b88c807SRodney W. Grimes 		sp = p->fts_statp;
1524b88c807SRodney W. Grimes 		if (f_inode)
153fb5cb208SSteve Price 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1544b88c807SRodney W. Grimes 		if (f_size)
15540feca3aSMark Murray 			(void)printf("%*jd ",
1564b88c807SRodney W. Grimes 			    dp->s_block, howmany(sp->st_blocks, blocksize));
1579052855aSMark Murray 		strmode(sp->st_mode, buf);
158f7b8687aSEdward Tomasz Napierala 		aclmode(buf, p);
1594b88c807SRodney W. Grimes 		np = p->fts_pointer;
1604b88c807SRodney W. Grimes 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1614b88c807SRodney W. Grimes 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1624b88c807SRodney W. Grimes 		    np->group);
1634b88c807SRodney W. Grimes 		if (f_flags)
1644b88c807SRodney W. Grimes 			(void)printf("%-*s ", dp->s_flags, np->flags);
1654d33b62eSRobert Watson 		if (f_label)
1664d33b62eSRobert Watson 			(void)printf("%-*s ", dp->s_label, np->label);
1674b88c807SRodney W. Grimes 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
168029b2bd0SBruce Evans 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
169df2fbf15SJoerg Wunsch 				(void)printf("%3d, 0x%08x ",
170029b2bd0SBruce Evans 				    major(sp->st_rdev),
171029b2bd0SBruce Evans 				    (u_int)minor(sp->st_rdev));
172df2fbf15SJoerg Wunsch 			else
1734b88c807SRodney W. Grimes 				(void)printf("%3d, %3d ",
1744b88c807SRodney W. Grimes 				    major(sp->st_rdev), minor(sp->st_rdev));
1754b88c807SRodney W. Grimes 		else if (dp->bcfile)
17640feca3aSMark Murray 			(void)printf("%*s%*jd ",
1774b88c807SRodney W. Grimes 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1784b88c807SRodney W. Grimes 		else
1790e8d1551SJosef Karthauser 			printsize(dp->s_size, sp->st_size);
1804b88c807SRodney W. Grimes 		if (f_accesstime)
1814b88c807SRodney W. Grimes 			printtime(sp->st_atime);
182fe79420eSJohn Baldwin 		else if (f_birthtime)
183fe79420eSJohn Baldwin 			printtime(sp->st_birthtime);
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
20640feca3aSMark Murray printstream(const DISPLAY *dp)
20794274c73STim J. Robbins {
20894274c73STim J. Robbins 	FTSENT *p;
20994274c73STim J. Robbins 	int chcnt;
21094274c73STim J. Robbins 
21194274c73STim J. Robbins 	for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
21294274c73STim J. Robbins 		if (p->fts_number == NO_PRINT)
21394274c73STim J. Robbins 			continue;
214107409f4STim J. Robbins 		/* XXX strlen does not take octal escapes into account. */
21594274c73STim J. Robbins 		if (strlen(p->fts_name) + chcnt +
21694274c73STim J. Robbins 		    (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
21794274c73STim J. Robbins 			putchar('\n');
21894274c73STim J. Robbins 			chcnt = 0;
21994274c73STim J. Robbins 		}
22094274c73STim J. Robbins 		chcnt += printaname(p, dp->s_inode, dp->s_block);
22194274c73STim J. Robbins 		if (p->fts_link) {
22294274c73STim J. Robbins 			printf(", ");
22394274c73STim J. Robbins 			chcnt += 2;
22494274c73STim J. Robbins 		}
22594274c73STim J. Robbins 	}
22694274c73STim J. Robbins 	if (chcnt)
22794274c73STim J. Robbins 		putchar('\n');
22894274c73STim J. Robbins }
22994274c73STim J. Robbins 
23094274c73STim J. Robbins void
23140feca3aSMark Murray printcol(const DISPLAY *dp)
2324b88c807SRodney W. Grimes {
2334b88c807SRodney W. Grimes 	static FTSENT **array;
2344b88c807SRodney W. Grimes 	static int lastentries = -1;
2354b88c807SRodney W. Grimes 	FTSENT *p;
236c5bc8709STim J. Robbins 	FTSENT **narray;
237b0bc91e9SJosef Karthauser 	int base;
238b0bc91e9SJosef Karthauser 	int chcnt;
239b0bc91e9SJosef Karthauser 	int cnt;
240b0bc91e9SJosef Karthauser 	int col;
241b0bc91e9SJosef Karthauser 	int colwidth;
242b0bc91e9SJosef Karthauser 	int endcol;
243b0bc91e9SJosef Karthauser 	int num;
244b0bc91e9SJosef Karthauser 	int numcols;
245b0bc91e9SJosef Karthauser 	int numrows;
246b0bc91e9SJosef Karthauser 	int row;
247545f583cSTim Vanderhoek 	int tabwidth;
248545f583cSTim Vanderhoek 
249545f583cSTim Vanderhoek 	if (f_notabs)
250545f583cSTim Vanderhoek 		tabwidth = 1;
251545f583cSTim Vanderhoek 	else
252545f583cSTim Vanderhoek 		tabwidth = 8;
2534b88c807SRodney W. Grimes 
2544b88c807SRodney W. Grimes 	/*
2554b88c807SRodney W. Grimes 	 * Have to do random access in the linked list -- build a table
2564b88c807SRodney W. Grimes 	 * of pointers.
2574b88c807SRodney W. Grimes 	 */
2584b88c807SRodney W. Grimes 	if (dp->entries > lastentries) {
259c5bc8709STim J. Robbins 		if ((narray =
2604b88c807SRodney W. Grimes 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
2614b88c807SRodney W. Grimes 			warn(NULL);
2624b88c807SRodney W. Grimes 			printscol(dp);
263c5bc8709STim J. Robbins 			return;
2644b88c807SRodney W. Grimes 		}
265c5bc8709STim J. Robbins 		lastentries = dp->entries;
266c5bc8709STim J. Robbins 		array = narray;
2674b88c807SRodney W. Grimes 	}
2684b88c807SRodney W. Grimes 	for (p = dp->list, num = 0; p; p = p->fts_link)
2694b88c807SRodney W. Grimes 		if (p->fts_number != NO_PRINT)
2704b88c807SRodney W. Grimes 			array[num++] = p;
2714b88c807SRodney W. Grimes 
2724b88c807SRodney W. Grimes 	colwidth = dp->maxlen;
2734b88c807SRodney W. Grimes 	if (f_inode)
2744b88c807SRodney W. Grimes 		colwidth += dp->s_inode + 1;
2754b88c807SRodney W. Grimes 	if (f_size)
2764b88c807SRodney W. Grimes 		colwidth += dp->s_block + 1;
2774b88c807SRodney W. Grimes 	if (f_type)
2784b88c807SRodney W. Grimes 		colwidth += 1;
2794b88c807SRodney W. Grimes 
280545f583cSTim Vanderhoek 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
2814b88c807SRodney W. Grimes 	if (termwidth < 2 * colwidth) {
2824b88c807SRodney W. Grimes 		printscol(dp);
2834b88c807SRodney W. Grimes 		return;
2844b88c807SRodney W. Grimes 	}
2854b88c807SRodney W. Grimes 	numcols = termwidth / colwidth;
2864b88c807SRodney W. Grimes 	numrows = num / numcols;
2874b88c807SRodney W. Grimes 	if (num % numcols)
2884b88c807SRodney W. Grimes 		++numrows;
2894b88c807SRodney W. Grimes 
29048a91b69SDavid Schultz 	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
29148a91b69SDavid Schultz 	    (f_longform || f_size)) {
2924b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
29348a91b69SDavid Schultz 	}
29494274c73STim J. Robbins 
29594274c73STim J. Robbins 	base = 0;
2964b88c807SRodney W. Grimes 	for (row = 0; row < numrows; ++row) {
2974b88c807SRodney W. Grimes 		endcol = colwidth;
29894274c73STim J. Robbins 		if (!f_sortacross)
29994274c73STim J. Robbins 			base = row;
30094274c73STim J. Robbins 		for (col = 0, chcnt = 0; col < numcols; ++col) {
3014b88c807SRodney W. Grimes 			chcnt += printaname(array[base], dp->s_inode,
3024b88c807SRodney W. Grimes 			    dp->s_block);
30394274c73STim J. Robbins 			if (f_sortacross)
30494274c73STim J. Robbins 				base++;
30594274c73STim J. Robbins 			else
30694274c73STim J. Robbins 				base += numrows;
30794274c73STim J. Robbins 			if (base >= num)
3084b88c807SRodney W. Grimes 				break;
309545f583cSTim Vanderhoek 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
310545f583cSTim Vanderhoek 			    <= endcol) {
31194274c73STim J. Robbins 				if (f_sortacross && col + 1 >= numcols)
31294274c73STim J. Robbins 					break;
313545f583cSTim Vanderhoek 				(void)putchar(f_notabs ? ' ' : '\t');
3144b88c807SRodney W. Grimes 				chcnt = cnt;
3154b88c807SRodney W. Grimes 			}
3164b88c807SRodney W. Grimes 			endcol += colwidth;
3174b88c807SRodney W. Grimes 		}
3184b88c807SRodney W. Grimes 		(void)putchar('\n');
3194b88c807SRodney W. Grimes 	}
3204b88c807SRodney W. Grimes }
3214b88c807SRodney W. Grimes 
3224b88c807SRodney W. Grimes /*
3234b88c807SRodney W. Grimes  * print [inode] [size] name
3244b88c807SRodney W. Grimes  * return # of characters printed, no trailing characters.
3254b88c807SRodney W. Grimes  */
3264b88c807SRodney W. Grimes static int
32740feca3aSMark Murray printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
3284b88c807SRodney W. Grimes {
3294b88c807SRodney W. Grimes 	struct stat *sp;
3304b88c807SRodney W. Grimes 	int chcnt;
33147bb6b11SAndrey A. Chernov #ifdef COLORLS
33247bb6b11SAndrey A. Chernov 	int color_printed = 0;
33347bb6b11SAndrey A. Chernov #endif
3344b88c807SRodney W. Grimes 
3354b88c807SRodney W. Grimes 	sp = p->fts_statp;
3364b88c807SRodney W. Grimes 	chcnt = 0;
3374b88c807SRodney W. Grimes 	if (f_inode)
338fb5cb208SSteve Price 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
3394b88c807SRodney W. Grimes 	if (f_size)
34040feca3aSMark Murray 		chcnt += printf("%*jd ",
3414b88c807SRodney W. Grimes 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
34274985094SJosef Karthauser #ifdef COLORLS
3433885812cSJosef Karthauser 	if (f_color)
344cf0feaeeSAndrey A. Chernov 		color_printed = colortype(sp->st_mode);
34574985094SJosef Karthauser #endif
346ee579ffbSAssar Westerlund 	chcnt += printname(p->fts_name);
34774985094SJosef Karthauser #ifdef COLORLS
348cf0feaeeSAndrey A. Chernov 	if (f_color && color_printed)
34938782c25SAndrey A. Chernov 		endcolor(0);
35074985094SJosef Karthauser #endif
3514b88c807SRodney W. Grimes 	if (f_type)
3524b88c807SRodney W. Grimes 		chcnt += printtype(sp->st_mode);
3534b88c807SRodney W. Grimes 	return (chcnt);
3544b88c807SRodney W. Grimes }
3554b88c807SRodney W. Grimes 
3564b88c807SRodney W. Grimes static void
35746251ddeSWarner Losh printtime(time_t ftime)
3584b88c807SRodney W. Grimes {
359fc4a9bafSAndrey A. Chernov 	char longstring[80];
36040feca3aSMark Murray 	static time_t now = 0;
36197e4e97bSJosef Karthauser 	const char *format;
3628234eb25SAndrey A. Chernov 	static int d_first = -1;
363f173abd0SMike Pritchard 
36428fd017aSAndrey A. Chernov 	if (d_first < 0)
36528fd017aSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
366f173abd0SMike Pritchard 	if (now == 0)
367f173abd0SMike Pritchard 		now = time(NULL);
3684b88c807SRodney W. Grimes 
369656dcd43SGarrett Wollman #define	SIXMONTHS	((365 / 2) * 86400)
3702269fa57SGreg Lehey 	if (f_timeformat)  /* user specified format */
3712269fa57SGreg Lehey 		format = f_timeformat;
3722269fa57SGreg Lehey 	else if (f_sectime)
3731e715e34SJosef Karthauser 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
37428fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
375f173abd0SMike Pritchard 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
3761e715e34SJosef Karthauser 		/* mmm dd hh:mm || dd mmm hh:mm */
37728fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %R" : "%b %e %R";
37897e4e97bSJosef Karthauser 	else
3791e715e34SJosef Karthauser 		/* mmm dd  yyyy || dd mmm  yyyy */
38028fd017aSAndrey A. Chernov 		format = d_first ? "%e %b  %Y" : "%b %e  %Y";
38197e4e97bSJosef Karthauser 	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
38297e4e97bSJosef Karthauser 	fputs(longstring, stdout);
3832269fa57SGreg Lehey 	fputc(' ', stdout);
3844b88c807SRodney W. Grimes }
3854b88c807SRodney W. Grimes 
3864b88c807SRodney W. Grimes static int
38746251ddeSWarner Losh printtype(u_int mode)
3884b88c807SRodney W. Grimes {
38994274c73STim J. Robbins 
39094274c73STim J. Robbins 	if (f_slash) {
39194274c73STim J. Robbins 		if ((mode & S_IFMT) == S_IFDIR) {
39294274c73STim J. Robbins 			(void)putchar('/');
39394274c73STim J. Robbins 			return (1);
39494274c73STim J. Robbins 		}
39594274c73STim J. Robbins 		return (0);
39694274c73STim J. Robbins 	}
39794274c73STim J. Robbins 
3984b88c807SRodney W. Grimes 	switch (mode & S_IFMT) {
3994b88c807SRodney W. Grimes 	case S_IFDIR:
4004b88c807SRodney W. Grimes 		(void)putchar('/');
4014b88c807SRodney W. Grimes 		return (1);
4024b88c807SRodney W. Grimes 	case S_IFIFO:
4034b88c807SRodney W. Grimes 		(void)putchar('|');
4044b88c807SRodney W. Grimes 		return (1);
4054b88c807SRodney W. Grimes 	case S_IFLNK:
4064b88c807SRodney W. Grimes 		(void)putchar('@');
4074b88c807SRodney W. Grimes 		return (1);
4084b88c807SRodney W. Grimes 	case S_IFSOCK:
4094b88c807SRodney W. Grimes 		(void)putchar('=');
4104b88c807SRodney W. Grimes 		return (1);
411fb5cb208SSteve Price 	case S_IFWHT:
412fb5cb208SSteve Price 		(void)putchar('%');
413fb5cb208SSteve Price 		return (1);
4149052855aSMark Murray 	default:
415568dcd5fSBill Fumerola 		break;
4164b88c807SRodney W. Grimes 	}
4174b88c807SRodney W. Grimes 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4184b88c807SRodney W. Grimes 		(void)putchar('*');
4194b88c807SRodney W. Grimes 		return (1);
4204b88c807SRodney W. Grimes 	}
4214b88c807SRodney W. Grimes 	return (0);
4224b88c807SRodney W. Grimes }
4234b88c807SRodney W. Grimes 
42474985094SJosef Karthauser #ifdef COLORLS
4251bf1478aSAndrey A. Chernov static int
42646251ddeSWarner Losh putch(int c)
427cf0feaeeSAndrey A. Chernov {
42838782c25SAndrey A. Chernov 	(void)putchar(c);
42938782c25SAndrey A. Chernov 	return 0;
430cf0feaeeSAndrey A. Chernov }
431cf0feaeeSAndrey A. Chernov 
4321bf1478aSAndrey A. Chernov static int
43346251ddeSWarner Losh writech(int c)
43438782c25SAndrey A. Chernov {
43540feca3aSMark Murray 	char tmp = (char)c;
43638782c25SAndrey A. Chernov 
43738782c25SAndrey A. Chernov 	(void)write(STDOUT_FILENO, &tmp, 1);
43838782c25SAndrey A. Chernov 	return 0;
43938782c25SAndrey A. Chernov }
440cf0feaeeSAndrey A. Chernov 
4411bf1478aSAndrey A. Chernov static void
44246251ddeSWarner Losh printcolor(Colors c)
4433885812cSJosef Karthauser {
44474985094SJosef Karthauser 	char *ansiseq;
44574985094SJosef Karthauser 
446c1499cf6SJosef Karthauser 	if (colors[c].bold)
447c1499cf6SJosef Karthauser 		tputs(enter_bold, 1, putch);
448c1499cf6SJosef Karthauser 
449c1499cf6SJosef Karthauser 	if (colors[c].num[0] != -1) {
450c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
45138782c25SAndrey A. Chernov 		if (ansiseq)
452cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
4533885812cSJosef Karthauser 	}
454c1499cf6SJosef Karthauser 	if (colors[c].num[1] != -1) {
455c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
45638782c25SAndrey A. Chernov 		if (ansiseq)
457cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
45874985094SJosef Karthauser 	}
45974985094SJosef Karthauser }
46074985094SJosef Karthauser 
46138782c25SAndrey A. Chernov static void
46246251ddeSWarner Losh endcolor(int sig)
46374985094SJosef Karthauser {
46438782c25SAndrey A. Chernov 	tputs(ansi_coloff, 1, sig ? writech : putch);
465c1499cf6SJosef Karthauser 	tputs(attrs_off, 1, sig ? writech : putch);
4663885812cSJosef Karthauser }
4673885812cSJosef Karthauser 
46838782c25SAndrey A. Chernov static int
46946251ddeSWarner Losh colortype(mode_t mode)
4703885812cSJosef Karthauser {
4713885812cSJosef Karthauser 	switch (mode & S_IFMT) {
4723885812cSJosef Karthauser 	case S_IFDIR:
4733885812cSJosef Karthauser 		if (mode & S_IWOTH)
4743885812cSJosef Karthauser 			if (mode & S_ISTXT)
4753885812cSJosef Karthauser 				printcolor(C_WSDIR);
4763885812cSJosef Karthauser 			else
4773885812cSJosef Karthauser 				printcolor(C_WDIR);
4783885812cSJosef Karthauser 		else
4793885812cSJosef Karthauser 			printcolor(C_DIR);
4803885812cSJosef Karthauser 		return (1);
4813885812cSJosef Karthauser 	case S_IFLNK:
4823885812cSJosef Karthauser 		printcolor(C_LNK);
4833885812cSJosef Karthauser 		return (1);
4843885812cSJosef Karthauser 	case S_IFSOCK:
4853885812cSJosef Karthauser 		printcolor(C_SOCK);
4863885812cSJosef Karthauser 		return (1);
4873885812cSJosef Karthauser 	case S_IFIFO:
4883885812cSJosef Karthauser 		printcolor(C_FIFO);
4893885812cSJosef Karthauser 		return (1);
4903885812cSJosef Karthauser 	case S_IFBLK:
4913885812cSJosef Karthauser 		printcolor(C_BLK);
4923885812cSJosef Karthauser 		return (1);
4933885812cSJosef Karthauser 	case S_IFCHR:
4943885812cSJosef Karthauser 		printcolor(C_CHR);
4953885812cSJosef Karthauser 		return (1);
49640feca3aSMark Murray 	default:;
4973885812cSJosef Karthauser 	}
4983885812cSJosef Karthauser 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4993885812cSJosef Karthauser 		if (mode & S_ISUID)
5003885812cSJosef Karthauser 			printcolor(C_SUID);
5013885812cSJosef Karthauser 		else if (mode & S_ISGID)
5023885812cSJosef Karthauser 			printcolor(C_SGID);
5033885812cSJosef Karthauser 		else
5043885812cSJosef Karthauser 			printcolor(C_EXEC);
5053885812cSJosef Karthauser 		return (1);
5063885812cSJosef Karthauser 	}
5073885812cSJosef Karthauser 	return (0);
5083885812cSJosef Karthauser }
5093885812cSJosef Karthauser 
5103885812cSJosef Karthauser void
51146251ddeSWarner Losh parsecolors(const char *cs)
5123885812cSJosef Karthauser {
513b0bc91e9SJosef Karthauser 	int i;
514b0bc91e9SJosef Karthauser 	int j;
515ca2993fbSMark Murray 	size_t len;
5163885812cSJosef Karthauser 	char c[2];
517c1499cf6SJosef Karthauser 	short legacy_warn = 0;
51838782c25SAndrey A. Chernov 
519b0bc91e9SJosef Karthauser 	if (cs == NULL)
520b0bc91e9SJosef Karthauser 		cs = "";	/* LSCOLORS not set */
5213885812cSJosef Karthauser 	len = strlen(cs);
52240feca3aSMark Murray 	for (i = 0; i < (int)C_NUMCOLORS; i++) {
523c1499cf6SJosef Karthauser 		colors[i].bold = 0;
524c1499cf6SJosef Karthauser 
52540feca3aSMark Murray 		if (len <= 2 * (size_t)i) {
5263885812cSJosef Karthauser 			c[0] = defcolors[2 * i];
5273885812cSJosef Karthauser 			c[1] = defcolors[2 * i + 1];
5285dda5d0dSJosef Karthauser 		} else {
5293885812cSJosef Karthauser 			c[0] = cs[2 * i];
5303885812cSJosef Karthauser 			c[1] = cs[2 * i + 1];
5313885812cSJosef Karthauser 		}
5323885812cSJosef Karthauser 		for (j = 0; j < 2; j++) {
533c1499cf6SJosef Karthauser 			/* Legacy colours used 0-7 */
534c1499cf6SJosef Karthauser 			if (c[j] >= '0' && c[j] <= '7') {
535c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - '0';
536c1499cf6SJosef Karthauser 				if (!legacy_warn) {
537e09fdabdSTim J. Robbins 					warnx("LSCOLORS should use "
538130d15dcSJosef Karthauser 					    "characters a-h instead of 0-9 ("
539e09fdabdSTim J. Robbins 					    "see the manual page)");
5403885812cSJosef Karthauser 				}
541c1499cf6SJosef Karthauser 				legacy_warn = 1;
542c1499cf6SJosef Karthauser 			} else if (c[j] >= 'a' && c[j] <= 'h')
543c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'a';
544c1499cf6SJosef Karthauser 			else if (c[j] >= 'A' && c[j] <= 'H') {
545c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'A';
546c1499cf6SJosef Karthauser 				colors[i].bold = 1;
54740feca3aSMark Murray 			} else if (tolower((unsigned char)c[j]) == 'x')
548c1499cf6SJosef Karthauser 				colors[i].num[j] = -1;
549c1499cf6SJosef Karthauser 			else {
550e09fdabdSTim J. Robbins 				warnx("invalid character '%c' in LSCOLORS"
551e09fdabdSTim J. Robbins 				    " env var", c[j]);
5520d72516eSJosef Karthauser 				colors[i].num[j] = -1;
553c1499cf6SJosef Karthauser 			}
5543885812cSJosef Karthauser 		}
5553885812cSJosef Karthauser 	}
5563885812cSJosef Karthauser }
557cf0feaeeSAndrey A. Chernov 
5581bf1478aSAndrey A. Chernov void
55946251ddeSWarner Losh colorquit(int sig)
560cf0feaeeSAndrey A. Chernov {
56138782c25SAndrey A. Chernov 	endcolor(sig);
562faebfe2eSAndrey A. Chernov 
563faebfe2eSAndrey A. Chernov 	(void)signal(sig, SIG_DFL);
564faebfe2eSAndrey A. Chernov 	(void)kill(getpid(), sig);
565cf0feaeeSAndrey A. Chernov }
5665dda5d0dSJosef Karthauser 
56774985094SJosef Karthauser #endif /* COLORLS */
5683885812cSJosef Karthauser 
5694b88c807SRodney W. Grimes static void
570ca2993fbSMark Murray printlink(const FTSENT *p)
5714b88c807SRodney W. Grimes {
5724b88c807SRodney W. Grimes 	int lnklen;
573b0bc91e9SJosef Karthauser 	char name[MAXPATHLEN + 1];
574b0bc91e9SJosef Karthauser 	char path[MAXPATHLEN + 1];
5754b88c807SRodney W. Grimes 
5764b88c807SRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL)
5774b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
5784b88c807SRodney W. Grimes 	else
5794b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name),
5804b88c807SRodney W. Grimes 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
5814b88c807SRodney W. Grimes 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
5824b88c807SRodney W. Grimes 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
5834b88c807SRodney W. Grimes 		return;
5844b88c807SRodney W. Grimes 	}
5854b88c807SRodney W. Grimes 	path[lnklen] = '\0';
5867ea30648SDag-Erling Smørgrav 	(void)printf(" -> ");
5879052855aSMark Murray 	(void)printname(path);
5884b88c807SRodney W. Grimes }
5890e8d1551SJosef Karthauser 
5900e8d1551SJosef Karthauser static void
59146251ddeSWarner Losh printsize(size_t width, off_t bytes)
5920e8d1551SJosef Karthauser {
5930e8d1551SJosef Karthauser 
5940e8d1551SJosef Karthauser 	if (f_humanval) {
595478aa805SPawel Jakub Dawidek 		char buf[5];
5960e8d1551SJosef Karthauser 
597478aa805SPawel Jakub Dawidek 		humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
598478aa805SPawel Jakub Dawidek 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
599478aa805SPawel Jakub Dawidek 		(void)printf("%5s ", buf);
6005dda5d0dSJosef Karthauser 	} else
60140feca3aSMark Murray 		(void)printf("%*jd ", (u_int)width, bytes);
6020e8d1551SJosef Karthauser }
6030e8d1551SJosef Karthauser 
604dd9aaeb0STim J. Robbins /*
605dd9aaeb0STim J. Robbins  * Add a + after the standard rwxrwxrwx mode if the file has an
606f7b8687aSEdward Tomasz Napierala  * ACL. strmode() reserves space at the end of the string.
607dd9aaeb0STim J. Robbins  */
608f7b8687aSEdward Tomasz Napierala static void
609f7b8687aSEdward Tomasz Napierala aclmode(char *buf, const FTSENT *p)
610f7b8687aSEdward Tomasz Napierala {
611f7b8687aSEdward Tomasz Napierala 	char name[MAXPATHLEN + 1];
612f7b8687aSEdward Tomasz Napierala 	int ret, trivial;
613f7b8687aSEdward Tomasz Napierala 	static dev_t previous_dev = NODEV;
614f7b8687aSEdward Tomasz Napierala 	static int supports_acls = -1;
615f7b8687aSEdward Tomasz Napierala 	static int type = ACL_TYPE_ACCESS;
616f7b8687aSEdward Tomasz Napierala 	acl_t facl;
617f7b8687aSEdward Tomasz Napierala 
618f7b8687aSEdward Tomasz Napierala 	/*
619f7b8687aSEdward Tomasz Napierala 	 * XXX: ACLs are not supported on whiteouts and device files
620f7b8687aSEdward Tomasz Napierala 	 * residing on UFS.
621f7b8687aSEdward Tomasz Napierala 	 */
622f7b8687aSEdward Tomasz Napierala 	if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
623f7b8687aSEdward Tomasz Napierala 	    S_ISWHT(p->fts_statp->st_mode))
624f7b8687aSEdward Tomasz Napierala 		return;
625f7b8687aSEdward Tomasz Napierala 
626f7b8687aSEdward Tomasz Napierala 	if (previous_dev != p->fts_statp->st_dev) {
627f7b8687aSEdward Tomasz Napierala 		previous_dev = p->fts_statp->st_dev;
628f7b8687aSEdward Tomasz Napierala 		supports_acls = 0;
629f7b8687aSEdward Tomasz Napierala 
630dd9aaeb0STim J. Robbins 		if (p->fts_level == FTS_ROOTLEVEL)
631dd9aaeb0STim J. Robbins 			snprintf(name, sizeof(name), "%s", p->fts_name);
632dd9aaeb0STim J. Robbins 		else
633dd9aaeb0STim J. Robbins 			snprintf(name, sizeof(name), "%s/%s",
634dd9aaeb0STim J. Robbins 			    p->fts_parent->fts_accpath, p->fts_name);
635f7b8687aSEdward Tomasz Napierala 		ret = lpathconf(name, _PC_ACL_NFS4);
636f7b8687aSEdward Tomasz Napierala 		if (ret > 0) {
637f7b8687aSEdward Tomasz Napierala 			type = ACL_TYPE_NFS4;
638f7b8687aSEdward Tomasz Napierala 			supports_acls = 1;
639f7b8687aSEdward Tomasz Napierala 		} else if (ret < 0 && errno != EINVAL) {
640dd9aaeb0STim J. Robbins 			warn("%s", name);
641dd9aaeb0STim J. Robbins 			return;
642dd9aaeb0STim J. Robbins 		}
643f7b8687aSEdward Tomasz Napierala 		if (supports_acls == 0) {
644f7b8687aSEdward Tomasz Napierala 			ret = lpathconf(name, _PC_ACL_EXTENDED);
645f7b8687aSEdward Tomasz Napierala 			if (ret > 0) {
646f7b8687aSEdward Tomasz Napierala 				type = ACL_TYPE_ACCESS;
647f7b8687aSEdward Tomasz Napierala 				supports_acls = 1;
648f7b8687aSEdward Tomasz Napierala 			} else if (ret < 0 && errno != EINVAL) {
649f7b8687aSEdward Tomasz Napierala 				warn("%s", name);
650f7b8687aSEdward Tomasz Napierala 				return;
651dd9aaeb0STim J. Robbins 			}
652f7b8687aSEdward Tomasz Napierala 		}
653f7b8687aSEdward Tomasz Napierala 	}
654f7b8687aSEdward Tomasz Napierala 	if (supports_acls == 0)
655f7b8687aSEdward Tomasz Napierala 		return;
656f7b8687aSEdward Tomasz Napierala 	facl = acl_get_link_np(name, type);
657f7b8687aSEdward Tomasz Napierala 	if (facl == NULL) {
658f7b8687aSEdward Tomasz Napierala 		warn("%s", name);
659f7b8687aSEdward Tomasz Napierala 		return;
660f7b8687aSEdward Tomasz Napierala 	}
661f7b8687aSEdward Tomasz Napierala 	if (acl_is_trivial_np(facl, &trivial)) {
662dd9aaeb0STim J. Robbins 		acl_free(facl);
663dd9aaeb0STim J. Robbins 		warn("%s", name);
664f7b8687aSEdward Tomasz Napierala 		return;
665f7b8687aSEdward Tomasz Napierala 	}
666f7b8687aSEdward Tomasz Napierala 	if (!trivial)
667f7b8687aSEdward Tomasz Napierala 		buf[10] = '+';
668f7b8687aSEdward Tomasz Napierala 	acl_free(facl);
669dd9aaeb0STim J. Robbins }
670