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