gdb - Local mods (compile)
[dragonfly.git] / bin / ls / print.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Michael Fischbein.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
dc71b7ab 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
1de703da
MD
31 *
32 * @(#)print.c 8.4 (Berkeley) 4/17/94
cd07f2f6 33 * $FreeBSD: src/bin/ls/print.c,v 1.73 2004/06/08 09:27:42 das Exp $
984263bc
MD
34 */
35
984263bc
MD
36#include <sys/param.h>
37#include <sys/stat.h>
38
39#include <err.h>
40#include <errno.h>
41#include <fts.h>
984263bc 42#include <langinfo.h>
cd07f2f6 43#include <libutil.h>
984263bc
MD
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <time.h>
48#include <unistd.h>
d4428785 49#include <locale.h>
984263bc
MD
50#ifdef COLORLS
51#include <ctype.h>
52#include <termcap.h>
53#include <signal.h>
54#endif
55
56#include "ls.h"
57#include "extern.h"
58
20d719bf 59static int printaname(const FTSENT *, u_long, u_long);
b534d61c 60static void printlink(const FTSENT *);
984263bc
MD
61static void printtime(time_t);
62static int printtype(u_int);
63static void printsize(size_t, off_t);
64#ifdef COLORLS
65static void endcolor(int);
66static int colortype(mode_t);
67#endif
68
69#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
70
984263bc
MD
71#ifdef COLORLS
72/* Most of these are taken from <sys/stat.h> */
73typedef enum Colors {
74 C_DIR, /* directory */
75 C_LNK, /* symbolic link */
76 C_SOCK, /* socket */
77 C_FIFO, /* pipe */
78 C_EXEC, /* executable */
79 C_BLK, /* block special */
80 C_CHR, /* character special */
81 C_SUID, /* setuid executable */
82 C_SGID, /* setgid executable */
83 C_WSDIR, /* directory writeble to others, with sticky
84 * bit */
85 C_WDIR, /* directory writeble to others, without
86 * sticky bit */
87 C_NUMCOLORS /* just a place-holder */
88} Colors;
89
90static const char *defcolors = "exfxcxdxbxegedabagacad";
91
92/* colors for file types */
93static struct {
94 int num[2];
95 int bold;
96} colors[C_NUMCOLORS];
97#endif
98
99void
20d719bf 100printscol(const DISPLAY *dp)
984263bc
MD
101{
102 FTSENT *p;
103
104 for (p = dp->list; p; p = p->fts_link) {
105 if (IS_NOPRINT(p))
106 continue;
57fed2af
EN
107 printaname(p, dp->s_inode, dp->s_block);
108 putchar('\n');
984263bc
MD
109 }
110}
111
112/*
113 * print name in current style
114 */
115int
116printname(const char *name)
117{
118 if (f_octal || f_octal_escape)
119 return prn_octal(name);
120 else if (f_nonprint)
121 return prn_printable(name);
122 else
aae9ea87 123 return prn_normal(name);
984263bc
MD
124}
125
126void
20d719bf 127printlong(const DISPLAY *dp)
984263bc
MD
128{
129 struct stat *sp;
130 FTSENT *p;
131 NAMES *np;
132 char buf[20];
133#ifdef COLORLS
134 int color_printed = 0;
135#endif
136
15a6c54c
JR
137 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
138 (f_longform || f_size)) {
57fed2af 139 printf("total %lu\n", howmany(dp->btotal, blocksize));
15a6c54c 140 }
984263bc
MD
141
142 for (p = dp->list; p; p = p->fts_link) {
143 if (IS_NOPRINT(p))
144 continue;
145 sp = p->fts_statp;
146 if (f_inode)
76dc7fb4 147 printf("%*ju ", dp->s_inode, (uintmax_t)sp->st_ino);
984263bc 148 if (f_size)
76dc7fb4 149 printf("%*jd ",
984263bc
MD
150 dp->s_block, howmany(sp->st_blocks, blocksize));
151 strmode(sp->st_mode, buf);
152 np = p->fts_pointer;
57fed2af 153 printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
984263bc
MD
154 sp->st_nlink, dp->s_user, np->user, dp->s_group,
155 np->group);
d20227c1
MD
156 if (f_fsmid)
157 printf("%s ", np->fsmid);
984263bc 158 if (f_flags)
57fed2af 159 printf("%-*s ", dp->s_flags, np->flags);
984263bc 160 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
f966adb2
MD
161 printf("%3d, 0x%08x ",
162 major(sp->st_rdev), (u_int)minor(sp->st_rdev));
984263bc 163 else if (dp->bcfile)
76dc7fb4
SW
164 printf("%*s%*jd ",
165 8 - dp->s_size, "", dp->s_size, (intmax_t)sp->st_size);
984263bc
MD
166 else
167 printsize(dp->s_size, sp->st_size);
168 if (f_accesstime)
169 printtime(sp->st_atime);
170 else if (f_statustime)
171 printtime(sp->st_ctime);
172 else
173 printtime(sp->st_mtime);
174#ifdef COLORLS
175 if (f_color)
176 color_printed = colortype(sp->st_mode);
177#endif
57fed2af 178 printname(p->fts_name);
984263bc
MD
179#ifdef COLORLS
180 if (f_color && color_printed)
181 endcolor(0);
182#endif
183 if (f_type)
57fed2af 184 printtype(sp->st_mode);
984263bc
MD
185 if (S_ISLNK(sp->st_mode))
186 printlink(p);
57fed2af 187 putchar('\n');
984263bc
MD
188 }
189}
190
191void
20d719bf 192printstream(const DISPLAY *dp)
984263bc
MD
193{
194 FTSENT *p;
984263bc
MD
195 int chcnt;
196
197 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
198 if (p->fts_number == NO_PRINT)
199 continue;
aae9ea87 200 /* XXX strlen does not take octal escapes into account. */
984263bc
MD
201 if (strlen(p->fts_name) + chcnt +
202 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
203 putchar('\n');
204 chcnt = 0;
205 }
206 chcnt += printaname(p, dp->s_inode, dp->s_block);
207 if (p->fts_link) {
208 printf(", ");
209 chcnt += 2;
210 }
211 }
212 if (chcnt)
213 putchar('\n');
214}
215
216void
20d719bf 217printcol(const DISPLAY *dp)
984263bc 218{
984263bc
MD
219 static FTSENT **array;
220 static int lastentries = -1;
221 FTSENT *p;
68918172 222 FTSENT **narray;
984263bc
MD
223 int base;
224 int chcnt;
225 int cnt;
226 int col;
227 int colwidth;
228 int endcol;
229 int num;
230 int numcols;
231 int numrows;
232 int row;
233 int tabwidth;
234
235 if (f_notabs)
236 tabwidth = 1;
237 else
238 tabwidth = 8;
239
240 /*
241 * Have to do random access in the linked list -- build a table
242 * of pointers.
243 */
244 if (dp->entries > lastentries) {
245 lastentries = dp->entries;
68918172 246 if ((narray =
984263bc
MD
247 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
248 warn(NULL);
249 printscol(dp);
68918172 250 return;
984263bc 251 }
68918172
DR
252 lastentries = dp->entries;
253 array = narray;
984263bc
MD
254 }
255 for (p = dp->list, num = 0; p; p = p->fts_link)
256 if (p->fts_number != NO_PRINT)
257 array[num++] = p;
258
259 colwidth = dp->maxlen;
260 if (f_inode)
261 colwidth += dp->s_inode + 1;
262 if (f_size)
263 colwidth += dp->s_block + 1;
264 if (f_type)
265 colwidth += 1;
266
267 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
268 if (termwidth < 2 * colwidth) {
269 printscol(dp);
270 return;
271 }
272 numcols = termwidth / colwidth;
273 numrows = num / numcols;
274 if (num % numcols)
275 ++numrows;
276
15a6c54c
JR
277 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
278 (f_longform || f_size)) {
57fed2af 279 printf("total %lu\n", howmany(dp->btotal, blocksize));
15a6c54c 280 }
984263bc 281
00a63dd3
MD
282 /* counter if f_sortacross, else case-by-case */
283 base = 0;
284
984263bc
MD
285 for (row = 0; row < numrows; ++row) {
286 endcol = colwidth;
287 if (!f_sortacross)
288 base = row;
289 for (col = 0, chcnt = 0; col < numcols; ++col) {
290 chcnt += printaname(array[base], dp->s_inode,
291 dp->s_block);
292 if (f_sortacross)
293 base++;
294 else
295 base += numrows;
296 if (base >= num)
297 break;
298 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
299 <= endcol) {
300 if (f_sortacross && col + 1 >= numcols)
301 break;
57fed2af 302 putchar(f_notabs ? ' ' : '\t');
984263bc
MD
303 chcnt = cnt;
304 }
305 endcol += colwidth;
306 }
57fed2af 307 putchar('\n');
984263bc
MD
308 }
309}
310
311/*
312 * print [inode] [size] name
313 * return # of characters printed, no trailing characters.
314 */
315static int
20d719bf 316printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
984263bc
MD
317{
318 struct stat *sp;
319 int chcnt;
320#ifdef COLORLS
321 int color_printed = 0;
322#endif
323
324 sp = p->fts_statp;
325 chcnt = 0;
6cbddef6 326 if (f_inode) {
76dc7fb4
SW
327 chcnt += printf("%*ju ", (int)inodefield,
328 (uintmax_t)sp->st_ino);
6cbddef6 329 }
984263bc 330 if (f_size)
76dc7fb4 331 chcnt += printf("%*jd ",
984263bc
MD
332 (int)sizefield, howmany(sp->st_blocks, blocksize));
333#ifdef COLORLS
334 if (f_color)
335 color_printed = colortype(sp->st_mode);
336#endif
337 chcnt += printname(p->fts_name);
338#ifdef COLORLS
339 if (f_color && color_printed)
340 endcolor(0);
341#endif
342 if (f_type)
343 chcnt += printtype(sp->st_mode);
344 return (chcnt);
345}
346
347static void
348printtime(time_t ftime)
349{
350 char longstring[80];
351 static time_t now;
352 const char *format;
d4428785
JM
353 static char *lc_time;
354 static int posix_time;
355
356#define SIXMONTHS ((365 / 2) * 86400)
984263bc 357
984263bc
MD
358 if (now == 0)
359 now = time(NULL);
360
d4428785
JM
361 if (lc_time == NULL) {
362 lc_time = setlocale(LC_TIME, NULL);
363 posix_time = (strcmp(lc_time, "C") == 0);
364 }
365
366 if (f_sectime) {
367 /*
368 * POSIX: Not covered by standard. If C/POSIX locale used
369 * the old convention of mmm dd hh:mm:ss yyyy is kept.
370 * Named locales use the extended ISO 8601 format without T:
371 * YYYY-DD-MM hh:mm:ss
372 */
373 format = posix_time ? "%b %e %T %Y " : "%F %T ";
374 } else if (posix_time) {
375 /*
376 * POSIX: Future or older than 6 months returns equivalent of
377 * date "+%b %e %Y"
378 * If file was modified within the past 6 months,
379 * return equivalent of: date "+%b %e %H:%M"
380 */
877e2cd4 381 if ((ftime > now) || (ftime < now - SIXMONTHS))
d4428785 382 format = "%b %e %Y ";
877e2cd4
JM
383 else
384 format = "%b %e %R ";
d4428785 385 } else {
c0a9d818
JM
386 /*
387 * Named locales use format DD-MMM-YYYY hh:mm
388 * Regardless of locale, English is used for month field
389 */
390 format = "%d-%b-%Y %H:%M ";
d4428785
JM
391 }
392
c0a9d818
JM
393 strftime_l(longstring, sizeof(longstring), format, localtime(&ftime),
394 NULL);
984263bc
MD
395 fputs(longstring, stdout);
396}
397
398static int
399printtype(u_int mode)
400{
401
402 if (f_slash) {
403 if ((mode & S_IFMT) == S_IFDIR) {
57fed2af 404 putchar('/');
984263bc
MD
405 return (1);
406 }
407 return (0);
408 }
409
410 switch (mode & S_IFMT) {
411 case S_IFDIR:
57fed2af 412 putchar('/');
984263bc
MD
413 return (1);
414 case S_IFIFO:
57fed2af 415 putchar('|');
984263bc
MD
416 return (1);
417 case S_IFLNK:
57fed2af 418 putchar('@');
984263bc
MD
419 return (1);
420 case S_IFSOCK:
57fed2af 421 putchar('=');
984263bc
MD
422 return (1);
423 case S_IFWHT:
57fed2af 424 putchar('%');
984263bc
MD
425 return (1);
426 default:
427 break;
428 }
429 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
57fed2af 430 putchar('*');
984263bc
MD
431 return (1);
432 }
433 return (0);
434}
435
436#ifdef COLORLS
437static int
438putch(int c)
439{
57fed2af 440 putchar(c);
984263bc
MD
441 return 0;
442}
443
444static int
445writech(int c)
446{
20d719bf 447 char tmp = (char)c;
984263bc 448
57fed2af 449 write(STDOUT_FILENO, &tmp, 1);
984263bc
MD
450 return 0;
451}
452
453static void
454printcolor(Colors c)
455{
456 char *ansiseq;
457
458 if (colors[c].bold)
459 tputs(enter_bold, 1, putch);
460
461 if (colors[c].num[0] != -1) {
462 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
463 if (ansiseq)
464 tputs(ansiseq, 1, putch);
465 }
466 if (colors[c].num[1] != -1) {
467 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
468 if (ansiseq)
469 tputs(ansiseq, 1, putch);
470 }
471}
472
473static void
474endcolor(int sig)
475{
476 tputs(ansi_coloff, 1, sig ? writech : putch);
477 tputs(attrs_off, 1, sig ? writech : putch);
478}
479
480static int
481colortype(mode_t mode)
482{
483 switch (mode & S_IFMT) {
484 case S_IFDIR:
485 if (mode & S_IWOTH)
486 if (mode & S_ISTXT)
487 printcolor(C_WSDIR);
488 else
489 printcolor(C_WDIR);
490 else
491 printcolor(C_DIR);
492 return (1);
493 case S_IFLNK:
494 printcolor(C_LNK);
495 return (1);
496 case S_IFSOCK:
497 printcolor(C_SOCK);
498 return (1);
499 case S_IFIFO:
500 printcolor(C_FIFO);
501 return (1);
502 case S_IFBLK:
503 printcolor(C_BLK);
504 return (1);
505 case S_IFCHR:
506 printcolor(C_CHR);
507 return (1);
20d719bf 508 default:;
984263bc
MD
509 }
510 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
511 if (mode & S_ISUID)
512 printcolor(C_SUID);
513 else if (mode & S_ISGID)
514 printcolor(C_SGID);
515 else
516 printcolor(C_EXEC);
517 return (1);
518 }
519 return (0);
520}
521
522void
523parsecolors(const char *cs)
524{
525 int i;
526 int j;
05695ef9 527 size_t len;
984263bc
MD
528 char c[2];
529 short legacy_warn = 0;
530
531 if (cs == NULL)
532 cs = ""; /* LSCOLORS not set */
533 len = strlen(cs);
534 for (i = 0; i < C_NUMCOLORS; i++) {
535 colors[i].bold = 0;
536
05695ef9 537 if (len <= 2 * (size_t)i) {
984263bc
MD
538 c[0] = defcolors[2 * i];
539 c[1] = defcolors[2 * i + 1];
540 } else {
541 c[0] = cs[2 * i];
542 c[1] = cs[2 * i + 1];
543 }
544 for (j = 0; j < 2; j++) {
545 /* Legacy colours used 0-7 */
546 if (c[j] >= '0' && c[j] <= '7') {
547 colors[i].num[j] = c[j] - '0';
548 if (!legacy_warn) {
b03b411e
JR
549 warnx("LSCOLORS should use "
550 "characters a-h instead of 0-9 ("
551 "see the manual page)\n");
984263bc
MD
552 }
553 legacy_warn = 1;
554 } else if (c[j] >= 'a' && c[j] <= 'h')
555 colors[i].num[j] = c[j] - 'a';
556 else if (c[j] >= 'A' && c[j] <= 'H') {
557 colors[i].num[j] = c[j] - 'A';
558 colors[i].bold = 1;
559 } else if (tolower((unsigned char)c[j] == 'x'))
560 colors[i].num[j] = -1;
561 else {
b03b411e
JR
562 warnx("invalid character '%c' in LSCOLORS"
563 " env var\n", c[j]);
984263bc
MD
564 colors[i].num[j] = -1;
565 }
566 }
567 }
568}
569
570void
571colorquit(int sig)
572{
573 endcolor(sig);
574
57fed2af
EN
575 signal(sig, SIG_DFL);
576 kill(getpid(), sig);
984263bc
MD
577}
578
579#endif /* COLORLS */
580
581static void
b534d61c 582printlink(const FTSENT *p)
984263bc
MD
583{
584 int lnklen;
585 char name[MAXPATHLEN + 1];
586 char path[MAXPATHLEN + 1];
587
588 if (p->fts_level == FTS_ROOTLEVEL)
57fed2af 589 snprintf(name, sizeof(name), "%s", p->fts_name);
984263bc 590 else
57fed2af 591 snprintf(name, sizeof(name),
984263bc
MD
592 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
593 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
57fed2af 594 fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
984263bc
MD
595 return;
596 }
597 path[lnklen] = '\0';
57fed2af
EN
598 printf(" -> ");
599 printname(path);
984263bc
MD
600}
601
602static void
603printsize(size_t width, off_t bytes)
604{
984263bc 605 if (f_humanval) {
cd07f2f6 606 char buf[5];
984263bc 607
cd07f2f6
JR
608 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
609 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1f0f7b35 610 printf("%5s ", buf);
984263bc 611 } else
76dc7fb4 612 printf("%*jd ", (int)width, (uintmax_t)bytes);
984263bc 613}