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