xref: /netbsd/bin/ls/print.c (revision b5b29542)
1 /*	$NetBSD: print.c,v 1.35 2003/08/07 09:05:15 agc 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.35 2003/08/07 09:05:15 agc 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 
59 #include "ls.h"
60 #include "extern.h"
61 
62 extern int termwidth;
63 
64 static int	printaname(FTSENT *, int, int);
65 static void	printlink(FTSENT *);
66 static void	printtime(time_t);
67 static int	printtype(u_int);
68 
69 static time_t	now;
70 
71 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
72 
73 void
74 printscol(DISPLAY *dp)
75 {
76 	FTSENT *p;
77 
78 	for (p = dp->list; p; p = p->fts_link) {
79 		if (IS_NOPRINT(p))
80 			continue;
81 		(void)printaname(p, dp->s_inode, dp->s_block);
82 		(void)putchar('\n');
83 	}
84 }
85 
86 void
87 printlong(DISPLAY *dp)
88 {
89 	struct stat *sp;
90 	FTSENT *p;
91 	NAMES *np;
92 	char buf[20];
93 
94 	now = time(NULL);
95 
96 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
97 		(void)printf("total %llu\n",
98 		    (long long)(howmany(dp->btotal, blocksize)));
99 
100 	for (p = dp->list; p; p = p->fts_link) {
101 		if (IS_NOPRINT(p))
102 			continue;
103 		sp = p->fts_statp;
104 		if (f_inode)
105 			(void)printf("%*lu ", dp->s_inode,
106 			    (unsigned long)sp->st_ino);
107 		if (f_size)
108 			(void)printf("%*llu ", dp->s_block,
109 			    (long long)howmany(sp->st_blocks, blocksize));
110 		(void)strmode(sp->st_mode, buf);
111 		np = p->fts_pointer;
112 		(void)printf("%s %*lu ", buf, dp->s_nlink,
113 		    (unsigned long)sp->st_nlink);
114 		if (!f_grouponly)
115 			(void)printf("%-*s  ", dp->s_user, np->user);
116 		(void)printf("%-*s  ", dp->s_group, np->group);
117 		if (f_flags)
118 			(void)printf("%-*s ", dp->s_flags, np->flags);
119 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
120 			(void)printf("%*u, %*u ",
121 			    dp->s_major, major(sp->st_rdev), dp->s_minor,
122 			    minor(sp->st_rdev));
123 		else
124 			(void)printf("%*llu ", dp->s_size,
125 			    (long long)sp->st_size);
126 		if (f_accesstime)
127 			printtime(sp->st_atime);
128 		else if (f_statustime)
129 			printtime(sp->st_ctime);
130 		else
131 			printtime(sp->st_mtime);
132 		if (f_nonprint)
133 			(void)printescaped(p->fts_name);
134 		else
135 			(void)printf("%s", p->fts_name);
136 
137 		if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
138 			(void)printtype(sp->st_mode);
139 		if (S_ISLNK(sp->st_mode))
140 			printlink(p);
141 		(void)putchar('\n');
142 	}
143 }
144 
145 void
146 printcol(DISPLAY *dp)
147 {
148 	static FTSENT **array;
149 	static int lastentries = -1;
150 	FTSENT *p;
151 	int base, chcnt, col, colwidth, num;
152 	int numcols, numrows, row;
153 
154 	colwidth = dp->maxlen;
155 	if (f_inode)
156 		colwidth += dp->s_inode + 1;
157 	if (f_size)
158 		colwidth += dp->s_block + 1;
159 	if (f_type || f_typedir)
160 		colwidth += 1;
161 
162 	colwidth += 1;
163 
164 	if (termwidth < 2 * colwidth) {
165 		printscol(dp);
166 		return;
167 	}
168 
169 	/*
170 	 * Have to do random access in the linked list -- build a table
171 	 * of pointers.
172 	 */
173 	if (dp->entries > lastentries) {
174 		lastentries = dp->entries;
175 		if ((array =
176 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
177 			warn(NULL);
178 			printscol(dp);
179 		}
180 	}
181 	for (p = dp->list, num = 0; p; p = p->fts_link)
182 		if (p->fts_number != NO_PRINT)
183 			array[num++] = p;
184 
185 	numcols = termwidth / colwidth;
186 	colwidth = termwidth / numcols;		/* spread out if possible */
187 	numrows = num / numcols;
188 	if (num % numcols)
189 		++numrows;
190 
191 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
192 		(void)printf("total %llu\n",
193 		    (long long)(howmany(dp->btotal, blocksize)));
194 	for (row = 0; row < numrows; ++row) {
195 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
196 			chcnt = printaname(array[base], dp->s_inode,
197 			    dp->s_block);
198 			if ((base += numrows) >= num)
199 				break;
200 			while (chcnt++ < colwidth)
201 				(void)putchar(' ');
202 		}
203 		(void)putchar('\n');
204 	}
205 }
206 
207 void
208 printacol(DISPLAY *dp)
209 {
210 	FTSENT *p;
211 	int chcnt, col, colwidth;
212 	int numcols;
213 
214 	colwidth = dp->maxlen;
215 	if (f_inode)
216 		colwidth += dp->s_inode + 1;
217 	if (f_size)
218 		colwidth += dp->s_block + 1;
219 	if (f_type || f_typedir)
220 		colwidth += 1;
221 
222 	colwidth += 1;
223 
224 	if (termwidth < 2 * colwidth) {
225 		printscol(dp);
226 		return;
227 	}
228 
229 	numcols = termwidth / colwidth;
230 	colwidth = termwidth / numcols;		/* spread out if possible */
231 
232 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
233 		(void)printf("total %llu\n",
234 		    (long long)(howmany(dp->btotal, blocksize)));
235 	chcnt = col = 0;
236 	for (p = dp->list; p; p = p->fts_link) {
237 		if (IS_NOPRINT(p))
238 			continue;
239 		if (col >= numcols) {
240 			chcnt = col = 0;
241 			(void)putchar('\n');
242 		}
243 		chcnt = printaname(p, dp->s_inode, dp->s_block);
244 		while (chcnt++ < colwidth)
245 			(void)putchar(' ');
246 		col++;
247 	}
248 	(void)putchar('\n');
249 }
250 
251 void
252 printstream(DISPLAY *dp)
253 {
254 	FTSENT *p;
255 	int col;
256 	int extwidth;
257 
258 	extwidth = 0;
259 	if (f_inode)
260 		extwidth += dp->s_inode + 1;
261 	if (f_size)
262 		extwidth += dp->s_block + 1;
263 	if (f_type)
264 		extwidth += 1;
265 
266 	for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
267 		if (IS_NOPRINT(p))
268 			continue;
269 		if (col > 0) {
270 			(void)putchar(','), col++;
271 			if (col + 1 + extwidth + p->fts_namelen >= termwidth)
272 				(void)putchar('\n'), col = 0;
273 			else
274 				(void)putchar(' '), col++;
275 		}
276 		col += printaname(p, dp->s_inode, dp->s_block);
277 	}
278 	(void)putchar('\n');
279 }
280 
281 /*
282  * print [inode] [size] name
283  * return # of characters printed, no trailing characters.
284  */
285 static int
286 printaname(FTSENT *p, int inodefield, int sizefield)
287 {
288 	struct stat *sp;
289 	int chcnt;
290 
291 	sp = p->fts_statp;
292 	chcnt = 0;
293 	if (f_inode)
294 		chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
295 	if (f_size)
296 		chcnt += printf("%*llu ", sizefield,
297 		    (long long)howmany(sp->st_blocks, blocksize));
298 	if (f_nonprint)
299 	    chcnt += printescaped(p->fts_name);
300 	else
301 	    chcnt += printf("%s", p->fts_name);
302 	if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
303 		chcnt += printtype(sp->st_mode);
304 	return (chcnt);
305 }
306 
307 static void
308 printtime(time_t ftime)
309 {
310 	int i;
311 	char *longstring;
312 
313 	longstring = ctime(&ftime);
314 	for (i = 4; i < 11; ++i)
315 		(void)putchar(longstring[i]);
316 
317 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
318 	if (f_sectime)
319 		for (i = 11; i < 24; i++)
320 			(void)putchar(longstring[i]);
321 	else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
322 		for (i = 11; i < 16; ++i)
323 			(void)putchar(longstring[i]);
324 	else {
325 		(void)putchar(' ');
326 		for (i = 20; i < 24; ++i)
327 			(void)putchar(longstring[i]);
328 	}
329 	(void)putchar(' ');
330 }
331 
332 static int
333 printtype(u_int mode)
334 {
335 	switch (mode & S_IFMT) {
336 	case S_IFDIR:
337 		(void)putchar('/');
338 		return (1);
339 	case S_IFIFO:
340 		(void)putchar('|');
341 		return (1);
342 	case S_IFLNK:
343 		(void)putchar('@');
344 		return (1);
345 	case S_IFSOCK:
346 		(void)putchar('=');
347 		return (1);
348 	case S_IFWHT:
349 		(void)putchar('%');
350 		return (1);
351 	}
352 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
353 		(void)putchar('*');
354 		return (1);
355 	}
356 	return (0);
357 }
358 
359 static void
360 printlink(FTSENT *p)
361 {
362 	int lnklen;
363 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
364 
365 	if (p->fts_level == FTS_ROOTLEVEL)
366 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
367 	else
368 		(void)snprintf(name, sizeof(name),
369 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
370 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
371 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
372 		return;
373 	}
374 	path[lnklen] = '\0';
375 	(void)printf(" -> ");
376 	if (f_nonprint)
377 		printescaped(path);
378 	else
379 		(void)printf("%s", path);
380 }
381