xref: /freebsd/bin/ls/print.c (revision 9ddb49cbe45441fa3f3a10f6dd355e9956480b5f)
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
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 
14548a91b69SDavid Schultz 	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
14648a91b69SDavid Schultz 	    (f_longform || f_size)) {
1474b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
14848a91b69SDavid Schultz 	}
1494b88c807SRodney W. Grimes 
150dd9aaeb0STim J. Robbins 	haveacls = 1;
151dd9aaeb0STim J. Robbins 	prevdev = (dev_t)-1;
1524b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1534b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1544b88c807SRodney W. Grimes 			continue;
1554b88c807SRodney W. Grimes 		sp = p->fts_statp;
1564b88c807SRodney W. Grimes 		if (f_inode)
157fb5cb208SSteve Price 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1584b88c807SRodney W. Grimes 		if (f_size)
15940feca3aSMark Murray 			(void)printf("%*jd ",
1604b88c807SRodney W. Grimes 			    dp->s_block, howmany(sp->st_blocks, blocksize));
1619052855aSMark Murray 		strmode(sp->st_mode, buf);
162dd9aaeb0STim J. Robbins 		/*
163dd9aaeb0STim J. Robbins 		 * Cache whether or not the filesystem supports ACL's to
164dd9aaeb0STim J. Robbins 		 * avoid expensive syscalls. Try again when we change devices.
165dd9aaeb0STim J. Robbins 		 */
166dd9aaeb0STim J. Robbins 		if (haveacls || sp->st_dev != prevdev) {
167dd9aaeb0STim J. Robbins 			aclmode(buf, p, &haveacls);
168dd9aaeb0STim J. Robbins 			prevdev = sp->st_dev;
169dd9aaeb0STim J. Robbins 		}
1704b88c807SRodney W. Grimes 		np = p->fts_pointer;
1714b88c807SRodney W. Grimes 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1724b88c807SRodney W. Grimes 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1734b88c807SRodney W. Grimes 		    np->group);
1744b88c807SRodney W. Grimes 		if (f_flags)
1754b88c807SRodney W. Grimes 			(void)printf("%-*s ", dp->s_flags, np->flags);
1764d33b62eSRobert Watson 		if (f_label)
1774d33b62eSRobert Watson 			(void)printf("%-*s ", dp->s_label, np->label);
1784b88c807SRodney W. Grimes 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
179029b2bd0SBruce Evans 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
180df2fbf15SJoerg Wunsch 				(void)printf("%3d, 0x%08x ",
181029b2bd0SBruce Evans 				    major(sp->st_rdev),
182029b2bd0SBruce Evans 				    (u_int)minor(sp->st_rdev));
183df2fbf15SJoerg Wunsch 			else
1844b88c807SRodney W. Grimes 				(void)printf("%3d, %3d ",
1854b88c807SRodney W. Grimes 				    major(sp->st_rdev), minor(sp->st_rdev));
1864b88c807SRodney W. Grimes 		else if (dp->bcfile)
18740feca3aSMark Murray 			(void)printf("%*s%*jd ",
1884b88c807SRodney W. Grimes 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1894b88c807SRodney W. Grimes 		else
1900e8d1551SJosef Karthauser 			printsize(dp->s_size, sp->st_size);
1914b88c807SRodney W. Grimes 		if (f_accesstime)
1924b88c807SRodney W. Grimes 			printtime(sp->st_atime);
1934b88c807SRodney W. Grimes 		else if (f_statustime)
1944b88c807SRodney W. Grimes 			printtime(sp->st_ctime);
1954b88c807SRodney W. Grimes 		else
1964b88c807SRodney W. Grimes 			printtime(sp->st_mtime);
19774985094SJosef Karthauser #ifdef COLORLS
1983885812cSJosef Karthauser 		if (f_color)
199cf0feaeeSAndrey A. Chernov 			color_printed = colortype(sp->st_mode);
20074985094SJosef Karthauser #endif
201ee579ffbSAssar Westerlund 		(void)printname(p->fts_name);
20274985094SJosef Karthauser #ifdef COLORLS
203cf0feaeeSAndrey A. Chernov 		if (f_color && color_printed)
20438782c25SAndrey A. Chernov 			endcolor(0);
20574985094SJosef Karthauser #endif
2064b88c807SRodney W. Grimes 		if (f_type)
2074b88c807SRodney W. Grimes 			(void)printtype(sp->st_mode);
2084b88c807SRodney W. Grimes 		if (S_ISLNK(sp->st_mode))
2094b88c807SRodney W. Grimes 			printlink(p);
2104b88c807SRodney W. Grimes 		(void)putchar('\n');
2114b88c807SRodney W. Grimes 	}
2124b88c807SRodney W. Grimes }
2134b88c807SRodney W. Grimes 
2144b88c807SRodney W. Grimes void
21540feca3aSMark Murray printstream(const DISPLAY *dp)
21694274c73STim J. Robbins {
21794274c73STim J. Robbins 	FTSENT *p;
21894274c73STim J. Robbins 	int chcnt;
21994274c73STim J. Robbins 
22094274c73STim J. Robbins 	for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
22194274c73STim J. Robbins 		if (p->fts_number == NO_PRINT)
22294274c73STim J. Robbins 			continue;
223107409f4STim J. Robbins 		/* XXX strlen does not take octal escapes into account. */
22494274c73STim J. Robbins 		if (strlen(p->fts_name) + chcnt +
22594274c73STim J. Robbins 		    (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
22694274c73STim J. Robbins 			putchar('\n');
22794274c73STim J. Robbins 			chcnt = 0;
22894274c73STim J. Robbins 		}
22994274c73STim J. Robbins 		chcnt += printaname(p, dp->s_inode, dp->s_block);
23094274c73STim J. Robbins 		if (p->fts_link) {
23194274c73STim J. Robbins 			printf(", ");
23294274c73STim J. Robbins 			chcnt += 2;
23394274c73STim J. Robbins 		}
23494274c73STim J. Robbins 	}
23594274c73STim J. Robbins 	if (chcnt)
23694274c73STim J. Robbins 		putchar('\n');
23794274c73STim J. Robbins }
23894274c73STim J. Robbins 
23994274c73STim J. Robbins void
24040feca3aSMark Murray printcol(const DISPLAY *dp)
2414b88c807SRodney W. Grimes {
2424b88c807SRodney W. Grimes 	static FTSENT **array;
2434b88c807SRodney W. Grimes 	static int lastentries = -1;
2444b88c807SRodney W. Grimes 	FTSENT *p;
245c5bc8709STim J. Robbins 	FTSENT **narray;
246b0bc91e9SJosef Karthauser 	int base;
247b0bc91e9SJosef Karthauser 	int chcnt;
248b0bc91e9SJosef Karthauser 	int cnt;
249b0bc91e9SJosef Karthauser 	int col;
250b0bc91e9SJosef Karthauser 	int colwidth;
251b0bc91e9SJosef Karthauser 	int endcol;
252b0bc91e9SJosef Karthauser 	int num;
253b0bc91e9SJosef Karthauser 	int numcols;
254b0bc91e9SJosef Karthauser 	int numrows;
255b0bc91e9SJosef Karthauser 	int row;
256545f583cSTim Vanderhoek 	int tabwidth;
257545f583cSTim Vanderhoek 
258545f583cSTim Vanderhoek 	if (f_notabs)
259545f583cSTim Vanderhoek 		tabwidth = 1;
260545f583cSTim Vanderhoek 	else
261545f583cSTim Vanderhoek 		tabwidth = 8;
2624b88c807SRodney W. Grimes 
2634b88c807SRodney W. Grimes 	/*
2644b88c807SRodney W. Grimes 	 * Have to do random access in the linked list -- build a table
2654b88c807SRodney W. Grimes 	 * of pointers.
2664b88c807SRodney W. Grimes 	 */
2674b88c807SRodney W. Grimes 	if (dp->entries > lastentries) {
268c5bc8709STim J. Robbins 		if ((narray =
2694b88c807SRodney W. Grimes 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
2704b88c807SRodney W. Grimes 			warn(NULL);
2714b88c807SRodney W. Grimes 			printscol(dp);
272c5bc8709STim J. Robbins 			return;
2734b88c807SRodney W. Grimes 		}
274c5bc8709STim J. Robbins 		lastentries = dp->entries;
275c5bc8709STim J. Robbins 		array = narray;
2764b88c807SRodney W. Grimes 	}
2774b88c807SRodney W. Grimes 	for (p = dp->list, num = 0; p; p = p->fts_link)
2784b88c807SRodney W. Grimes 		if (p->fts_number != NO_PRINT)
2794b88c807SRodney W. Grimes 			array[num++] = p;
2804b88c807SRodney W. Grimes 
2814b88c807SRodney W. Grimes 	colwidth = dp->maxlen;
2824b88c807SRodney W. Grimes 	if (f_inode)
2834b88c807SRodney W. Grimes 		colwidth += dp->s_inode + 1;
2844b88c807SRodney W. Grimes 	if (f_size)
2854b88c807SRodney W. Grimes 		colwidth += dp->s_block + 1;
2864b88c807SRodney W. Grimes 	if (f_type)
2874b88c807SRodney W. Grimes 		colwidth += 1;
2884b88c807SRodney W. Grimes 
289545f583cSTim Vanderhoek 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
2904b88c807SRodney W. Grimes 	if (termwidth < 2 * colwidth) {
2914b88c807SRodney W. Grimes 		printscol(dp);
2924b88c807SRodney W. Grimes 		return;
2934b88c807SRodney W. Grimes 	}
2944b88c807SRodney W. Grimes 	numcols = termwidth / colwidth;
2954b88c807SRodney W. Grimes 	numrows = num / numcols;
2964b88c807SRodney W. Grimes 	if (num % numcols)
2974b88c807SRodney W. Grimes 		++numrows;
2984b88c807SRodney W. Grimes 
29948a91b69SDavid Schultz 	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
30048a91b69SDavid Schultz 	    (f_longform || f_size)) {
3014b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
30248a91b69SDavid Schultz 	}
30394274c73STim J. Robbins 
30494274c73STim J. Robbins 	base = 0;
3054b88c807SRodney W. Grimes 	for (row = 0; row < numrows; ++row) {
3064b88c807SRodney W. Grimes 		endcol = colwidth;
30794274c73STim J. Robbins 		if (!f_sortacross)
30894274c73STim J. Robbins 			base = row;
30994274c73STim J. Robbins 		for (col = 0, chcnt = 0; col < numcols; ++col) {
3104b88c807SRodney W. Grimes 			chcnt += printaname(array[base], dp->s_inode,
3114b88c807SRodney W. Grimes 			    dp->s_block);
31294274c73STim J. Robbins 			if (f_sortacross)
31394274c73STim J. Robbins 				base++;
31494274c73STim J. Robbins 			else
31594274c73STim J. Robbins 				base += numrows;
31694274c73STim J. Robbins 			if (base >= num)
3174b88c807SRodney W. Grimes 				break;
318545f583cSTim Vanderhoek 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
319545f583cSTim Vanderhoek 			    <= endcol) {
32094274c73STim J. Robbins 				if (f_sortacross && col + 1 >= numcols)
32194274c73STim J. Robbins 					break;
322545f583cSTim Vanderhoek 				(void)putchar(f_notabs ? ' ' : '\t');
3234b88c807SRodney W. Grimes 				chcnt = cnt;
3244b88c807SRodney W. Grimes 			}
3254b88c807SRodney W. Grimes 			endcol += colwidth;
3264b88c807SRodney W. Grimes 		}
3274b88c807SRodney W. Grimes 		(void)putchar('\n');
3284b88c807SRodney W. Grimes 	}
3294b88c807SRodney W. Grimes }
3304b88c807SRodney W. Grimes 
3314b88c807SRodney W. Grimes /*
3324b88c807SRodney W. Grimes  * print [inode] [size] name
3334b88c807SRodney W. Grimes  * return # of characters printed, no trailing characters.
3344b88c807SRodney W. Grimes  */
3354b88c807SRodney W. Grimes static int
33640feca3aSMark Murray printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
3374b88c807SRodney W. Grimes {
3384b88c807SRodney W. Grimes 	struct stat *sp;
3394b88c807SRodney W. Grimes 	int chcnt;
34047bb6b11SAndrey A. Chernov #ifdef COLORLS
34147bb6b11SAndrey A. Chernov 	int color_printed = 0;
34247bb6b11SAndrey A. Chernov #endif
3434b88c807SRodney W. Grimes 
3444b88c807SRodney W. Grimes 	sp = p->fts_statp;
3454b88c807SRodney W. Grimes 	chcnt = 0;
3464b88c807SRodney W. Grimes 	if (f_inode)
347fb5cb208SSteve Price 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
3484b88c807SRodney W. Grimes 	if (f_size)
34940feca3aSMark Murray 		chcnt += printf("%*jd ",
3504b88c807SRodney W. Grimes 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
35174985094SJosef Karthauser #ifdef COLORLS
3523885812cSJosef Karthauser 	if (f_color)
353cf0feaeeSAndrey A. Chernov 		color_printed = colortype(sp->st_mode);
35474985094SJosef Karthauser #endif
355ee579ffbSAssar Westerlund 	chcnt += printname(p->fts_name);
35674985094SJosef Karthauser #ifdef COLORLS
357cf0feaeeSAndrey A. Chernov 	if (f_color && color_printed)
35838782c25SAndrey A. Chernov 		endcolor(0);
35974985094SJosef Karthauser #endif
3604b88c807SRodney W. Grimes 	if (f_type)
3614b88c807SRodney W. Grimes 		chcnt += printtype(sp->st_mode);
3624b88c807SRodney W. Grimes 	return (chcnt);
3634b88c807SRodney W. Grimes }
3644b88c807SRodney W. Grimes 
3654b88c807SRodney W. Grimes static void
36646251ddeSWarner Losh printtime(time_t ftime)
3674b88c807SRodney W. Grimes {
368fc4a9bafSAndrey A. Chernov 	char longstring[80];
36940feca3aSMark Murray 	static time_t now = 0;
37097e4e97bSJosef Karthauser 	const char *format;
3718234eb25SAndrey A. Chernov 	static int d_first = -1;
372f173abd0SMike Pritchard 
37328fd017aSAndrey A. Chernov 	if (d_first < 0)
37428fd017aSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
375f173abd0SMike Pritchard 	if (now == 0)
376f173abd0SMike Pritchard 		now = time(NULL);
3774b88c807SRodney W. Grimes 
378656dcd43SGarrett Wollman #define	SIXMONTHS	((365 / 2) * 86400)
3794b88c807SRodney W. Grimes 	if (f_sectime)
3801e715e34SJosef Karthauser 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
38128fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
382f173abd0SMike Pritchard 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
3831e715e34SJosef Karthauser 		/* mmm dd hh:mm || dd mmm hh:mm */
38428fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %R " : "%b %e %R ";
38597e4e97bSJosef Karthauser 	else
3861e715e34SJosef Karthauser 		/* mmm dd  yyyy || dd mmm  yyyy */
38728fd017aSAndrey A. Chernov 		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
38897e4e97bSJosef Karthauser 	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
38997e4e97bSJosef Karthauser 	fputs(longstring, stdout);
3904b88c807SRodney W. Grimes }
3914b88c807SRodney W. Grimes 
3924b88c807SRodney W. Grimes static int
39346251ddeSWarner Losh printtype(u_int mode)
3944b88c807SRodney W. Grimes {
39594274c73STim J. Robbins 
39694274c73STim J. Robbins 	if (f_slash) {
39794274c73STim J. Robbins 		if ((mode & S_IFMT) == S_IFDIR) {
39894274c73STim J. Robbins 			(void)putchar('/');
39994274c73STim J. Robbins 			return (1);
40094274c73STim J. Robbins 		}
40194274c73STim J. Robbins 		return (0);
40294274c73STim J. Robbins 	}
40394274c73STim J. Robbins 
4044b88c807SRodney W. Grimes 	switch (mode & S_IFMT) {
4054b88c807SRodney W. Grimes 	case S_IFDIR:
4064b88c807SRodney W. Grimes 		(void)putchar('/');
4074b88c807SRodney W. Grimes 		return (1);
4084b88c807SRodney W. Grimes 	case S_IFIFO:
4094b88c807SRodney W. Grimes 		(void)putchar('|');
4104b88c807SRodney W. Grimes 		return (1);
4114b88c807SRodney W. Grimes 	case S_IFLNK:
4124b88c807SRodney W. Grimes 		(void)putchar('@');
4134b88c807SRodney W. Grimes 		return (1);
4144b88c807SRodney W. Grimes 	case S_IFSOCK:
4154b88c807SRodney W. Grimes 		(void)putchar('=');
4164b88c807SRodney W. Grimes 		return (1);
417fb5cb208SSteve Price 	case S_IFWHT:
418fb5cb208SSteve Price 		(void)putchar('%');
419fb5cb208SSteve Price 		return (1);
4209052855aSMark Murray 	default:
421568dcd5fSBill Fumerola 		break;
4224b88c807SRodney W. Grimes 	}
4234b88c807SRodney W. Grimes 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4244b88c807SRodney W. Grimes 		(void)putchar('*');
4254b88c807SRodney W. Grimes 		return (1);
4264b88c807SRodney W. Grimes 	}
4274b88c807SRodney W. Grimes 	return (0);
4284b88c807SRodney W. Grimes }
4294b88c807SRodney W. Grimes 
43074985094SJosef Karthauser #ifdef COLORLS
4311bf1478aSAndrey A. Chernov static int
43246251ddeSWarner Losh putch(int c)
433cf0feaeeSAndrey A. Chernov {
43438782c25SAndrey A. Chernov 	(void)putchar(c);
43538782c25SAndrey A. Chernov 	return 0;
436cf0feaeeSAndrey A. Chernov }
437cf0feaeeSAndrey A. Chernov 
4381bf1478aSAndrey A. Chernov static int
43946251ddeSWarner Losh writech(int c)
44038782c25SAndrey A. Chernov {
44140feca3aSMark Murray 	char tmp = (char)c;
44238782c25SAndrey A. Chernov 
44338782c25SAndrey A. Chernov 	(void)write(STDOUT_FILENO, &tmp, 1);
44438782c25SAndrey A. Chernov 	return 0;
44538782c25SAndrey A. Chernov }
446cf0feaeeSAndrey A. Chernov 
4471bf1478aSAndrey A. Chernov static void
44846251ddeSWarner Losh printcolor(Colors c)
4493885812cSJosef Karthauser {
45074985094SJosef Karthauser 	char *ansiseq;
45174985094SJosef Karthauser 
452c1499cf6SJosef Karthauser 	if (colors[c].bold)
453c1499cf6SJosef Karthauser 		tputs(enter_bold, 1, putch);
454c1499cf6SJosef Karthauser 
455c1499cf6SJosef Karthauser 	if (colors[c].num[0] != -1) {
456c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
45738782c25SAndrey A. Chernov 		if (ansiseq)
458cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
4593885812cSJosef Karthauser 	}
460c1499cf6SJosef Karthauser 	if (colors[c].num[1] != -1) {
461c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
46238782c25SAndrey A. Chernov 		if (ansiseq)
463cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
46474985094SJosef Karthauser 	}
46574985094SJosef Karthauser }
46674985094SJosef Karthauser 
46738782c25SAndrey A. Chernov static void
46846251ddeSWarner Losh endcolor(int sig)
46974985094SJosef Karthauser {
47038782c25SAndrey A. Chernov 	tputs(ansi_coloff, 1, sig ? writech : putch);
471c1499cf6SJosef Karthauser 	tputs(attrs_off, 1, sig ? writech : putch);
4723885812cSJosef Karthauser }
4733885812cSJosef Karthauser 
47438782c25SAndrey A. Chernov static int
47546251ddeSWarner Losh colortype(mode_t mode)
4763885812cSJosef Karthauser {
4773885812cSJosef Karthauser 	switch (mode & S_IFMT) {
4783885812cSJosef Karthauser 	case S_IFDIR:
4793885812cSJosef Karthauser 		if (mode & S_IWOTH)
4803885812cSJosef Karthauser 			if (mode & S_ISTXT)
4813885812cSJosef Karthauser 				printcolor(C_WSDIR);
4823885812cSJosef Karthauser 			else
4833885812cSJosef Karthauser 				printcolor(C_WDIR);
4843885812cSJosef Karthauser 		else
4853885812cSJosef Karthauser 			printcolor(C_DIR);
4863885812cSJosef Karthauser 		return (1);
4873885812cSJosef Karthauser 	case S_IFLNK:
4883885812cSJosef Karthauser 		printcolor(C_LNK);
4893885812cSJosef Karthauser 		return (1);
4903885812cSJosef Karthauser 	case S_IFSOCK:
4913885812cSJosef Karthauser 		printcolor(C_SOCK);
4923885812cSJosef Karthauser 		return (1);
4933885812cSJosef Karthauser 	case S_IFIFO:
4943885812cSJosef Karthauser 		printcolor(C_FIFO);
4953885812cSJosef Karthauser 		return (1);
4963885812cSJosef Karthauser 	case S_IFBLK:
4973885812cSJosef Karthauser 		printcolor(C_BLK);
4983885812cSJosef Karthauser 		return (1);
4993885812cSJosef Karthauser 	case S_IFCHR:
5003885812cSJosef Karthauser 		printcolor(C_CHR);
5013885812cSJosef Karthauser 		return (1);
50240feca3aSMark Murray 	default:;
5033885812cSJosef Karthauser 	}
5043885812cSJosef Karthauser 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
5053885812cSJosef Karthauser 		if (mode & S_ISUID)
5063885812cSJosef Karthauser 			printcolor(C_SUID);
5073885812cSJosef Karthauser 		else if (mode & S_ISGID)
5083885812cSJosef Karthauser 			printcolor(C_SGID);
5093885812cSJosef Karthauser 		else
5103885812cSJosef Karthauser 			printcolor(C_EXEC);
5113885812cSJosef Karthauser 		return (1);
5123885812cSJosef Karthauser 	}
5133885812cSJosef Karthauser 	return (0);
5143885812cSJosef Karthauser }
5153885812cSJosef Karthauser 
5163885812cSJosef Karthauser void
51746251ddeSWarner Losh parsecolors(const char *cs)
5183885812cSJosef Karthauser {
519b0bc91e9SJosef Karthauser 	int i;
520b0bc91e9SJosef Karthauser 	int j;
521ca2993fbSMark Murray 	size_t len;
5223885812cSJosef Karthauser 	char c[2];
523c1499cf6SJosef Karthauser 	short legacy_warn = 0;
52438782c25SAndrey A. Chernov 
525b0bc91e9SJosef Karthauser 	if (cs == NULL)
526b0bc91e9SJosef Karthauser 		cs = "";	/* LSCOLORS not set */
5273885812cSJosef Karthauser 	len = strlen(cs);
52840feca3aSMark Murray 	for (i = 0; i < (int)C_NUMCOLORS; i++) {
529c1499cf6SJosef Karthauser 		colors[i].bold = 0;
530c1499cf6SJosef Karthauser 
53140feca3aSMark Murray 		if (len <= 2 * (size_t)i) {
5323885812cSJosef Karthauser 			c[0] = defcolors[2 * i];
5333885812cSJosef Karthauser 			c[1] = defcolors[2 * i + 1];
5345dda5d0dSJosef Karthauser 		} else {
5353885812cSJosef Karthauser 			c[0] = cs[2 * i];
5363885812cSJosef Karthauser 			c[1] = cs[2 * i + 1];
5373885812cSJosef Karthauser 		}
5383885812cSJosef Karthauser 		for (j = 0; j < 2; j++) {
539c1499cf6SJosef Karthauser 			/* Legacy colours used 0-7 */
540c1499cf6SJosef Karthauser 			if (c[j] >= '0' && c[j] <= '7') {
541c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - '0';
542c1499cf6SJosef Karthauser 				if (!legacy_warn) {
543e09fdabdSTim J. Robbins 					warnx("LSCOLORS should use "
544130d15dcSJosef Karthauser 					    "characters a-h instead of 0-9 ("
545e09fdabdSTim J. Robbins 					    "see the manual page)");
5463885812cSJosef Karthauser 				}
547c1499cf6SJosef Karthauser 				legacy_warn = 1;
548c1499cf6SJosef Karthauser 			} else if (c[j] >= 'a' && c[j] <= 'h')
549c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'a';
550c1499cf6SJosef Karthauser 			else if (c[j] >= 'A' && c[j] <= 'H') {
551c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'A';
552c1499cf6SJosef Karthauser 				colors[i].bold = 1;
55340feca3aSMark Murray 			} else if (tolower((unsigned char)c[j]) == 'x')
554c1499cf6SJosef Karthauser 				colors[i].num[j] = -1;
555c1499cf6SJosef Karthauser 			else {
556e09fdabdSTim J. Robbins 				warnx("invalid character '%c' in LSCOLORS"
557e09fdabdSTim J. Robbins 				    " env var", c[j]);
5580d72516eSJosef Karthauser 				colors[i].num[j] = -1;
559c1499cf6SJosef Karthauser 			}
5603885812cSJosef Karthauser 		}
5613885812cSJosef Karthauser 	}
5623885812cSJosef Karthauser }
563cf0feaeeSAndrey A. Chernov 
5641bf1478aSAndrey A. Chernov void
56546251ddeSWarner Losh colorquit(int sig)
566cf0feaeeSAndrey A. Chernov {
56738782c25SAndrey A. Chernov 	endcolor(sig);
568faebfe2eSAndrey A. Chernov 
569faebfe2eSAndrey A. Chernov 	(void)signal(sig, SIG_DFL);
570faebfe2eSAndrey A. Chernov 	(void)kill(getpid(), sig);
571cf0feaeeSAndrey A. Chernov }
5725dda5d0dSJosef Karthauser 
57374985094SJosef Karthauser #endif /* COLORLS */
5743885812cSJosef Karthauser 
5754b88c807SRodney W. Grimes static void
576ca2993fbSMark Murray printlink(const FTSENT *p)
5774b88c807SRodney W. Grimes {
5784b88c807SRodney W. Grimes 	int lnklen;
579b0bc91e9SJosef Karthauser 	char name[MAXPATHLEN + 1];
580b0bc91e9SJosef Karthauser 	char path[MAXPATHLEN + 1];
5814b88c807SRodney W. Grimes 
5824b88c807SRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL)
5834b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
5844b88c807SRodney W. Grimes 	else
5854b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name),
5864b88c807SRodney W. Grimes 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
5874b88c807SRodney W. Grimes 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
5884b88c807SRodney W. Grimes 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
5894b88c807SRodney W. Grimes 		return;
5904b88c807SRodney W. Grimes 	}
5914b88c807SRodney W. Grimes 	path[lnklen] = '\0';
5927ea30648SDag-Erling Smørgrav 	(void)printf(" -> ");
5939052855aSMark Murray 	(void)printname(path);
5944b88c807SRodney W. Grimes }
5950e8d1551SJosef Karthauser 
5960e8d1551SJosef Karthauser static void
59746251ddeSWarner Losh printsize(size_t width, off_t bytes)
5980e8d1551SJosef Karthauser {
5990e8d1551SJosef Karthauser 
6000e8d1551SJosef Karthauser 	if (f_humanval) {
601478aa805SPawel Jakub Dawidek 		char buf[5];
6020e8d1551SJosef Karthauser 
603478aa805SPawel Jakub Dawidek 		humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
604478aa805SPawel Jakub Dawidek 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
605478aa805SPawel Jakub Dawidek 		(void)printf("%5s ", buf);
6065dda5d0dSJosef Karthauser 	} else
60740feca3aSMark Murray 		(void)printf("%*jd ", (u_int)width, bytes);
6080e8d1551SJosef Karthauser }
6090e8d1551SJosef Karthauser 
610dd9aaeb0STim J. Robbins static void
61140feca3aSMark Murray aclmode(char *buf, const FTSENT *p, int *haveacls)
612dd9aaeb0STim J. Robbins {
613dd9aaeb0STim J. Robbins 	char name[MAXPATHLEN + 1];
614dd9aaeb0STim J. Robbins 	int entries, ret;
615dd9aaeb0STim J. Robbins 	acl_t facl;
616dd9aaeb0STim J. Robbins 	acl_entry_t ae;
617dd9aaeb0STim J. Robbins 
618dd9aaeb0STim J. Robbins 	/*
619dd9aaeb0STim J. Robbins 	 * Add a + after the standard rwxrwxrwx mode if the file has an
620dd9aaeb0STim J. Robbins 	 * extended ACL. strmode() reserves space at the end of the string.
621dd9aaeb0STim J. Robbins 	 */
622dd9aaeb0STim J. Robbins 	if (p->fts_level == FTS_ROOTLEVEL)
623dd9aaeb0STim J. Robbins 		snprintf(name, sizeof(name), "%s", p->fts_name);
624dd9aaeb0STim J. Robbins 	else
625dd9aaeb0STim J. Robbins 		snprintf(name, sizeof(name), "%s/%s",
626dd9aaeb0STim J. Robbins 		    p->fts_parent->fts_accpath, p->fts_name);
6273fceb9fdSTim J. Robbins 	/*
6283fceb9fdSTim J. Robbins 	 * We have no way to tell whether a symbolic link has an ACL since
6293fceb9fdSTim J. Robbins 	 * pathconf() and acl_get_file() both follow them.
6303fceb9fdSTim J. Robbins 	 */
6313fceb9fdSTim J. Robbins 	if (S_ISLNK(p->fts_statp->st_mode)) {
6323fceb9fdSTim J. Robbins 		*haveacls = 1;
6333fceb9fdSTim J. Robbins 		return;
6343fceb9fdSTim J. Robbins 	}
635dd9aaeb0STim J. Robbins 	if ((ret = pathconf(name, _PC_ACL_EXTENDED)) <= 0) {
636dd9aaeb0STim J. Robbins 		if (ret < 0 && errno != EINVAL)
637dd9aaeb0STim J. Robbins 			warn("%s", name);
638dd9aaeb0STim J. Robbins 		else
639dd9aaeb0STim J. Robbins 			*haveacls = 0;
640dd9aaeb0STim J. Robbins 		return;
641dd9aaeb0STim J. Robbins 	}
642dd9aaeb0STim J. Robbins 	*haveacls = 1;
643dd9aaeb0STim J. Robbins 	if ((facl = acl_get_file(name, ACL_TYPE_ACCESS)) != NULL) {
644dd9aaeb0STim J. Robbins 		if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
64526337733SBosko Milekic 			entries = 1;
64626337733SBosko Milekic 			while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1)
64726337733SBosko Milekic 				if (++entries > 3)
64826337733SBosko Milekic 					break;
64926337733SBosko Milekic 			/*
65026337733SBosko Milekic 			 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
65126337733SBosko Milekic 			 * must have at least three entries (owner, group,
65226337733SBosko Milekic 			 * and other). So anything with more than 3 ACLs looks
65326337733SBosko Milekic 			 * interesting to us.
65426337733SBosko Milekic 			 */
65526337733SBosko Milekic 			if (entries > 3)
656dd9aaeb0STim J. Robbins 				buf[10] = '+';
657dd9aaeb0STim J. Robbins 		}
658dd9aaeb0STim J. Robbins 		acl_free(facl);
659dd9aaeb0STim J. Robbins 	} else
660dd9aaeb0STim J. Robbins 		warn("%s", name);
661dd9aaeb0STim J. Robbins }
662