No empty .Rs/.Re
[netbsd-mini2440.git] / bin / ls / print.c
blobde8ae2d7795dc370a7a95fc9fce7aadd63bea0b8
1 /* $NetBSD: print.c,v 1.44 2008/12/28 19:30:33 christos Exp $ */
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Michael Fischbein.
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.
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.
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.44 2008/12/28 19:30:33 christos Exp $");
41 #endif
42 #endif /* not lint */
44 #include <sys/param.h>
45 #include <sys/stat.h>
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>
60 #include "ls.h"
61 #include "extern.h"
63 extern int termwidth;
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);
71 static time_t now;
73 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
75 void
76 printscol(DISPLAY *dp)
78 FTSENT *p;
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');
88 void
89 printlong(DISPLAY *dp)
91 struct stat *sp;
92 FTSENT *p;
93 NAMES *np;
94 char buf[20], szbuf[5];
96 now = time(NULL);
98 printtotal(dp); /* "total: %u\n" */
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));
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);
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);
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');
166 void
167 printcol(DISPLAY *dp)
169 static FTSENT **array;
170 static int lastentries = -1;
171 FTSENT *p;
172 int base, chcnt, col, colwidth, num;
173 int numcols, numrows, row;
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;
184 if (f_type || f_typedir)
185 colwidth += 1;
187 colwidth += 1;
189 if (termwidth < 2 * colwidth) {
190 printscol(dp);
191 return;
195 * Have to do random access in the linked list -- build a table
196 * of pointers.
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);
206 for (p = dp->list, num = 0; p; p = p->fts_link)
207 if (p->fts_number != NO_PRINT)
208 array[num++] = p;
210 numcols = termwidth / colwidth;
211 colwidth = termwidth / numcols; /* spread out if possible */
212 numrows = num / numcols;
213 if (num % numcols)
214 ++numrows;
216 printtotal(dp); /* "total: %u\n" */
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(' ');
227 (void)putchar('\n');
231 void
232 printacol(DISPLAY *dp)
234 FTSENT *p;
235 int chcnt, col, colwidth;
236 int numcols;
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;
247 if (f_type || f_typedir)
248 colwidth += 1;
250 colwidth += 1;
252 if (termwidth < 2 * colwidth) {
253 printscol(dp);
254 return;
257 numcols = termwidth / colwidth;
258 colwidth = termwidth / numcols; /* spread out if possible */
260 printtotal(dp); /* "total: %u\n" */
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');
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++;
276 (void)putchar('\n');
279 void
280 printstream(DISPLAY *dp)
282 FTSENT *p;
283 int col;
284 int extwidth;
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;
295 if (f_type)
296 extwidth += 1;
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++;
308 col += printaname(p, dp->s_inode,
309 f_humanize ? dp->s_size : dp->s_block);
311 (void)putchar('\n');
315 * print [inode] [size] name
316 * return # of characters printed, no trailing characters.
318 static int
319 printaname(FTSENT *p, int inodefield, int sizefield)
321 struct stat *sp;
322 int chcnt;
323 char szbuf[5];
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));
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);
352 static void
353 printtime(time_t ftime)
355 int i;
356 char *longstring;
358 longstring = ctime(&ftime);
359 for (i = 4; i < 11; ++i)
360 (void)putchar(longstring[i]);
362 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
363 if (f_sectime)
364 for (i = 11; i < 24; i++)
365 (void)putchar(longstring[i]);
366 else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
367 for (i = 11; i < 16; ++i)
368 (void)putchar(longstring[i]);
369 else {
370 (void)putchar(' ');
371 for (i = 20; i < 24; ++i)
372 (void)putchar(longstring[i]);
374 (void)putchar(' ');
378 * Display total used disk space in the form "total: %u\n".
379 * Note: POSIX (IEEE Std 1003.1-2001) says this should be always in 512 blocks,
380 * but we humanise it with -h and use 1024 with -k.
382 static void
383 printtotal(DISPLAY *dp)
385 char szbuf[5];
387 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
388 if (f_humanize) {
389 if ((humanize_number(szbuf, sizeof(szbuf), (int64_t)dp->stotal,
390 "", HN_AUTOSCALE,
391 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
392 err(1, "humanize_number");
393 (void)printf("total %s\n", szbuf);
394 } else {
395 (void)printf("total %llu\n",
396 (long long)(howmany(dp->btotal, blocksize)));
401 static int
402 printtype(u_int mode)
404 switch (mode & S_IFMT) {
405 case S_IFDIR:
406 (void)putchar('/');
407 return (1);
408 case S_IFIFO:
409 (void)putchar('|');
410 return (1);
411 case S_IFLNK:
412 (void)putchar('@');
413 return (1);
414 case S_IFSOCK:
415 (void)putchar('=');
416 return (1);
417 case S_IFWHT:
418 (void)putchar('%');
419 return (1);
421 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
422 (void)putchar('*');
423 return (1);
425 return (0);
428 static void
429 printlink(FTSENT *p)
431 int lnklen;
432 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
434 if (p->fts_level == FTS_ROOTLEVEL)
435 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
436 else
437 (void)snprintf(name, sizeof(name),
438 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
439 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
440 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
441 return;
443 path[lnklen] = '\0';
444 (void)printf(" -> ");
445 if (f_octal || f_octal_escape)
446 (void)safe_print(path);
447 else if (f_nonprint)
448 (void)printescaped(path);
449 else
450 (void)printf("%s", path);