print.c revision 128823
1/*
2 * Copyright (c) 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Michael Fischbein.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if 0
34#ifndef lint
35static char sccsid[] = "@(#)print.c	8.4 (Berkeley) 4/17/94";
36#endif /* not lint */
37#endif
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/ls/print.c 128823 2004-05-02 11:25:37Z tjr $");
40
41#include <sys/param.h>
42#include <sys/stat.h>
43#include <sys/acl.h>
44
45#include <err.h>
46#include <errno.h>
47#include <fts.h>
48#include <math.h>
49#include <langinfo.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <time.h>
54#include <unistd.h>
55#ifdef COLORLS
56#include <ctype.h>
57#include <termcap.h>
58#include <signal.h>
59#endif
60
61#include "ls.h"
62#include "extern.h"
63
64static int	printaname(const FTSENT *, u_long, u_long);
65static void	printlink(const FTSENT *);
66static void	printtime(time_t);
67static int	printtype(u_int);
68static void	printsize(size_t, off_t);
69#ifdef COLORLS
70static void	endcolor(int);
71static int	colortype(mode_t);
72#endif
73static void	aclmode(char *, const FTSENT *, int *);
74
75#define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
76
77#define KILO_SZ(n) (n)
78#define MEGA_SZ(n) ((n) * (n))
79#define GIGA_SZ(n) ((n) * (n) * (n))
80#define TERA_SZ(n) ((n) * (n) * (n) * (n))
81#define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
82
83#define KILO_2_SZ (KILO_SZ(1024ULL))
84#define MEGA_2_SZ (MEGA_SZ(1024ULL))
85#define GIGA_2_SZ (GIGA_SZ(1024ULL))
86#define TERA_2_SZ (TERA_SZ(1024ULL))
87#define PETA_2_SZ (PETA_SZ(1024ULL))
88
89static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
90
91typedef enum {
92	NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX
93} unit_t;
94static unit_t unit_adjust(double *);
95
96static unit_t unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA};
97
98#ifdef COLORLS
99/* Most of these are taken from <sys/stat.h> */
100typedef enum Colors {
101	C_DIR,			/* directory */
102	C_LNK,			/* symbolic link */
103	C_SOCK,			/* socket */
104	C_FIFO,			/* pipe */
105	C_EXEC,			/* executable */
106	C_BLK,			/* block special */
107	C_CHR,			/* character special */
108	C_SUID,			/* setuid executable */
109	C_SGID,			/* setgid executable */
110	C_WSDIR,		/* directory writeble to others, with sticky
111				 * bit */
112	C_WDIR,			/* directory writeble to others, without
113				 * sticky bit */
114	C_NUMCOLORS		/* just a place-holder */
115} Colors;
116
117static const char *defcolors = "exfxcxdxbxegedabagacad";
118
119/* colors for file types */
120static struct {
121	int	num[2];
122	int	bold;
123} colors[C_NUMCOLORS];
124#endif
125
126void
127printscol(const DISPLAY *dp)
128{
129	FTSENT *p;
130
131	for (p = dp->list; p; p = p->fts_link) {
132		if (IS_NOPRINT(p))
133			continue;
134		(void)printaname(p, dp->s_inode, dp->s_block);
135		(void)putchar('\n');
136	}
137}
138
139/*
140 * print name in current style
141 */
142int
143printname(const char *name)
144{
145	if (f_octal || f_octal_escape)
146		return prn_octal(name);
147	else if (f_nonprint)
148		return prn_printable(name);
149	else
150		return prn_normal(name);
151}
152
153void
154printlong(const DISPLAY *dp)
155{
156	struct stat *sp;
157	FTSENT *p;
158	NAMES *np;
159	char buf[20];
160#ifdef COLORLS
161	int color_printed = 0;
162#endif
163	int haveacls;
164	dev_t prevdev;
165
166	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
167		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
168
169	haveacls = 1;
170	prevdev = (dev_t)-1;
171	for (p = dp->list; p; p = p->fts_link) {
172		if (IS_NOPRINT(p))
173			continue;
174		sp = p->fts_statp;
175		if (f_inode)
176			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
177		if (f_size)
178			(void)printf("%*jd ",
179			    dp->s_block, howmany(sp->st_blocks, blocksize));
180		strmode(sp->st_mode, buf);
181		/*
182		 * Cache whether or not the filesystem supports ACL's to
183		 * avoid expensive syscalls. Try again when we change devices.
184		 */
185		if (haveacls || sp->st_dev != prevdev) {
186			aclmode(buf, p, &haveacls);
187			prevdev = sp->st_dev;
188		}
189		np = p->fts_pointer;
190		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
191		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
192		    np->group);
193		if (f_flags)
194			(void)printf("%-*s ", dp->s_flags, np->flags);
195		if (f_label)
196			(void)printf("%-*s ", dp->s_label, np->label);
197		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
198			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
199				(void)printf("%3d, 0x%08x ",
200				    major(sp->st_rdev),
201				    (u_int)minor(sp->st_rdev));
202			else
203				(void)printf("%3d, %3d ",
204				    major(sp->st_rdev), minor(sp->st_rdev));
205		else if (dp->bcfile)
206			(void)printf("%*s%*jd ",
207			    8 - dp->s_size, "", dp->s_size, sp->st_size);
208		else
209			printsize(dp->s_size, sp->st_size);
210		if (f_accesstime)
211			printtime(sp->st_atime);
212		else if (f_statustime)
213			printtime(sp->st_ctime);
214		else
215			printtime(sp->st_mtime);
216#ifdef COLORLS
217		if (f_color)
218			color_printed = colortype(sp->st_mode);
219#endif
220		(void)printname(p->fts_name);
221#ifdef COLORLS
222		if (f_color && color_printed)
223			endcolor(0);
224#endif
225		if (f_type)
226			(void)printtype(sp->st_mode);
227		if (S_ISLNK(sp->st_mode))
228			printlink(p);
229		(void)putchar('\n');
230	}
231}
232
233void
234printstream(const DISPLAY *dp)
235{
236	FTSENT *p;
237	int chcnt;
238
239	for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
240		if (p->fts_number == NO_PRINT)
241			continue;
242		/* XXX strlen does not take octal escapes into account. */
243		if (strlen(p->fts_name) + chcnt +
244		    (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
245			putchar('\n');
246			chcnt = 0;
247		}
248		chcnt += printaname(p, dp->s_inode, dp->s_block);
249		if (p->fts_link) {
250			printf(", ");
251			chcnt += 2;
252		}
253	}
254	if (chcnt)
255		putchar('\n');
256}
257
258void
259printcol(const DISPLAY *dp)
260{
261	static FTSENT **array;
262	static int lastentries = -1;
263	FTSENT *p;
264	FTSENT **narray;
265	int base;
266	int chcnt;
267	int cnt;
268	int col;
269	int colwidth;
270	int endcol;
271	int num;
272	int numcols;
273	int numrows;
274	int row;
275	int tabwidth;
276
277	if (f_notabs)
278		tabwidth = 1;
279	else
280		tabwidth = 8;
281
282	/*
283	 * Have to do random access in the linked list -- build a table
284	 * of pointers.
285	 */
286	if (dp->entries > lastentries) {
287		if ((narray =
288		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
289			warn(NULL);
290			printscol(dp);
291			return;
292		}
293		lastentries = dp->entries;
294		array = narray;
295	}
296	for (p = dp->list, num = 0; p; p = p->fts_link)
297		if (p->fts_number != NO_PRINT)
298			array[num++] = p;
299
300	colwidth = dp->maxlen;
301	if (f_inode)
302		colwidth += dp->s_inode + 1;
303	if (f_size)
304		colwidth += dp->s_block + 1;
305	if (f_type)
306		colwidth += 1;
307
308	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
309	if (termwidth < 2 * colwidth) {
310		printscol(dp);
311		return;
312	}
313	numcols = termwidth / colwidth;
314	numrows = num / numcols;
315	if (num % numcols)
316		++numrows;
317
318	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
319		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
320
321	base = 0;
322	for (row = 0; row < numrows; ++row) {
323		endcol = colwidth;
324		if (!f_sortacross)
325			base = row;
326		for (col = 0, chcnt = 0; col < numcols; ++col) {
327			chcnt += printaname(array[base], dp->s_inode,
328			    dp->s_block);
329			if (f_sortacross)
330				base++;
331			else
332				base += numrows;
333			if (base >= num)
334				break;
335			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
336			    <= endcol) {
337				if (f_sortacross && col + 1 >= numcols)
338					break;
339				(void)putchar(f_notabs ? ' ' : '\t');
340				chcnt = cnt;
341			}
342			endcol += colwidth;
343		}
344		(void)putchar('\n');
345	}
346}
347
348/*
349 * print [inode] [size] name
350 * return # of characters printed, no trailing characters.
351 */
352static int
353printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
354{
355	struct stat *sp;
356	int chcnt;
357#ifdef COLORLS
358	int color_printed = 0;
359#endif
360
361	sp = p->fts_statp;
362	chcnt = 0;
363	if (f_inode)
364		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
365	if (f_size)
366		chcnt += printf("%*jd ",
367		    (int)sizefield, howmany(sp->st_blocks, blocksize));
368#ifdef COLORLS
369	if (f_color)
370		color_printed = colortype(sp->st_mode);
371#endif
372	chcnt += printname(p->fts_name);
373#ifdef COLORLS
374	if (f_color && color_printed)
375		endcolor(0);
376#endif
377	if (f_type)
378		chcnt += printtype(sp->st_mode);
379	return (chcnt);
380}
381
382static void
383printtime(time_t ftime)
384{
385	char longstring[80];
386	static time_t now = 0;
387	const char *format;
388	static int d_first = -1;
389
390	if (d_first < 0)
391		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
392	if (now == 0)
393		now = time(NULL);
394
395#define	SIXMONTHS	((365 / 2) * 86400)
396	if (f_sectime)
397		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
398		format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
399	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
400		/* mmm dd hh:mm || dd mmm hh:mm */
401		format = d_first ? "%e %b %R " : "%b %e %R ";
402	else
403		/* mmm dd  yyyy || dd mmm  yyyy */
404		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
405	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
406	fputs(longstring, stdout);
407}
408
409static int
410printtype(u_int mode)
411{
412
413	if (f_slash) {
414		if ((mode & S_IFMT) == S_IFDIR) {
415			(void)putchar('/');
416			return (1);
417		}
418		return (0);
419	}
420
421	switch (mode & S_IFMT) {
422	case S_IFDIR:
423		(void)putchar('/');
424		return (1);
425	case S_IFIFO:
426		(void)putchar('|');
427		return (1);
428	case S_IFLNK:
429		(void)putchar('@');
430		return (1);
431	case S_IFSOCK:
432		(void)putchar('=');
433		return (1);
434	case S_IFWHT:
435		(void)putchar('%');
436		return (1);
437	default:
438		break;
439	}
440	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
441		(void)putchar('*');
442		return (1);
443	}
444	return (0);
445}
446
447#ifdef COLORLS
448static int
449putch(int c)
450{
451	(void)putchar(c);
452	return 0;
453}
454
455static int
456writech(int c)
457{
458	char tmp = (char)c;
459
460	(void)write(STDOUT_FILENO, &tmp, 1);
461	return 0;
462}
463
464static void
465printcolor(Colors c)
466{
467	char *ansiseq;
468
469	if (colors[c].bold)
470		tputs(enter_bold, 1, putch);
471
472	if (colors[c].num[0] != -1) {
473		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
474		if (ansiseq)
475			tputs(ansiseq, 1, putch);
476	}
477	if (colors[c].num[1] != -1) {
478		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
479		if (ansiseq)
480			tputs(ansiseq, 1, putch);
481	}
482}
483
484static void
485endcolor(int sig)
486{
487	tputs(ansi_coloff, 1, sig ? writech : putch);
488	tputs(attrs_off, 1, sig ? writech : putch);
489}
490
491static int
492colortype(mode_t mode)
493{
494	switch (mode & S_IFMT) {
495	case S_IFDIR:
496		if (mode & S_IWOTH)
497			if (mode & S_ISTXT)
498				printcolor(C_WSDIR);
499			else
500				printcolor(C_WDIR);
501		else
502			printcolor(C_DIR);
503		return (1);
504	case S_IFLNK:
505		printcolor(C_LNK);
506		return (1);
507	case S_IFSOCK:
508		printcolor(C_SOCK);
509		return (1);
510	case S_IFIFO:
511		printcolor(C_FIFO);
512		return (1);
513	case S_IFBLK:
514		printcolor(C_BLK);
515		return (1);
516	case S_IFCHR:
517		printcolor(C_CHR);
518		return (1);
519	default:;
520	}
521	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
522		if (mode & S_ISUID)
523			printcolor(C_SUID);
524		else if (mode & S_ISGID)
525			printcolor(C_SGID);
526		else
527			printcolor(C_EXEC);
528		return (1);
529	}
530	return (0);
531}
532
533void
534parsecolors(const char *cs)
535{
536	int i;
537	int j;
538	size_t len;
539	char c[2];
540	short legacy_warn = 0;
541
542	if (cs == NULL)
543		cs = "";	/* LSCOLORS not set */
544	len = strlen(cs);
545	for (i = 0; i < (int)C_NUMCOLORS; i++) {
546		colors[i].bold = 0;
547
548		if (len <= 2 * (size_t)i) {
549			c[0] = defcolors[2 * i];
550			c[1] = defcolors[2 * i + 1];
551		} else {
552			c[0] = cs[2 * i];
553			c[1] = cs[2 * i + 1];
554		}
555		for (j = 0; j < 2; j++) {
556			/* Legacy colours used 0-7 */
557			if (c[j] >= '0' && c[j] <= '7') {
558				colors[i].num[j] = c[j] - '0';
559				if (!legacy_warn) {
560					warnx("LSCOLORS should use "
561					    "characters a-h instead of 0-9 ("
562					    "see the manual page)");
563				}
564				legacy_warn = 1;
565			} else if (c[j] >= 'a' && c[j] <= 'h')
566				colors[i].num[j] = c[j] - 'a';
567			else if (c[j] >= 'A' && c[j] <= 'H') {
568				colors[i].num[j] = c[j] - 'A';
569				colors[i].bold = 1;
570			} else if (tolower((unsigned char)c[j]) == 'x')
571				colors[i].num[j] = -1;
572			else {
573				warnx("invalid character '%c' in LSCOLORS"
574				    " env var", c[j]);
575				colors[i].num[j] = -1;
576			}
577		}
578	}
579}
580
581void
582colorquit(int sig)
583{
584	endcolor(sig);
585
586	(void)signal(sig, SIG_DFL);
587	(void)kill(getpid(), sig);
588}
589
590#endif /* COLORLS */
591
592static void
593printlink(const FTSENT *p)
594{
595	int lnklen;
596	char name[MAXPATHLEN + 1];
597	char path[MAXPATHLEN + 1];
598
599	if (p->fts_level == FTS_ROOTLEVEL)
600		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
601	else
602		(void)snprintf(name, sizeof(name),
603		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
604	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
605		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
606		return;
607	}
608	path[lnklen] = '\0';
609	(void)printf(" -> ");
610	(void)printname(path);
611}
612
613static void
614printsize(size_t width, off_t bytes)
615{
616	double dbytes;
617	unit_t unit;
618
619	if (f_humanval) {
620		dbytes = bytes;
621		unit = unit_adjust(&dbytes);
622
623		if (unit == 0)
624			(void)printf("%*d%c ", 4, (int)bytes, 'B');
625		else
626			(void)printf("%*.*f%c ", 4,
627			    dbytes >= 99.95 ? 0 : 1, dbytes, "BKMGTPE"[unit]);
628	} else
629		(void)printf("%*jd ", (u_int)width, bytes);
630}
631
632/*
633 * Output in "human-readable" format.  Uses 3 digits max and puts
634 * unit suffixes at the end.  Makes output compact and easy to read,
635 * especially on huge disks.
636 *
637 */
638static unit_t
639unit_adjust(double *val)
640{
641	double abval;
642	unit_t unit;
643	u_int unit_sz;
644
645	abval = fabs(*val);
646
647	unit_sz = abval ? (u_int)ilogb(abval) / 10 : 0;
648
649	if (unit_sz >= (u_int)UNIT_MAX) {
650		unit = NONE;
651	} else {
652		unit = unitp[unit_sz];
653		*val /= (double)vals_base2[unit_sz];
654	}
655
656	return (unit);
657}
658
659static void
660aclmode(char *buf, const FTSENT *p, int *haveacls)
661{
662	char name[MAXPATHLEN + 1];
663	int entries, ret;
664	acl_t facl;
665	acl_entry_t ae;
666
667	/*
668	 * Add a + after the standard rwxrwxrwx mode if the file has an
669	 * extended ACL. strmode() reserves space at the end of the string.
670	 */
671	if (p->fts_level == FTS_ROOTLEVEL)
672		snprintf(name, sizeof(name), "%s", p->fts_name);
673	else
674		snprintf(name, sizeof(name), "%s/%s",
675		    p->fts_parent->fts_accpath, p->fts_name);
676	/*
677	 * We have no way to tell whether a symbolic link has an ACL since
678	 * pathconf() and acl_get_file() both follow them.
679	 */
680	if (S_ISLNK(p->fts_statp->st_mode)) {
681		*haveacls = 1;
682		return;
683	}
684	if ((ret = pathconf(name, _PC_ACL_EXTENDED)) <= 0) {
685		if (ret < 0 && errno != EINVAL)
686			warn("%s", name);
687		else
688			*haveacls = 0;
689		return;
690	}
691	*haveacls = 1;
692	if ((facl = acl_get_file(name, ACL_TYPE_ACCESS)) != NULL) {
693		if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
694			entries = 1;
695			while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1)
696				if (++entries > 3)
697					break;
698			/*
699			 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
700			 * must have at least three entries (owner, group,
701			 * and other). So anything with more than 3 ACLs looks
702			 * interesting to us.
703			 */
704			if (entries > 3)
705				buf[10] = '+';
706		}
707		acl_free(facl);
708	} else
709		warn("%s", name);
710}
711