xref: /original-bsd/bin/ls/print.c (revision 17b51d1b)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Michael Fischbein.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)print.c	5.37 (Berkeley) 07/20/92";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <fts.h>
18 #include <time.h>
19 #include <errno.h>
20 #include <grp.h>
21 #include <pwd.h>
22 #include <utmp.h>
23 #include <unistd.h>
24 #include <tzfile.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include "ls.h"
29 #include "extern.h"
30 
31 static int	printaname __P((FTSENT *, u_long, u_long));
32 static void	printlink __P((FTSENT *));
33 static void	printtime __P((time_t));
34 static int	printtype __P((u_int));
35 
36 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
37 
38 void
39 printscol(dp)
40 	DISPLAY *dp;
41 {
42 	register FTSENT *p;
43 
44 	for (p = dp->list; p; p = p->fts_link) {
45 		if (IS_NOPRINT(p))
46 			continue;
47 		(void)printaname(p, dp->s_inode, dp->s_block);
48 		(void)putchar('\n');
49 	}
50 }
51 
52 void
53 printlong(dp)
54 	DISPLAY *dp;
55 {
56 	register FTSENT *p;
57 	register struct stat *sp;
58 	NAMES *np;
59 	char buf[20];
60 
61 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
62 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
63 
64 	for (p = dp->list; p; p = p->fts_link) {
65 		if (IS_NOPRINT(p))
66 			continue;
67 		sp = p->fts_statp;
68 		if (f_inode)
69 			(void)printf("%*lu ", dp->s_inode, sp->st_ino);
70 		if (f_size)
71 			(void)printf("%*qd ",
72 			    dp->s_block, howmany(sp->st_blocks, blocksize));
73 		(void)strmode(sp->st_mode, buf);
74 		np = p->fts_pointer;
75 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
76 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
77 		    np->group);
78 		if (f_flags)
79 			(void)printf("%-*s ", dp->s_flags, np->flags);
80 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
81 			(void)printf("%3d, %3d ",
82 			    major(sp->st_rdev), minor(sp->st_rdev));
83 		else if (dp->bcfile)
84 			(void)printf("%*s%*qd ",
85 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
86 		else
87 			(void)printf("%*qd ", dp->s_size, sp->st_size);
88 		if (f_accesstime)
89 			printtime(sp->st_atime);
90 		else if (f_statustime)
91 			printtime(sp->st_ctime);
92 		else
93 			printtime(sp->st_mtime);
94 		(void)printf("%s", p->fts_name);
95 		if (f_type)
96 			(void)printtype(sp->st_mode);
97 		if (S_ISLNK(sp->st_mode))
98 			printlink(p);
99 		(void)putchar('\n');
100 	}
101 }
102 
103 #define	TAB	8
104 
105 void
106 printcol(dp)
107 	DISPLAY *dp;
108 {
109 	extern int termwidth;
110 	static FTSENT **array;
111 	static int lastentries = -1;
112 	register FTSENT *p;
113 	register int base, chcnt, cnt, col, colwidth, num;
114 	int endcol, numcols, numrows, row;
115 
116 	/*
117 	 * Have to do random access in the linked list -- build a table
118 	 * of pointers.
119 	 */
120 	if (dp->entries > lastentries) {
121 		lastentries = dp->entries;
122 		if ((array =
123 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
124 			err(0, "%s", strerror(errno));
125 			printscol(dp);
126 		}
127 	}
128 	for (p = dp->list, num = 0; p; p = p->fts_link)
129 		if (p->fts_number != NO_PRINT)
130 			array[num++] = p;
131 
132 	colwidth = dp->maxlen;
133 	if (f_inode)
134 		colwidth += dp->s_inode + 1;
135 	if (f_size)
136 		colwidth += dp->s_block + 1;
137 	if (f_type)
138 		colwidth += 1;
139 
140 	colwidth = (colwidth + TAB) & ~(TAB - 1);
141 	if (termwidth < 2 * colwidth) {
142 		printscol(dp);
143 		return;
144 	}
145 
146 	numcols = termwidth / colwidth;
147 	numrows = num / numcols;
148 	if (num % numcols)
149 		++numrows;
150 
151 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
152 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
153 	for (row = 0; row < numrows; ++row) {
154 		endcol = colwidth;
155 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
156 			chcnt += printaname(array[base], dp->s_inode,
157 			    dp->s_block);
158 			if ((base += numrows) >= num)
159 				break;
160 			while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
161 				(void)putchar('\t');
162 				chcnt = cnt;
163 			}
164 			endcol += colwidth;
165 		}
166 		(void)putchar('\n');
167 	}
168 }
169 
170 /*
171  * print [inode] [size] name
172  * return # of characters printed, no trailing characters.
173  */
174 static int
175 printaname(p, inodefield, sizefield)
176 	register FTSENT *p;
177 	u_long sizefield, inodefield;
178 {
179 	struct stat *sp;
180 	int chcnt;
181 
182 	sp = p->fts_statp;
183 	chcnt = 0;
184 	if (f_inode)
185 		chcnt += printf("%*lu ", inodefield, sp->st_ino);
186 	if (f_size)
187 		chcnt += printf("%*qd ",
188 		    sizefield, howmany(sp->st_blocks, blocksize));
189 	chcnt += printf("%s", p->fts_name);
190 	if (f_type)
191 		chcnt += printtype(sp->st_mode);
192 	return (chcnt);
193 }
194 
195 static void
196 printtime(ftime)
197 	time_t ftime;
198 {
199 	int i;
200 	char *longstring;
201 
202 	longstring = ctime(&ftime);
203 	for (i = 4; i < 11; ++i)
204 		(void)putchar(longstring[i]);
205 
206 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
207 	if (f_sectime)
208 		for (i = 11; i < 24; i++)
209 			(void)putchar(longstring[i]);
210 	else if (ftime + SIXMONTHS > time(NULL))
211 		for (i = 11; i < 16; ++i)
212 			(void)putchar(longstring[i]);
213 	else {
214 		(void)putchar(' ');
215 		for (i = 20; i < 24; ++i)
216 			(void)putchar(longstring[i]);
217 	}
218 	(void)putchar(' ');
219 }
220 
221 static int
222 printtype(mode)
223 	u_int mode;
224 {
225 	switch(mode & S_IFMT) {
226 	case S_IFDIR:
227 		(void)putchar('/');
228 		return (1);
229 	case S_IFLNK:
230 		(void)putchar('@');
231 		return (1);
232 	case S_IFSOCK:
233 		(void)putchar('=');
234 		return (1);
235 	}
236 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
237 		(void)putchar('*');
238 		return (1);
239 	}
240 	return (0);
241 }
242 
243 static void
244 printlink(p)
245 	FTSENT *p;
246 {
247 	int lnklen;
248 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
249 
250 
251 	if (p->fts_level == FTS_ROOTLEVEL)
252 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
253 	else
254 		(void)snprintf(name, sizeof(name),
255 		    "%s/%s", p->fts_path, p->fts_name);
256 	if ((lnklen = readlink(name, path, sizeof(name) - 1)) == -1) {
257 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
258 		return;
259 	}
260 	path[lnklen] = '\0';
261 	(void)printf(" -> %s", path);
262 }
263