xref: /freebsd/bin/ls/print.c (revision d46c1a60d3a40f4cfa23c901a682b9629f2a2003)
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  *	from: @(#)print.c	8.4 (Berkeley) 4/17/94";
37  */
38 
39 #ifndef lint
40 static const char rcsid[] =
41 	"$Id: print.c,v 1.12 1997/02/22 14:04:01 peter Exp $";
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 <unistd.h>
57 
58 #include "ls.h"
59 #include "extern.h"
60 
61 static int	printaname __P((FTSENT *, u_long, u_long));
62 static void	printlink __P((FTSENT *));
63 static void	printtime __P((time_t));
64 static int	printtype __P((u_int));
65 
66 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
67 
68 void
69 printscol(dp)
70 	DISPLAY *dp;
71 {
72 	FTSENT *p;
73 
74 	for (p = dp->list; p; p = p->fts_link) {
75 		if (IS_NOPRINT(p))
76 			continue;
77 		(void)printaname(p, dp->s_inode, dp->s_block);
78 		(void)putchar('\n');
79 	}
80 }
81 
82 void
83 printlong(dp)
84 	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("%*lu ", dp->s_inode, (u_long)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 			if (minor(sp->st_rdev) > 255)
112 				(void)printf("%3d, 0x%08x ",
113 				    major(sp->st_rdev), minor(sp->st_rdev));
114 			else
115 				(void)printf("%3d, %3d ",
116 				    major(sp->st_rdev), minor(sp->st_rdev));
117 		else if (dp->bcfile)
118 			(void)printf("%*s%*qd ",
119 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
120 		else
121 			(void)printf("%*qd ", dp->s_size, sp->st_size);
122 		if (f_accesstime)
123 			printtime(sp->st_atime);
124 		else if (f_statustime)
125 			printtime(sp->st_ctime);
126 		else
127 			printtime(sp->st_mtime);
128 		(void)printf("%s", p->fts_name);
129 		if (f_type)
130 			(void)printtype(sp->st_mode);
131 		if (S_ISLNK(sp->st_mode))
132 			printlink(p);
133 		(void)putchar('\n');
134 	}
135 }
136 
137 #define	TAB	8
138 
139 void
140 printcol(dp)
141 	DISPLAY *dp;
142 {
143 	extern int termwidth;
144 	static FTSENT **array;
145 	static int lastentries = -1;
146 	FTSENT *p;
147 	int base, chcnt, cnt, col, colwidth, num;
148 	int endcol, numcols, numrows, row;
149 
150 	/*
151 	 * Have to do random access in the linked list -- build a table
152 	 * of pointers.
153 	 */
154 	if (dp->entries > lastentries) {
155 		lastentries = dp->entries;
156 		if ((array =
157 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
158 			warn(NULL);
159 			printscol(dp);
160 		}
161 	}
162 	for (p = dp->list, num = 0; p; p = p->fts_link)
163 		if (p->fts_number != NO_PRINT)
164 			array[num++] = p;
165 
166 	colwidth = dp->maxlen;
167 	if (f_inode)
168 		colwidth += dp->s_inode + 1;
169 	if (f_size)
170 		colwidth += dp->s_block + 1;
171 	if (f_type)
172 		colwidth += 1;
173 
174 	colwidth = (colwidth + TAB) & ~(TAB - 1);
175 	if (termwidth < 2 * colwidth) {
176 		printscol(dp);
177 		return;
178 	}
179 
180 	numcols = termwidth / colwidth;
181 	numrows = num / numcols;
182 	if (num % numcols)
183 		++numrows;
184 
185 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
186 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
187 	for (row = 0; row < numrows; ++row) {
188 		endcol = colwidth;
189 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
190 			chcnt += printaname(array[base], dp->s_inode,
191 			    dp->s_block);
192 			if ((base += numrows) >= num)
193 				break;
194 			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol){
195 				(void)putchar('\t');
196 				chcnt = cnt;
197 			}
198 			endcol += colwidth;
199 		}
200 		(void)putchar('\n');
201 	}
202 }
203 
204 /*
205  * print [inode] [size] name
206  * return # of characters printed, no trailing characters.
207  */
208 static int
209 printaname(p, inodefield, sizefield)
210 	FTSENT *p;
211 	u_long sizefield, inodefield;
212 {
213 	struct stat *sp;
214 	int chcnt;
215 
216 	sp = p->fts_statp;
217 	chcnt = 0;
218 	if (f_inode)
219 		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
220 	if (f_size)
221 		chcnt += printf("%*qd ",
222 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
223 	chcnt += printf("%s", p->fts_name);
224 	if (f_type)
225 		chcnt += printtype(sp->st_mode);
226 	return (chcnt);
227 }
228 
229 static void
230 printtime(ftime)
231 	time_t ftime;
232 {
233 	int i;
234 	char longstring[80];
235 	static time_t now;
236 
237 	if (now == 0)
238 		now = time(NULL);
239 
240 	strftime(longstring, sizeof(longstring), "%c", localtime(&ftime));
241 	for (i = 4; i < 11; ++i)
242 		(void)putchar(longstring[i]);
243 
244 #define	SIXMONTHS	((365 / 2) * 86400)
245 	if (f_sectime)
246 		for (i = 11; i < 24; i++)
247 			(void)putchar(longstring[i]);
248 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
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 static int
260 printtype(mode)
261 	u_int mode;
262 {
263 	switch (mode & S_IFMT) {
264 	case S_IFDIR:
265 		(void)putchar('/');
266 		return (1);
267 	case S_IFIFO:
268 		(void)putchar('|');
269 		return (1);
270 	case S_IFLNK:
271 		(void)putchar('@');
272 		return (1);
273 	case S_IFSOCK:
274 		(void)putchar('=');
275 		return (1);
276 	case S_IFWHT:
277 		(void)putchar('%');
278 		return (1);
279 	}
280 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
281 		(void)putchar('*');
282 		return (1);
283 	}
284 	return (0);
285 }
286 
287 static void
288 printlink(p)
289 	FTSENT *p;
290 {
291 	int lnklen;
292 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
293 
294 	if (p->fts_level == FTS_ROOTLEVEL)
295 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
296 	else
297 		(void)snprintf(name, sizeof(name),
298 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
299 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
300 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
301 		return;
302 	}
303 	path[lnklen] = '\0';
304 	(void)printf(" -> %s", path);
305 }
306