xref: /original-bsd/bin/ls/print.c (revision a5a45b47)
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.33 (Berkeley) 03/13/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("%*ld ",
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
84 			(void)printf("%*ld ", dp->s_size, sp->st_size);
85 		if (f_accesstime)
86 			printtime(sp->st_atime);
87 		else if (f_statustime)
88 			printtime(sp->st_ctime);
89 		else
90 			printtime(sp->st_mtime);
91 		(void)printf("%s", p->fts_name);
92 		if (f_type)
93 			(void)printtype(sp->st_mode);
94 		if (S_ISLNK(sp->st_mode))
95 			printlink(p);
96 		(void)putchar('\n');
97 	}
98 }
99 
100 #define	TAB	8
101 
102 void
103 printcol(dp)
104 	DISPLAY *dp;
105 {
106 	extern int termwidth;
107 	static FTSENT **array;
108 	static int lastentries = -1;
109 	register FTSENT *p;
110 	register int base, chcnt, cnt, col, colwidth, num;
111 	int endcol, numcols, numrows, row;
112 
113 	/*
114 	 * Have to do random access in the linked list -- build a table
115 	 * of pointers.
116 	 */
117 	if (dp->entries > lastentries) {
118 		lastentries = dp->entries;
119 		if ((array =
120 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
121 			err(0, "%s", strerror(errno));
122 			printscol(dp);
123 		}
124 	}
125 	for (p = dp->list, num = 0; p; p = p->fts_link)
126 		if (p->fts_number != NO_PRINT)
127 			array[num++] = p;
128 
129 	colwidth = dp->maxlen;
130 	if (f_inode)
131 		colwidth += dp->s_inode + 1;
132 	if (f_size)
133 		colwidth += dp->s_block + 1;
134 	if (f_type)
135 		colwidth += 1;
136 
137 	colwidth = (colwidth + TAB) & ~(TAB - 1);
138 	if (termwidth < 2 * colwidth) {
139 		printscol(dp);
140 		return;
141 	}
142 
143 	numcols = termwidth / colwidth;
144 	numrows = num / numcols;
145 	if (num % numcols)
146 		++numrows;
147 
148 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
149 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
150 	for (row = 0; row < numrows; ++row) {
151 		endcol = colwidth;
152 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
153 			chcnt += printaname(array[base], dp->s_inode,
154 			    dp->s_block);
155 			if ((base += numrows) >= num)
156 				break;
157 			while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
158 				(void)putchar('\t');
159 				chcnt = cnt;
160 			}
161 			endcol += colwidth;
162 		}
163 		(void)putchar('\n');
164 	}
165 }
166 
167 /*
168  * print [inode] [size] name
169  * return # of characters printed, no trailing characters.
170  */
171 static int
172 printaname(p, inodefield, sizefield)
173 	register FTSENT *p;
174 	u_long sizefield, inodefield;
175 {
176 	struct stat *sp;
177 	int chcnt;
178 
179 	sp = p->fts_statp;
180 	chcnt = 0;
181 	if (f_inode)
182 		chcnt += printf("%*lu ", inodefield, sp->st_ino);
183 	if (f_size)
184 		chcnt += printf("%*ld ",
185 		    sizefield, howmany(sp->st_blocks, blocksize));
186 	chcnt += printf("%s", p->fts_name);
187 	if (f_type)
188 		chcnt += printtype(sp->st_mode);
189 	return (chcnt);
190 }
191 
192 static void
193 printtime(ftime)
194 	time_t ftime;
195 {
196 	int i;
197 	char *longstring;
198 
199 	longstring = ctime(&ftime);
200 	for (i = 4; i < 11; ++i)
201 		(void)putchar(longstring[i]);
202 
203 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
204 	if (f_sectime)
205 		for (i = 11; i < 24; i++)
206 			(void)putchar(longstring[i]);
207 	else if (ftime + SIXMONTHS > time(NULL))
208 		for (i = 11; i < 16; ++i)
209 			(void)putchar(longstring[i]);
210 	else {
211 		(void)putchar(' ');
212 		for (i = 20; i < 24; ++i)
213 			(void)putchar(longstring[i]);
214 	}
215 	(void)putchar(' ');
216 }
217 
218 static int
219 printtype(mode)
220 	u_int mode;
221 {
222 	switch(mode & S_IFMT) {
223 	case S_IFDIR:
224 		(void)putchar('/');
225 		return (1);
226 	case S_IFLNK:
227 		(void)putchar('@');
228 		return (1);
229 	case S_IFSOCK:
230 		(void)putchar('=');
231 		return (1);
232 	}
233 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
234 		(void)putchar('*');
235 		return (1);
236 	}
237 	return (0);
238 }
239 
240 static void
241 printlink(p)
242 	FTSENT *p;
243 {
244 	int lnklen;
245 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
246 
247 	if (p->fts_level == FTS_ROOTLEVEL)
248 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
249 	else
250 		(void)snprintf(name, sizeof(name),
251 		    "%s/%s", p->fts_parent->fts_name, p->fts_name);
252 
253 	if ((lnklen = readlink(name, path, sizeof(name) - 1)) == -1) {
254 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
255 		return;
256 	}
257 	path[lnklen] = '\0';
258 	(void)printf(" -> %s", path);
259 }
260