print.c revision 1.47 1 /* $NetBSD: print.c,v 1.47 2010/04/02 15:09:12 christos 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
39 #else
40 __RCSID("$NetBSD: print.c,v 1.47 2010/04/02 15:09:12 christos Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/stat.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <fts.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <tzfile.h>
57 #include <unistd.h>
58 #include <util.h>
59
60 #include "ls.h"
61 #include "extern.h"
62
63 extern int termwidth;
64
65 static int printaname(FTSENT *, int, int);
66 static void printlink(FTSENT *);
67 static void printtime(time_t);
68 static void printtotal(DISPLAY *dp);
69 static int printtype(u_int);
70
71 static time_t now;
72
73 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
74
75 void
76 printscol(DISPLAY *dp)
77 {
78 FTSENT *p;
79
80 for (p = dp->list; p; p = p->fts_link) {
81 if (IS_NOPRINT(p))
82 continue;
83 (void)printaname(p, dp->s_inode, dp->s_block);
84 (void)putchar('\n');
85 }
86 }
87
88 void
89 printlong(DISPLAY *dp)
90 {
91 struct stat *sp;
92 FTSENT *p;
93 NAMES *np;
94 char buf[20], szbuf[5];
95
96 now = time(NULL);
97
98 printtotal(dp); /* "total: %u\n" */
99
100 for (p = dp->list; p; p = p->fts_link) {
101 if (IS_NOPRINT(p))
102 continue;
103 sp = p->fts_statp;
104 if (f_inode)
105 (void)printf("%*lu ", dp->s_inode,
106 (unsigned long)sp->st_ino);
107 if (f_size) {
108 if (f_humanize) {
109 if ((humanize_number(szbuf, sizeof(szbuf),
110 sp->st_blocks * S_BLKSIZE,
111 "", HN_AUTOSCALE,
112 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
113 err(1, "humanize_number");
114 (void)printf("%*s ", dp->s_block, szbuf);
115 } else {
116 (void)printf("%*llu ", dp->s_block,
117 (long long)howmany(sp->st_blocks,
118 blocksize));
119 }
120 }
121 (void)strmode(sp->st_mode, buf);
122 np = p->fts_pointer;
123 (void)printf("%s %*lu ", buf, dp->s_nlink,
124 (unsigned long)sp->st_nlink);
125 if (!f_grouponly)
126 (void)printf("%-*s ", dp->s_user, np->user);
127 (void)printf("%-*s ", dp->s_group, np->group);
128 if (f_flags)
129 (void)printf("%-*s ", dp->s_flags, np->flags);
130 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
131 (void)printf("%*lld, %*lld ",
132 dp->s_major, (long long)major(sp->st_rdev),
133 dp->s_minor, (long long)minor(sp->st_rdev));
134 else
135 if (f_humanize) {
136 if ((humanize_number(szbuf, sizeof(szbuf),
137 sp->st_size, "", HN_AUTOSCALE,
138 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
139 err(1, "humanize_number");
140 (void)printf("%*s ", dp->s_size, szbuf);
141 } else {
142 (void)printf("%*llu ", dp->s_size,
143 (long long)sp->st_size);
144 }
145 if (f_accesstime)
146 printtime(sp->st_atime);
147 else if (f_statustime)
148 printtime(sp->st_ctime);
149 else
150 printtime(sp->st_mtime);
151 if (f_octal || f_octal_escape)
152 (void)safe_print(p->fts_name);
153 else if (f_nonprint)
154 (void)printescaped(p->fts_name);
155 else
156 (void)printf("%s", p->fts_name);
157
158 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
159 (void)printtype(sp->st_mode);
160 if (S_ISLNK(sp->st_mode))
161 printlink(p);
162 (void)putchar('\n');
163 }
164 }
165
166 void
167 printcol(DISPLAY *dp)
168 {
169 static FTSENT **array;
170 static int lastentries = -1;
171 FTSENT *p;
172 int base, chcnt, col, colwidth, num;
173 int numcols, numrows, row;
174
175 colwidth = dp->maxlen;
176 if (f_inode)
177 colwidth += dp->s_inode + 1;
178 if (f_size) {
179 if (f_humanize)
180 colwidth += dp->s_size + 1;
181 else
182 colwidth += dp->s_block + 1;
183 }
184 if (f_type || f_typedir)
185 colwidth += 1;
186
187 colwidth += 1;
188
189 if (termwidth < 2 * colwidth) {
190 printscol(dp);
191 return;
192 }
193
194 /*
195 * Have to do random access in the linked list -- build a table
196 * of pointers.
197 */
198 if (dp->entries > lastentries) {
199 lastentries = dp->entries;
200 if ((array =
201 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
202 warn(NULL);
203 printscol(dp);
204 }
205 }
206 for (p = dp->list, num = 0; p; p = p->fts_link)
207 if (p->fts_number != NO_PRINT)
208 array[num++] = p;
209
210 numcols = termwidth / colwidth;
211 colwidth = termwidth / numcols; /* spread out if possible */
212 numrows = num / numcols;
213 if (num % numcols)
214 ++numrows;
215
216 printtotal(dp); /* "total: %u\n" */
217
218 for (row = 0; row < numrows; ++row) {
219 for (base = row, chcnt = col = 0; col < numcols; ++col) {
220 chcnt = printaname(array[base], dp->s_inode,
221 f_humanize ? dp->s_size : dp->s_block);
222 if ((base += numrows) >= num)
223 break;
224 while (chcnt++ < colwidth)
225 (void)putchar(' ');
226 }
227 (void)putchar('\n');
228 }
229 }
230
231 void
232 printacol(DISPLAY *dp)
233 {
234 FTSENT *p;
235 int chcnt, col, colwidth;
236 int numcols;
237
238 colwidth = dp->maxlen;
239 if (f_inode)
240 colwidth += dp->s_inode + 1;
241 if (f_size) {
242 if (f_humanize)
243 colwidth += dp->s_size + 1;
244 else
245 colwidth += dp->s_block + 1;
246 }
247 if (f_type || f_typedir)
248 colwidth += 1;
249
250 colwidth += 1;
251
252 if (termwidth < 2 * colwidth) {
253 printscol(dp);
254 return;
255 }
256
257 numcols = termwidth / colwidth;
258 colwidth = termwidth / numcols; /* spread out if possible */
259
260 printtotal(dp); /* "total: %u\n" */
261
262 chcnt = col = 0;
263 for (p = dp->list; p; p = p->fts_link) {
264 if (IS_NOPRINT(p))
265 continue;
266 if (col >= numcols) {
267 chcnt = col = 0;
268 (void)putchar('\n');
269 }
270 chcnt = printaname(p, dp->s_inode,
271 f_humanize ? dp->s_size : dp->s_block);
272 while (chcnt++ < colwidth)
273 (void)putchar(' ');
274 col++;
275 }
276 (void)putchar('\n');
277 }
278
279 void
280 printstream(DISPLAY *dp)
281 {
282 FTSENT *p;
283 int col;
284 int extwidth;
285
286 extwidth = 0;
287 if (f_inode)
288 extwidth += dp->s_inode + 1;
289 if (f_size) {
290 if (f_humanize)
291 extwidth += dp->s_size + 1;
292 else
293 extwidth += dp->s_block + 1;
294 }
295 if (f_type)
296 extwidth += 1;
297
298 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
299 if (IS_NOPRINT(p))
300 continue;
301 if (col > 0) {
302 (void)putchar(','), col++;
303 if (col + 1 + extwidth + (int)p->fts_namelen >= termwidth)
304 (void)putchar('\n'), col = 0;
305 else
306 (void)putchar(' '), col++;
307 }
308 col += printaname(p, dp->s_inode,
309 f_humanize ? dp->s_size : dp->s_block);
310 }
311 (void)putchar('\n');
312 }
313
314 /*
315 * print [inode] [size] name
316 * return # of characters printed, no trailing characters.
317 */
318 static int
319 printaname(FTSENT *p, int inodefield, int sizefield)
320 {
321 struct stat *sp;
322 int chcnt;
323 char szbuf[5];
324
325 sp = p->fts_statp;
326 chcnt = 0;
327 if (f_inode)
328 chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
329 if (f_size) {
330 if (f_humanize) {
331 if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
332 "", HN_AUTOSCALE,
333 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
334 err(1, "humanize_number");
335 chcnt += printf("%*s ", sizefield, szbuf);
336 } else {
337 chcnt += printf("%*llu ", sizefield,
338 (long long)howmany(sp->st_blocks, blocksize));
339 }
340 }
341 if (f_octal || f_octal_escape)
342 chcnt += safe_print(p->fts_name);
343 else if (f_nonprint)
344 chcnt += printescaped(p->fts_name);
345 else
346 chcnt += printf("%s", p->fts_name);
347 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
348 chcnt += printtype(sp->st_mode);
349 return (chcnt);
350 }
351
352 static void
353 printtime(time_t ftime)
354 {
355 int i;
356 const char *longstring;
357
358 if ((longstring = ctime(&ftime)) == NULL) {
359 /* 012345678901234567890123 */
360 longstring = "????????????????????????";
361 }
362 for (i = 4; i < 11; ++i)
363 (void)putchar(longstring[i]);
364
365 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
366 if (f_sectime)
367 for (i = 11; i < 24; i++)
368 (void)putchar(longstring[i]);
369 else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
370 for (i = 11; i < 16; ++i)
371 (void)putchar(longstring[i]);
372 else {
373 (void)putchar(' ');
374 for (i = 20; i < 24; ++i)
375 (void)putchar(longstring[i]);
376 }
377 (void)putchar(' ');
378 }
379
380 /*
381 * Display total used disk space in the form "total: %u\n".
382 * Note: POSIX (IEEE Std 1003.1-2001) says this should be always in 512 blocks,
383 * but we humanise it with -h and use 1024 with -k.
384 */
385 static void
386 printtotal(DISPLAY *dp)
387 {
388 char szbuf[5];
389
390 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
391 if (f_humanize) {
392 if ((humanize_number(szbuf, sizeof(szbuf), (int64_t)dp->stotal,
393 "", HN_AUTOSCALE,
394 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
395 err(1, "humanize_number");
396 (void)printf("total %s\n", szbuf);
397 } else {
398 (void)printf("total %llu\n",
399 (long long)(howmany(dp->btotal, blocksize)));
400 }
401 }
402 }
403
404 static int
405 printtype(u_int mode)
406 {
407 switch (mode & S_IFMT) {
408 case S_IFDIR:
409 (void)putchar('/');
410 return (1);
411 case S_IFIFO:
412 (void)putchar('|');
413 return (1);
414 case S_IFLNK:
415 (void)putchar('@');
416 return (1);
417 case S_IFSOCK:
418 (void)putchar('=');
419 return (1);
420 case S_IFWHT:
421 (void)putchar('%');
422 return (1);
423 }
424 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
425 (void)putchar('*');
426 return (1);
427 }
428 return (0);
429 }
430
431 static void
432 printlink(FTSENT *p)
433 {
434 int lnklen;
435 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
436
437 if (p->fts_level == FTS_ROOTLEVEL)
438 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
439 else
440 (void)snprintf(name, sizeof(name),
441 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
442 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
443 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
444 return;
445 }
446 path[lnklen] = '\0';
447 (void)printf(" -> ");
448 if (f_octal || f_octal_escape)
449 (void)safe_print(path);
450 else if (f_nonprint)
451 (void)printescaped(path);
452 else
453 (void)printf("%s", path);
454 }
455