xref: /netbsd-src/bin/ls/print.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: print.c,v 1.39 2004/08/09 20:14:54 jschauma Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Michael Fischbein.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)print.c	8.5 (Berkeley) 7/28/94";
39 #else
40 __RCSID("$NetBSD: print.c,v 1.39 2004/08/09 20:14:54 jschauma Exp $");
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fts.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <tzfile.h>
57 #include <unistd.h>
58 #include <util.h>
59 
60 #include "ls.h"
61 #include "extern.h"
62 
63 extern int termwidth;
64 
65 static int	printaname(FTSENT *, int, int);
66 static void	printlink(FTSENT *);
67 static void	printtime(time_t);
68 static int	printtype(u_int);
69 
70 static time_t	now;
71 
72 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
73 
74 void
75 printscol(DISPLAY *dp)
76 {
77 	FTSENT *p;
78 
79 	for (p = dp->list; p; p = p->fts_link) {
80 		if (IS_NOPRINT(p))
81 			continue;
82 		(void)printaname(p, dp->s_inode, dp->s_block);
83 		(void)putchar('\n');
84 	}
85 }
86 
87 void
88 printlong(DISPLAY *dp)
89 {
90 	struct stat *sp;
91 	FTSENT *p;
92 	NAMES *np;
93 	char buf[20], szbuf[5];
94 
95 	now = time(NULL);
96 
97 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
98 		if (f_humanize) {
99 			if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
100 			    "", HN_AUTOSCALE,
101 			    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
102 				err(1, "humanize_number");
103 			(void)printf("total %s\n", szbuf);
104 		} else {
105 			(void)printf("total %llu\n",
106 			    (long long)(howmany(dp->btotal, blocksize)));
107 		}
108 	}
109 
110 	for (p = dp->list; p; p = p->fts_link) {
111 		if (IS_NOPRINT(p))
112 			continue;
113 		sp = p->fts_statp;
114 		if (f_inode)
115 			(void)printf("%*lu ", dp->s_inode,
116 			    (unsigned long)sp->st_ino);
117 		if (f_size && !f_humanize) {
118 			(void)printf("%*llu ", dp->s_block,
119 			    (long long)howmany(sp->st_blocks, blocksize));
120 		}
121 		(void)strmode(sp->st_mode, buf);
122 		np = p->fts_pointer;
123 		(void)printf("%s %*lu ", buf, dp->s_nlink,
124 		    (unsigned long)sp->st_nlink);
125 		if (!f_grouponly)
126 			(void)printf("%-*s  ", dp->s_user, np->user);
127 		(void)printf("%-*s  ", dp->s_group, np->group);
128 		if (f_flags)
129 			(void)printf("%-*s ", dp->s_flags, np->flags);
130 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
131 			(void)printf("%*u, %*u ",
132 			    dp->s_major, major(sp->st_rdev), dp->s_minor,
133 			    minor(sp->st_rdev));
134 		else
135 			if (f_humanize) {
136 				if ((humanize_number(szbuf, sizeof(szbuf),
137 				    sp->st_size, "", HN_AUTOSCALE,
138 				    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
139 					err(1, "humanize_number");
140 				(void)printf("%*s ", dp->s_size, szbuf);
141 			} else {
142 				(void)printf("%*llu ", dp->s_size,
143 				    (long long)sp->st_size);
144 			}
145 		if (f_accesstime)
146 			printtime(sp->st_atime);
147 		else if (f_statustime)
148 			printtime(sp->st_ctime);
149 		else
150 			printtime(sp->st_mtime);
151 		if (f_octal || f_octal_escape)
152 			(void)safe_print(p->fts_name);
153 		else if (f_nonprint)
154 			(void)printescaped(p->fts_name);
155 		else
156 			(void)printf("%s", p->fts_name);
157 
158 		if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
159 			(void)printtype(sp->st_mode);
160 		if (S_ISLNK(sp->st_mode))
161 			printlink(p);
162 		(void)putchar('\n');
163 	}
164 }
165 
166 void
167 printcol(DISPLAY *dp)
168 {
169 	static FTSENT **array;
170 	static int lastentries = -1;
171 	FTSENT *p;
172 	int base, chcnt, col, colwidth, num;
173 	int numcols, numrows, row;
174 	char szbuf[5];
175 
176 	colwidth = dp->maxlen;
177 	if (f_inode)
178 		colwidth += dp->s_inode + 1;
179 	if (f_size) {
180 		if (f_humanize)
181 			colwidth += dp->s_size + 1;
182 		else
183 			colwidth += dp->s_block + 1;
184 	}
185 	if (f_type || f_typedir)
186 		colwidth += 1;
187 
188 	colwidth += 1;
189 
190 	if (termwidth < 2 * colwidth) {
191 		printscol(dp);
192 		return;
193 	}
194 
195 	/*
196 	 * Have to do random access in the linked list -- build a table
197 	 * of pointers.
198 	 */
199 	if (dp->entries > lastentries) {
200 		lastentries = dp->entries;
201 		if ((array =
202 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
203 			warn(NULL);
204 			printscol(dp);
205 		}
206 	}
207 	for (p = dp->list, num = 0; p; p = p->fts_link)
208 		if (p->fts_number != NO_PRINT)
209 			array[num++] = p;
210 
211 	numcols = termwidth / colwidth;
212 	colwidth = termwidth / numcols;		/* spread out if possible */
213 	numrows = num / numcols;
214 	if (num % numcols)
215 		++numrows;
216 
217 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
218 		if (f_humanize) {
219 			if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
220 			    "", HN_AUTOSCALE,
221 			    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
222 				err(1, "humanize_number");
223 			(void)printf("total %s\n", szbuf);
224 		} else {
225 			(void)printf("total %llu\n",
226 			    (long long)(howmany(dp->btotal, blocksize)));
227 		}
228 	}
229 	for (row = 0; row < numrows; ++row) {
230 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
231 			chcnt = printaname(array[base], dp->s_inode,
232 			    f_humanize ? dp->s_size : dp->s_block);
233 			if ((base += numrows) >= num)
234 				break;
235 			while (chcnt++ < colwidth)
236 				(void)putchar(' ');
237 		}
238 		(void)putchar('\n');
239 	}
240 }
241 
242 void
243 printacol(DISPLAY *dp)
244 {
245 	FTSENT *p;
246 	int chcnt, col, colwidth;
247 	int numcols;
248 	char szbuf[5];
249 
250 	colwidth = dp->maxlen;
251 	if (f_inode)
252 		colwidth += dp->s_inode + 1;
253 	if (f_size) {
254 		if (f_humanize)
255 			colwidth += dp->s_size + 1;
256 		else
257 			colwidth += dp->s_block + 1;
258 	}
259 	if (f_type || f_typedir)
260 		colwidth += 1;
261 
262 	colwidth += 1;
263 
264 	if (termwidth < 2 * colwidth) {
265 		printscol(dp);
266 		return;
267 	}
268 
269 	numcols = termwidth / colwidth;
270 	colwidth = termwidth / numcols;		/* spread out if possible */
271 
272 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
273 		if (f_humanize) {
274 			if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
275 			    "", HN_AUTOSCALE,
276 			    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
277 				err(1, "humanize_number");
278 			(void)printf("total %s\n", szbuf);
279 		} else {
280 			(void)printf("total %llu\n",
281 			    (long long)(howmany(dp->btotal, blocksize)));
282 		}
283 	}
284 	chcnt = col = 0;
285 	for (p = dp->list; p; p = p->fts_link) {
286 		if (IS_NOPRINT(p))
287 			continue;
288 		if (col >= numcols) {
289 			chcnt = col = 0;
290 			(void)putchar('\n');
291 		}
292 		chcnt = printaname(p, dp->s_inode,
293 		    f_humanize ? dp->s_size : dp->s_block);
294 		while (chcnt++ < colwidth)
295 			(void)putchar(' ');
296 		col++;
297 	}
298 	(void)putchar('\n');
299 }
300 
301 void
302 printstream(DISPLAY *dp)
303 {
304 	FTSENT *p;
305 	int col;
306 	int extwidth;
307 
308 	extwidth = 0;
309 	if (f_inode)
310 		extwidth += dp->s_inode + 1;
311 	if (f_size) {
312 		if (f_humanize)
313 			extwidth += dp->s_size + 1;
314 		else
315 			extwidth += dp->s_block + 1;
316 	}
317 	if (f_type)
318 		extwidth += 1;
319 
320 	for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
321 		if (IS_NOPRINT(p))
322 			continue;
323 		if (col > 0) {
324 			(void)putchar(','), col++;
325 			if (col + 1 + extwidth + p->fts_namelen >= termwidth)
326 				(void)putchar('\n'), col = 0;
327 			else
328 				(void)putchar(' '), col++;
329 		}
330 		col += printaname(p, dp->s_inode,
331 		    f_humanize ? dp->s_size : dp->s_block);
332 	}
333 	(void)putchar('\n');
334 }
335 
336 /*
337  * print [inode] [size] name
338  * return # of characters printed, no trailing characters.
339  */
340 static int
341 printaname(FTSENT *p, int inodefield, int sizefield)
342 {
343 	struct stat *sp;
344 	int chcnt;
345 	char szbuf[5];
346 
347 	sp = p->fts_statp;
348 	chcnt = 0;
349 	if (f_inode)
350 		chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
351 	if (f_size) {
352 		if (f_humanize) {
353 			if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
354 			    "", HN_AUTOSCALE,
355 			    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
356 				err(1, "humanize_number");
357 			chcnt += printf("%*s ", sizefield, szbuf);
358 		} else {
359 			chcnt += printf("%*llu ", sizefield,
360 			    (long long)howmany(sp->st_blocks, blocksize));
361 		}
362 	}
363 	if (f_octal || f_octal_escape)
364 		chcnt += safe_print(p->fts_name);
365 	else if (f_nonprint)
366 		chcnt += printescaped(p->fts_name);
367 	else
368 		chcnt += printf("%s", p->fts_name);
369 	if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
370 		chcnt += printtype(sp->st_mode);
371 	return (chcnt);
372 }
373 
374 static void
375 printtime(time_t ftime)
376 {
377 	int i;
378 	char *longstring;
379 
380 	longstring = ctime(&ftime);
381 	for (i = 4; i < 11; ++i)
382 		(void)putchar(longstring[i]);
383 
384 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
385 	if (f_sectime)
386 		for (i = 11; i < 24; i++)
387 			(void)putchar(longstring[i]);
388 	else if (ftime + SIXMONTHS > now && ftime <= now)
389 		for (i = 11; i < 16; ++i)
390 			(void)putchar(longstring[i]);
391 	else {
392 		(void)putchar(' ');
393 		for (i = 20; i < 24; ++i)
394 			(void)putchar(longstring[i]);
395 	}
396 	(void)putchar(' ');
397 }
398 
399 static int
400 printtype(u_int mode)
401 {
402 	switch (mode & S_IFMT) {
403 	case S_IFDIR:
404 		(void)putchar('/');
405 		return (1);
406 	case S_IFIFO:
407 		(void)putchar('|');
408 		return (1);
409 	case S_IFLNK:
410 		(void)putchar('@');
411 		return (1);
412 	case S_IFSOCK:
413 		(void)putchar('=');
414 		return (1);
415 	case S_IFWHT:
416 		(void)putchar('%');
417 		return (1);
418 	}
419 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
420 		(void)putchar('*');
421 		return (1);
422 	}
423 	return (0);
424 }
425 
426 static void
427 printlink(FTSENT *p)
428 {
429 	int lnklen;
430 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
431 
432 	if (p->fts_level == FTS_ROOTLEVEL)
433 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
434 	else
435 		(void)snprintf(name, sizeof(name),
436 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
437 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
438 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
439 		return;
440 	}
441 	path[lnklen] = '\0';
442 	(void)printf(" -> ");
443 	if (f_octal || f_octal_escape)
444 		(void)safe_print(path);
445 	else if (f_nonprint)
446 		(void)printescaped(path);
447 	else
448 		(void)printf("%s", path);
449 }
450