xref: /freebsd/bin/ls/print.c (revision 478aa805105a77be3272d4c3a9cdcca7de0c54c0)
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  * 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
7340feca3aSMark Murray static void	aclmode(char *, const FTSENT *, int *);
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
142dd9aaeb0STim J. Robbins 	int haveacls;
143dd9aaeb0STim J. Robbins 	dev_t prevdev;
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1464b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1474b88c807SRodney W. Grimes 
148dd9aaeb0STim J. Robbins 	haveacls = 1;
149dd9aaeb0STim J. Robbins 	prevdev = (dev_t)-1;
1504b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1514b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1524b88c807SRodney W. Grimes 			continue;
1534b88c807SRodney W. Grimes 		sp = p->fts_statp;
1544b88c807SRodney W. Grimes 		if (f_inode)
155fb5cb208SSteve Price 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1564b88c807SRodney W. Grimes 		if (f_size)
15740feca3aSMark Murray 			(void)printf("%*jd ",
1584b88c807SRodney W. Grimes 			    dp->s_block, howmany(sp->st_blocks, blocksize));
1599052855aSMark Murray 		strmode(sp->st_mode, buf);
160dd9aaeb0STim J. Robbins 		/*
161dd9aaeb0STim J. Robbins 		 * Cache whether or not the filesystem supports ACL's to
162dd9aaeb0STim J. Robbins 		 * avoid expensive syscalls. Try again when we change devices.
163dd9aaeb0STim J. Robbins 		 */
164dd9aaeb0STim J. Robbins 		if (haveacls || sp->st_dev != prevdev) {
165dd9aaeb0STim J. Robbins 			aclmode(buf, p, &haveacls);
166dd9aaeb0STim J. Robbins 			prevdev = sp->st_dev;
167dd9aaeb0STim J. Robbins 		}
1684b88c807SRodney W. Grimes 		np = p->fts_pointer;
1694b88c807SRodney W. Grimes 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1704b88c807SRodney W. Grimes 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1714b88c807SRodney W. Grimes 		    np->group);
1724b88c807SRodney W. Grimes 		if (f_flags)
1734b88c807SRodney W. Grimes 			(void)printf("%-*s ", dp->s_flags, np->flags);
1744d33b62eSRobert Watson 		if (f_label)
1754d33b62eSRobert Watson 			(void)printf("%-*s ", dp->s_label, np->label);
1764b88c807SRodney W. Grimes 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
177029b2bd0SBruce Evans 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
178df2fbf15SJoerg Wunsch 				(void)printf("%3d, 0x%08x ",
179029b2bd0SBruce Evans 				    major(sp->st_rdev),
180029b2bd0SBruce Evans 				    (u_int)minor(sp->st_rdev));
181df2fbf15SJoerg Wunsch 			else
1824b88c807SRodney W. Grimes 				(void)printf("%3d, %3d ",
1834b88c807SRodney W. Grimes 				    major(sp->st_rdev), minor(sp->st_rdev));
1844b88c807SRodney W. Grimes 		else if (dp->bcfile)
18540feca3aSMark Murray 			(void)printf("%*s%*jd ",
1864b88c807SRodney W. Grimes 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1874b88c807SRodney W. Grimes 		else
1880e8d1551SJosef Karthauser 			printsize(dp->s_size, sp->st_size);
1894b88c807SRodney W. Grimes 		if (f_accesstime)
1904b88c807SRodney W. Grimes 			printtime(sp->st_atime);
1914b88c807SRodney W. Grimes 		else if (f_statustime)
1924b88c807SRodney W. Grimes 			printtime(sp->st_ctime);
1934b88c807SRodney W. Grimes 		else
1944b88c807SRodney W. Grimes 			printtime(sp->st_mtime);
19574985094SJosef Karthauser #ifdef COLORLS
1963885812cSJosef Karthauser 		if (f_color)
197cf0feaeeSAndrey A. Chernov 			color_printed = colortype(sp->st_mode);
19874985094SJosef Karthauser #endif
199ee579ffbSAssar Westerlund 		(void)printname(p->fts_name);
20074985094SJosef Karthauser #ifdef COLORLS
201cf0feaeeSAndrey A. Chernov 		if (f_color && color_printed)
20238782c25SAndrey A. Chernov 			endcolor(0);
20374985094SJosef Karthauser #endif
2044b88c807SRodney W. Grimes 		if (f_type)
2054b88c807SRodney W. Grimes 			(void)printtype(sp->st_mode);
2064b88c807SRodney W. Grimes 		if (S_ISLNK(sp->st_mode))
2074b88c807SRodney W. Grimes 			printlink(p);
2084b88c807SRodney W. Grimes 		(void)putchar('\n');
2094b88c807SRodney W. Grimes 	}
2104b88c807SRodney W. Grimes }
2114b88c807SRodney W. Grimes 
2124b88c807SRodney W. Grimes void
21340feca3aSMark Murray printstream(const DISPLAY *dp)
21494274c73STim J. Robbins {
21594274c73STim J. Robbins 	FTSENT *p;
21694274c73STim J. Robbins 	int chcnt;
21794274c73STim J. Robbins 
21894274c73STim J. Robbins 	for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
21994274c73STim J. Robbins 		if (p->fts_number == NO_PRINT)
22094274c73STim J. Robbins 			continue;
221107409f4STim J. Robbins 		/* XXX strlen does not take octal escapes into account. */
22294274c73STim J. Robbins 		if (strlen(p->fts_name) + chcnt +
22394274c73STim J. Robbins 		    (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
22494274c73STim J. Robbins 			putchar('\n');
22594274c73STim J. Robbins 			chcnt = 0;
22694274c73STim J. Robbins 		}
22794274c73STim J. Robbins 		chcnt += printaname(p, dp->s_inode, dp->s_block);
22894274c73STim J. Robbins 		if (p->fts_link) {
22994274c73STim J. Robbins 			printf(", ");
23094274c73STim J. Robbins 			chcnt += 2;
23194274c73STim J. Robbins 		}
23294274c73STim J. Robbins 	}
23394274c73STim J. Robbins 	if (chcnt)
23494274c73STim J. Robbins 		putchar('\n');
23594274c73STim J. Robbins }
23694274c73STim J. Robbins 
23794274c73STim J. Robbins void
23840feca3aSMark Murray printcol(const DISPLAY *dp)
2394b88c807SRodney W. Grimes {
2404b88c807SRodney W. Grimes 	static FTSENT **array;
2414b88c807SRodney W. Grimes 	static int lastentries = -1;
2424b88c807SRodney W. Grimes 	FTSENT *p;
243c5bc8709STim J. Robbins 	FTSENT **narray;
244b0bc91e9SJosef Karthauser 	int base;
245b0bc91e9SJosef Karthauser 	int chcnt;
246b0bc91e9SJosef Karthauser 	int cnt;
247b0bc91e9SJosef Karthauser 	int col;
248b0bc91e9SJosef Karthauser 	int colwidth;
249b0bc91e9SJosef Karthauser 	int endcol;
250b0bc91e9SJosef Karthauser 	int num;
251b0bc91e9SJosef Karthauser 	int numcols;
252b0bc91e9SJosef Karthauser 	int numrows;
253b0bc91e9SJosef Karthauser 	int row;
254545f583cSTim Vanderhoek 	int tabwidth;
255545f583cSTim Vanderhoek 
256545f583cSTim Vanderhoek 	if (f_notabs)
257545f583cSTim Vanderhoek 		tabwidth = 1;
258545f583cSTim Vanderhoek 	else
259545f583cSTim Vanderhoek 		tabwidth = 8;
2604b88c807SRodney W. Grimes 
2614b88c807SRodney W. Grimes 	/*
2624b88c807SRodney W. Grimes 	 * Have to do random access in the linked list -- build a table
2634b88c807SRodney W. Grimes 	 * of pointers.
2644b88c807SRodney W. Grimes 	 */
2654b88c807SRodney W. Grimes 	if (dp->entries > lastentries) {
266c5bc8709STim J. Robbins 		if ((narray =
2674b88c807SRodney W. Grimes 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
2684b88c807SRodney W. Grimes 			warn(NULL);
2694b88c807SRodney W. Grimes 			printscol(dp);
270c5bc8709STim J. Robbins 			return;
2714b88c807SRodney W. Grimes 		}
272c5bc8709STim J. Robbins 		lastentries = dp->entries;
273c5bc8709STim J. Robbins 		array = narray;
2744b88c807SRodney W. Grimes 	}
2754b88c807SRodney W. Grimes 	for (p = dp->list, num = 0; p; p = p->fts_link)
2764b88c807SRodney W. Grimes 		if (p->fts_number != NO_PRINT)
2774b88c807SRodney W. Grimes 			array[num++] = p;
2784b88c807SRodney W. Grimes 
2794b88c807SRodney W. Grimes 	colwidth = dp->maxlen;
2804b88c807SRodney W. Grimes 	if (f_inode)
2814b88c807SRodney W. Grimes 		colwidth += dp->s_inode + 1;
2824b88c807SRodney W. Grimes 	if (f_size)
2834b88c807SRodney W. Grimes 		colwidth += dp->s_block + 1;
2844b88c807SRodney W. Grimes 	if (f_type)
2854b88c807SRodney W. Grimes 		colwidth += 1;
2864b88c807SRodney W. Grimes 
287545f583cSTim Vanderhoek 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
2884b88c807SRodney W. Grimes 	if (termwidth < 2 * colwidth) {
2894b88c807SRodney W. Grimes 		printscol(dp);
2904b88c807SRodney W. Grimes 		return;
2914b88c807SRodney W. Grimes 	}
2924b88c807SRodney W. Grimes 	numcols = termwidth / colwidth;
2934b88c807SRodney W. Grimes 	numrows = num / numcols;
2944b88c807SRodney W. Grimes 	if (num % numcols)
2954b88c807SRodney W. Grimes 		++numrows;
2964b88c807SRodney W. Grimes 
2974b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
2984b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
29994274c73STim J. Robbins 
30094274c73STim J. Robbins 	base = 0;
3014b88c807SRodney W. Grimes 	for (row = 0; row < numrows; ++row) {
3024b88c807SRodney W. Grimes 		endcol = colwidth;
30394274c73STim J. Robbins 		if (!f_sortacross)
30494274c73STim J. Robbins 			base = row;
30594274c73STim J. Robbins 		for (col = 0, chcnt = 0; col < numcols; ++col) {
3064b88c807SRodney W. Grimes 			chcnt += printaname(array[base], dp->s_inode,
3074b88c807SRodney W. Grimes 			    dp->s_block);
30894274c73STim J. Robbins 			if (f_sortacross)
30994274c73STim J. Robbins 				base++;
31094274c73STim J. Robbins 			else
31194274c73STim J. Robbins 				base += numrows;
31294274c73STim J. Robbins 			if (base >= num)
3134b88c807SRodney W. Grimes 				break;
314545f583cSTim Vanderhoek 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
315545f583cSTim Vanderhoek 			    <= endcol) {
31694274c73STim J. Robbins 				if (f_sortacross && col + 1 >= numcols)
31794274c73STim J. Robbins 					break;
318545f583cSTim Vanderhoek 				(void)putchar(f_notabs ? ' ' : '\t');
3194b88c807SRodney W. Grimes 				chcnt = cnt;
3204b88c807SRodney W. Grimes 			}
3214b88c807SRodney W. Grimes 			endcol += colwidth;
3224b88c807SRodney W. Grimes 		}
3234b88c807SRodney W. Grimes 		(void)putchar('\n');
3244b88c807SRodney W. Grimes 	}
3254b88c807SRodney W. Grimes }
3264b88c807SRodney W. Grimes 
3274b88c807SRodney W. Grimes /*
3284b88c807SRodney W. Grimes  * print [inode] [size] name
3294b88c807SRodney W. Grimes  * return # of characters printed, no trailing characters.
3304b88c807SRodney W. Grimes  */
3314b88c807SRodney W. Grimes static int
33240feca3aSMark Murray printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
3334b88c807SRodney W. Grimes {
3344b88c807SRodney W. Grimes 	struct stat *sp;
3354b88c807SRodney W. Grimes 	int chcnt;
33647bb6b11SAndrey A. Chernov #ifdef COLORLS
33747bb6b11SAndrey A. Chernov 	int color_printed = 0;
33847bb6b11SAndrey A. Chernov #endif
3394b88c807SRodney W. Grimes 
3404b88c807SRodney W. Grimes 	sp = p->fts_statp;
3414b88c807SRodney W. Grimes 	chcnt = 0;
3424b88c807SRodney W. Grimes 	if (f_inode)
343fb5cb208SSteve Price 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
3444b88c807SRodney W. Grimes 	if (f_size)
34540feca3aSMark Murray 		chcnt += printf("%*jd ",
3464b88c807SRodney W. Grimes 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
34774985094SJosef Karthauser #ifdef COLORLS
3483885812cSJosef Karthauser 	if (f_color)
349cf0feaeeSAndrey A. Chernov 		color_printed = colortype(sp->st_mode);
35074985094SJosef Karthauser #endif
351ee579ffbSAssar Westerlund 	chcnt += printname(p->fts_name);
35274985094SJosef Karthauser #ifdef COLORLS
353cf0feaeeSAndrey A. Chernov 	if (f_color && color_printed)
35438782c25SAndrey A. Chernov 		endcolor(0);
35574985094SJosef Karthauser #endif
3564b88c807SRodney W. Grimes 	if (f_type)
3574b88c807SRodney W. Grimes 		chcnt += printtype(sp->st_mode);
3584b88c807SRodney W. Grimes 	return (chcnt);
3594b88c807SRodney W. Grimes }
3604b88c807SRodney W. Grimes 
3614b88c807SRodney W. Grimes static void
36246251ddeSWarner Losh printtime(time_t ftime)
3634b88c807SRodney W. Grimes {
364fc4a9bafSAndrey A. Chernov 	char longstring[80];
36540feca3aSMark Murray 	static time_t now = 0;
36697e4e97bSJosef Karthauser 	const char *format;
3678234eb25SAndrey A. Chernov 	static int d_first = -1;
368f173abd0SMike Pritchard 
36928fd017aSAndrey A. Chernov 	if (d_first < 0)
37028fd017aSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
371f173abd0SMike Pritchard 	if (now == 0)
372f173abd0SMike Pritchard 		now = time(NULL);
3734b88c807SRodney W. Grimes 
374656dcd43SGarrett Wollman #define	SIXMONTHS	((365 / 2) * 86400)
3754b88c807SRodney W. Grimes 	if (f_sectime)
3761e715e34SJosef Karthauser 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
37728fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
378f173abd0SMike Pritchard 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
3791e715e34SJosef Karthauser 		/* mmm dd hh:mm || dd mmm hh:mm */
38028fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %R " : "%b %e %R ";
38197e4e97bSJosef Karthauser 	else
3821e715e34SJosef Karthauser 		/* mmm dd  yyyy || dd mmm  yyyy */
38328fd017aSAndrey A. Chernov 		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
38497e4e97bSJosef Karthauser 	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
38597e4e97bSJosef Karthauser 	fputs(longstring, stdout);
3864b88c807SRodney W. Grimes }
3874b88c807SRodney W. Grimes 
3884b88c807SRodney W. Grimes static int
38946251ddeSWarner Losh printtype(u_int mode)
3904b88c807SRodney W. Grimes {
39194274c73STim J. Robbins 
39294274c73STim J. Robbins 	if (f_slash) {
39394274c73STim J. Robbins 		if ((mode & S_IFMT) == S_IFDIR) {
39494274c73STim J. Robbins 			(void)putchar('/');
39594274c73STim J. Robbins 			return (1);
39694274c73STim J. Robbins 		}
39794274c73STim J. Robbins 		return (0);
39894274c73STim J. Robbins 	}
39994274c73STim J. Robbins 
4004b88c807SRodney W. Grimes 	switch (mode & S_IFMT) {
4014b88c807SRodney W. Grimes 	case S_IFDIR:
4024b88c807SRodney W. Grimes 		(void)putchar('/');
4034b88c807SRodney W. Grimes 		return (1);
4044b88c807SRodney W. Grimes 	case S_IFIFO:
4054b88c807SRodney W. Grimes 		(void)putchar('|');
4064b88c807SRodney W. Grimes 		return (1);
4074b88c807SRodney W. Grimes 	case S_IFLNK:
4084b88c807SRodney W. Grimes 		(void)putchar('@');
4094b88c807SRodney W. Grimes 		return (1);
4104b88c807SRodney W. Grimes 	case S_IFSOCK:
4114b88c807SRodney W. Grimes 		(void)putchar('=');
4124b88c807SRodney W. Grimes 		return (1);
413fb5cb208SSteve Price 	case S_IFWHT:
414fb5cb208SSteve Price 		(void)putchar('%');
415fb5cb208SSteve Price 		return (1);
4169052855aSMark Murray 	default:
417568dcd5fSBill Fumerola 		break;
4184b88c807SRodney W. Grimes 	}
4194b88c807SRodney W. Grimes 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4204b88c807SRodney W. Grimes 		(void)putchar('*');
4214b88c807SRodney W. Grimes 		return (1);
4224b88c807SRodney W. Grimes 	}
4234b88c807SRodney W. Grimes 	return (0);
4244b88c807SRodney W. Grimes }
4254b88c807SRodney W. Grimes 
42674985094SJosef Karthauser #ifdef COLORLS
4271bf1478aSAndrey A. Chernov static int
42846251ddeSWarner Losh putch(int c)
429cf0feaeeSAndrey A. Chernov {
43038782c25SAndrey A. Chernov 	(void)putchar(c);
43138782c25SAndrey A. Chernov 	return 0;
432cf0feaeeSAndrey A. Chernov }
433cf0feaeeSAndrey A. Chernov 
4341bf1478aSAndrey A. Chernov static int
43546251ddeSWarner Losh writech(int c)
43638782c25SAndrey A. Chernov {
43740feca3aSMark Murray 	char tmp = (char)c;
43838782c25SAndrey A. Chernov 
43938782c25SAndrey A. Chernov 	(void)write(STDOUT_FILENO, &tmp, 1);
44038782c25SAndrey A. Chernov 	return 0;
44138782c25SAndrey A. Chernov }
442cf0feaeeSAndrey A. Chernov 
4431bf1478aSAndrey A. Chernov static void
44446251ddeSWarner Losh printcolor(Colors c)
4453885812cSJosef Karthauser {
44674985094SJosef Karthauser 	char *ansiseq;
44774985094SJosef Karthauser 
448c1499cf6SJosef Karthauser 	if (colors[c].bold)
449c1499cf6SJosef Karthauser 		tputs(enter_bold, 1, putch);
450c1499cf6SJosef Karthauser 
451c1499cf6SJosef Karthauser 	if (colors[c].num[0] != -1) {
452c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
45338782c25SAndrey A. Chernov 		if (ansiseq)
454cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
4553885812cSJosef Karthauser 	}
456c1499cf6SJosef Karthauser 	if (colors[c].num[1] != -1) {
457c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
45838782c25SAndrey A. Chernov 		if (ansiseq)
459cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
46074985094SJosef Karthauser 	}
46174985094SJosef Karthauser }
46274985094SJosef Karthauser 
46338782c25SAndrey A. Chernov static void
46446251ddeSWarner Losh endcolor(int sig)
46574985094SJosef Karthauser {
46638782c25SAndrey A. Chernov 	tputs(ansi_coloff, 1, sig ? writech : putch);
467c1499cf6SJosef Karthauser 	tputs(attrs_off, 1, sig ? writech : putch);
4683885812cSJosef Karthauser }
4693885812cSJosef Karthauser 
47038782c25SAndrey A. Chernov static int
47146251ddeSWarner Losh colortype(mode_t mode)
4723885812cSJosef Karthauser {
4733885812cSJosef Karthauser 	switch (mode & S_IFMT) {
4743885812cSJosef Karthauser 	case S_IFDIR:
4753885812cSJosef Karthauser 		if (mode & S_IWOTH)
4763885812cSJosef Karthauser 			if (mode & S_ISTXT)
4773885812cSJosef Karthauser 				printcolor(C_WSDIR);
4783885812cSJosef Karthauser 			else
4793885812cSJosef Karthauser 				printcolor(C_WDIR);
4803885812cSJosef Karthauser 		else
4813885812cSJosef Karthauser 			printcolor(C_DIR);
4823885812cSJosef Karthauser 		return (1);
4833885812cSJosef Karthauser 	case S_IFLNK:
4843885812cSJosef Karthauser 		printcolor(C_LNK);
4853885812cSJosef Karthauser 		return (1);
4863885812cSJosef Karthauser 	case S_IFSOCK:
4873885812cSJosef Karthauser 		printcolor(C_SOCK);
4883885812cSJosef Karthauser 		return (1);
4893885812cSJosef Karthauser 	case S_IFIFO:
4903885812cSJosef Karthauser 		printcolor(C_FIFO);
4913885812cSJosef Karthauser 		return (1);
4923885812cSJosef Karthauser 	case S_IFBLK:
4933885812cSJosef Karthauser 		printcolor(C_BLK);
4943885812cSJosef Karthauser 		return (1);
4953885812cSJosef Karthauser 	case S_IFCHR:
4963885812cSJosef Karthauser 		printcolor(C_CHR);
4973885812cSJosef Karthauser 		return (1);
49840feca3aSMark Murray 	default:;
4993885812cSJosef Karthauser 	}
5003885812cSJosef Karthauser 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
5013885812cSJosef Karthauser 		if (mode & S_ISUID)
5023885812cSJosef Karthauser 			printcolor(C_SUID);
5033885812cSJosef Karthauser 		else if (mode & S_ISGID)
5043885812cSJosef Karthauser 			printcolor(C_SGID);
5053885812cSJosef Karthauser 		else
5063885812cSJosef Karthauser 			printcolor(C_EXEC);
5073885812cSJosef Karthauser 		return (1);
5083885812cSJosef Karthauser 	}
5093885812cSJosef Karthauser 	return (0);
5103885812cSJosef Karthauser }
5113885812cSJosef Karthauser 
5123885812cSJosef Karthauser void
51346251ddeSWarner Losh parsecolors(const char *cs)
5143885812cSJosef Karthauser {
515b0bc91e9SJosef Karthauser 	int i;
516b0bc91e9SJosef Karthauser 	int j;
517ca2993fbSMark Murray 	size_t len;
5183885812cSJosef Karthauser 	char c[2];
519c1499cf6SJosef Karthauser 	short legacy_warn = 0;
52038782c25SAndrey A. Chernov 
521b0bc91e9SJosef Karthauser 	if (cs == NULL)
522b0bc91e9SJosef Karthauser 		cs = "";	/* LSCOLORS not set */
5233885812cSJosef Karthauser 	len = strlen(cs);
52440feca3aSMark Murray 	for (i = 0; i < (int)C_NUMCOLORS; i++) {
525c1499cf6SJosef Karthauser 		colors[i].bold = 0;
526c1499cf6SJosef Karthauser 
52740feca3aSMark Murray 		if (len <= 2 * (size_t)i) {
5283885812cSJosef Karthauser 			c[0] = defcolors[2 * i];
5293885812cSJosef Karthauser 			c[1] = defcolors[2 * i + 1];
5305dda5d0dSJosef Karthauser 		} else {
5313885812cSJosef Karthauser 			c[0] = cs[2 * i];
5323885812cSJosef Karthauser 			c[1] = cs[2 * i + 1];
5333885812cSJosef Karthauser 		}
5343885812cSJosef Karthauser 		for (j = 0; j < 2; j++) {
535c1499cf6SJosef Karthauser 			/* Legacy colours used 0-7 */
536c1499cf6SJosef Karthauser 			if (c[j] >= '0' && c[j] <= '7') {
537c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - '0';
538c1499cf6SJosef Karthauser 				if (!legacy_warn) {
539e09fdabdSTim J. Robbins 					warnx("LSCOLORS should use "
540130d15dcSJosef Karthauser 					    "characters a-h instead of 0-9 ("
541e09fdabdSTim J. Robbins 					    "see the manual page)");
5423885812cSJosef Karthauser 				}
543c1499cf6SJosef Karthauser 				legacy_warn = 1;
544c1499cf6SJosef Karthauser 			} else if (c[j] >= 'a' && c[j] <= 'h')
545c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'a';
546c1499cf6SJosef Karthauser 			else if (c[j] >= 'A' && c[j] <= 'H') {
547c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'A';
548c1499cf6SJosef Karthauser 				colors[i].bold = 1;
54940feca3aSMark Murray 			} else if (tolower((unsigned char)c[j]) == 'x')
550c1499cf6SJosef Karthauser 				colors[i].num[j] = -1;
551c1499cf6SJosef Karthauser 			else {
552e09fdabdSTim J. Robbins 				warnx("invalid character '%c' in LSCOLORS"
553e09fdabdSTim J. Robbins 				    " env var", c[j]);
5540d72516eSJosef Karthauser 				colors[i].num[j] = -1;
555c1499cf6SJosef Karthauser 			}
5563885812cSJosef Karthauser 		}
5573885812cSJosef Karthauser 	}
5583885812cSJosef Karthauser }
559cf0feaeeSAndrey A. Chernov 
5601bf1478aSAndrey A. Chernov void
56146251ddeSWarner Losh colorquit(int sig)
562cf0feaeeSAndrey A. Chernov {
56338782c25SAndrey A. Chernov 	endcolor(sig);
564faebfe2eSAndrey A. Chernov 
565faebfe2eSAndrey A. Chernov 	(void)signal(sig, SIG_DFL);
566faebfe2eSAndrey A. Chernov 	(void)kill(getpid(), sig);
567cf0feaeeSAndrey A. Chernov }
5685dda5d0dSJosef Karthauser 
56974985094SJosef Karthauser #endif /* COLORLS */
5703885812cSJosef Karthauser 
5714b88c807SRodney W. Grimes static void
572ca2993fbSMark Murray printlink(const FTSENT *p)
5734b88c807SRodney W. Grimes {
5744b88c807SRodney W. Grimes 	int lnklen;
575b0bc91e9SJosef Karthauser 	char name[MAXPATHLEN + 1];
576b0bc91e9SJosef Karthauser 	char path[MAXPATHLEN + 1];
5774b88c807SRodney W. Grimes 
5784b88c807SRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL)
5794b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
5804b88c807SRodney W. Grimes 	else
5814b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name),
5824b88c807SRodney W. Grimes 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
5834b88c807SRodney W. Grimes 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
5844b88c807SRodney W. Grimes 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
5854b88c807SRodney W. Grimes 		return;
5864b88c807SRodney W. Grimes 	}
5874b88c807SRodney W. Grimes 	path[lnklen] = '\0';
5887ea30648SDag-Erling Smørgrav 	(void)printf(" -> ");
5899052855aSMark Murray 	(void)printname(path);
5904b88c807SRodney W. Grimes }
5910e8d1551SJosef Karthauser 
5920e8d1551SJosef Karthauser static void
59346251ddeSWarner Losh printsize(size_t width, off_t bytes)
5940e8d1551SJosef Karthauser {
5950e8d1551SJosef Karthauser 
5960e8d1551SJosef Karthauser 	if (f_humanval) {
597478aa805SPawel Jakub Dawidek 		char buf[5];
5980e8d1551SJosef Karthauser 
599478aa805SPawel Jakub Dawidek 		humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
600478aa805SPawel Jakub Dawidek 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
601478aa805SPawel Jakub Dawidek 		(void)printf("%5s ", buf);
6025dda5d0dSJosef Karthauser 	} else
60340feca3aSMark Murray 		(void)printf("%*jd ", (u_int)width, bytes);
6040e8d1551SJosef Karthauser }
6050e8d1551SJosef Karthauser 
606dd9aaeb0STim J. Robbins static void
60740feca3aSMark Murray aclmode(char *buf, const FTSENT *p, int *haveacls)
608dd9aaeb0STim J. Robbins {
609dd9aaeb0STim J. Robbins 	char name[MAXPATHLEN + 1];
610dd9aaeb0STim J. Robbins 	int entries, ret;
611dd9aaeb0STim J. Robbins 	acl_t facl;
612dd9aaeb0STim J. Robbins 	acl_entry_t ae;
613dd9aaeb0STim J. Robbins 
614dd9aaeb0STim J. Robbins 	/*
615dd9aaeb0STim J. Robbins 	 * Add a + after the standard rwxrwxrwx mode if the file has an
616dd9aaeb0STim J. Robbins 	 * extended ACL. strmode() reserves space at the end of the string.
617dd9aaeb0STim J. Robbins 	 */
618dd9aaeb0STim J. Robbins 	if (p->fts_level == FTS_ROOTLEVEL)
619dd9aaeb0STim J. Robbins 		snprintf(name, sizeof(name), "%s", p->fts_name);
620dd9aaeb0STim J. Robbins 	else
621dd9aaeb0STim J. Robbins 		snprintf(name, sizeof(name), "%s/%s",
622dd9aaeb0STim J. Robbins 		    p->fts_parent->fts_accpath, p->fts_name);
6233fceb9fdSTim J. Robbins 	/*
6243fceb9fdSTim J. Robbins 	 * We have no way to tell whether a symbolic link has an ACL since
6253fceb9fdSTim J. Robbins 	 * pathconf() and acl_get_file() both follow them.
6263fceb9fdSTim J. Robbins 	 */
6273fceb9fdSTim J. Robbins 	if (S_ISLNK(p->fts_statp->st_mode)) {
6283fceb9fdSTim J. Robbins 		*haveacls = 1;
6293fceb9fdSTim J. Robbins 		return;
6303fceb9fdSTim J. Robbins 	}
631dd9aaeb0STim J. Robbins 	if ((ret = pathconf(name, _PC_ACL_EXTENDED)) <= 0) {
632dd9aaeb0STim J. Robbins 		if (ret < 0 && errno != EINVAL)
633dd9aaeb0STim J. Robbins 			warn("%s", name);
634dd9aaeb0STim J. Robbins 		else
635dd9aaeb0STim J. Robbins 			*haveacls = 0;
636dd9aaeb0STim J. Robbins 		return;
637dd9aaeb0STim J. Robbins 	}
638dd9aaeb0STim J. Robbins 	*haveacls = 1;
639dd9aaeb0STim J. Robbins 	if ((facl = acl_get_file(name, ACL_TYPE_ACCESS)) != NULL) {
640dd9aaeb0STim J. Robbins 		if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
64126337733SBosko Milekic 			entries = 1;
64226337733SBosko Milekic 			while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1)
64326337733SBosko Milekic 				if (++entries > 3)
64426337733SBosko Milekic 					break;
64526337733SBosko Milekic 			/*
64626337733SBosko Milekic 			 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
64726337733SBosko Milekic 			 * must have at least three entries (owner, group,
64826337733SBosko Milekic 			 * and other). So anything with more than 3 ACLs looks
64926337733SBosko Milekic 			 * interesting to us.
65026337733SBosko Milekic 			 */
65126337733SBosko Milekic 			if (entries > 3)
652dd9aaeb0STim J. Robbins 				buf[10] = '+';
653dd9aaeb0STim J. Robbins 		}
654dd9aaeb0STim J. Robbins 		acl_free(facl);
655dd9aaeb0STim J. Robbins 	} else
656dd9aaeb0STim J. Robbins 		warn("%s", name);
657dd9aaeb0STim J. Robbins }
658