Remove building with NOCRYPTO option
[minix.git] / bin / ls / print.c
blobb3aecd8c53941ca6e88d7ac9a68948075b6028fe
1 /* $NetBSD: print.c,v 1.55 2014/05/10 09:39:18 martin 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.55 2014/05/10 09:39:18 martin 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 <inttypes.h>
50 #include <fts.h>
51 #include <grp.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <tzfile.h>
58 #include <unistd.h>
59 #include <util.h>
61 #include "ls.h"
62 #include "extern.h"
64 extern int termwidth;
66 static int printaname(FTSENT *, int, int);
67 static void printlink(FTSENT *);
68 static void printtime(time_t);
69 static void printtotal(DISPLAY *dp);
70 static int printtype(u_int);
72 static time_t now;
74 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
76 static int
77 safe_printpath(const FTSENT *p) {
78 int chcnt;
80 if (f_fullpath) {
81 chcnt = safe_print(p->fts_path);
82 chcnt += safe_print("/");
83 } else
84 chcnt = 0;
85 return chcnt + safe_print(p->fts_name);
88 static int
89 printescapedpath(const FTSENT *p) {
90 int chcnt;
92 if (f_fullpath) {
93 chcnt = printescaped(p->fts_path);
94 chcnt += printescaped("/");
95 } else
96 chcnt = 0;
98 return chcnt + printescaped(p->fts_name);
101 static int
102 printpath(const FTSENT *p) {
103 if (f_fullpath)
104 return printf("%s/%s", p->fts_path, p->fts_name);
105 else
106 return printf("%s", p->fts_name);
109 void
110 printscol(DISPLAY *dp)
112 FTSENT *p;
114 for (p = dp->list; p; p = p->fts_link) {
115 if (IS_NOPRINT(p))
116 continue;
117 (void)printaname(p, dp->s_inode, dp->s_block);
118 (void)putchar('\n');
122 void
123 printlong(DISPLAY *dp)
125 struct stat *sp;
126 FTSENT *p;
127 NAMES *np;
128 char buf[20], szbuf[5];
130 now = time(NULL);
132 if (!f_leafonly)
133 printtotal(dp); /* "total: %u\n" */
135 for (p = dp->list; p; p = p->fts_link) {
136 if (IS_NOPRINT(p))
137 continue;
138 sp = p->fts_statp;
139 if (f_inode)
140 (void)printf("%*"PRIu64" ", dp->s_inode, sp->st_ino);
141 if (f_size) {
142 if (f_humanize) {
143 if ((humanize_number(szbuf, sizeof(szbuf),
144 sp->st_blocks * S_BLKSIZE,
145 "", HN_AUTOSCALE,
146 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
147 err(1, "humanize_number");
148 (void)printf("%*s ", dp->s_block, szbuf);
149 } else {
150 (void)printf(f_commas ? "%'*llu " : "%*llu ",
151 dp->s_block,
152 (unsigned long long)howmany(sp->st_blocks,
153 blocksize));
156 (void)strmode(sp->st_mode, buf);
157 np = p->fts_pointer;
158 (void)printf("%s %*lu ", buf, dp->s_nlink,
159 (unsigned long)sp->st_nlink);
160 if (!f_grouponly)
161 (void)printf("%-*s ", dp->s_user, np->user);
162 (void)printf("%-*s ", dp->s_group, np->group);
163 if (f_flags)
164 (void)printf("%-*s ", dp->s_flags, np->flags);
165 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
166 (void)printf("%*lld, %*lld ",
167 dp->s_major, (long long)major(sp->st_rdev),
168 dp->s_minor, (long long)minor(sp->st_rdev));
169 else
170 if (f_humanize) {
171 if ((humanize_number(szbuf, sizeof(szbuf),
172 sp->st_size, "", HN_AUTOSCALE,
173 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
174 err(1, "humanize_number");
175 (void)printf("%*s ", dp->s_size, szbuf);
176 } else {
177 (void)printf(f_commas ? "%'*llu " : "%*llu ",
178 dp->s_size, (unsigned long long)
179 sp->st_size);
181 if (f_accesstime)
182 printtime(sp->st_atime);
183 else if (f_statustime)
184 printtime(sp->st_ctime);
185 else
186 printtime(sp->st_mtime);
187 if (f_octal || f_octal_escape)
188 (void)safe_printpath(p);
189 else if (f_nonprint)
190 (void)printescapedpath(p);
191 else
192 (void)printpath(p);
194 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
195 (void)printtype(sp->st_mode);
196 if (S_ISLNK(sp->st_mode))
197 printlink(p);
198 (void)putchar('\n');
202 void
203 printcol(DISPLAY *dp)
205 static FTSENT **array;
206 static int lastentries = -1;
207 FTSENT *p;
208 int base, chcnt, col, colwidth, num;
209 int numcols, numrows, row;
211 colwidth = dp->maxlen;
212 if (f_inode)
213 colwidth += dp->s_inode + 1;
214 if (f_size) {
215 if (f_humanize)
216 colwidth += dp->s_size + 1;
217 else
218 colwidth += dp->s_block + 1;
220 if (f_type || f_typedir)
221 colwidth += 1;
223 colwidth += 1;
225 if (termwidth < 2 * colwidth) {
226 printscol(dp);
227 return;
231 * Have to do random access in the linked list -- build a table
232 * of pointers.
234 if (dp->entries > lastentries) {
235 FTSENT **newarray;
237 newarray = realloc(array, dp->entries * sizeof(FTSENT *));
238 if (newarray == NULL) {
239 warn(NULL);
240 printscol(dp);
241 return;
243 lastentries = dp->entries;
244 array = newarray;
246 for (p = dp->list, num = 0; p; p = p->fts_link)
247 if (p->fts_number != NO_PRINT)
248 array[num++] = p;
250 numcols = termwidth / colwidth;
251 colwidth = termwidth / numcols; /* spread out if possible */
252 numrows = num / numcols;
253 if (num % numcols)
254 ++numrows;
256 printtotal(dp); /* "total: %u\n" */
258 for (row = 0; row < numrows; ++row) {
259 for (base = row, chcnt = col = 0; col < numcols; ++col) {
260 chcnt = printaname(array[base], dp->s_inode,
261 f_humanize ? dp->s_size : dp->s_block);
262 if ((base += numrows) >= num)
263 break;
264 while (chcnt++ < colwidth)
265 (void)putchar(' ');
267 (void)putchar('\n');
271 void
272 printacol(DISPLAY *dp)
274 FTSENT *p;
275 int chcnt, col, colwidth;
276 int numcols;
278 colwidth = dp->maxlen;
279 if (f_inode)
280 colwidth += dp->s_inode + 1;
281 if (f_size) {
282 if (f_humanize)
283 colwidth += dp->s_size + 1;
284 else
285 colwidth += dp->s_block + 1;
287 if (f_type || f_typedir)
288 colwidth += 1;
290 colwidth += 1;
292 if (termwidth < 2 * colwidth) {
293 printscol(dp);
294 return;
297 numcols = termwidth / colwidth;
298 colwidth = termwidth / numcols; /* spread out if possible */
300 printtotal(dp); /* "total: %u\n" */
302 chcnt = col = 0;
303 for (p = dp->list; p; p = p->fts_link) {
304 if (IS_NOPRINT(p))
305 continue;
306 if (col >= numcols) {
307 chcnt = col = 0;
308 (void)putchar('\n');
310 chcnt = printaname(p, dp->s_inode,
311 f_humanize ? dp->s_size : dp->s_block);
312 while (chcnt++ < colwidth)
313 (void)putchar(' ');
314 col++;
316 (void)putchar('\n');
319 void
320 printstream(DISPLAY *dp)
322 FTSENT *p;
323 int col;
324 int extwidth;
326 extwidth = 0;
327 if (f_inode)
328 extwidth += dp->s_inode + 1;
329 if (f_size) {
330 if (f_humanize)
331 extwidth += dp->s_size + 1;
332 else
333 extwidth += dp->s_block + 1;
335 if (f_type)
336 extwidth += 1;
338 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
339 if (IS_NOPRINT(p))
340 continue;
341 if (col > 0) {
342 (void)putchar(','), col++;
343 if (col + 1 + extwidth + (int)p->fts_namelen >= termwidth)
344 (void)putchar('\n'), col = 0;
345 else
346 (void)putchar(' '), col++;
348 col += printaname(p, dp->s_inode,
349 f_humanize ? dp->s_size : dp->s_block);
351 (void)putchar('\n');
355 * print [inode] [size] name
356 * return # of characters printed, no trailing characters.
358 static int
359 printaname(FTSENT *p, int inodefield, int sizefield)
361 struct stat *sp;
362 int chcnt;
363 char szbuf[5];
365 sp = p->fts_statp;
366 chcnt = 0;
367 if (f_inode)
368 chcnt += printf("%*"PRIu64" ", inodefield, sp->st_ino);
369 if (f_size) {
370 if (f_humanize) {
371 if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
372 "", HN_AUTOSCALE,
373 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
374 err(1, "humanize_number");
375 chcnt += printf("%*s ", sizefield, szbuf);
376 } else {
377 chcnt += printf(f_commas ? "%'*llu " : "%*llu ",
378 sizefield, (unsigned long long)
379 howmany(sp->st_blocks, blocksize));
382 if (f_octal || f_octal_escape)
383 chcnt += safe_printpath(p);
384 else if (f_nonprint)
385 chcnt += printescapedpath(p);
386 else
387 chcnt += printpath(p);
388 if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
389 chcnt += printtype(sp->st_mode);
390 return (chcnt);
393 static void
394 printtime(time_t ftime)
396 int i;
397 const char *longstring;
399 if ((longstring = ctime(&ftime)) == NULL) {
400 /* 012345678901234567890123 */
401 longstring = "????????????????????????";
403 for (i = 4; i < 11; ++i)
404 (void)putchar(longstring[i]);
406 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
407 if (f_sectime)
408 for (i = 11; i < 24; i++)
409 (void)putchar(longstring[i]);
410 else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
411 for (i = 11; i < 16; ++i)
412 (void)putchar(longstring[i]);
413 else {
414 (void)putchar(' ');
415 for (i = 20; i < 24; ++i)
416 (void)putchar(longstring[i]);
418 (void)putchar(' ');
422 * Display total used disk space in the form "total: %u\n".
423 * Note: POSIX (IEEE Std 1003.1-2001) says this should be always in 512 blocks,
424 * but we humanise it with -h, or separate it with commas with -M, and use 1024
425 * with -k.
427 static void
428 printtotal(DISPLAY *dp)
430 char szbuf[5];
432 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
433 if (f_humanize) {
434 if ((humanize_number(szbuf, sizeof(szbuf), (int64_t)dp->stotal,
435 "", HN_AUTOSCALE,
436 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
437 err(1, "humanize_number");
438 (void)printf("total %s\n", szbuf);
439 } else {
440 (void)printf(f_commas ? "total %'llu\n" :
441 "total %llu\n", (unsigned long long)
442 howmany(dp->btotal, blocksize));
447 static int
448 printtype(u_int mode)
450 switch (mode & S_IFMT) {
451 case S_IFDIR:
452 (void)putchar('/');
453 return (1);
454 case S_IFIFO:
455 (void)putchar('|');
456 return (1);
457 case S_IFLNK:
458 (void)putchar('@');
459 return (1);
460 case S_IFSOCK:
461 (void)putchar('=');
462 return (1);
463 case S_IFWHT:
464 (void)putchar('%');
465 return (1);
467 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
468 (void)putchar('*');
469 return (1);
471 return (0);
474 static void
475 printlink(FTSENT *p)
477 int lnklen;
478 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
480 if (p->fts_level == FTS_ROOTLEVEL)
481 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
482 else
483 (void)snprintf(name, sizeof(name),
484 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
485 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
486 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
487 return;
489 path[lnklen] = '\0';
490 (void)printf(" -> ");
491 if (f_octal || f_octal_escape)
492 (void)safe_print(path);
493 else if (f_nonprint)
494 (void)printescaped(path);
495 else
496 (void)printf("%s", path);