print.c revision 1.34 1 /* $NetBSD: print.c,v 1.34 2003/05/07 13:00:24 grant 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.34 2003/05/07 13:00:24 grant 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
63 #include "ls.h"
64 #include "extern.h"
65
66 extern int termwidth;
67
68 static int printaname(FTSENT *, int, int);
69 static void printlink(FTSENT *);
70 static void printtime(time_t);
71 static int printtype(u_int);
72
73 static time_t now;
74
75 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
76
77 void
78 printscol(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(DISPLAY *dp)
92 {
93 struct stat *sp;
94 FTSENT *p;
95 NAMES *np;
96 char buf[20];
97
98 now = time(NULL);
99
100 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
101 (void)printf("total %llu\n",
102 (long long)(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("%*lu ", dp->s_inode,
110 (unsigned long)sp->st_ino);
111 if (f_size)
112 (void)printf("%*llu ", 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 %*lu ", buf, dp->s_nlink,
117 (unsigned long)sp->st_nlink);
118 if (!f_grouponly)
119 (void)printf("%-*s ", dp->s_user, np->user);
120 (void)printf("%-*s ", dp->s_group, np->group);
121 if (f_flags)
122 (void)printf("%-*s ", dp->s_flags, np->flags);
123 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
124 (void)printf("%*u, %*u ",
125 dp->s_major, major(sp->st_rdev), dp->s_minor,
126 minor(sp->st_rdev));
127 else
128 (void)printf("%*llu ", dp->s_size,
129 (long long)sp->st_size);
130 if (f_accesstime)
131 printtime(sp->st_atime);
132 else if (f_statustime)
133 printtime(sp->st_ctime);
134 else
135 printtime(sp->st_mtime);
136 if (f_nonprint)
137 (void)printescaped(p->fts_name);
138 else
139 (void)printf("%s", p->fts_name);
140
141 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
142 (void)printtype(sp->st_mode);
143 if (S_ISLNK(sp->st_mode))
144 printlink(p);
145 (void)putchar('\n');
146 }
147 }
148
149 void
150 printcol(DISPLAY *dp)
151 {
152 static FTSENT **array;
153 static int lastentries = -1;
154 FTSENT *p;
155 int base, chcnt, col, colwidth, num;
156 int numcols, numrows, row;
157
158 colwidth = dp->maxlen;
159 if (f_inode)
160 colwidth += dp->s_inode + 1;
161 if (f_size)
162 colwidth += dp->s_block + 1;
163 if (f_type || f_typedir)
164 colwidth += 1;
165
166 colwidth += 1;
167
168 if (termwidth < 2 * colwidth) {
169 printscol(dp);
170 return;
171 }
172
173 /*
174 * Have to do random access in the linked list -- build a table
175 * of pointers.
176 */
177 if (dp->entries > lastentries) {
178 lastentries = dp->entries;
179 if ((array =
180 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
181 warn(NULL);
182 printscol(dp);
183 }
184 }
185 for (p = dp->list, num = 0; p; p = p->fts_link)
186 if (p->fts_number != NO_PRINT)
187 array[num++] = p;
188
189 numcols = termwidth / colwidth;
190 colwidth = termwidth / numcols; /* spread out if possible */
191 numrows = num / numcols;
192 if (num % numcols)
193 ++numrows;
194
195 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
196 (void)printf("total %llu\n",
197 (long long)(howmany(dp->btotal, blocksize)));
198 for (row = 0; row < numrows; ++row) {
199 for (base = row, chcnt = col = 0; col < numcols; ++col) {
200 chcnt = printaname(array[base], dp->s_inode,
201 dp->s_block);
202 if ((base += numrows) >= num)
203 break;
204 while (chcnt++ < colwidth)
205 (void)putchar(' ');
206 }
207 (void)putchar('\n');
208 }
209 }
210
211 void
212 printacol(DISPLAY *dp)
213 {
214 FTSENT *p;
215 int chcnt, col, colwidth;
216 int numcols;
217
218 colwidth = dp->maxlen;
219 if (f_inode)
220 colwidth += dp->s_inode + 1;
221 if (f_size)
222 colwidth += dp->s_block + 1;
223 if (f_type || f_typedir)
224 colwidth += 1;
225
226 colwidth += 1;
227
228 if (termwidth < 2 * colwidth) {
229 printscol(dp);
230 return;
231 }
232
233 numcols = termwidth / colwidth;
234 colwidth = termwidth / numcols; /* spread out if possible */
235
236 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
237 (void)printf("total %llu\n",
238 (long long)(howmany(dp->btotal, blocksize)));
239 chcnt = col = 0;
240 for (p = dp->list; p; p = p->fts_link) {
241 if (IS_NOPRINT(p))
242 continue;
243 if (col >= numcols) {
244 chcnt = col = 0;
245 (void)putchar('\n');
246 }
247 chcnt = printaname(p, dp->s_inode, dp->s_block);
248 while (chcnt++ < colwidth)
249 (void)putchar(' ');
250 col++;
251 }
252 (void)putchar('\n');
253 }
254
255 void
256 printstream(DISPLAY *dp)
257 {
258 FTSENT *p;
259 int col;
260 int extwidth;
261
262 extwidth = 0;
263 if (f_inode)
264 extwidth += dp->s_inode + 1;
265 if (f_size)
266 extwidth += dp->s_block + 1;
267 if (f_type)
268 extwidth += 1;
269
270 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
271 if (IS_NOPRINT(p))
272 continue;
273 if (col > 0) {
274 (void)putchar(','), col++;
275 if (col + 1 + extwidth + p->fts_namelen >= termwidth)
276 (void)putchar('\n'), col = 0;
277 else
278 (void)putchar(' '), col++;
279 }
280 col += printaname(p, dp->s_inode, dp->s_block);
281 }
282 (void)putchar('\n');
283 }
284
285 /*
286 * print [inode] [size] name
287 * return # of characters printed, no trailing characters.
288 */
289 static int
290 printaname(FTSENT *p, int inodefield, int sizefield)
291 {
292 struct stat *sp;
293 int chcnt;
294
295 sp = p->fts_statp;
296 chcnt = 0;
297 if (f_inode)
298 chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
299 if (f_size)
300 chcnt += printf("%*llu ", sizefield,
301 (long long)howmany(sp->st_blocks, blocksize));
302 if (f_nonprint)
303 chcnt += printescaped(p->fts_name);
304 else
305 chcnt += printf("%s", p->fts_name);
306 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
307 chcnt += printtype(sp->st_mode);
308 return (chcnt);
309 }
310
311 static void
312 printtime(time_t ftime)
313 {
314 int i;
315 char *longstring;
316
317 longstring = ctime(&ftime);
318 for (i = 4; i < 11; ++i)
319 (void)putchar(longstring[i]);
320
321 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
322 if (f_sectime)
323 for (i = 11; i < 24; i++)
324 (void)putchar(longstring[i]);
325 else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
326 for (i = 11; i < 16; ++i)
327 (void)putchar(longstring[i]);
328 else {
329 (void)putchar(' ');
330 for (i = 20; i < 24; ++i)
331 (void)putchar(longstring[i]);
332 }
333 (void)putchar(' ');
334 }
335
336 static int
337 printtype(u_int mode)
338 {
339 switch (mode & S_IFMT) {
340 case S_IFDIR:
341 (void)putchar('/');
342 return (1);
343 case S_IFIFO:
344 (void)putchar('|');
345 return (1);
346 case S_IFLNK:
347 (void)putchar('@');
348 return (1);
349 case S_IFSOCK:
350 (void)putchar('=');
351 return (1);
352 case S_IFWHT:
353 (void)putchar('%');
354 return (1);
355 }
356 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
357 (void)putchar('*');
358 return (1);
359 }
360 return (0);
361 }
362
363 static void
364 printlink(FTSENT *p)
365 {
366 int lnklen;
367 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
368
369 if (p->fts_level == FTS_ROOTLEVEL)
370 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
371 else
372 (void)snprintf(name, sizeof(name),
373 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
374 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
375 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
376 return;
377 }
378 path[lnklen] = '\0';
379 (void)printf(" -> ");
380 if (f_nonprint)
381 printescaped(path);
382 else
383 (void)printf("%s", path);
384 }
385