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