print.c revision 1.17.2.1 1 /* $NetBSD: print.c,v 1.17.2.1 1998/01/29 11:26:19 mellon Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Michael Fischbein.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
43 #else
44 __RCSID("$NetBSD: print.c,v 1.17.2.1 1998/01/29 11:26:19 mellon Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/stat.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <fts.h>
54 #include <grp.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62 #include <utmp.h>
63
64 #include "ls.h"
65 #include "extern.h"
66
67 static int printaname __P((FTSENT *, u_long, u_long));
68 static void printlink __P((FTSENT *));
69 static void printtime __P((time_t));
70 static int printtype __P((u_int));
71
72 static time_t now;
73
74 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
75
76 void
77 printscol(dp)
78 DISPLAY *dp;
79 {
80 FTSENT *p;
81
82 for (p = dp->list; p; p = p->fts_link) {
83 if (IS_NOPRINT(p))
84 continue;
85 (void)printaname(p, dp->s_inode, dp->s_block);
86 (void)putchar('\n');
87 }
88 }
89
90 void
91 printlong(dp)
92 DISPLAY *dp;
93 {
94 struct stat *sp;
95 FTSENT *p;
96 NAMES *np;
97 char buf[20];
98
99 time(&now);
100
101 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
102 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
103
104 for (p = dp->list; p; p = p->fts_link) {
105 if (IS_NOPRINT(p))
106 continue;
107 sp = p->fts_statp;
108 if (f_inode)
109 (void)printf("%*u ", dp->s_inode, sp->st_ino);
110 if (f_size)
111 (void)printf("%*qd ",
112 dp->s_block,
113 (long long)howmany(sp->st_blocks, blocksize));
114 (void)strmode(sp->st_mode, buf);
115 np = p->fts_pointer;
116 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
117 sp->st_nlink, dp->s_user, np->user, dp->s_group,
118 np->group);
119 if (f_flags)
120 (void)printf("%-*s ", dp->s_flags, np->flags);
121 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
122 (void)printf("%3d, %3d ",
123 major(sp->st_rdev), minor(sp->st_rdev));
124 else if (dp->bcfile)
125 (void)printf("%*s%*qd ",
126 8 - dp->s_size, "", dp->s_size,
127 (long long)sp->st_size);
128 else
129 (void)printf("%*qd ", dp->s_size,
130 (long long)sp->st_size);
131 if (f_accesstime)
132 printtime(sp->st_atime);
133 else if (f_statustime)
134 printtime(sp->st_ctime);
135 else
136 printtime(sp->st_mtime);
137 (void)printf("%s", p->fts_name);
138 if (f_type)
139 (void)printtype(sp->st_mode);
140 if (S_ISLNK(sp->st_mode))
141 printlink(p);
142 (void)putchar('\n');
143 }
144 }
145
146 void
147 printcol(dp)
148 DISPLAY *dp;
149 {
150 extern int termwidth;
151 static FTSENT **array;
152 static int lastentries = -1;
153 FTSENT *p;
154 int base, chcnt, col, colwidth, num;
155 int numcols, numrows, row;
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("%s", "");
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 += 1;
182
183 if (termwidth < 2 * colwidth) {
184 printscol(dp);
185 return;
186 }
187
188 numcols = termwidth / colwidth;
189 colwidth = termwidth / numcols; /* spread out if possible */
190 numrows = num / numcols;
191 if (num % numcols)
192 ++numrows;
193
194 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
195 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
196 for (row = 0; row < numrows; ++row) {
197 for (base = row, chcnt = col = 0; col < numcols; ++col) {
198 chcnt = printaname(array[base], dp->s_inode, dp->s_block);
199 if ((base += numrows) >= num)
200 break;
201 while (chcnt++ < colwidth)
202 putchar(' ');
203 }
204 (void)putchar('\n');
205 }
206 }
207
208 /*
209 * print [inode] [size] name
210 * return # of characters printed, no trailing characters.
211 */
212 static int
213 printaname(p, inodefield, sizefield)
214 FTSENT *p;
215 u_long sizefield, inodefield;
216 {
217 struct stat *sp;
218 int chcnt;
219
220 sp = p->fts_statp;
221 chcnt = 0;
222 if (f_inode)
223 chcnt += printf("%*u ", (int)inodefield, sp->st_ino);
224 if (f_size)
225 chcnt += printf("%*qd ",
226 (int)sizefield,
227 (long long)howmany(sp->st_blocks, blocksize));
228 chcnt += printf("%s", p->fts_name);
229 if (f_type)
230 chcnt += printtype(sp->st_mode);
231 return (chcnt);
232 }
233
234 static void
235 printtime(ftime)
236 time_t ftime;
237 {
238 int i;
239 char *longstring;
240
241 longstring = ctime(&ftime);
242 for (i = 4; i < 11; ++i)
243 (void)putchar(longstring[i]);
244
245 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
246 if (f_sectime)
247 for (i = 11; i < 24; i++)
248 (void)putchar(longstring[i]);
249 else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
250 for (i = 11; i < 16; ++i)
251 (void)putchar(longstring[i]);
252 else {
253 (void)putchar(' ');
254 for (i = 20; i < 24; ++i)
255 (void)putchar(longstring[i]);
256 }
257 (void)putchar(' ');
258 }
259
260 static int
261 printtype(mode)
262 u_int mode;
263 {
264 switch (mode & S_IFMT) {
265 case S_IFDIR:
266 (void)putchar('/');
267 return (1);
268 case S_IFIFO:
269 (void)putchar('|');
270 return (1);
271 case S_IFLNK:
272 (void)putchar('@');
273 return (1);
274 case S_IFSOCK:
275 (void)putchar('=');
276 return (1);
277 case S_IFWHT:
278 (void)putchar('%');
279 return (1);
280 }
281 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
282 (void)putchar('*');
283 return (1);
284 }
285 return (0);
286 }
287
288 static void
289 printlink(p)
290 FTSENT *p;
291 {
292 int lnklen;
293 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
294
295 if (p->fts_level == FTS_ROOTLEVEL)
296 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
297 else
298 (void)snprintf(name, sizeof(name),
299 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
300 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
301 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
302 return;
303 }
304 path[lnklen] = '\0';
305 (void)printf(" -> %s", path);
306 }
307