print.c revision 1.41 1 1.41 jschauma /* $NetBSD: print.c,v 1.41 2005/10/31 14:13:33 jschauma 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.41 jschauma __RCSID("$NetBSD: print.c,v 1.41 2005/10/31 14:13:33 jschauma 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.30 lukem static int printtype(u_int);
69 1.5 mycroft
70 1.20 mycroft static time_t now;
71 1.20 mycroft
72 1.5 mycroft #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
73 1.5 mycroft
74 1.5 mycroft void
75 1.30 lukem printscol(DISPLAY *dp)
76 1.1 cgd {
77 1.11 mycroft FTSENT *p;
78 1.5 mycroft
79 1.5 mycroft for (p = dp->list; p; p = p->fts_link) {
80 1.5 mycroft if (IS_NOPRINT(p))
81 1.5 mycroft continue;
82 1.5 mycroft (void)printaname(p, dp->s_inode, dp->s_block);
83 1.1 cgd (void)putchar('\n');
84 1.1 cgd }
85 1.1 cgd }
86 1.1 cgd
87 1.5 mycroft void
88 1.30 lukem printlong(DISPLAY *dp)
89 1.5 mycroft {
90 1.11 mycroft struct stat *sp;
91 1.11 mycroft FTSENT *p;
92 1.5 mycroft NAMES *np;
93 1.38 grant char buf[20], szbuf[5];
94 1.5 mycroft
95 1.22 mycroft now = time(NULL);
96 1.20 mycroft
97 1.38 grant if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
98 1.38 grant if (f_humanize) {
99 1.38 grant if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
100 1.38 grant "", HN_AUTOSCALE,
101 1.38 grant (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
102 1.38 grant err(1, "humanize_number");
103 1.38 grant (void)printf("total %s\n", szbuf);
104 1.38 grant } else {
105 1.38 grant (void)printf("total %llu\n",
106 1.38 grant (long long)(howmany(dp->btotal, blocksize)));
107 1.38 grant }
108 1.38 grant }
109 1.5 mycroft
110 1.5 mycroft for (p = dp->list; p; p = p->fts_link) {
111 1.5 mycroft if (IS_NOPRINT(p))
112 1.5 mycroft continue;
113 1.5 mycroft sp = p->fts_statp;
114 1.1 cgd if (f_inode)
115 1.23 christos (void)printf("%*lu ", dp->s_inode,
116 1.23 christos (unsigned long)sp->st_ino);
117 1.41 jschauma if (f_size) {
118 1.41 jschauma if (f_humanize) {
119 1.41 jschauma if ((humanize_number(szbuf, sizeof(szbuf),
120 1.41 jschauma sp->st_blocks * S_BLKSIZE,
121 1.41 jschauma "", HN_AUTOSCALE,
122 1.41 jschauma (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
123 1.41 jschauma err(1, "humanize_number");
124 1.41 jschauma (void)printf("%*s ", dp->s_block, szbuf);
125 1.41 jschauma } else {
126 1.24 christos (void)printf("%*llu ", dp->s_block,
127 1.41 jschauma (long long)howmany(sp->st_blocks,
128 1.41 jschauma blocksize));
129 1.41 jschauma }
130 1.38 grant }
131 1.5 mycroft (void)strmode(sp->st_mode, buf);
132 1.5 mycroft np = p->fts_pointer;
133 1.34 grant (void)printf("%s %*lu ", buf, dp->s_nlink,
134 1.34 grant (unsigned long)sp->st_nlink);
135 1.34 grant if (!f_grouponly)
136 1.34 grant (void)printf("%-*s ", dp->s_user, np->user);
137 1.34 grant (void)printf("%-*s ", dp->s_group, np->group);
138 1.5 mycroft if (f_flags)
139 1.5 mycroft (void)printf("%-*s ", dp->s_flags, np->flags);
140 1.5 mycroft if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
141 1.18 mycroft (void)printf("%*u, %*u ",
142 1.18 mycroft dp->s_major, major(sp->st_rdev), dp->s_minor,
143 1.18 mycroft minor(sp->st_rdev));
144 1.1 cgd else
145 1.38 grant if (f_humanize) {
146 1.38 grant if ((humanize_number(szbuf, sizeof(szbuf),
147 1.38 grant sp->st_size, "", HN_AUTOSCALE,
148 1.38 grant (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
149 1.38 grant err(1, "humanize_number");
150 1.38 grant (void)printf("%*s ", dp->s_size, szbuf);
151 1.38 grant } else {
152 1.38 grant (void)printf("%*llu ", dp->s_size,
153 1.38 grant (long long)sp->st_size);
154 1.38 grant }
155 1.1 cgd if (f_accesstime)
156 1.5 mycroft printtime(sp->st_atime);
157 1.1 cgd else if (f_statustime)
158 1.5 mycroft printtime(sp->st_ctime);
159 1.1 cgd else
160 1.5 mycroft printtime(sp->st_mtime);
161 1.37 jschauma if (f_octal || f_octal_escape)
162 1.36 jschauma (void)safe_print(p->fts_name);
163 1.36 jschauma else if (f_nonprint)
164 1.29 assar (void)printescaped(p->fts_name);
165 1.28 assar else
166 1.28 assar (void)printf("%s", p->fts_name);
167 1.28 assar
168 1.26 kleink if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
169 1.5 mycroft (void)printtype(sp->st_mode);
170 1.5 mycroft if (S_ISLNK(sp->st_mode))
171 1.5 mycroft printlink(p);
172 1.1 cgd (void)putchar('\n');
173 1.1 cgd }
174 1.1 cgd }
175 1.1 cgd
176 1.5 mycroft void
177 1.30 lukem printcol(DISPLAY *dp)
178 1.1 cgd {
179 1.5 mycroft static FTSENT **array;
180 1.5 mycroft static int lastentries = -1;
181 1.11 mycroft FTSENT *p;
182 1.16 christos int base, chcnt, col, colwidth, num;
183 1.15 thorpej int numcols, numrows, row;
184 1.38 grant char szbuf[5];
185 1.1 cgd
186 1.19 lukem colwidth = dp->maxlen;
187 1.19 lukem if (f_inode)
188 1.19 lukem colwidth += dp->s_inode + 1;
189 1.38 grant if (f_size) {
190 1.38 grant if (f_humanize)
191 1.38 grant colwidth += dp->s_size + 1;
192 1.38 grant else
193 1.38 grant colwidth += dp->s_block + 1;
194 1.38 grant }
195 1.26 kleink if (f_type || f_typedir)
196 1.19 lukem colwidth += 1;
197 1.19 lukem
198 1.19 lukem colwidth += 1;
199 1.19 lukem
200 1.19 lukem if (termwidth < 2 * colwidth) {
201 1.19 lukem printscol(dp);
202 1.19 lukem return;
203 1.19 lukem }
204 1.19 lukem
205 1.5 mycroft /*
206 1.5 mycroft * Have to do random access in the linked list -- build a table
207 1.5 mycroft * of pointers.
208 1.5 mycroft */
209 1.5 mycroft if (dp->entries > lastentries) {
210 1.5 mycroft lastentries = dp->entries;
211 1.5 mycroft if ((array =
212 1.5 mycroft realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
213 1.27 drochner warn(NULL);
214 1.5 mycroft printscol(dp);
215 1.5 mycroft }
216 1.5 mycroft }
217 1.5 mycroft for (p = dp->list, num = 0; p; p = p->fts_link)
218 1.5 mycroft if (p->fts_number != NO_PRINT)
219 1.5 mycroft array[num++] = p;
220 1.5 mycroft
221 1.19 lukem numcols = termwidth / colwidth;
222 1.19 lukem colwidth = termwidth / numcols; /* spread out if possible */
223 1.19 lukem numrows = num / numcols;
224 1.19 lukem if (num % numcols)
225 1.19 lukem ++numrows;
226 1.19 lukem
227 1.38 grant if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
228 1.38 grant if (f_humanize) {
229 1.38 grant if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
230 1.38 grant "", HN_AUTOSCALE,
231 1.38 grant (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
232 1.38 grant err(1, "humanize_number");
233 1.38 grant (void)printf("total %s\n", szbuf);
234 1.38 grant } else {
235 1.38 grant (void)printf("total %llu\n",
236 1.38 grant (long long)(howmany(dp->btotal, blocksize)));
237 1.38 grant }
238 1.38 grant }
239 1.19 lukem for (row = 0; row < numrows; ++row) {
240 1.19 lukem for (base = row, chcnt = col = 0; col < numcols; ++col) {
241 1.19 lukem chcnt = printaname(array[base], dp->s_inode,
242 1.38 grant f_humanize ? dp->s_size : dp->s_block);
243 1.19 lukem if ((base += numrows) >= num)
244 1.19 lukem break;
245 1.19 lukem while (chcnt++ < colwidth)
246 1.22 mycroft (void)putchar(' ');
247 1.19 lukem }
248 1.19 lukem (void)putchar('\n');
249 1.19 lukem }
250 1.19 lukem }
251 1.19 lukem
252 1.19 lukem void
253 1.30 lukem printacol(DISPLAY *dp)
254 1.19 lukem {
255 1.19 lukem FTSENT *p;
256 1.19 lukem int chcnt, col, colwidth;
257 1.19 lukem int numcols;
258 1.38 grant char szbuf[5];
259 1.19 lukem
260 1.5 mycroft colwidth = dp->maxlen;
261 1.1 cgd if (f_inode)
262 1.5 mycroft colwidth += dp->s_inode + 1;
263 1.38 grant if (f_size) {
264 1.38 grant if (f_humanize)
265 1.38 grant colwidth += dp->s_size + 1;
266 1.38 grant else
267 1.38 grant colwidth += dp->s_block + 1;
268 1.38 grant }
269 1.26 kleink if (f_type || f_typedir)
270 1.1 cgd colwidth += 1;
271 1.1 cgd
272 1.15 thorpej colwidth += 1;
273 1.15 thorpej
274 1.1 cgd if (termwidth < 2 * colwidth) {
275 1.5 mycroft printscol(dp);
276 1.1 cgd return;
277 1.1 cgd }
278 1.1 cgd
279 1.1 cgd numcols = termwidth / colwidth;
280 1.15 thorpej colwidth = termwidth / numcols; /* spread out if possible */
281 1.1 cgd
282 1.38 grant if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
283 1.38 grant if (f_humanize) {
284 1.38 grant if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
285 1.38 grant "", HN_AUTOSCALE,
286 1.38 grant (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
287 1.38 grant err(1, "humanize_number");
288 1.38 grant (void)printf("total %s\n", szbuf);
289 1.38 grant } else {
290 1.38 grant (void)printf("total %llu\n",
291 1.38 grant (long long)(howmany(dp->btotal, blocksize)));
292 1.38 grant }
293 1.38 grant }
294 1.19 lukem chcnt = col = 0;
295 1.19 lukem for (p = dp->list; p; p = p->fts_link) {
296 1.19 lukem if (IS_NOPRINT(p))
297 1.19 lukem continue;
298 1.19 lukem if (col >= numcols) {
299 1.19 lukem chcnt = col = 0;
300 1.22 mycroft (void)putchar('\n');
301 1.1 cgd }
302 1.38 grant chcnt = printaname(p, dp->s_inode,
303 1.38 grant f_humanize ? dp->s_size : dp->s_block);
304 1.19 lukem while (chcnt++ < colwidth)
305 1.22 mycroft (void)putchar(' ');
306 1.19 lukem col++;
307 1.25 kleink }
308 1.25 kleink (void)putchar('\n');
309 1.25 kleink }
310 1.25 kleink
311 1.25 kleink void
312 1.30 lukem printstream(DISPLAY *dp)
313 1.25 kleink {
314 1.25 kleink FTSENT *p;
315 1.25 kleink int col;
316 1.25 kleink int extwidth;
317 1.25 kleink
318 1.25 kleink extwidth = 0;
319 1.25 kleink if (f_inode)
320 1.25 kleink extwidth += dp->s_inode + 1;
321 1.38 grant if (f_size) {
322 1.38 grant if (f_humanize)
323 1.38 grant extwidth += dp->s_size + 1;
324 1.38 grant else
325 1.38 grant extwidth += dp->s_block + 1;
326 1.38 grant }
327 1.25 kleink if (f_type)
328 1.25 kleink extwidth += 1;
329 1.25 kleink
330 1.25 kleink for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
331 1.25 kleink if (IS_NOPRINT(p))
332 1.25 kleink continue;
333 1.25 kleink if (col > 0) {
334 1.25 kleink (void)putchar(','), col++;
335 1.25 kleink if (col + 1 + extwidth + p->fts_namelen >= termwidth)
336 1.25 kleink (void)putchar('\n'), col = 0;
337 1.25 kleink else
338 1.25 kleink (void)putchar(' '), col++;
339 1.25 kleink }
340 1.38 grant col += printaname(p, dp->s_inode,
341 1.38 grant f_humanize ? dp->s_size : dp->s_block);
342 1.1 cgd }
343 1.22 mycroft (void)putchar('\n');
344 1.1 cgd }
345 1.1 cgd
346 1.1 cgd /*
347 1.1 cgd * print [inode] [size] name
348 1.5 mycroft * return # of characters printed, no trailing characters.
349 1.1 cgd */
350 1.5 mycroft static int
351 1.30 lukem printaname(FTSENT *p, int inodefield, int sizefield)
352 1.1 cgd {
353 1.5 mycroft struct stat *sp;
354 1.1 cgd int chcnt;
355 1.38 grant char szbuf[5];
356 1.1 cgd
357 1.5 mycroft sp = p->fts_statp;
358 1.1 cgd chcnt = 0;
359 1.1 cgd if (f_inode)
360 1.23 christos chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
361 1.38 grant if (f_size) {
362 1.38 grant if (f_humanize) {
363 1.38 grant if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
364 1.38 grant "", HN_AUTOSCALE,
365 1.38 grant (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
366 1.38 grant err(1, "humanize_number");
367 1.38 grant chcnt += printf("%*s ", sizefield, szbuf);
368 1.38 grant } else {
369 1.38 grant chcnt += printf("%*llu ", sizefield,
370 1.38 grant (long long)howmany(sp->st_blocks, blocksize));
371 1.38 grant }
372 1.38 grant }
373 1.37 jschauma if (f_octal || f_octal_escape)
374 1.36 jschauma chcnt += safe_print(p->fts_name);
375 1.36 jschauma else if (f_nonprint)
376 1.36 jschauma chcnt += printescaped(p->fts_name);
377 1.29 assar else
378 1.36 jschauma chcnt += printf("%s", p->fts_name);
379 1.26 kleink if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
380 1.5 mycroft chcnt += printtype(sp->st_mode);
381 1.5 mycroft return (chcnt);
382 1.1 cgd }
383 1.1 cgd
384 1.5 mycroft static void
385 1.30 lukem printtime(time_t ftime)
386 1.1 cgd {
387 1.1 cgd int i;
388 1.5 mycroft char *longstring;
389 1.1 cgd
390 1.5 mycroft longstring = ctime(&ftime);
391 1.1 cgd for (i = 4; i < 11; ++i)
392 1.1 cgd (void)putchar(longstring[i]);
393 1.1 cgd
394 1.1 cgd #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
395 1.1 cgd if (f_sectime)
396 1.1 cgd for (i = 11; i < 24; i++)
397 1.1 cgd (void)putchar(longstring[i]);
398 1.40 mycroft else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
399 1.1 cgd for (i = 11; i < 16; ++i)
400 1.1 cgd (void)putchar(longstring[i]);
401 1.1 cgd else {
402 1.1 cgd (void)putchar(' ');
403 1.1 cgd for (i = 20; i < 24; ++i)
404 1.1 cgd (void)putchar(longstring[i]);
405 1.1 cgd }
406 1.1 cgd (void)putchar(' ');
407 1.1 cgd }
408 1.1 cgd
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