xref: /freebsd/bin/ls/print.c (revision 1656f85050b2719bc67723f341086e956b62864a)
14b88c807SRodney W. Grimes /*
24b88c807SRodney W. Grimes  * Copyright (c) 1989, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Michael Fischbein.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
37febad2fcSSteve Price #if 0
38c73d77ceSMark Murray #ifndef lint
39febad2fcSSteve Price static char sccsid[] = "@(#)print.c	8.4 (Berkeley) 4/17/94";
404b88c807SRodney W. Grimes #endif /* not lint */
41c73d77ceSMark Murray #endif
425eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
435eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes #include <sys/param.h>
464b88c807SRodney W. Grimes #include <sys/stat.h>
474b88c807SRodney W. Grimes 
484b88c807SRodney W. Grimes #include <err.h>
494b88c807SRodney W. Grimes #include <errno.h>
504b88c807SRodney W. Grimes #include <fts.h>
510e8d1551SJosef Karthauser #include <math.h>
5228fd017aSAndrey A. Chernov #include <langinfo.h>
534b88c807SRodney W. Grimes #include <stdio.h>
544b88c807SRodney W. Grimes #include <stdlib.h>
554b88c807SRodney W. Grimes #include <string.h>
56434b6ea4SBruce Evans #include <time.h>
574b88c807SRodney W. Grimes #include <unistd.h>
58faebfe2eSAndrey A. Chernov #ifdef COLORLS
59faebfe2eSAndrey A. Chernov #include <ctype.h>
60faebfe2eSAndrey A. Chernov #include <termcap.h>
61faebfe2eSAndrey A. Chernov #include <signal.h>
62faebfe2eSAndrey A. Chernov #endif
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes #include "ls.h"
654b88c807SRodney W. Grimes #include "extern.h"
664b88c807SRodney W. Grimes 
6746251ddeSWarner Losh static int	printaname(FTSENT *, u_long, u_long);
6846251ddeSWarner Losh static void	printlink(FTSENT *);
6946251ddeSWarner Losh static void	printtime(time_t);
7046251ddeSWarner Losh static int	printtype(u_int);
7146251ddeSWarner Losh static void	printsize(size_t, off_t);
7238782c25SAndrey A. Chernov #ifdef COLORLS
7346251ddeSWarner Losh static void	endcolor(int);
7446251ddeSWarner Losh static int	colortype(mode_t);
7538782c25SAndrey A. Chernov #endif
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
780e8d1551SJosef Karthauser 
790e8d1551SJosef Karthauser #define KILO_SZ(n) (n)
800e8d1551SJosef Karthauser #define MEGA_SZ(n) ((n) * (n))
810e8d1551SJosef Karthauser #define GIGA_SZ(n) ((n) * (n) * (n))
820e8d1551SJosef Karthauser #define TERA_SZ(n) ((n) * (n) * (n) * (n))
830e8d1551SJosef Karthauser #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
840e8d1551SJosef Karthauser 
850e8d1551SJosef Karthauser #define KILO_2_SZ (KILO_SZ(1024ULL))
860e8d1551SJosef Karthauser #define MEGA_2_SZ (MEGA_SZ(1024ULL))
870e8d1551SJosef Karthauser #define GIGA_2_SZ (GIGA_SZ(1024ULL))
880e8d1551SJosef Karthauser #define TERA_2_SZ (TERA_SZ(1024ULL))
890e8d1551SJosef Karthauser #define PETA_2_SZ (PETA_SZ(1024ULL))
900e8d1551SJosef Karthauser 
919052855aSMark Murray static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
920e8d1551SJosef Karthauser 
935dda5d0dSJosef Karthauser typedef enum {
945dda5d0dSJosef Karthauser 	NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX
955dda5d0dSJosef Karthauser } unit_t;
96598420eeSDima Dorfman static unit_t unit_adjust(double *);
970e8d1551SJosef Karthauser 
989052855aSMark Murray static int unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA};
994b88c807SRodney W. Grimes 
10074985094SJosef Karthauser #ifdef COLORLS
1013885812cSJosef Karthauser /* Most of these are taken from <sys/stat.h> */
1023885812cSJosef Karthauser typedef enum Colors {
1033885812cSJosef Karthauser 	C_DIR,			/* directory */
1043885812cSJosef Karthauser 	C_LNK,			/* symbolic link */
1053885812cSJosef Karthauser 	C_SOCK,			/* socket */
1063885812cSJosef Karthauser 	C_FIFO,			/* pipe */
1073885812cSJosef Karthauser 	C_EXEC,			/* executable */
1083885812cSJosef Karthauser 	C_BLK,			/* block special */
1093885812cSJosef Karthauser 	C_CHR,			/* character special */
1103885812cSJosef Karthauser 	C_SUID,			/* setuid executable */
1113885812cSJosef Karthauser 	C_SGID,			/* setgid executable */
1125dda5d0dSJosef Karthauser 	C_WSDIR,		/* directory writeble to others, with sticky
1135dda5d0dSJosef Karthauser 				 * bit */
1145dda5d0dSJosef Karthauser 	C_WDIR,			/* directory writeble to others, without
1155dda5d0dSJosef Karthauser 				 * sticky bit */
1163885812cSJosef Karthauser 	C_NUMCOLORS		/* just a place-holder */
1173885812cSJosef Karthauser } Colors;
1183885812cSJosef Karthauser 
1199052855aSMark Murray static const char *defcolors = "exfxcxdxbxegedabagacad";
1203885812cSJosef Karthauser 
121c1499cf6SJosef Karthauser /* colors for file types */
122c1499cf6SJosef Karthauser static struct {
123c1499cf6SJosef Karthauser 	int	num[2];
124c1499cf6SJosef Karthauser 	int	bold;
125c1499cf6SJosef Karthauser } colors[C_NUMCOLORS];
12674985094SJosef Karthauser #endif
1273885812cSJosef Karthauser 
1284b88c807SRodney W. Grimes void
12946251ddeSWarner Losh printscol(DISPLAY *dp)
1304b88c807SRodney W. Grimes {
1314b88c807SRodney W. Grimes 	FTSENT *p;
1324b88c807SRodney W. Grimes 
1334b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1344b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1354b88c807SRodney W. Grimes 			continue;
1364b88c807SRodney W. Grimes 		(void)printaname(p, dp->s_inode, dp->s_block);
1374b88c807SRodney W. Grimes 		(void)putchar('\n');
1384b88c807SRodney W. Grimes 	}
1394b88c807SRodney W. Grimes }
1404b88c807SRodney W. Grimes 
141ee579ffbSAssar Westerlund /*
142ee579ffbSAssar Westerlund  * print name in current style
143ee579ffbSAssar Westerlund  */
1441656f850STim J. Robbins int
14546251ddeSWarner Losh printname(const char *name)
146ee579ffbSAssar Westerlund {
147ee579ffbSAssar Westerlund 	if (f_octal || f_octal_escape)
148ee579ffbSAssar Westerlund 		return prn_octal(name);
149ee579ffbSAssar Westerlund 	else if (f_nonprint)
150ee579ffbSAssar Westerlund 		return prn_printable(name);
151ee579ffbSAssar Westerlund 	else
152ee579ffbSAssar Westerlund 		return printf("%s", name);
153ee579ffbSAssar Westerlund }
154ee579ffbSAssar Westerlund 
1554b88c807SRodney W. Grimes void
15646251ddeSWarner Losh printlong(DISPLAY *dp)
1574b88c807SRodney W. Grimes {
1584b88c807SRodney W. Grimes 	struct stat *sp;
1594b88c807SRodney W. Grimes 	FTSENT *p;
1604b88c807SRodney W. Grimes 	NAMES *np;
1614b88c807SRodney W. Grimes 	char buf[20];
16247bb6b11SAndrey A. Chernov #ifdef COLORLS
16347bb6b11SAndrey A. Chernov 	int color_printed = 0;
16447bb6b11SAndrey A. Chernov #endif
1654b88c807SRodney W. Grimes 
1664b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1674b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1684b88c807SRodney W. Grimes 
1694b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1704b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1714b88c807SRodney W. Grimes 			continue;
1724b88c807SRodney W. Grimes 		sp = p->fts_statp;
1734b88c807SRodney W. Grimes 		if (f_inode)
174fb5cb208SSteve Price 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1754b88c807SRodney W. Grimes 		if (f_size)
1769052855aSMark Murray 			(void)printf("%*lld ",
1774b88c807SRodney W. Grimes 			    dp->s_block, howmany(sp->st_blocks, blocksize));
1789052855aSMark Murray 		strmode(sp->st_mode, buf);
1794b88c807SRodney W. Grimes 		np = p->fts_pointer;
1804b88c807SRodney W. Grimes 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1814b88c807SRodney W. Grimes 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1824b88c807SRodney W. Grimes 		    np->group);
1834b88c807SRodney W. Grimes 		if (f_flags)
1844b88c807SRodney W. Grimes 			(void)printf("%-*s ", dp->s_flags, np->flags);
1857304f61fSBrian Feldman 		if (f_lomac)
1867304f61fSBrian Feldman 			(void)printf("%-*s ", dp->s_lattr, np->lattr);
1874b88c807SRodney W. Grimes 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
188029b2bd0SBruce Evans 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
189df2fbf15SJoerg Wunsch 				(void)printf("%3d, 0x%08x ",
190029b2bd0SBruce Evans 				    major(sp->st_rdev),
191029b2bd0SBruce Evans 				    (u_int)minor(sp->st_rdev));
192df2fbf15SJoerg Wunsch 			else
1934b88c807SRodney W. Grimes 				(void)printf("%3d, %3d ",
1944b88c807SRodney W. Grimes 				    major(sp->st_rdev), minor(sp->st_rdev));
1954b88c807SRodney W. Grimes 		else if (dp->bcfile)
1969052855aSMark Murray 			(void)printf("%*s%*lld ",
1974b88c807SRodney W. Grimes 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1984b88c807SRodney W. Grimes 		else
1990e8d1551SJosef Karthauser 			printsize(dp->s_size, sp->st_size);
2004b88c807SRodney W. Grimes 		if (f_accesstime)
2014b88c807SRodney W. Grimes 			printtime(sp->st_atime);
2024b88c807SRodney W. Grimes 		else if (f_statustime)
2034b88c807SRodney W. Grimes 			printtime(sp->st_ctime);
2044b88c807SRodney W. Grimes 		else
2054b88c807SRodney W. Grimes 			printtime(sp->st_mtime);
20674985094SJosef Karthauser #ifdef COLORLS
2073885812cSJosef Karthauser 		if (f_color)
208cf0feaeeSAndrey A. Chernov 			color_printed = colortype(sp->st_mode);
20974985094SJosef Karthauser #endif
210ee579ffbSAssar Westerlund 		(void)printname(p->fts_name);
21174985094SJosef Karthauser #ifdef COLORLS
212cf0feaeeSAndrey A. Chernov 		if (f_color && color_printed)
21338782c25SAndrey A. Chernov 			endcolor(0);
21474985094SJosef Karthauser #endif
2154b88c807SRodney W. Grimes 		if (f_type)
2164b88c807SRodney W. Grimes 			(void)printtype(sp->st_mode);
2174b88c807SRodney W. Grimes 		if (S_ISLNK(sp->st_mode))
2184b88c807SRodney W. Grimes 			printlink(p);
2194b88c807SRodney W. Grimes 		(void)putchar('\n');
2204b88c807SRodney W. Grimes 	}
2214b88c807SRodney W. Grimes }
2224b88c807SRodney W. Grimes 
2234b88c807SRodney W. Grimes void
22494274c73STim J. Robbins printstream(DISPLAY *dp)
22594274c73STim J. Robbins {
22694274c73STim J. Robbins 	FTSENT *p;
22794274c73STim J. Robbins 	extern int termwidth;
22894274c73STim J. Robbins 	int chcnt;
22994274c73STim J. Robbins 
23094274c73STim J. Robbins 	for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
23194274c73STim J. Robbins 		if (p->fts_number == NO_PRINT)
23294274c73STim J. Robbins 			continue;
23394274c73STim J. Robbins 		if (strlen(p->fts_name) + chcnt +
23494274c73STim J. Robbins 		    (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
23594274c73STim J. Robbins 			putchar('\n');
23694274c73STim J. Robbins 			chcnt = 0;
23794274c73STim J. Robbins 		}
23894274c73STim J. Robbins 		chcnt += printaname(p, dp->s_inode, dp->s_block);
23994274c73STim J. Robbins 		if (p->fts_link) {
24094274c73STim J. Robbins 			printf(", ");
24194274c73STim J. Robbins 			chcnt += 2;
24294274c73STim J. Robbins 		}
24394274c73STim J. Robbins 	}
24494274c73STim J. Robbins 	if (chcnt)
24594274c73STim J. Robbins 		putchar('\n');
24694274c73STim J. Robbins }
24794274c73STim J. Robbins 
24894274c73STim J. Robbins void
24946251ddeSWarner Losh printcol(DISPLAY *dp)
2504b88c807SRodney W. Grimes {
2514b88c807SRodney W. Grimes 	extern int termwidth;
2524b88c807SRodney W. Grimes 	static FTSENT **array;
2534b88c807SRodney W. Grimes 	static int lastentries = -1;
2544b88c807SRodney W. Grimes 	FTSENT *p;
255b0bc91e9SJosef Karthauser 	int base;
256b0bc91e9SJosef Karthauser 	int chcnt;
257b0bc91e9SJosef Karthauser 	int cnt;
258b0bc91e9SJosef Karthauser 	int col;
259b0bc91e9SJosef Karthauser 	int colwidth;
260b0bc91e9SJosef Karthauser 	int endcol;
261b0bc91e9SJosef Karthauser 	int num;
262b0bc91e9SJosef Karthauser 	int numcols;
263b0bc91e9SJosef Karthauser 	int numrows;
264b0bc91e9SJosef Karthauser 	int row;
265545f583cSTim Vanderhoek 	int tabwidth;
266545f583cSTim Vanderhoek 
267545f583cSTim Vanderhoek 	if (f_notabs)
268545f583cSTim Vanderhoek 		tabwidth = 1;
269545f583cSTim Vanderhoek 	else
270545f583cSTim Vanderhoek 		tabwidth = 8;
2714b88c807SRodney W. Grimes 
2724b88c807SRodney W. Grimes 	/*
2734b88c807SRodney W. Grimes 	 * Have to do random access in the linked list -- build a table
2744b88c807SRodney W. Grimes 	 * of pointers.
2754b88c807SRodney W. Grimes 	 */
2764b88c807SRodney W. Grimes 	if (dp->entries > lastentries) {
2774b88c807SRodney W. Grimes 		lastentries = dp->entries;
2784b88c807SRodney W. Grimes 		if ((array =
2794b88c807SRodney W. Grimes 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
2804b88c807SRodney W. Grimes 			warn(NULL);
2814b88c807SRodney W. Grimes 			printscol(dp);
2824b88c807SRodney W. Grimes 		}
2834b88c807SRodney W. Grimes 	}
2844b88c807SRodney W. Grimes 	for (p = dp->list, num = 0; p; p = p->fts_link)
2854b88c807SRodney W. Grimes 		if (p->fts_number != NO_PRINT)
2864b88c807SRodney W. Grimes 			array[num++] = p;
2874b88c807SRodney W. Grimes 
2884b88c807SRodney W. Grimes 	colwidth = dp->maxlen;
2894b88c807SRodney W. Grimes 	if (f_inode)
2904b88c807SRodney W. Grimes 		colwidth += dp->s_inode + 1;
2914b88c807SRodney W. Grimes 	if (f_size)
2924b88c807SRodney W. Grimes 		colwidth += dp->s_block + 1;
2934b88c807SRodney W. Grimes 	if (f_type)
2944b88c807SRodney W. Grimes 		colwidth += 1;
2954b88c807SRodney W. Grimes 
296545f583cSTim Vanderhoek 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
2974b88c807SRodney W. Grimes 	if (termwidth < 2 * colwidth) {
2984b88c807SRodney W. Grimes 		printscol(dp);
2994b88c807SRodney W. Grimes 		return;
3004b88c807SRodney W. Grimes 	}
3014b88c807SRodney W. Grimes 	numcols = termwidth / colwidth;
3024b88c807SRodney W. Grimes 	numrows = num / numcols;
3034b88c807SRodney W. Grimes 	if (num % numcols)
3044b88c807SRodney W. Grimes 		++numrows;
3054b88c807SRodney W. Grimes 
3064b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
3074b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
30894274c73STim J. Robbins 
30994274c73STim J. Robbins 	base = 0;
3104b88c807SRodney W. Grimes 	for (row = 0; row < numrows; ++row) {
3114b88c807SRodney W. Grimes 		endcol = colwidth;
31294274c73STim J. Robbins 		if (!f_sortacross)
31394274c73STim J. Robbins 			base = row;
31494274c73STim J. Robbins 		for (col = 0, chcnt = 0; col < numcols; ++col) {
3154b88c807SRodney W. Grimes 			chcnt += printaname(array[base], dp->s_inode,
3164b88c807SRodney W. Grimes 			    dp->s_block);
31794274c73STim J. Robbins 			if (f_sortacross)
31894274c73STim J. Robbins 				base++;
31994274c73STim J. Robbins 			else
32094274c73STim J. Robbins 				base += numrows;
32194274c73STim J. Robbins 			if (base >= num)
3224b88c807SRodney W. Grimes 				break;
323545f583cSTim Vanderhoek 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
324545f583cSTim Vanderhoek 			    <= endcol) {
32594274c73STim J. Robbins 				if (f_sortacross && col + 1 >= numcols)
32694274c73STim J. Robbins 					break;
327545f583cSTim Vanderhoek 				(void)putchar(f_notabs ? ' ' : '\t');
3284b88c807SRodney W. Grimes 				chcnt = cnt;
3294b88c807SRodney W. Grimes 			}
3304b88c807SRodney W. Grimes 			endcol += colwidth;
3314b88c807SRodney W. Grimes 		}
3324b88c807SRodney W. Grimes 		(void)putchar('\n');
3334b88c807SRodney W. Grimes 	}
3344b88c807SRodney W. Grimes }
3354b88c807SRodney W. Grimes 
3364b88c807SRodney W. Grimes /*
3374b88c807SRodney W. Grimes  * print [inode] [size] name
3384b88c807SRodney W. Grimes  * return # of characters printed, no trailing characters.
3394b88c807SRodney W. Grimes  */
3404b88c807SRodney W. Grimes static int
34146251ddeSWarner Losh printaname(FTSENT *p, u_long inodefield, u_long sizefield)
3424b88c807SRodney W. Grimes {
3434b88c807SRodney W. Grimes 	struct stat *sp;
3444b88c807SRodney W. Grimes 	int chcnt;
34547bb6b11SAndrey A. Chernov #ifdef COLORLS
34647bb6b11SAndrey A. Chernov 	int color_printed = 0;
34747bb6b11SAndrey A. Chernov #endif
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes 	sp = p->fts_statp;
3504b88c807SRodney W. Grimes 	chcnt = 0;
3514b88c807SRodney W. Grimes 	if (f_inode)
352fb5cb208SSteve Price 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
3534b88c807SRodney W. Grimes 	if (f_size)
3549052855aSMark Murray 		chcnt += printf("%*lld ",
3554b88c807SRodney W. Grimes 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
35674985094SJosef Karthauser #ifdef COLORLS
3573885812cSJosef Karthauser 	if (f_color)
358cf0feaeeSAndrey A. Chernov 		color_printed = colortype(sp->st_mode);
35974985094SJosef Karthauser #endif
360ee579ffbSAssar Westerlund 	chcnt += printname(p->fts_name);
36174985094SJosef Karthauser #ifdef COLORLS
362cf0feaeeSAndrey A. Chernov 	if (f_color && color_printed)
36338782c25SAndrey A. Chernov 		endcolor(0);
36474985094SJosef Karthauser #endif
3654b88c807SRodney W. Grimes 	if (f_type)
3664b88c807SRodney W. Grimes 		chcnt += printtype(sp->st_mode);
3674b88c807SRodney W. Grimes 	return (chcnt);
3684b88c807SRodney W. Grimes }
3694b88c807SRodney W. Grimes 
3704b88c807SRodney W. Grimes static void
37146251ddeSWarner Losh printtime(time_t ftime)
3724b88c807SRodney W. Grimes {
373fc4a9bafSAndrey A. Chernov 	char longstring[80];
374f173abd0SMike Pritchard 	static time_t now;
37597e4e97bSJosef Karthauser 	const char *format;
3768234eb25SAndrey A. Chernov 	static int d_first = -1;
377f173abd0SMike Pritchard 
37828fd017aSAndrey A. Chernov 	if (d_first < 0)
37928fd017aSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
380f173abd0SMike Pritchard 	if (now == 0)
381f173abd0SMike Pritchard 		now = time(NULL);
3824b88c807SRodney W. Grimes 
383656dcd43SGarrett Wollman #define	SIXMONTHS	((365 / 2) * 86400)
3844b88c807SRodney W. Grimes 	if (f_sectime)
3851e715e34SJosef Karthauser 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
38628fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
387f173abd0SMike Pritchard 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
3881e715e34SJosef Karthauser 		/* mmm dd hh:mm || dd mmm hh:mm */
38928fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %R " : "%b %e %R ";
39097e4e97bSJosef Karthauser 	else
3911e715e34SJosef Karthauser 		/* mmm dd  yyyy || dd mmm  yyyy */
39228fd017aSAndrey A. Chernov 		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
39397e4e97bSJosef Karthauser 	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
39497e4e97bSJosef Karthauser 	fputs(longstring, stdout);
3954b88c807SRodney W. Grimes }
3964b88c807SRodney W. Grimes 
3974b88c807SRodney W. Grimes static int
39846251ddeSWarner Losh printtype(u_int mode)
3994b88c807SRodney W. Grimes {
40094274c73STim J. Robbins 
40194274c73STim J. Robbins 	if (f_slash) {
40294274c73STim J. Robbins 		if ((mode & S_IFMT) == S_IFDIR) {
40394274c73STim J. Robbins 			(void)putchar('/');
40494274c73STim J. Robbins 			return (1);
40594274c73STim J. Robbins 		}
40694274c73STim J. Robbins 		return (0);
40794274c73STim J. Robbins 	}
40894274c73STim J. Robbins 
4094b88c807SRodney W. Grimes 	switch (mode & S_IFMT) {
4104b88c807SRodney W. Grimes 	case S_IFDIR:
4114b88c807SRodney W. Grimes 		(void)putchar('/');
4124b88c807SRodney W. Grimes 		return (1);
4134b88c807SRodney W. Grimes 	case S_IFIFO:
4144b88c807SRodney W. Grimes 		(void)putchar('|');
4154b88c807SRodney W. Grimes 		return (1);
4164b88c807SRodney W. Grimes 	case S_IFLNK:
4174b88c807SRodney W. Grimes 		(void)putchar('@');
4184b88c807SRodney W. Grimes 		return (1);
4194b88c807SRodney W. Grimes 	case S_IFSOCK:
4204b88c807SRodney W. Grimes 		(void)putchar('=');
4214b88c807SRodney W. Grimes 		return (1);
422fb5cb208SSteve Price 	case S_IFWHT:
423fb5cb208SSteve Price 		(void)putchar('%');
424fb5cb208SSteve Price 		return (1);
4259052855aSMark Murray 	default:
426568dcd5fSBill Fumerola 		break;
4274b88c807SRodney W. Grimes 	}
4284b88c807SRodney W. Grimes 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4294b88c807SRodney W. Grimes 		(void)putchar('*');
4304b88c807SRodney W. Grimes 		return (1);
4314b88c807SRodney W. Grimes 	}
4324b88c807SRodney W. Grimes 	return (0);
4334b88c807SRodney W. Grimes }
4344b88c807SRodney W. Grimes 
43574985094SJosef Karthauser #ifdef COLORLS
4361bf1478aSAndrey A. Chernov static int
43746251ddeSWarner Losh putch(int c)
438cf0feaeeSAndrey A. Chernov {
43938782c25SAndrey A. Chernov 	(void)putchar(c);
44038782c25SAndrey A. Chernov 	return 0;
441cf0feaeeSAndrey A. Chernov }
442cf0feaeeSAndrey A. Chernov 
4431bf1478aSAndrey A. Chernov static int
44446251ddeSWarner Losh writech(int c)
44538782c25SAndrey A. Chernov {
44638782c25SAndrey A. Chernov 	char tmp = c;
44738782c25SAndrey A. Chernov 
44838782c25SAndrey A. Chernov 	(void)write(STDOUT_FILENO, &tmp, 1);
44938782c25SAndrey A. Chernov 	return 0;
45038782c25SAndrey A. Chernov }
451cf0feaeeSAndrey A. Chernov 
4521bf1478aSAndrey A. Chernov static void
45346251ddeSWarner Losh printcolor(Colors c)
4543885812cSJosef Karthauser {
45574985094SJosef Karthauser 	char *ansiseq;
45674985094SJosef Karthauser 
457c1499cf6SJosef Karthauser 	if (colors[c].bold)
458c1499cf6SJosef Karthauser 		tputs(enter_bold, 1, putch);
459c1499cf6SJosef Karthauser 
460c1499cf6SJosef Karthauser 	if (colors[c].num[0] != -1) {
461c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
46238782c25SAndrey A. Chernov 		if (ansiseq)
463cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
4643885812cSJosef Karthauser 	}
465c1499cf6SJosef Karthauser 	if (colors[c].num[1] != -1) {
466c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
46738782c25SAndrey A. Chernov 		if (ansiseq)
468cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
46974985094SJosef Karthauser 	}
47074985094SJosef Karthauser }
47174985094SJosef Karthauser 
47238782c25SAndrey A. Chernov static void
47346251ddeSWarner Losh endcolor(int sig)
47474985094SJosef Karthauser {
47538782c25SAndrey A. Chernov 	tputs(ansi_coloff, 1, sig ? writech : putch);
476c1499cf6SJosef Karthauser 	tputs(attrs_off, 1, sig ? writech : putch);
4773885812cSJosef Karthauser }
4783885812cSJosef Karthauser 
47938782c25SAndrey A. Chernov static int
48046251ddeSWarner Losh colortype(mode_t mode)
4813885812cSJosef Karthauser {
4823885812cSJosef Karthauser 	switch (mode & S_IFMT) {
4833885812cSJosef Karthauser 	case S_IFDIR:
4843885812cSJosef Karthauser 		if (mode & S_IWOTH)
4853885812cSJosef Karthauser 			if (mode & S_ISTXT)
4863885812cSJosef Karthauser 				printcolor(C_WSDIR);
4873885812cSJosef Karthauser 			else
4883885812cSJosef Karthauser 				printcolor(C_WDIR);
4893885812cSJosef Karthauser 		else
4903885812cSJosef Karthauser 			printcolor(C_DIR);
4913885812cSJosef Karthauser 		return (1);
4923885812cSJosef Karthauser 	case S_IFLNK:
4933885812cSJosef Karthauser 		printcolor(C_LNK);
4943885812cSJosef Karthauser 		return (1);
4953885812cSJosef Karthauser 	case S_IFSOCK:
4963885812cSJosef Karthauser 		printcolor(C_SOCK);
4973885812cSJosef Karthauser 		return (1);
4983885812cSJosef Karthauser 	case S_IFIFO:
4993885812cSJosef Karthauser 		printcolor(C_FIFO);
5003885812cSJosef Karthauser 		return (1);
5013885812cSJosef Karthauser 	case S_IFBLK:
5023885812cSJosef Karthauser 		printcolor(C_BLK);
5033885812cSJosef Karthauser 		return (1);
5043885812cSJosef Karthauser 	case S_IFCHR:
5053885812cSJosef Karthauser 		printcolor(C_CHR);
5063885812cSJosef Karthauser 		return (1);
5073885812cSJosef Karthauser 	}
5083885812cSJosef Karthauser 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
5093885812cSJosef Karthauser 		if (mode & S_ISUID)
5103885812cSJosef Karthauser 			printcolor(C_SUID);
5113885812cSJosef Karthauser 		else if (mode & S_ISGID)
5123885812cSJosef Karthauser 			printcolor(C_SGID);
5133885812cSJosef Karthauser 		else
5143885812cSJosef Karthauser 			printcolor(C_EXEC);
5153885812cSJosef Karthauser 		return (1);
5163885812cSJosef Karthauser 	}
5173885812cSJosef Karthauser 	return (0);
5183885812cSJosef Karthauser }
5193885812cSJosef Karthauser 
5203885812cSJosef Karthauser void
52146251ddeSWarner Losh parsecolors(const char *cs)
5223885812cSJosef Karthauser {
523b0bc91e9SJosef Karthauser 	int i;
524b0bc91e9SJosef Karthauser 	int j;
525b0bc91e9SJosef Karthauser 	int len;
5263885812cSJosef Karthauser 	char c[2];
527c1499cf6SJosef Karthauser 	short legacy_warn = 0;
52838782c25SAndrey A. Chernov 
529b0bc91e9SJosef Karthauser 	if (cs == NULL)
530b0bc91e9SJosef Karthauser 		cs = "";	/* LSCOLORS not set */
5313885812cSJosef Karthauser 	len = strlen(cs);
5323885812cSJosef Karthauser 	for (i = 0; i < C_NUMCOLORS; i++) {
533c1499cf6SJosef Karthauser 		colors[i].bold = 0;
534c1499cf6SJosef Karthauser 
5353885812cSJosef Karthauser 		if (len <= 2 * i) {
5363885812cSJosef Karthauser 			c[0] = defcolors[2 * i];
5373885812cSJosef Karthauser 			c[1] = defcolors[2 * i + 1];
5385dda5d0dSJosef Karthauser 		} else {
5393885812cSJosef Karthauser 			c[0] = cs[2 * i];
5403885812cSJosef Karthauser 			c[1] = cs[2 * i + 1];
5413885812cSJosef Karthauser 		}
5423885812cSJosef Karthauser 		for (j = 0; j < 2; j++) {
543c1499cf6SJosef Karthauser 			/* Legacy colours used 0-7 */
544c1499cf6SJosef Karthauser 			if (c[j] >= '0' && c[j] <= '7') {
545c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - '0';
546c1499cf6SJosef Karthauser 				if (!legacy_warn) {
5473885812cSJosef Karthauser 					fprintf(stderr,
548ac639e90SJosef Karthauser 					    "warn: LSCOLORS should use "
549130d15dcSJosef Karthauser 					    "characters a-h instead of 0-9 ("
550130d15dcSJosef Karthauser 					    "see the manual page)\n");
5513885812cSJosef Karthauser 				}
552c1499cf6SJosef Karthauser 				legacy_warn = 1;
553c1499cf6SJosef Karthauser 			} else if (c[j] >= 'a' && c[j] <= 'h')
554c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'a';
555c1499cf6SJosef Karthauser 			else if (c[j] >= 'A' && c[j] <= 'H') {
556c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'A';
557c1499cf6SJosef Karthauser 				colors[i].bold = 1;
558c1499cf6SJosef Karthauser 			} else if (tolower((unsigned char)c[j] == 'x'))
559c1499cf6SJosef Karthauser 				colors[i].num[j] = -1;
560c1499cf6SJosef Karthauser 			else {
561c1499cf6SJosef Karthauser 				fprintf(stderr,
562c1499cf6SJosef Karthauser 				    "error: invalid character '%c' in LSCOLORS"
563c1499cf6SJosef Karthauser 				    " env var\n", c[j]);
5640d72516eSJosef Karthauser 				colors[i].num[j] = -1;
565c1499cf6SJosef Karthauser 			}
5663885812cSJosef Karthauser 		}
5673885812cSJosef Karthauser 	}
5683885812cSJosef Karthauser }
569cf0feaeeSAndrey A. Chernov 
5701bf1478aSAndrey A. Chernov void
57146251ddeSWarner Losh colorquit(int sig)
572cf0feaeeSAndrey A. Chernov {
57338782c25SAndrey A. Chernov 	endcolor(sig);
574faebfe2eSAndrey A. Chernov 
575faebfe2eSAndrey A. Chernov 	(void)signal(sig, SIG_DFL);
576faebfe2eSAndrey A. Chernov 	(void)kill(getpid(), sig);
577cf0feaeeSAndrey A. Chernov }
5785dda5d0dSJosef Karthauser 
57974985094SJosef Karthauser #endif /* COLORLS */
5803885812cSJosef Karthauser 
5814b88c807SRodney W. Grimes static void
58246251ddeSWarner Losh printlink(FTSENT *p)
5834b88c807SRodney W. Grimes {
5844b88c807SRodney W. Grimes 	int lnklen;
585b0bc91e9SJosef Karthauser 	char name[MAXPATHLEN + 1];
586b0bc91e9SJosef Karthauser 	char path[MAXPATHLEN + 1];
5874b88c807SRodney W. Grimes 
5884b88c807SRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL)
5894b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
5904b88c807SRodney W. Grimes 	else
5914b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name),
5924b88c807SRodney W. Grimes 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
5934b88c807SRodney W. Grimes 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
5944b88c807SRodney W. Grimes 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
5954b88c807SRodney W. Grimes 		return;
5964b88c807SRodney W. Grimes 	}
5974b88c807SRodney W. Grimes 	path[lnklen] = '\0';
5987ea30648SDag-Erling Smørgrav 	(void)printf(" -> ");
5999052855aSMark Murray 	(void)printname(path);
6004b88c807SRodney W. Grimes }
6010e8d1551SJosef Karthauser 
6020e8d1551SJosef Karthauser static void
60346251ddeSWarner Losh printsize(size_t width, off_t bytes)
6040e8d1551SJosef Karthauser {
605598420eeSDima Dorfman 	double dbytes;
6060e8d1551SJosef Karthauser 	unit_t unit;
6070e8d1551SJosef Karthauser 
6080e8d1551SJosef Karthauser 	if (f_humanval) {
609598420eeSDima Dorfman 		dbytes = bytes;
610598420eeSDima Dorfman 		unit = unit_adjust(&dbytes);
6110e8d1551SJosef Karthauser 
612598420eeSDima Dorfman 		if (dbytes == 0)
6130e8d1551SJosef Karthauser 			(void)printf("%*s ", width, "0B");
6140e8d1551SJosef Karthauser 		else
615598420eeSDima Dorfman 			(void)printf("%*.*f%c ", width - 1, dbytes > 10 ? 0 : 1,
616598420eeSDima Dorfman 			    dbytes, "BKMGTPE"[unit]);
6175dda5d0dSJosef Karthauser 	} else
6189052855aSMark Murray 		(void)printf("%*lld ", width, bytes);
6190e8d1551SJosef Karthauser }
6200e8d1551SJosef Karthauser 
6210e8d1551SJosef Karthauser /*
6220e8d1551SJosef Karthauser  * Output in "human-readable" format.  Uses 3 digits max and puts
6230e8d1551SJosef Karthauser  * unit suffixes at the end.  Makes output compact and easy to read,
6240e8d1551SJosef Karthauser  * especially on huge disks.
6250e8d1551SJosef Karthauser  *
6260e8d1551SJosef Karthauser  */
6270e8d1551SJosef Karthauser unit_t
628598420eeSDima Dorfman unit_adjust(double *val)
6290e8d1551SJosef Karthauser {
6300e8d1551SJosef Karthauser 	double abval;
6310e8d1551SJosef Karthauser 	unit_t unit;
6320e8d1551SJosef Karthauser 	unsigned int unit_sz;
6330e8d1551SJosef Karthauser 
634598420eeSDima Dorfman 	abval = fabs(*val);
6350e8d1551SJosef Karthauser 
6360e8d1551SJosef Karthauser 	unit_sz = abval ? ilogb(abval) / 10 : 0;
6370e8d1551SJosef Karthauser 
6380e8d1551SJosef Karthauser 	if (unit_sz >= UNIT_MAX) {
6390e8d1551SJosef Karthauser 		unit = NONE;
6400e8d1551SJosef Karthauser 	} else {
6410e8d1551SJosef Karthauser 		unit = unitp[unit_sz];
6420e8d1551SJosef Karthauser 		*val /= (double)vals_base2[unit_sz];
6430e8d1551SJosef Karthauser 	}
6440e8d1551SJosef Karthauser 
6450e8d1551SJosef Karthauser 	return (unit);
6460e8d1551SJosef Karthauser }
647