xref: /freebsd/bin/ls/print.c (revision 434b6ea45aaa41fb78958dee5d7f383fc8b7f010)
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 
37c73d77ceSMark Murray #include <sys/cdefs.h>
38c73d77ceSMark Murray 
39c73d77ceSMark Murray __FBSDID("$FreeBSD$");
40c73d77ceSMark Murray 
41febad2fcSSteve Price #if 0
42c73d77ceSMark Murray #ifndef lint
43febad2fcSSteve Price static char sccsid[] = "@(#)print.c	8.4 (Berkeley) 4/17/94";
444b88c807SRodney W. Grimes #endif /* not lint */
45c73d77ceSMark Murray #endif
464b88c807SRodney W. Grimes 
474b88c807SRodney W. Grimes #include <sys/param.h>
484b88c807SRodney W. Grimes #include <sys/stat.h>
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes #include <err.h>
514b88c807SRodney W. Grimes #include <errno.h>
524b88c807SRodney W. Grimes #include <fts.h>
530e8d1551SJosef Karthauser #include <math.h>
5428fd017aSAndrey A. Chernov #include <langinfo.h>
554b88c807SRodney W. Grimes #include <stdio.h>
564b88c807SRodney W. Grimes #include <stdlib.h>
574b88c807SRodney W. Grimes #include <string.h>
58434b6ea4SBruce Evans #include <time.h>
594b88c807SRodney W. Grimes #include <unistd.h>
60faebfe2eSAndrey A. Chernov #ifdef COLORLS
61faebfe2eSAndrey A. Chernov #include <ctype.h>
62faebfe2eSAndrey A. Chernov #include <termcap.h>
63faebfe2eSAndrey A. Chernov #include <signal.h>
64faebfe2eSAndrey A. Chernov #endif
654b88c807SRodney W. Grimes 
664b88c807SRodney W. Grimes #include "ls.h"
674b88c807SRodney W. Grimes #include "extern.h"
684b88c807SRodney W. Grimes 
6946251ddeSWarner Losh static int	printaname(FTSENT *, u_long, u_long);
7046251ddeSWarner Losh static void	printlink(FTSENT *);
7146251ddeSWarner Losh static void	printtime(time_t);
7246251ddeSWarner Losh static int	printtype(u_int);
7346251ddeSWarner Losh static void	printsize(size_t, off_t);
7438782c25SAndrey A. Chernov #ifdef COLORLS
7546251ddeSWarner Losh static void	endcolor(int);
7646251ddeSWarner Losh static int	colortype(mode_t);
7738782c25SAndrey A. Chernov #endif
784b88c807SRodney W. Grimes 
794b88c807SRodney W. Grimes #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
800e8d1551SJosef Karthauser 
810e8d1551SJosef Karthauser #define KILO_SZ(n) (n)
820e8d1551SJosef Karthauser #define MEGA_SZ(n) ((n) * (n))
830e8d1551SJosef Karthauser #define GIGA_SZ(n) ((n) * (n) * (n))
840e8d1551SJosef Karthauser #define TERA_SZ(n) ((n) * (n) * (n) * (n))
850e8d1551SJosef Karthauser #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
860e8d1551SJosef Karthauser 
870e8d1551SJosef Karthauser #define KILO_2_SZ (KILO_SZ(1024ULL))
880e8d1551SJosef Karthauser #define MEGA_2_SZ (MEGA_SZ(1024ULL))
890e8d1551SJosef Karthauser #define GIGA_2_SZ (GIGA_SZ(1024ULL))
900e8d1551SJosef Karthauser #define TERA_2_SZ (TERA_SZ(1024ULL))
910e8d1551SJosef Karthauser #define PETA_2_SZ (PETA_SZ(1024ULL))
920e8d1551SJosef Karthauser 
939052855aSMark Murray static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
940e8d1551SJosef Karthauser 
955dda5d0dSJosef Karthauser typedef enum {
965dda5d0dSJosef Karthauser 	NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX
975dda5d0dSJosef Karthauser } unit_t;
9846251ddeSWarner Losh static unit_t unit_adjust(off_t *);
990e8d1551SJosef Karthauser 
1009052855aSMark Murray static int unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA};
1014b88c807SRodney W. Grimes 
10274985094SJosef Karthauser #ifdef COLORLS
1033885812cSJosef Karthauser /* Most of these are taken from <sys/stat.h> */
1043885812cSJosef Karthauser typedef enum Colors {
1053885812cSJosef Karthauser 	C_DIR,			/* directory */
1063885812cSJosef Karthauser 	C_LNK,			/* symbolic link */
1073885812cSJosef Karthauser 	C_SOCK,			/* socket */
1083885812cSJosef Karthauser 	C_FIFO,			/* pipe */
1093885812cSJosef Karthauser 	C_EXEC,			/* executable */
1103885812cSJosef Karthauser 	C_BLK,			/* block special */
1113885812cSJosef Karthauser 	C_CHR,			/* character special */
1123885812cSJosef Karthauser 	C_SUID,			/* setuid executable */
1133885812cSJosef Karthauser 	C_SGID,			/* setgid executable */
1145dda5d0dSJosef Karthauser 	C_WSDIR,		/* directory writeble to others, with sticky
1155dda5d0dSJosef Karthauser 				 * bit */
1165dda5d0dSJosef Karthauser 	C_WDIR,			/* directory writeble to others, without
1175dda5d0dSJosef Karthauser 				 * sticky bit */
1183885812cSJosef Karthauser 	C_NUMCOLORS		/* just a place-holder */
1193885812cSJosef Karthauser } Colors;
1203885812cSJosef Karthauser 
1219052855aSMark Murray static const char *defcolors = "exfxcxdxbxegedabagacad";
1223885812cSJosef Karthauser 
123c1499cf6SJosef Karthauser /* colors for file types */
124c1499cf6SJosef Karthauser static struct {
125c1499cf6SJosef Karthauser 	int	num[2];
126c1499cf6SJosef Karthauser 	int	bold;
127c1499cf6SJosef Karthauser } colors[C_NUMCOLORS];
12874985094SJosef Karthauser #endif
1293885812cSJosef Karthauser 
1304b88c807SRodney W. Grimes void
13146251ddeSWarner Losh printscol(DISPLAY *dp)
1324b88c807SRodney W. Grimes {
1334b88c807SRodney W. Grimes 	FTSENT *p;
1344b88c807SRodney W. Grimes 
1354b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1364b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1374b88c807SRodney W. Grimes 			continue;
1384b88c807SRodney W. Grimes 		(void)printaname(p, dp->s_inode, dp->s_block);
1394b88c807SRodney W. Grimes 		(void)putchar('\n');
1404b88c807SRodney W. Grimes 	}
1414b88c807SRodney W. Grimes }
1424b88c807SRodney W. Grimes 
143ee579ffbSAssar Westerlund /*
144ee579ffbSAssar Westerlund  * print name in current style
145ee579ffbSAssar Westerlund  */
146ee579ffbSAssar Westerlund static int
14746251ddeSWarner Losh printname(const char *name)
148ee579ffbSAssar Westerlund {
149ee579ffbSAssar Westerlund 	if (f_octal || f_octal_escape)
150ee579ffbSAssar Westerlund 		return prn_octal(name);
151ee579ffbSAssar Westerlund 	else if (f_nonprint)
152ee579ffbSAssar Westerlund 		return prn_printable(name);
153ee579ffbSAssar Westerlund 	else
154ee579ffbSAssar Westerlund 		return printf("%s", name);
155ee579ffbSAssar Westerlund }
156ee579ffbSAssar Westerlund 
1574b88c807SRodney W. Grimes void
15846251ddeSWarner Losh printlong(DISPLAY *dp)
1594b88c807SRodney W. Grimes {
1604b88c807SRodney W. Grimes 	struct stat *sp;
1614b88c807SRodney W. Grimes 	FTSENT *p;
1624b88c807SRodney W. Grimes 	NAMES *np;
1634b88c807SRodney W. Grimes 	char buf[20];
16447bb6b11SAndrey A. Chernov #ifdef COLORLS
16547bb6b11SAndrey A. Chernov 	int color_printed = 0;
16647bb6b11SAndrey A. Chernov #endif
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1694b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1704b88c807SRodney W. Grimes 
1714b88c807SRodney W. Grimes 	for (p = dp->list; p; p = p->fts_link) {
1724b88c807SRodney W. Grimes 		if (IS_NOPRINT(p))
1734b88c807SRodney W. Grimes 			continue;
1744b88c807SRodney W. Grimes 		sp = p->fts_statp;
1754b88c807SRodney W. Grimes 		if (f_inode)
176fb5cb208SSteve Price 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1774b88c807SRodney W. Grimes 		if (f_size)
1789052855aSMark Murray 			(void)printf("%*lld ",
1794b88c807SRodney W. Grimes 			    dp->s_block, howmany(sp->st_blocks, blocksize));
1809052855aSMark Murray 		strmode(sp->st_mode, buf);
1814b88c807SRodney W. Grimes 		np = p->fts_pointer;
1824b88c807SRodney W. Grimes 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1834b88c807SRodney W. Grimes 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1844b88c807SRodney W. Grimes 		    np->group);
1854b88c807SRodney W. Grimes 		if (f_flags)
1864b88c807SRodney W. Grimes 			(void)printf("%-*s ", dp->s_flags, np->flags);
1877304f61fSBrian Feldman 		if (f_lomac)
1887304f61fSBrian Feldman 			(void)printf("%-*s ", dp->s_lattr, np->lattr);
1894b88c807SRodney W. Grimes 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
190029b2bd0SBruce Evans 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
191df2fbf15SJoerg Wunsch 				(void)printf("%3d, 0x%08x ",
192029b2bd0SBruce Evans 				    major(sp->st_rdev),
193029b2bd0SBruce Evans 				    (u_int)minor(sp->st_rdev));
194df2fbf15SJoerg Wunsch 			else
1954b88c807SRodney W. Grimes 				(void)printf("%3d, %3d ",
1964b88c807SRodney W. Grimes 				    major(sp->st_rdev), minor(sp->st_rdev));
1974b88c807SRodney W. Grimes 		else if (dp->bcfile)
1989052855aSMark Murray 			(void)printf("%*s%*lld ",
1994b88c807SRodney W. Grimes 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
2004b88c807SRodney W. Grimes 		else
2010e8d1551SJosef Karthauser 			printsize(dp->s_size, sp->st_size);
2024b88c807SRodney W. Grimes 		if (f_accesstime)
2034b88c807SRodney W. Grimes 			printtime(sp->st_atime);
2044b88c807SRodney W. Grimes 		else if (f_statustime)
2054b88c807SRodney W. Grimes 			printtime(sp->st_ctime);
2064b88c807SRodney W. Grimes 		else
2074b88c807SRodney W. Grimes 			printtime(sp->st_mtime);
20874985094SJosef Karthauser #ifdef COLORLS
2093885812cSJosef Karthauser 		if (f_color)
210cf0feaeeSAndrey A. Chernov 			color_printed = colortype(sp->st_mode);
21174985094SJosef Karthauser #endif
212ee579ffbSAssar Westerlund 		(void)printname(p->fts_name);
21374985094SJosef Karthauser #ifdef COLORLS
214cf0feaeeSAndrey A. Chernov 		if (f_color && color_printed)
21538782c25SAndrey A. Chernov 			endcolor(0);
21674985094SJosef Karthauser #endif
2174b88c807SRodney W. Grimes 		if (f_type)
2184b88c807SRodney W. Grimes 			(void)printtype(sp->st_mode);
2194b88c807SRodney W. Grimes 		if (S_ISLNK(sp->st_mode))
2204b88c807SRodney W. Grimes 			printlink(p);
2214b88c807SRodney W. Grimes 		(void)putchar('\n');
2224b88c807SRodney W. Grimes 	}
2234b88c807SRodney W. Grimes }
2244b88c807SRodney W. Grimes 
2254b88c807SRodney W. Grimes void
22646251ddeSWarner Losh printcol(DISPLAY *dp)
2274b88c807SRodney W. Grimes {
2284b88c807SRodney W. Grimes 	extern int termwidth;
2294b88c807SRodney W. Grimes 	static FTSENT **array;
2304b88c807SRodney W. Grimes 	static int lastentries = -1;
2314b88c807SRodney W. Grimes 	FTSENT *p;
232b0bc91e9SJosef Karthauser 	int base;
233b0bc91e9SJosef Karthauser 	int chcnt;
234b0bc91e9SJosef Karthauser 	int cnt;
235b0bc91e9SJosef Karthauser 	int col;
236b0bc91e9SJosef Karthauser 	int colwidth;
237b0bc91e9SJosef Karthauser 	int endcol;
238b0bc91e9SJosef Karthauser 	int num;
239b0bc91e9SJosef Karthauser 	int numcols;
240b0bc91e9SJosef Karthauser 	int numrows;
241b0bc91e9SJosef Karthauser 	int row;
242545f583cSTim Vanderhoek 	int tabwidth;
243545f583cSTim Vanderhoek 
244545f583cSTim Vanderhoek 	if (f_notabs)
245545f583cSTim Vanderhoek 		tabwidth = 1;
246545f583cSTim Vanderhoek 	else
247545f583cSTim Vanderhoek 		tabwidth = 8;
2484b88c807SRodney W. Grimes 
2494b88c807SRodney W. Grimes 	/*
2504b88c807SRodney W. Grimes 	 * Have to do random access in the linked list -- build a table
2514b88c807SRodney W. Grimes 	 * of pointers.
2524b88c807SRodney W. Grimes 	 */
2534b88c807SRodney W. Grimes 	if (dp->entries > lastentries) {
2544b88c807SRodney W. Grimes 		lastentries = dp->entries;
2554b88c807SRodney W. Grimes 		if ((array =
2564b88c807SRodney W. Grimes 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
2574b88c807SRodney W. Grimes 			warn(NULL);
2584b88c807SRodney W. Grimes 			printscol(dp);
2594b88c807SRodney W. Grimes 		}
2604b88c807SRodney W. Grimes 	}
2614b88c807SRodney W. Grimes 	for (p = dp->list, num = 0; p; p = p->fts_link)
2624b88c807SRodney W. Grimes 		if (p->fts_number != NO_PRINT)
2634b88c807SRodney W. Grimes 			array[num++] = p;
2644b88c807SRodney W. Grimes 
2654b88c807SRodney W. Grimes 	colwidth = dp->maxlen;
2664b88c807SRodney W. Grimes 	if (f_inode)
2674b88c807SRodney W. Grimes 		colwidth += dp->s_inode + 1;
2684b88c807SRodney W. Grimes 	if (f_size)
2694b88c807SRodney W. Grimes 		colwidth += dp->s_block + 1;
2704b88c807SRodney W. Grimes 	if (f_type)
2714b88c807SRodney W. Grimes 		colwidth += 1;
2724b88c807SRodney W. Grimes 
273545f583cSTim Vanderhoek 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
2744b88c807SRodney W. Grimes 	if (termwidth < 2 * colwidth) {
2754b88c807SRodney W. Grimes 		printscol(dp);
2764b88c807SRodney W. Grimes 		return;
2774b88c807SRodney W. Grimes 	}
2784b88c807SRodney W. Grimes 	numcols = termwidth / colwidth;
2794b88c807SRodney W. Grimes 	numrows = num / numcols;
2804b88c807SRodney W. Grimes 	if (num % numcols)
2814b88c807SRodney W. Grimes 		++numrows;
2824b88c807SRodney W. Grimes 
2834b88c807SRodney W. Grimes 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
2844b88c807SRodney W. Grimes 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
2854b88c807SRodney W. Grimes 	for (row = 0; row < numrows; ++row) {
2864b88c807SRodney W. Grimes 		endcol = colwidth;
2874b88c807SRodney W. Grimes 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
2884b88c807SRodney W. Grimes 			chcnt += printaname(array[base], dp->s_inode,
2894b88c807SRodney W. Grimes 			    dp->s_block);
2904b88c807SRodney W. Grimes 			if ((base += numrows) >= num)
2914b88c807SRodney W. Grimes 				break;
292545f583cSTim Vanderhoek 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
293545f583cSTim Vanderhoek 			    <= endcol) {
294545f583cSTim Vanderhoek 				(void)putchar(f_notabs ? ' ' : '\t');
2954b88c807SRodney W. Grimes 				chcnt = cnt;
2964b88c807SRodney W. Grimes 			}
2974b88c807SRodney W. Grimes 			endcol += colwidth;
2984b88c807SRodney W. Grimes 		}
2994b88c807SRodney W. Grimes 		(void)putchar('\n');
3004b88c807SRodney W. Grimes 	}
3014b88c807SRodney W. Grimes }
3024b88c807SRodney W. Grimes 
3034b88c807SRodney W. Grimes /*
3044b88c807SRodney W. Grimes  * print [inode] [size] name
3054b88c807SRodney W. Grimes  * return # of characters printed, no trailing characters.
3064b88c807SRodney W. Grimes  */
3074b88c807SRodney W. Grimes static int
30846251ddeSWarner Losh printaname(FTSENT *p, u_long inodefield, u_long sizefield)
3094b88c807SRodney W. Grimes {
3104b88c807SRodney W. Grimes 	struct stat *sp;
3114b88c807SRodney W. Grimes 	int chcnt;
31247bb6b11SAndrey A. Chernov #ifdef COLORLS
31347bb6b11SAndrey A. Chernov 	int color_printed = 0;
31447bb6b11SAndrey A. Chernov #endif
3154b88c807SRodney W. Grimes 
3164b88c807SRodney W. Grimes 	sp = p->fts_statp;
3174b88c807SRodney W. Grimes 	chcnt = 0;
3184b88c807SRodney W. Grimes 	if (f_inode)
319fb5cb208SSteve Price 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
3204b88c807SRodney W. Grimes 	if (f_size)
3219052855aSMark Murray 		chcnt += printf("%*lld ",
3224b88c807SRodney W. Grimes 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
32374985094SJosef Karthauser #ifdef COLORLS
3243885812cSJosef Karthauser 	if (f_color)
325cf0feaeeSAndrey A. Chernov 		color_printed = colortype(sp->st_mode);
32674985094SJosef Karthauser #endif
327ee579ffbSAssar Westerlund 	chcnt += printname(p->fts_name);
32874985094SJosef Karthauser #ifdef COLORLS
329cf0feaeeSAndrey A. Chernov 	if (f_color && color_printed)
33038782c25SAndrey A. Chernov 		endcolor(0);
33174985094SJosef Karthauser #endif
3324b88c807SRodney W. Grimes 	if (f_type)
3334b88c807SRodney W. Grimes 		chcnt += printtype(sp->st_mode);
3344b88c807SRodney W. Grimes 	return (chcnt);
3354b88c807SRodney W. Grimes }
3364b88c807SRodney W. Grimes 
3374b88c807SRodney W. Grimes static void
33846251ddeSWarner Losh printtime(time_t ftime)
3394b88c807SRodney W. Grimes {
340fc4a9bafSAndrey A. Chernov 	char longstring[80];
341f173abd0SMike Pritchard 	static time_t now;
34297e4e97bSJosef Karthauser 	const char *format;
3438234eb25SAndrey A. Chernov 	static int d_first = -1;
344f173abd0SMike Pritchard 
34528fd017aSAndrey A. Chernov 	if (d_first < 0)
34628fd017aSAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
347f173abd0SMike Pritchard 	if (now == 0)
348f173abd0SMike Pritchard 		now = time(NULL);
3494b88c807SRodney W. Grimes 
350656dcd43SGarrett Wollman #define	SIXMONTHS	((365 / 2) * 86400)
3514b88c807SRodney W. Grimes 	if (f_sectime)
3521e715e34SJosef Karthauser 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
35328fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
354f173abd0SMike Pritchard 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
3551e715e34SJosef Karthauser 		/* mmm dd hh:mm || dd mmm hh:mm */
35628fd017aSAndrey A. Chernov 		format = d_first ? "%e %b %R " : "%b %e %R ";
35797e4e97bSJosef Karthauser 	else
3581e715e34SJosef Karthauser 		/* mmm dd  yyyy || dd mmm  yyyy */
35928fd017aSAndrey A. Chernov 		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
36097e4e97bSJosef Karthauser 	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
36197e4e97bSJosef Karthauser 	fputs(longstring, stdout);
3624b88c807SRodney W. Grimes }
3634b88c807SRodney W. Grimes 
3644b88c807SRodney W. Grimes static int
36546251ddeSWarner Losh printtype(u_int mode)
3664b88c807SRodney W. Grimes {
3674b88c807SRodney W. Grimes 	switch (mode & S_IFMT) {
3684b88c807SRodney W. Grimes 	case S_IFDIR:
3694b88c807SRodney W. Grimes 		(void)putchar('/');
3704b88c807SRodney W. Grimes 		return (1);
3714b88c807SRodney W. Grimes 	case S_IFIFO:
3724b88c807SRodney W. Grimes 		(void)putchar('|');
3734b88c807SRodney W. Grimes 		return (1);
3744b88c807SRodney W. Grimes 	case S_IFLNK:
3754b88c807SRodney W. Grimes 		(void)putchar('@');
3764b88c807SRodney W. Grimes 		return (1);
3774b88c807SRodney W. Grimes 	case S_IFSOCK:
3784b88c807SRodney W. Grimes 		(void)putchar('=');
3794b88c807SRodney W. Grimes 		return (1);
380fb5cb208SSteve Price 	case S_IFWHT:
381fb5cb208SSteve Price 		(void)putchar('%');
382fb5cb208SSteve Price 		return (1);
3839052855aSMark Murray 	default:
3844b88c807SRodney W. Grimes 	}
3854b88c807SRodney W. Grimes 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
3864b88c807SRodney W. Grimes 		(void)putchar('*');
3874b88c807SRodney W. Grimes 		return (1);
3884b88c807SRodney W. Grimes 	}
3894b88c807SRodney W. Grimes 	return (0);
3904b88c807SRodney W. Grimes }
3914b88c807SRodney W. Grimes 
39274985094SJosef Karthauser #ifdef COLORLS
3931bf1478aSAndrey A. Chernov static int
39446251ddeSWarner Losh putch(int c)
395cf0feaeeSAndrey A. Chernov {
39638782c25SAndrey A. Chernov 	(void)putchar(c);
39738782c25SAndrey A. Chernov 	return 0;
398cf0feaeeSAndrey A. Chernov }
399cf0feaeeSAndrey A. Chernov 
4001bf1478aSAndrey A. Chernov static int
40146251ddeSWarner Losh writech(int c)
40238782c25SAndrey A. Chernov {
40338782c25SAndrey A. Chernov 	char tmp = c;
40438782c25SAndrey A. Chernov 
40538782c25SAndrey A. Chernov 	(void)write(STDOUT_FILENO, &tmp, 1);
40638782c25SAndrey A. Chernov 	return 0;
40738782c25SAndrey A. Chernov }
408cf0feaeeSAndrey A. Chernov 
4091bf1478aSAndrey A. Chernov static void
41046251ddeSWarner Losh printcolor(Colors c)
4113885812cSJosef Karthauser {
41274985094SJosef Karthauser 	char *ansiseq;
41374985094SJosef Karthauser 
414c1499cf6SJosef Karthauser 	if (colors[c].bold)
415c1499cf6SJosef Karthauser 		tputs(enter_bold, 1, putch);
416c1499cf6SJosef Karthauser 
417c1499cf6SJosef Karthauser 	if (colors[c].num[0] != -1) {
418c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
41938782c25SAndrey A. Chernov 		if (ansiseq)
420cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
4213885812cSJosef Karthauser 	}
422c1499cf6SJosef Karthauser 	if (colors[c].num[1] != -1) {
423c1499cf6SJosef Karthauser 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
42438782c25SAndrey A. Chernov 		if (ansiseq)
425cf0feaeeSAndrey A. Chernov 			tputs(ansiseq, 1, putch);
42674985094SJosef Karthauser 	}
42774985094SJosef Karthauser }
42874985094SJosef Karthauser 
42938782c25SAndrey A. Chernov static void
43046251ddeSWarner Losh endcolor(int sig)
43174985094SJosef Karthauser {
43238782c25SAndrey A. Chernov 	tputs(ansi_coloff, 1, sig ? writech : putch);
433c1499cf6SJosef Karthauser 	tputs(attrs_off, 1, sig ? writech : putch);
4343885812cSJosef Karthauser }
4353885812cSJosef Karthauser 
43638782c25SAndrey A. Chernov static int
43746251ddeSWarner Losh colortype(mode_t mode)
4383885812cSJosef Karthauser {
4393885812cSJosef Karthauser 	switch (mode & S_IFMT) {
4403885812cSJosef Karthauser 	case S_IFDIR:
4413885812cSJosef Karthauser 		if (mode & S_IWOTH)
4423885812cSJosef Karthauser 			if (mode & S_ISTXT)
4433885812cSJosef Karthauser 				printcolor(C_WSDIR);
4443885812cSJosef Karthauser 			else
4453885812cSJosef Karthauser 				printcolor(C_WDIR);
4463885812cSJosef Karthauser 		else
4473885812cSJosef Karthauser 			printcolor(C_DIR);
4483885812cSJosef Karthauser 		return (1);
4493885812cSJosef Karthauser 	case S_IFLNK:
4503885812cSJosef Karthauser 		printcolor(C_LNK);
4513885812cSJosef Karthauser 		return (1);
4523885812cSJosef Karthauser 	case S_IFSOCK:
4533885812cSJosef Karthauser 		printcolor(C_SOCK);
4543885812cSJosef Karthauser 		return (1);
4553885812cSJosef Karthauser 	case S_IFIFO:
4563885812cSJosef Karthauser 		printcolor(C_FIFO);
4573885812cSJosef Karthauser 		return (1);
4583885812cSJosef Karthauser 	case S_IFBLK:
4593885812cSJosef Karthauser 		printcolor(C_BLK);
4603885812cSJosef Karthauser 		return (1);
4613885812cSJosef Karthauser 	case S_IFCHR:
4623885812cSJosef Karthauser 		printcolor(C_CHR);
4633885812cSJosef Karthauser 		return (1);
4643885812cSJosef Karthauser 	}
4653885812cSJosef Karthauser 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
4663885812cSJosef Karthauser 		if (mode & S_ISUID)
4673885812cSJosef Karthauser 			printcolor(C_SUID);
4683885812cSJosef Karthauser 		else if (mode & S_ISGID)
4693885812cSJosef Karthauser 			printcolor(C_SGID);
4703885812cSJosef Karthauser 		else
4713885812cSJosef Karthauser 			printcolor(C_EXEC);
4723885812cSJosef Karthauser 		return (1);
4733885812cSJosef Karthauser 	}
4743885812cSJosef Karthauser 	return (0);
4753885812cSJosef Karthauser }
4763885812cSJosef Karthauser 
4773885812cSJosef Karthauser void
47846251ddeSWarner Losh parsecolors(const char *cs)
4793885812cSJosef Karthauser {
480b0bc91e9SJosef Karthauser 	int i;
481b0bc91e9SJosef Karthauser 	int j;
482b0bc91e9SJosef Karthauser 	int len;
4833885812cSJosef Karthauser 	char c[2];
484c1499cf6SJosef Karthauser 	short legacy_warn = 0;
48538782c25SAndrey A. Chernov 
486b0bc91e9SJosef Karthauser 	if (cs == NULL)
487b0bc91e9SJosef Karthauser 		cs = "";	/* LSCOLORS not set */
4883885812cSJosef Karthauser 	len = strlen(cs);
4893885812cSJosef Karthauser 	for (i = 0; i < C_NUMCOLORS; i++) {
490c1499cf6SJosef Karthauser 		colors[i].bold = 0;
491c1499cf6SJosef Karthauser 
4923885812cSJosef Karthauser 		if (len <= 2 * i) {
4933885812cSJosef Karthauser 			c[0] = defcolors[2 * i];
4943885812cSJosef Karthauser 			c[1] = defcolors[2 * i + 1];
4955dda5d0dSJosef Karthauser 		} else {
4963885812cSJosef Karthauser 			c[0] = cs[2 * i];
4973885812cSJosef Karthauser 			c[1] = cs[2 * i + 1];
4983885812cSJosef Karthauser 		}
4993885812cSJosef Karthauser 		for (j = 0; j < 2; j++) {
500c1499cf6SJosef Karthauser 			/* Legacy colours used 0-7 */
501c1499cf6SJosef Karthauser 			if (c[j] >= '0' && c[j] <= '7') {
502c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - '0';
503c1499cf6SJosef Karthauser 				if (!legacy_warn) {
5043885812cSJosef Karthauser 					fprintf(stderr,
505ac639e90SJosef Karthauser 					    "warn: LSCOLORS should use "
506130d15dcSJosef Karthauser 					    "characters a-h instead of 0-9 ("
507130d15dcSJosef Karthauser 					    "see the manual page)\n");
5083885812cSJosef Karthauser 				}
509c1499cf6SJosef Karthauser 				legacy_warn = 1;
510c1499cf6SJosef Karthauser 			} else if (c[j] >= 'a' && c[j] <= 'h')
511c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'a';
512c1499cf6SJosef Karthauser 			else if (c[j] >= 'A' && c[j] <= 'H') {
513c1499cf6SJosef Karthauser 				colors[i].num[j] = c[j] - 'A';
514c1499cf6SJosef Karthauser 				colors[i].bold = 1;
515c1499cf6SJosef Karthauser 			} else if (tolower((unsigned char)c[j] == 'x'))
516c1499cf6SJosef Karthauser 				colors[i].num[j] = -1;
517c1499cf6SJosef Karthauser 			else {
518c1499cf6SJosef Karthauser 				fprintf(stderr,
519c1499cf6SJosef Karthauser 				    "error: invalid character '%c' in LSCOLORS"
520c1499cf6SJosef Karthauser 				    " env var\n", c[j]);
5210d72516eSJosef Karthauser 				colors[i].num[j] = -1;
522c1499cf6SJosef Karthauser 			}
5233885812cSJosef Karthauser 		}
5243885812cSJosef Karthauser 	}
5253885812cSJosef Karthauser }
526cf0feaeeSAndrey A. Chernov 
5271bf1478aSAndrey A. Chernov void
52846251ddeSWarner Losh colorquit(int sig)
529cf0feaeeSAndrey A. Chernov {
53038782c25SAndrey A. Chernov 	endcolor(sig);
531faebfe2eSAndrey A. Chernov 
532faebfe2eSAndrey A. Chernov 	(void)signal(sig, SIG_DFL);
533faebfe2eSAndrey A. Chernov 	(void)kill(getpid(), sig);
534cf0feaeeSAndrey A. Chernov }
5355dda5d0dSJosef Karthauser 
53674985094SJosef Karthauser #endif /* COLORLS */
5373885812cSJosef Karthauser 
5384b88c807SRodney W. Grimes static void
53946251ddeSWarner Losh printlink(FTSENT *p)
5404b88c807SRodney W. Grimes {
5414b88c807SRodney W. Grimes 	int lnklen;
542b0bc91e9SJosef Karthauser 	char name[MAXPATHLEN + 1];
543b0bc91e9SJosef Karthauser 	char path[MAXPATHLEN + 1];
5444b88c807SRodney W. Grimes 
5454b88c807SRodney W. Grimes 	if (p->fts_level == FTS_ROOTLEVEL)
5464b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
5474b88c807SRodney W. Grimes 	else
5484b88c807SRodney W. Grimes 		(void)snprintf(name, sizeof(name),
5494b88c807SRodney W. Grimes 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
5504b88c807SRodney W. Grimes 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
5514b88c807SRodney W. Grimes 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
5524b88c807SRodney W. Grimes 		return;
5534b88c807SRodney W. Grimes 	}
5544b88c807SRodney W. Grimes 	path[lnklen] = '\0';
5557ea30648SDag-Erling Smørgrav 	(void)printf(" -> ");
5569052855aSMark Murray 	(void)printname(path);
5574b88c807SRodney W. Grimes }
5580e8d1551SJosef Karthauser 
5590e8d1551SJosef Karthauser static void
56046251ddeSWarner Losh printsize(size_t width, off_t bytes)
5610e8d1551SJosef Karthauser {
5620e8d1551SJosef Karthauser 	unit_t unit;
5630e8d1551SJosef Karthauser 
5640e8d1551SJosef Karthauser 	if (f_humanval) {
5650e8d1551SJosef Karthauser 		unit = unit_adjust(&bytes);
5660e8d1551SJosef Karthauser 
5670e8d1551SJosef Karthauser 		if (bytes == 0)
5680e8d1551SJosef Karthauser 			(void)printf("%*s ", width, "0B");
5690e8d1551SJosef Karthauser 		else
5709052855aSMark Murray 			(void)printf("%*lld%c ", width - 1, bytes,
5710e8d1551SJosef Karthauser 			    "BKMGTPE"[unit]);
5725dda5d0dSJosef Karthauser 	} else
5739052855aSMark Murray 		(void)printf("%*lld ", width, bytes);
5740e8d1551SJosef Karthauser }
5750e8d1551SJosef Karthauser 
5760e8d1551SJosef Karthauser /*
5770e8d1551SJosef Karthauser  * Output in "human-readable" format.  Uses 3 digits max and puts
5780e8d1551SJosef Karthauser  * unit suffixes at the end.  Makes output compact and easy to read,
5790e8d1551SJosef Karthauser  * especially on huge disks.
5800e8d1551SJosef Karthauser  *
5810e8d1551SJosef Karthauser  */
5820e8d1551SJosef Karthauser unit_t
58346251ddeSWarner Losh unit_adjust(off_t *val)
5840e8d1551SJosef Karthauser {
5850e8d1551SJosef Karthauser 	double abval;
5860e8d1551SJosef Karthauser 	unit_t unit;
5870e8d1551SJosef Karthauser 	unsigned int unit_sz;
5880e8d1551SJosef Karthauser 
5899052855aSMark Murray 	abval = fabs((double)*val);
5900e8d1551SJosef Karthauser 
5910e8d1551SJosef Karthauser 	unit_sz = abval ? ilogb(abval) / 10 : 0;
5920e8d1551SJosef Karthauser 
5930e8d1551SJosef Karthauser 	if (unit_sz >= UNIT_MAX) {
5940e8d1551SJosef Karthauser 		unit = NONE;
5950e8d1551SJosef Karthauser 	} else {
5960e8d1551SJosef Karthauser 		unit = unitp[unit_sz];
5970e8d1551SJosef Karthauser 		*val /= (double)vals_base2[unit_sz];
5980e8d1551SJosef Karthauser 	}
5990e8d1551SJosef Karthauser 
6000e8d1551SJosef Karthauser 	return (unit);
6010e8d1551SJosef Karthauser }
602