xref: /openbsd/bin/ls/print.c (revision db3296cf)
1 /*	$OpenBSD: print.c,v 1.19 2003/06/11 23:42:12 deraadt Exp $	*/
2 /*	$NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Michael Fischbein.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)print.c	8.5 (Berkeley) 7/28/94";
39 #else
40 static char rcsid[] = "$OpenBSD: print.c,v 1.19 2003/06/11 23:42:12 deraadt 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 static int	printaname(FTSENT *, u_long, u_long);
63 static void	printlink(FTSENT *);
64 static void	printtime(time_t);
65 static int	printtype(u_int);
66 static int	compute_columns(DISPLAY *, int *);
67 
68 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
69 
70 void
71 printscol(DISPLAY *dp)
72 {
73 	FTSENT *p;
74 
75 	for (p = dp->list; p; p = p->fts_link) {
76 		if (IS_NOPRINT(p))
77 			continue;
78 		(void)printaname(p, dp->s_inode, dp->s_block);
79 		(void)putchar('\n');
80 	}
81 }
82 
83 void
84 printlong(DISPLAY *dp)
85 {
86 	struct stat *sp;
87 	FTSENT *p;
88 	NAMES *np;
89 	char buf[20];
90 
91 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
92 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
93 
94 	for (p = dp->list; p; p = p->fts_link) {
95 		if (IS_NOPRINT(p))
96 			continue;
97 		sp = p->fts_statp;
98 		if (f_inode)
99 			(void)printf("%*u ", dp->s_inode, sp->st_ino);
100 		if (f_size)
101 			(void)printf("%*qd ",
102 			    dp->s_block, howmany(sp->st_blocks, blocksize));
103 		(void)strmode(sp->st_mode, buf);
104 		np = p->fts_pointer;
105 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
106 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
107 		    np->group);
108 		if (f_flags)
109 			(void)printf("%-*s ", dp->s_flags, np->flags);
110 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
111 			(void)printf("%3d, %3d ",
112 			    major(sp->st_rdev), minor(sp->st_rdev));
113 		else if (dp->bcfile)
114 			(void)printf("%*s%*qd ",
115 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
116 		else
117 			(void)printf("%*qd ", dp->s_size, sp->st_size);
118 		if (f_accesstime)
119 			printtime(sp->st_atime);
120 		else if (f_statustime)
121 			printtime(sp->st_ctime);
122 		else
123 			printtime(sp->st_mtime);
124 		(void)putname(p->fts_name);
125 		if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
126 			(void)printtype(sp->st_mode);
127 		if (S_ISLNK(sp->st_mode))
128 			printlink(p);
129 		(void)putchar('\n');
130 	}
131 }
132 
133 static int
134 compute_columns(DISPLAY *dp, int *pnum)
135 {
136 	int colwidth;
137 	extern int termwidth;
138 	int mywidth;
139 
140 	colwidth = dp->maxlen;
141 	if (f_inode)
142 		colwidth += dp->s_inode + 1;
143 	if (f_size)
144 		colwidth += dp->s_block + 1;
145 	if (f_type || f_typedir)
146 		colwidth += 1;
147 
148 	colwidth += 1;
149 	mywidth = termwidth + 1;	/* no extra space for last column */
150 
151 	if (mywidth < 2 * colwidth) {
152 		printscol(dp);
153 		return (0);
154 	}
155 
156 	*pnum = mywidth / colwidth;
157 	return (mywidth / *pnum);		/* spread out if possible */
158 }
159 
160 void
161 printcol(DISPLAY *dp)
162 {
163 	static FTSENT **array;
164 	static int lastentries = -1;
165 	FTSENT *p;
166 	int base, chcnt, col, colwidth, num;
167 	int numcols, numrows, row;
168 
169 	if ( (colwidth = compute_columns(dp, &numcols)) == 0)
170 		return;
171 	/*
172 	 * Have to do random access in the linked list -- build a table
173 	 * of pointers.
174 	 */
175 	if (dp->entries > lastentries) {
176 		FTSENT **a;
177 
178 		if ((a =
179 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
180 			warn(NULL);
181 			printscol(dp);
182 			return;
183 		}
184 		lastentries = dp->entries;
185 		array = a;
186 	}
187 	for (p = dp->list, num = 0; p; p = p->fts_link)
188 		if (p->fts_number != NO_PRINT)
189 			array[num++] = p;
190 
191 	numrows = num / numcols;
192 	if (num % numcols)
193 		++numrows;
194 
195 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
196 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
197 	for (row = 0; row < numrows; ++row) {
198 		for (base = row, col = 0;;) {
199 			chcnt = printaname(array[base], dp->s_inode, dp->s_block);
200 			if ((base += numrows) >= num)
201 				break;
202 			if (++col == numcols)
203 				break;
204 			while (chcnt++ < colwidth)
205 				putchar(' ');
206 		}
207 		(void)putchar('\n');
208 	}
209 }
210 
211 /*
212  * print [inode] [size] name
213  * return # of characters printed, no trailing characters.
214  */
215 static int
216 printaname(FTSENT *p, u_long inodefield, u_long sizefield)
217 {
218 	struct stat *sp;
219 	int chcnt;
220 
221 	sp = p->fts_statp;
222 	chcnt = 0;
223 	if (f_inode)
224 		chcnt += printf("%*u ", (int)inodefield, sp->st_ino);
225 	if (f_size)
226 		chcnt += printf("%*qd ",
227 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
228 	chcnt += putname(p->fts_name);
229 	if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
230 		chcnt += printtype(sp->st_mode);
231 	return (chcnt);
232 }
233 
234 static void
235 printtime(time_t ftime)
236 {
237 	int i;
238 	char *longstring;
239 
240 	longstring = ctime(&ftime);
241 	for (i = 4; i < 11; ++i)
242 		(void)putchar(longstring[i]);
243 
244 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
245 	if (f_sectime)
246 		for (i = 11; i < 24; i++)
247 			(void)putchar(longstring[i]);
248 	else if (ftime + SIXMONTHS > time(NULL))
249 		for (i = 11; i < 16; ++i)
250 			(void)putchar(longstring[i]);
251 	else {
252 		(void)putchar(' ');
253 		for (i = 20; i < 24; ++i)
254 			(void)putchar(longstring[i]);
255 	}
256 	(void)putchar(' ');
257 }
258 
259 void
260 printacol(DISPLAY *dp)
261 {
262 	FTSENT *p;
263 	int chcnt, col, colwidth;
264 	int numcols;
265 
266 	if ( (colwidth = compute_columns(dp, &numcols)) == 0)
267 		return;
268 
269 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
270 		(void)printf("total %llu\n",
271 		    (long long)(howmany(dp->btotal, blocksize)));
272 	col = 0;
273 	for (p = dp->list; p; p = p->fts_link) {
274 		if (IS_NOPRINT(p))
275 			continue;
276 		if (col >= numcols) {
277 			col = 0;
278 			(void)putchar('\n');
279 		}
280 		chcnt = printaname(p, dp->s_inode, dp->s_block);
281 		col++;
282 		if (col < numcols)
283 			while (chcnt++ < colwidth)
284 				(void)putchar(' ');
285 	}
286 	(void)putchar('\n');
287 }
288 
289 void
290 printstream(DISPLAY *dp)
291 {
292 	extern int termwidth;
293 	FTSENT *p;
294 	int col;
295 	int extwidth;
296 
297 	extwidth = 0;
298 	if (f_inode)
299 		extwidth += dp->s_inode + 1;
300 	if (f_size)
301 		extwidth += dp->s_block + 1;
302 	if (f_type)
303 		extwidth += 1;
304 
305 	for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
306 		if (IS_NOPRINT(p))
307 			continue;
308 		if (col > 0) {
309 			(void)putchar(','), col++;
310 			if (col + 1 + extwidth + p->fts_namelen >= termwidth)
311 				(void)putchar('\n'), col = 0;
312 			else
313 				(void)putchar(' '), col++;
314 		}
315 		col += printaname(p, dp->s_inode, dp->s_block);
316 	}
317 	(void)putchar('\n');
318 }
319 
320 static int
321 printtype(u_int mode)
322 {
323 	switch (mode & S_IFMT) {
324 	case S_IFDIR:
325 		(void)putchar('/');
326 		return (1);
327 	case S_IFIFO:
328 		(void)putchar('|');
329 		return (1);
330 	case S_IFLNK:
331 		(void)putchar('@');
332 		return (1);
333 	case S_IFSOCK:
334 		(void)putchar('=');
335 		return (1);
336 	case S_IFWHT:
337 		(void)putchar('%');
338 		return (1);
339 	}
340 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
341 		(void)putchar('*');
342 		return (1);
343 	}
344 	return (0);
345 }
346 
347 static void
348 printlink(FTSENT *p)
349 {
350 	int lnklen;
351 	char name[MAXPATHLEN], path[MAXPATHLEN];
352 
353 	if (p->fts_level == FTS_ROOTLEVEL)
354 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
355 	else
356 		(void)snprintf(name, sizeof(name),
357 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
358 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
359 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
360 		return;
361 	}
362 	path[lnklen] = '\0';
363 	(void)printf(" -> ");
364 	(void)putname(path);
365 }
366