print.c revision 1.49 1 /* $NetBSD: print.c,v 1.49 2011/03/15 03:52:38 erh 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.49 2011/03/15 03:52:38 erh 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 char commabuf[27]; /* 64 bits == 20 digits, +6 for commas, +1 for NUL */
96
97 now = time(NULL);
98
99 printtotal(dp); /* "total: %u\n" */
100
101 for (p = dp->list; p; p = p->fts_link) {
102 if (IS_NOPRINT(p))
103 continue;
104 sp = p->fts_statp;
105 if (f_inode)
106 (void)printf("%*lu ", dp->s_inode,
107 (unsigned long)sp->st_ino);
108 if (f_size) {
109 if (f_humanize) {
110 if ((humanize_number(szbuf, sizeof(szbuf),
111 sp->st_blocks * S_BLKSIZE,
112 "", HN_AUTOSCALE,
113 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
114 err(1, "humanize_number");
115 (void)printf("%*s ", dp->s_block, szbuf);
116 } else if (f_commas) {
117 if (commaize_number(commabuf, sizeof(commabuf),
118 (long long)howmany(sp->st_blocks,
119 blocksize)) == -1)
120 err(1, "commaize_number(blocks=%lld)",
121 (long long)howmany(sp->st_blocks, blocksize));
122 (void)printf("%*s ", dp->s_block, commabuf);
123 } else {
124 (void)printf("%*llu ", dp->s_block,
125 (long long)howmany(sp->st_blocks,
126 blocksize));
127 }
128 }
129 (void)strmode(sp->st_mode, buf);
130 np = p->fts_pointer;
131 (void)printf("%s %*lu ", buf, dp->s_nlink,
132 (unsigned long)sp->st_nlink);
133 if (!f_grouponly)
134 (void)printf("%-*s ", dp->s_user, np->user);
135 (void)printf("%-*s ", dp->s_group, np->group);
136 if (f_flags)
137 (void)printf("%-*s ", dp->s_flags, np->flags);
138 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
139 (void)printf("%*lld, %*lld ",
140 dp->s_major, (long long)major(sp->st_rdev),
141 dp->s_minor, (long long)minor(sp->st_rdev));
142 else
143 if (f_humanize) {
144 if ((humanize_number(szbuf, sizeof(szbuf),
145 sp->st_size, "", HN_AUTOSCALE,
146 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
147 err(1, "humanize_number");
148 (void)printf("%*s ", dp->s_size, szbuf);
149 } else if (f_commas) {
150 if (commaize_number(commabuf, sizeof(commabuf),
151 sp->st_size) == -1)
152 err(1, "commaize_number(size=%lld)", sp->st_size);
153 (void)printf("%*s ", dp->s_size, commabuf);
154 } else {
155 (void)printf("%*llu ", dp->s_size,
156 (long long)sp->st_size);
157 }
158 if (f_accesstime)
159 printtime(sp->st_atime);
160 else if (f_statustime)
161 printtime(sp->st_ctime);
162 else
163 printtime(sp->st_mtime);
164 if (f_octal || f_octal_escape)
165 (void)safe_print(p->fts_name);
166 else if (f_nonprint)
167 (void)printescaped(p->fts_name);
168 else
169 (void)printf("%s", p->fts_name);
170
171 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
172 (void)printtype(sp->st_mode);
173 if (S_ISLNK(sp->st_mode))
174 printlink(p);
175 (void)putchar('\n');
176 }
177 }
178
179 void
180 printcol(DISPLAY *dp)
181 {
182 static FTSENT **array;
183 static int lastentries = -1;
184 FTSENT *p;
185 int base, chcnt, col, colwidth, num;
186 int numcols, numrows, row;
187
188 colwidth = dp->maxlen;
189 if (f_inode)
190 colwidth += dp->s_inode + 1;
191 if (f_size) {
192 if (f_humanize)
193 colwidth += dp->s_size + 1;
194 else
195 colwidth += dp->s_block + 1;
196 }
197 if (f_type || f_typedir)
198 colwidth += 1;
199
200 colwidth += 1;
201
202 if (termwidth < 2 * colwidth) {
203 printscol(dp);
204 return;
205 }
206
207 /*
208 * Have to do random access in the linked list -- build a table
209 * of pointers.
210 */
211 if (dp->entries > lastentries) {
212 lastentries = dp->entries;
213 if ((array =
214 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
215 warn(NULL);
216 printscol(dp);
217 }
218 }
219 for (p = dp->list, num = 0; p; p = p->fts_link)
220 if (p->fts_number != NO_PRINT)
221 array[num++] = p;
222
223 numcols = termwidth / colwidth;
224 colwidth = termwidth / numcols; /* spread out if possible */
225 numrows = num / numcols;
226 if (num % numcols)
227 ++numrows;
228
229 printtotal(dp); /* "total: %u\n" */
230
231 for (row = 0; row < numrows; ++row) {
232 for (base = row, chcnt = col = 0; col < numcols; ++col) {
233 chcnt = printaname(array[base], dp->s_inode,
234 f_humanize ? dp->s_size : dp->s_block);
235 if ((base += numrows) >= num)
236 break;
237 while (chcnt++ < colwidth)
238 (void)putchar(' ');
239 }
240 (void)putchar('\n');
241 }
242 }
243
244 void
245 printacol(DISPLAY *dp)
246 {
247 FTSENT *p;
248 int chcnt, col, colwidth;
249 int numcols;
250
251 colwidth = dp->maxlen;
252 if (f_inode)
253 colwidth += dp->s_inode + 1;
254 if (f_size) {
255 if (f_humanize)
256 colwidth += dp->s_size + 1;
257 else
258 colwidth += dp->s_block + 1;
259 }
260 if (f_type || f_typedir)
261 colwidth += 1;
262
263 colwidth += 1;
264
265 if (termwidth < 2 * colwidth) {
266 printscol(dp);
267 return;
268 }
269
270 numcols = termwidth / colwidth;
271 colwidth = termwidth / numcols; /* spread out if possible */
272
273 printtotal(dp); /* "total: %u\n" */
274
275 chcnt = col = 0;
276 for (p = dp->list; p; p = p->fts_link) {
277 if (IS_NOPRINT(p))
278 continue;
279 if (col >= numcols) {
280 chcnt = col = 0;
281 (void)putchar('\n');
282 }
283 chcnt = printaname(p, dp->s_inode,
284 f_humanize ? dp->s_size : dp->s_block);
285 while (chcnt++ < colwidth)
286 (void)putchar(' ');
287 col++;
288 }
289 (void)putchar('\n');
290 }
291
292 void
293 printstream(DISPLAY *dp)
294 {
295 FTSENT *p;
296 int col;
297 int extwidth;
298
299 extwidth = 0;
300 if (f_inode)
301 extwidth += dp->s_inode + 1;
302 if (f_size) {
303 if (f_humanize)
304 extwidth += dp->s_size + 1;
305 else
306 extwidth += dp->s_block + 1;
307 }
308 if (f_type)
309 extwidth += 1;
310
311 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
312 if (IS_NOPRINT(p))
313 continue;
314 if (col > 0) {
315 (void)putchar(','), col++;
316 if (col + 1 + extwidth + (int)p->fts_namelen >= termwidth)
317 (void)putchar('\n'), col = 0;
318 else
319 (void)putchar(' '), col++;
320 }
321 col += printaname(p, dp->s_inode,
322 f_humanize ? dp->s_size : dp->s_block);
323 }
324 (void)putchar('\n');
325 }
326
327 /*
328 * print [inode] [size] name
329 * return # of characters printed, no trailing characters.
330 */
331 static int
332 printaname(FTSENT *p, int inodefield, int sizefield)
333 {
334 struct stat *sp;
335 int chcnt;
336 char szbuf[5];
337 char commabuf[27]; /* 64 bits == 20 digits, +6 for commas, +1 for NUL */
338
339 sp = p->fts_statp;
340 chcnt = 0;
341 if (f_inode)
342 chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
343 if (f_size) {
344 if (f_humanize) {
345 if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
346 "", HN_AUTOSCALE,
347 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
348 err(1, "humanize_number");
349 chcnt += printf("%*s ", sizefield, szbuf);
350 } else if (f_commas) {
351 if (commaize_number(commabuf, sizeof(commabuf),
352 (long long)howmany(sp->st_blocks, blocksize) == -1))
353 err(1, "commaize_number(blocks=%lld)",
354 (long long)howmany(sp->st_blocks, blocksize));
355 (void)printf("%*s ", sizefield, commabuf);
356 } else {
357 chcnt += printf("%*llu ", sizefield,
358 (long long)howmany(sp->st_blocks, blocksize));
359 }
360 }
361 if (f_octal || f_octal_escape)
362 chcnt += safe_print(p->fts_name);
363 else if (f_nonprint)
364 chcnt += printescaped(p->fts_name);
365 else
366 chcnt += printf("%s", p->fts_name);
367 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
368 chcnt += printtype(sp->st_mode);
369 return (chcnt);
370 }
371
372 static void
373 printtime(time_t ftime)
374 {
375 int i;
376 const char *longstring;
377
378 if ((longstring = ctime(&ftime)) == NULL) {
379 /* 012345678901234567890123 */
380 longstring = "????????????????????????";
381 }
382 for (i = 4; i < 11; ++i)
383 (void)putchar(longstring[i]);
384
385 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
386 if (f_sectime)
387 for (i = 11; i < 24; i++)
388 (void)putchar(longstring[i]);
389 else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
390 for (i = 11; i < 16; ++i)
391 (void)putchar(longstring[i]);
392 else {
393 (void)putchar(' ');
394 for (i = 20; i < 24; ++i)
395 (void)putchar(longstring[i]);
396 }
397 (void)putchar(' ');
398 }
399
400 /*
401 * Display total used disk space in the form "total: %u\n".
402 * Note: POSIX (IEEE Std 1003.1-2001) says this should be always in 512 blocks,
403 * but we humanise it with -h, or separate it with commas with -M, and use 1024
404 * with -k.
405 */
406 static void
407 printtotal(DISPLAY *dp)
408 {
409 char szbuf[5];
410 char commabuf[27]; /* 64 bits == 20 digits, +6 for commas, +1 for NUL */
411
412 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
413 if (f_humanize) {
414 if ((humanize_number(szbuf, sizeof(szbuf), (int64_t)dp->stotal,
415 "", HN_AUTOSCALE,
416 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
417 err(1, "humanize_number");
418 (void)printf("total %s\n", szbuf);
419 } else if (f_commas) {
420 if (commaize_number(commabuf, sizeof(commabuf),
421 (long long)howmany(dp->btotal, blocksize)) == -1)
422 err(1, "commaize_number(total=%lld)",
423 (long long)howmany(dp->btotal, blocksize));
424 (void)printf("total %s\n", commabuf);
425 } else {
426 (void)printf("total %llu\n",
427 (long long)(howmany(dp->btotal, blocksize)));
428 }
429 }
430 }
431
432 static int
433 printtype(u_int mode)
434 {
435 switch (mode & S_IFMT) {
436 case S_IFDIR:
437 (void)putchar('/');
438 return (1);
439 case S_IFIFO:
440 (void)putchar('|');
441 return (1);
442 case S_IFLNK:
443 (void)putchar('@');
444 return (1);
445 case S_IFSOCK:
446 (void)putchar('=');
447 return (1);
448 case S_IFWHT:
449 (void)putchar('%');
450 return (1);
451 }
452 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
453 (void)putchar('*');
454 return (1);
455 }
456 return (0);
457 }
458
459 static void
460 printlink(FTSENT *p)
461 {
462 int lnklen;
463 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
464
465 if (p->fts_level == FTS_ROOTLEVEL)
466 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
467 else
468 (void)snprintf(name, sizeof(name),
469 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
470 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
471 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
472 return;
473 }
474 path[lnklen] = '\0';
475 (void)printf(" -> ");
476 if (f_octal || f_octal_escape)
477 (void)safe_print(path);
478 else if (f_nonprint)
479 (void)printescaped(path);
480 else
481 (void)printf("%s", path);
482 }
483
484