xref: /freebsd/bin/ls/print.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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 	"$Id: print.c,v 1.16 1998/04/24 07:49:50 des Exp $";
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 <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 
60 #include "ls.h"
61 #include "extern.h"
62 
63 static int	printaname __P((FTSENT *, u_long, u_long));
64 static void	printlink __P((FTSENT *));
65 static void	printtime __P((time_t));
66 static int	printtype __P((u_int));
67 
68 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
69 
70 void
71 printscol(dp)
72 	DISPLAY *dp;
73 {
74 	FTSENT *p;
75 
76 	for (p = dp->list; p; p = p->fts_link) {
77 		if (IS_NOPRINT(p))
78 			continue;
79 		(void)printaname(p, dp->s_inode, dp->s_block);
80 		(void)putchar('\n');
81 	}
82 }
83 
84 void
85 printlong(dp)
86 	DISPLAY *dp;
87 {
88 	struct stat *sp;
89 	FTSENT *p;
90 	NAMES *np;
91 	char buf[20];
92 
93 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
94 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
95 
96 	for (p = dp->list; p; p = p->fts_link) {
97 		if (IS_NOPRINT(p))
98 			continue;
99 		sp = p->fts_statp;
100 		if (f_inode)
101 			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
102 		if (f_size)
103 			(void)printf("%*qd ",
104 			    dp->s_block, howmany(sp->st_blocks, blocksize));
105 		(void)strmode(sp->st_mode, buf);
106 		np = p->fts_pointer;
107 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
108 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
109 		    np->group);
110 		if (f_flags)
111 			(void)printf("%-*s ", dp->s_flags, np->flags);
112 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
113 			if (minor(sp->st_rdev) > 255)
114 				(void)printf("%3d, 0x%08x ",
115 				    major(sp->st_rdev), minor(sp->st_rdev));
116 			else
117 				(void)printf("%3d, %3d ",
118 				    major(sp->st_rdev), minor(sp->st_rdev));
119 		else if (dp->bcfile)
120 			(void)printf("%*s%*qd ",
121 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
122 		else
123 			(void)printf("%*qd ", dp->s_size, sp->st_size);
124 		if (f_accesstime)
125 			printtime(sp->st_atime);
126 		else if (f_statustime)
127 			printtime(sp->st_ctime);
128 		else
129 			printtime(sp->st_mtime);
130 		if (f_octal || f_octal_escape) (void)prn_octal(p->fts_name);
131 		else (void)printf("%s", p->fts_name);
132 		if (f_type)
133 			(void)printtype(sp->st_mode);
134 		if (S_ISLNK(sp->st_mode))
135 			printlink(p);
136 		(void)putchar('\n');
137 	}
138 }
139 
140 void
141 printcol(dp)
142 	DISPLAY *dp;
143 {
144 	extern int termwidth;
145 	static FTSENT **array;
146 	static int lastentries = -1;
147 	FTSENT *p;
148 	int base, chcnt, cnt, col, colwidth, num;
149 	int endcol, numcols, numrows, row;
150 	int tabwidth;
151 
152 	if (f_notabs)
153 		tabwidth = 1;
154 	else
155 		tabwidth = 8;
156 
157 	/*
158 	 * Have to do random access in the linked list -- build a table
159 	 * of pointers.
160 	 */
161 	if (dp->entries > lastentries) {
162 		lastentries = dp->entries;
163 		if ((array =
164 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
165 			warn(NULL);
166 			printscol(dp);
167 		}
168 	}
169 	for (p = dp->list, num = 0; p; p = p->fts_link)
170 		if (p->fts_number != NO_PRINT)
171 			array[num++] = p;
172 
173 	colwidth = dp->maxlen;
174 	if (f_inode)
175 		colwidth += dp->s_inode + 1;
176 	if (f_size)
177 		colwidth += dp->s_block + 1;
178 	if (f_type)
179 		colwidth += 1;
180 
181 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
182 	if (termwidth < 2 * colwidth) {
183 		printscol(dp);
184 		return;
185 	}
186 
187 	numcols = termwidth / colwidth;
188 	numrows = num / numcols;
189 	if (num % numcols)
190 		++numrows;
191 
192 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
193 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
194 	for (row = 0; row < numrows; ++row) {
195 		endcol = colwidth;
196 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
197 			chcnt += printaname(array[base], dp->s_inode,
198 			    dp->s_block);
199 			if ((base += numrows) >= num)
200 				break;
201 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
202 			    <= endcol){
203 				(void)putchar(f_notabs ? ' ' : '\t');
204 				chcnt = cnt;
205 			}
206 			endcol += colwidth;
207 		}
208 		(void)putchar('\n');
209 	}
210 }
211 
212 /*
213  * print [inode] [size] name
214  * return # of characters printed, no trailing characters.
215  */
216 static int
217 printaname(p, inodefield, sizefield)
218 	FTSENT *p;
219 	u_long sizefield, inodefield;
220 {
221 	struct stat *sp;
222 	int chcnt;
223 
224 	sp = p->fts_statp;
225 	chcnt = 0;
226 	if (f_inode)
227 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
228 	if (f_size)
229 		chcnt += printf("%*qd ",
230 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
231 	chcnt += (f_octal || f_octal_escape) ? prn_octal(p->fts_name)
232 	                                     : printf("%s", p->fts_name);
233 	if (f_type)
234 		chcnt += printtype(sp->st_mode);
235 	return (chcnt);
236 }
237 
238 static void
239 printtime(ftime)
240 	time_t ftime;
241 {
242 	int i;
243 	char longstring[80];
244 	static time_t now;
245 
246 	if (now == 0)
247 		now = time(NULL);
248 
249 	strftime(longstring, sizeof(longstring), "%c", localtime(&ftime));
250 	for (i = 4; i < 11; ++i)
251 		(void)putchar(longstring[i]);
252 
253 #define	SIXMONTHS	((365 / 2) * 86400)
254 	if (f_sectime)
255 		for (i = 11; i < 24; i++)
256 			(void)putchar(longstring[i]);
257 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
258 		for (i = 11; i < 16; ++i)
259 			(void)putchar(longstring[i]);
260 	else {
261 		(void)putchar(' ');
262 		for (i = 20; i < 24; ++i)
263 			(void)putchar(longstring[i]);
264 	}
265 	(void)putchar(' ');
266 }
267 
268 static int
269 printtype(mode)
270 	u_int mode;
271 {
272 	switch (mode & S_IFMT) {
273 	case S_IFDIR:
274 		(void)putchar('/');
275 		return (1);
276 	case S_IFIFO:
277 		(void)putchar('|');
278 		return (1);
279 	case S_IFLNK:
280 		(void)putchar('@');
281 		return (1);
282 	case S_IFSOCK:
283 		(void)putchar('=');
284 		return (1);
285 	case S_IFWHT:
286 		(void)putchar('%');
287 		return (1);
288 	}
289 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
290 		(void)putchar('*');
291 		return (1);
292 	}
293 	return (0);
294 }
295 
296 static void
297 printlink(p)
298 	FTSENT *p;
299 {
300 	int lnklen;
301 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
302 
303 	if (p->fts_level == FTS_ROOTLEVEL)
304 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
305 	else
306 		(void)snprintf(name, sizeof(name),
307 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
308 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
309 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
310 		return;
311 	}
312 	path[lnklen] = '\0';
313 	if (f_octal || f_octal_escape) {
314 	        (void)printf(" -> ");
315 	        (void)prn_octal(path);
316 	}
317 	else (void)printf(" -> %s", path);
318 }
319