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