xref: /freebsd/bin/ls/print.c (revision 8fa113e5fc65fe6abc757f0089f477a87ee4d185)
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)print.c	8.4 (Berkeley) 4/17/94";
40 #else
41 static const char rcsid[] =
42   "$FreeBSD$";
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <fts.h>
52 #include <grp.h>
53 #include <langinfo.h>
54 #include <pwd.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59 #include <unistd.h>
60 #ifdef COLORLS
61 #include <ctype.h>
62 #include <termcap.h>
63 #include <signal.h>
64 #endif
65 
66 #include "ls.h"
67 #include "extern.h"
68 
69 static int	printaname __P((FTSENT *, u_long, u_long));
70 static void	printlink __P((FTSENT *));
71 static void	printtime __P((time_t));
72 static int	printtype __P((u_int));
73 #ifdef COLORLS
74 static void     endcolor __P((int));
75 static int      colortype __P((mode_t));
76 #endif
77 
78 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
79 
80 #ifdef COLORLS
81 /* Most of these are taken from <sys/stat.h> */
82 typedef enum Colors {
83     C_DIR,     /* directory */
84     C_LNK,     /* symbolic link */
85     C_SOCK,    /* socket */
86     C_FIFO,    /* pipe */
87     C_EXEC,    /* executable */
88     C_BLK,     /* block special */
89     C_CHR,     /* character special */
90     C_SUID,    /* setuid executable */
91     C_SGID,    /* setgid executable */
92     C_WSDIR,   /* directory writeble to others, with sticky bit */
93     C_WDIR,    /* directory writeble to others, without sticky bit */
94     C_NUMCOLORS        /* just a place-holder */
95 } Colors ;
96 
97 char *defcolors = "4x5x2x3x1x464301060203";
98 
99 static int colors[C_NUMCOLORS][2];
100 #endif
101 
102 void
103 printscol(dp)
104 	DISPLAY *dp;
105 {
106 	FTSENT *p;
107 
108 	for (p = dp->list; p; p = p->fts_link) {
109 		if (IS_NOPRINT(p))
110 			continue;
111 		(void)printaname(p, dp->s_inode, dp->s_block);
112 		(void)putchar('\n');
113 	}
114 }
115 
116 /*
117  * print name in current style
118  */
119 static int
120 printname(name)
121 	const char *name;
122 {
123 	if (f_octal || f_octal_escape)
124 		return prn_octal(name);
125 	else if (f_nonprint)
126 		return prn_printable(name);
127 	else
128 		return printf("%s", name);
129 }
130 
131 void
132 printlong(dp)
133 	DISPLAY *dp;
134 {
135 	struct stat *sp;
136 	FTSENT *p;
137 	NAMES *np;
138 	char buf[20];
139 #ifdef COLORLS
140 	int color_printed = 0;
141 #endif
142 
143 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
144 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
145 
146 	for (p = dp->list; p; p = p->fts_link) {
147 		if (IS_NOPRINT(p))
148 			continue;
149 		sp = p->fts_statp;
150 		if (f_inode)
151 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
152 		if (f_size)
153 			(void)printf("%*qd ",
154 			    dp->s_block, howmany(sp->st_blocks, blocksize));
155 		(void)strmode(sp->st_mode, buf);
156 		np = p->fts_pointer;
157 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
158 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
159 		    np->group);
160 		if (f_flags)
161 			(void)printf("%-*s ", dp->s_flags, np->flags);
162 		if (f_lomac)
163 			(void)printf("%-*s ", dp->s_lattr, np->lattr);
164 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
165 			if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
166 				(void)printf("%3d, 0x%08x ",
167 				    major(sp->st_rdev),
168 				    (u_int)minor(sp->st_rdev));
169 			else
170 				(void)printf("%3d, %3d ",
171 				    major(sp->st_rdev), minor(sp->st_rdev));
172 		else if (dp->bcfile)
173 			(void)printf("%*s%*qd ",
174 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
175 		else
176 			(void)printf("%*qd ", dp->s_size, sp->st_size);
177 		if (f_accesstime)
178 			printtime(sp->st_atime);
179 		else if (f_statustime)
180 			printtime(sp->st_ctime);
181 		else
182 			printtime(sp->st_mtime);
183 #ifdef COLORLS
184 		if (f_color)
185 			color_printed = colortype(sp->st_mode);
186 #endif
187 		(void)printname(p->fts_name);
188 #ifdef COLORLS
189 		if (f_color && color_printed)
190 			endcolor(0);
191 #endif
192 		if (f_type)
193 			(void)printtype(sp->st_mode);
194 		if (S_ISLNK(sp->st_mode))
195 			printlink(p);
196 		(void)putchar('\n');
197 	}
198 }
199 
200 void
201 printcol(dp)
202 	DISPLAY *dp;
203 {
204 	extern int termwidth;
205 	static FTSENT **array;
206 	static int lastentries = -1;
207 	FTSENT *p;
208 	int base, chcnt, cnt, col, colwidth, num;
209 	int endcol, numcols, numrows, row;
210 	int tabwidth;
211 
212 	if (f_notabs)
213 		tabwidth = 1;
214 	else
215 		tabwidth = 8;
216 
217 	/*
218 	 * Have to do random access in the linked list -- build a table
219 	 * of pointers.
220 	 */
221 	if (dp->entries > lastentries) {
222 		lastentries = dp->entries;
223 		if ((array =
224 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
225 			warn(NULL);
226 			printscol(dp);
227 		}
228 	}
229 	for (p = dp->list, num = 0; p; p = p->fts_link)
230 		if (p->fts_number != NO_PRINT)
231 			array[num++] = p;
232 
233 	colwidth = dp->maxlen;
234 	if (f_inode)
235 		colwidth += dp->s_inode + 1;
236 	if (f_size)
237 		colwidth += dp->s_block + 1;
238 	if (f_type)
239 		colwidth += 1;
240 
241 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
242 	if (termwidth < 2 * colwidth) {
243 		printscol(dp);
244 		return;
245 	}
246 
247 	numcols = termwidth / colwidth;
248 	numrows = num / numcols;
249 	if (num % numcols)
250 		++numrows;
251 
252 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
253 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
254 	for (row = 0; row < numrows; ++row) {
255 		endcol = colwidth;
256 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
257 			chcnt += printaname(array[base], dp->s_inode,
258 			    dp->s_block);
259 			if ((base += numrows) >= num)
260 				break;
261 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
262 			    <= endcol){
263 				(void)putchar(f_notabs ? ' ' : '\t');
264 				chcnt = cnt;
265 			}
266 			endcol += colwidth;
267 		}
268 		(void)putchar('\n');
269 	}
270 }
271 
272 /*
273  * print [inode] [size] name
274  * return # of characters printed, no trailing characters.
275  */
276 static int
277 printaname(p, inodefield, sizefield)
278 	FTSENT *p;
279 	u_long sizefield, inodefield;
280 {
281 	struct stat *sp;
282 	int chcnt;
283 #ifdef COLORLS
284 	int color_printed = 0;
285 #endif
286 
287 	sp = p->fts_statp;
288 	chcnt = 0;
289 	if (f_inode)
290 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
291 	if (f_size)
292 		chcnt += printf("%*qd ",
293 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
294 #ifdef COLORLS
295 	if (f_color)
296 		color_printed = colortype(sp->st_mode);
297 #endif
298 	chcnt += printname(p->fts_name);
299 #ifdef COLORLS
300 	if (f_color && color_printed)
301 		endcolor(0);
302 #endif
303 	if (f_type)
304 		chcnt += printtype(sp->st_mode);
305 	return (chcnt);
306 }
307 
308 static void
309 printtime(ftime)
310 	time_t ftime;
311 {
312 	char longstring[80];
313 	static time_t now;
314 	const char *format;
315 	static int d_first = -1;
316 
317 	if (d_first < 0)
318 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
319 	if (now == 0)
320 		now = time(NULL);
321 
322 #define	SIXMONTHS	((365 / 2) * 86400)
323 	if (f_sectime)
324 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
325 		format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
326 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
327 		/* mmm dd hh:mm || dd mmm hh:mm */
328 		format = d_first ? "%e %b %R " : "%b %e %R ";
329 	else
330 		/* mmm dd  yyyy || dd mmm  yyyy */
331 		format = d_first ? "%e %b  %Y " : "%b %e  %Y ";
332 	strftime(longstring, sizeof(longstring), format, localtime(&ftime));
333 	fputs(longstring, stdout);
334 }
335 
336 static int
337 printtype(mode)
338 	u_int mode;
339 {
340 	switch (mode & S_IFMT) {
341 	case S_IFDIR:
342 		(void)putchar('/');
343 		return (1);
344 	case S_IFIFO:
345 		(void)putchar('|');
346 		return (1);
347 	case S_IFLNK:
348 		(void)putchar('@');
349 		return (1);
350 	case S_IFSOCK:
351 		(void)putchar('=');
352 		return (1);
353 	case S_IFWHT:
354 		(void)putchar('%');
355 		return (1);
356 	}
357 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
358 		(void)putchar('*');
359 		return (1);
360 	}
361 	return (0);
362 }
363 
364 #ifdef COLORLS
365 static int
366 putch(c)
367 	int c;
368 {
369 	(void) putchar(c);
370 	return 0;
371 }
372 
373 static int
374 writech(c)
375 	int c;
376 {
377 	char tmp = c;
378 
379 	(void) write(STDOUT_FILENO, &tmp, 1);
380 	return 0;
381 }
382 
383 static void
384 printcolor(c)
385        Colors c;
386 {
387 	char *ansiseq;
388 
389 	if (colors[c][0] != -1) {
390 		ansiseq = tgoto(ansi_fgcol, 0, colors[c][0]);
391 		if (ansiseq)
392 			tputs(ansiseq, 1, putch);
393 	}
394 
395 	if (colors[c][1] != -1) {
396 		ansiseq = tgoto(ansi_bgcol, 0, colors[c][1]);
397 		if (ansiseq)
398 			tputs(ansiseq, 1, putch);
399 	}
400 }
401 
402 static void
403 endcolor(sig)
404 	int sig;
405 {
406 	tputs(ansi_coloff, 1, sig ? writech : putch);
407 }
408 
409 static int
410 colortype(mode)
411        mode_t mode;
412 {
413 	switch(mode & S_IFMT) {
414 	      case S_IFDIR:
415 		if (mode & S_IWOTH)
416 		    if (mode & S_ISTXT)
417 			printcolor(C_WSDIR);
418 		    else
419 			printcolor(C_WDIR);
420 		else
421 		    printcolor(C_DIR);
422 		return(1);
423 	      case S_IFLNK:
424 		printcolor(C_LNK);
425 		return(1);
426 	      case S_IFSOCK:
427 		printcolor(C_SOCK);
428 		return(1);
429 	      case S_IFIFO:
430 		printcolor(C_FIFO);
431 		return(1);
432 	      case S_IFBLK:
433 		printcolor(C_BLK);
434 		return(1);
435 	      case S_IFCHR:
436 		printcolor(C_CHR);
437 		return(1);
438 	}
439 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
440 		if (mode & S_ISUID)
441 		    printcolor(C_SUID);
442 		else if (mode & S_ISGID)
443 		    printcolor(C_SGID);
444 		else
445 		    printcolor(C_EXEC);
446 		return(1);
447 	}
448 	return(0);
449 }
450 
451 void
452 parsecolors(cs)
453 char *cs;
454 {
455 	int i, j, len;
456 	char c[2];
457 
458 	if (cs == NULL)    cs = ""; /* LSCOLORS not set */
459 	len = strlen(cs);
460 	for (i = 0 ; i < C_NUMCOLORS ; i++) {
461 		if (len <= 2*i) {
462 			c[0] = defcolors[2*i];
463 			c[1] = defcolors[2*i+1];
464 		}
465 		else {
466 			c[0] = cs[2*i];
467 			c[1] = cs[2*i+1];
468 		}
469 		for (j = 0 ; j < 2 ; j++) {
470 			if ((c[j] < '0' || c[j] > '7') &&
471 			    tolower((unsigned char)c[j]) != 'x') {
472 				fprintf(stderr,
473 					"error: invalid character '%c' in LSCOLORS env var\n",
474 					c[j]);
475 				c[j] = defcolors[2*i+j];
476 			}
477 			if (tolower((unsigned char)c[j]) == 'x')
478 			    colors[i][j] = -1;
479 			else
480 			    colors[i][j] = c[j]-'0';
481 		}
482 	}
483 }
484 
485 void
486 colorquit(sig)
487 	int sig;
488 {
489 	endcolor(sig);
490 
491 	(void) signal(sig, SIG_DFL);
492 	(void) kill(getpid(), sig);
493 }
494 #endif /*COLORLS*/
495 
496 static void
497 printlink(p)
498 	FTSENT *p;
499 {
500 	int lnklen;
501 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
502 
503 	if (p->fts_level == FTS_ROOTLEVEL)
504 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
505 	else
506 		(void)snprintf(name, sizeof(name),
507 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
508 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
509 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
510 		return;
511 	}
512 	path[lnklen] = '\0';
513 	(void)printf(" -> ");
514 	printname(path);
515 }
516