Deleted Added
full compact
ls.c (90115) ls.c (90150)
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

--- 31 unchanged lines hidden (view full) ---

40 The Regents of the University of California. All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94";
46#else
47static const char rcsid[] =
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

--- 31 unchanged lines hidden (view full) ---

40 The Regents of the University of California. All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94";
46#else
47static const char rcsid[] =
48 "$FreeBSD: head/bin/ls/ls.c 90115 2002-02-02 07:10:02Z imp $";
48 "$FreeBSD: head/bin/ls/ls.c 90150 2002-02-03 19:11:32Z markm $";
49#endif
50#endif /* not lint */
51
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <sys/ioctl.h>
55
56#include <dirent.h>

--- 29 unchanged lines hidden (view full) ---

86
87static void (*printfcn)(DISPLAY *);
88static int (*sortfcn)(const FTSENT *, const FTSENT *);
89
90long blocksize; /* block size units */
91int termwidth = 80; /* default terminal width */
92
93/* flags */
49#endif
50#endif /* not lint */
51
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <sys/ioctl.h>
55
56#include <dirent.h>

--- 29 unchanged lines hidden (view full) ---

86
87static void (*printfcn)(DISPLAY *);
88static int (*sortfcn)(const FTSENT *, const FTSENT *);
89
90long blocksize; /* block size units */
91int termwidth = 80; /* default terminal width */
92
93/* flags */
94int f_accesstime; /* use time of last access */
95int f_column; /* columnated format */
96int f_flags; /* show flags associated with a file */
97int f_humanval; /* show human-readable file sizes */
98int f_inode; /* print inode */
99int f_kblocks; /* print size in kilobytes */
100int f_listdir; /* list actual directory, not contents */
101int f_listdot; /* list files beginning with . */
102int f_longform; /* long listing format */
103int f_nonprint; /* show unprintables as ? */
104int f_nosort; /* don't sort output */
105int f_notabs; /* don't use tab-separated multi-col output */
106int f_numericonly; /* don't convert uid/gid to name */
107int f_octal; /* show unprintables as \xxx */
108int f_octal_escape; /* like f_octal but use C escapes if possible */
109int f_recursive; /* ls subdirectories also */
110int f_reversesort; /* reverse whatever sort is used */
111int f_sectime; /* print the real time for all files */
112int f_singlecol; /* use single column output */
113int f_size; /* list size in short listing */
114int f_statustime; /* use time of last mode change */
115int f_timesort; /* sort by time vice name */
116int f_type; /* add type character for non-regular files */
117int f_whiteout; /* show whiteout entries */
118int f_lomac; /* show LOMAC attributes */
94 int f_accesstime; /* use time of last access */
95 int f_flags; /* show flags associated with a file */
96 int f_humanval; /* show human-readable file sizes */
97 int f_inode; /* print inode */
98static int f_kblocks; /* print size in kilobytes */
99static int f_listdir; /* list actual directory, not contents */
100static int f_listdot; /* list files beginning with . */
101 int f_longform; /* long listing format */
102 int f_nonprint; /* show unprintables as ? */
103static int f_nosort; /* don't sort output */
104 int f_notabs; /* don't use tab-separated multi-col output */
105static int f_numericonly; /* don't convert uid/gid to name */
106 int f_octal; /* show unprintables as \xxx */
107 int f_octal_escape; /* like f_octal but use C escapes if possible */
108static int f_recursive; /* ls subdirectories also */
109static int f_reversesort; /* reverse whatever sort is used */
110 int f_sectime; /* print the real time for all files */
111static int f_singlecol; /* use single column output */
112 int f_size; /* list size in short listing */
113 int f_statustime; /* use time of last mode change */
114static int f_timesort; /* sort by time vice name */
115 int f_type; /* add type character for non-regular files */
116static int f_whiteout; /* show whiteout entries */
117 int f_lomac; /* show LOMAC attributes */
119#ifdef COLORLS
118#ifdef COLORLS
120int f_color; /* add type in color for non-regular files */
119 int f_color; /* add type in color for non-regular files */
121
122char *ansi_bgcol; /* ANSI sequence to set background colour */
123char *ansi_fgcol; /* ANSI sequence to set foreground colour */
124char *ansi_coloff; /* ANSI sequence to reset colours */
125char *attrs_off; /* ANSI sequence to turn off attributes */
126char *enter_bold; /* ANSI sequence to set color to bold mode */
127#endif
128
120
121char *ansi_bgcol; /* ANSI sequence to set background colour */
122char *ansi_fgcol; /* ANSI sequence to set foreground colour */
123char *ansi_coloff; /* ANSI sequence to reset colours */
124char *attrs_off; /* ANSI sequence to turn off attributes */
125char *enter_bold; /* ANSI sequence to set color to bold mode */
126#endif
127
129int rval;
128static int rval;
130
131int
132main(int argc, char *argv[])
133{
134 static char dot[] = ".", *dotav[] = {dot, NULL};
135 struct winsize win;
136 int ch, fts_options, notused;
137 char *p;

--- 8 unchanged lines hidden (view full) ---

146 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
147 if (isatty(STDOUT_FILENO)) {
148 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
149 !win.ws_col) {
150 if ((p = getenv("COLUMNS")) != NULL)
151 termwidth = atoi(p);
152 } else
153 termwidth = win.ws_col;
129
130int
131main(int argc, char *argv[])
132{
133 static char dot[] = ".", *dotav[] = {dot, NULL};
134 struct winsize win;
135 int ch, fts_options, notused;
136 char *p;

--- 8 unchanged lines hidden (view full) ---

145 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
146 if (isatty(STDOUT_FILENO)) {
147 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 ||
148 !win.ws_col) {
149 if ((p = getenv("COLUMNS")) != NULL)
150 termwidth = atoi(p);
151 } else
152 termwidth = win.ws_col;
154 f_column = f_nonprint = 1;
153 f_nonprint = 1;
155 } else {
156 f_singlecol = 1;
157 /* retrieve environment variable, in case of explicit -C */
154 } else {
155 f_singlecol = 1;
156 /* retrieve environment variable, in case of explicit -C */
158 if ((p = getenv("COLUMNS")))
157 p = getenv("COLUMNS");
158 if (p)
159 termwidth = atoi(p);
160 }
161
162 /* Root is -A automatically. */
163 if (!getuid())
164 f_listdot = 1;
165
166 fts_options = FTS_PHYSICAL;
167 while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfghiklnoqrstuw")) != -1) {
168 switch (ch) {
169 /*
170 * The -1, -C and -l options all override each other so shell
171 * aliasing works right.
172 */
173 case '1':
174 f_singlecol = 1;
159 termwidth = atoi(p);
160 }
161
162 /* Root is -A automatically. */
163 if (!getuid())
164 f_listdot = 1;
165
166 fts_options = FTS_PHYSICAL;
167 while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfghiklnoqrstuw")) != -1) {
168 switch (ch) {
169 /*
170 * The -1, -C and -l options all override each other so shell
171 * aliasing works right.
172 */
173 case '1':
174 f_singlecol = 1;
175 f_column = f_longform = 0;
175 f_longform = 0;
176 break;
177 case 'B':
178 f_nonprint = 0;
179 f_octal = 1;
180 f_octal_escape = 0;
181 break;
182 case 'C':
176 break;
177 case 'B':
178 f_nonprint = 0;
179 f_octal = 1;
180 f_octal_escape = 0;
181 break;
182 case 'C':
183 f_column = 1;
184 f_longform = f_singlecol = 0;
185 break;
186 case 'l':
187 f_longform = 1;
183 f_longform = f_singlecol = 0;
184 break;
185 case 'l':
186 f_longform = 1;
188 f_column = f_singlecol = 0;
187 f_singlecol = 0;
189 break;
190 /* The -c and -u options override each other. */
191 case 'c':
192 f_statustime = 1;
193 f_accesstime = 0;
194 break;
195 case 'u':
196 f_accesstime = 1;

--- 257 unchanged lines hidden (view full) ---

454 output = 1;
455 }
456 chp = fts_children(ftsp, ch_options);
457 display(p, chp);
458
459 if (!f_recursive && chp != NULL)
460 (void)fts_set(ftsp, p, FTS_SKIP);
461 break;
188 break;
189 /* The -c and -u options override each other. */
190 case 'c':
191 f_statustime = 1;
192 f_accesstime = 0;
193 break;
194 case 'u':
195 f_accesstime = 1;

--- 257 unchanged lines hidden (view full) ---

453 output = 1;
454 }
455 chp = fts_children(ftsp, ch_options);
456 display(p, chp);
457
458 if (!f_recursive && chp != NULL)
459 (void)fts_set(ftsp, p, FTS_SKIP);
460 break;
461 default:
462 }
463 if (errno)
464 err(1, "fts_read");
465}
466
467/*
468 * Display() takes a linked list of FTSENT structures and passes the list
469 * along with any other necessary information to the print function. P
470 * points to the parent directory of the display list.
471 */
472static void
473display(FTSENT *p, FTSENT *list)
474{
475 struct stat *sp;
476 DISPLAY d;
477 FTSENT *cur;
478 NAMES *np;
462 }
463 if (errno)
464 err(1, "fts_read");
465}
466
467/*
468 * Display() takes a linked list of FTSENT structures and passes the list
469 * along with any other necessary information to the print function. P
470 * points to the parent directory of the display list.
471 */
472static void
473display(FTSENT *p, FTSENT *list)
474{
475 struct stat *sp;
476 DISPLAY d;
477 FTSENT *cur;
478 NAMES *np;
479 u_quad_t maxsize;
480 u_long btotal, maxblock, maxinode, maxlen, maxnlink, maxlattr;
481 int bcfile, flen, glen, ulen, lattrlen, maxflags, maxgroup, maxuser;
479 off_t maxsize;
480 u_long btotal, lattrlen, maxblock, maxinode, maxlen, maxnlink, maxlattr;
481 int bcfile, maxflags;
482 gid_t maxgroup;
483 uid_t maxuser;
484 size_t flen, ulen, glen;
482 char *initmax;
483 int entries, needstats;
485 char *initmax;
486 int entries, needstats;
484 char *user, *group, *flags, *lattr;
487 char *user, *group, *flags, *lattr = NULL;
485 char buf[STRBUF_SIZEOF(u_quad_t) + 1];
486 char ngroup[STRBUF_SIZEOF(uid_t) + 1];
487 char nuser[STRBUF_SIZEOF(gid_t) + 1];
488
489 /*
490 * If list is NULL there are two possibilities: that the parent
491 * directory p has no children, or that fts_children() returned an
492 * error. We ignore the error case since it will be replicated
493 * on the next call to fts_read() on the post-order visit to the
494 * directory p, and will be signaled in traverse().
495 */
496 if (list == NULL)
497 return;
498
499 needstats = f_inode || f_longform || f_size;
500 flen = 0;
501 btotal = 0;
502 initmax = getenv("LS_COLWIDTHS");
503 /* Fields match -lios order. New ones should be added at the end. */
488 char buf[STRBUF_SIZEOF(u_quad_t) + 1];
489 char ngroup[STRBUF_SIZEOF(uid_t) + 1];
490 char nuser[STRBUF_SIZEOF(gid_t) + 1];
491
492 /*
493 * If list is NULL there are two possibilities: that the parent
494 * directory p has no children, or that fts_children() returned an
495 * error. We ignore the error case since it will be replicated
496 * on the next call to fts_read() on the post-order visit to the
497 * directory p, and will be signaled in traverse().
498 */
499 if (list == NULL)
500 return;
501
502 needstats = f_inode || f_longform || f_size;
503 flen = 0;
504 btotal = 0;
505 initmax = getenv("LS_COLWIDTHS");
506 /* Fields match -lios order. New ones should be added at the end. */
507 maxlattr = maxblock = maxinode = maxlen = maxnlink =
508 maxuser = maxgroup = maxflags = maxsize = 0;
504 if (initmax != NULL && *initmax != '\0') {
505 char *initmax2, *jinitmax;
506 int ninitmax;
507
508 /* Fill-in "::" as "0:0:0" for the sake of scanf. */
509 jinitmax = initmax2 = malloc(strlen(initmax) * 2 + 2);
510 if (jinitmax == NULL)
511 err(1, NULL);

--- 10 unchanged lines hidden (view full) ---

522 *initmax2++ = initmax[0];
523 initmax2[1] = '\0';
524 }
525 }
526 if (initmax2[-1] == ':')
527 strcpy(initmax2, "0");
528
529 ninitmax = sscanf(jinitmax,
509 if (initmax != NULL && *initmax != '\0') {
510 char *initmax2, *jinitmax;
511 int ninitmax;
512
513 /* Fill-in "::" as "0:0:0" for the sake of scanf. */
514 jinitmax = initmax2 = malloc(strlen(initmax) * 2 + 2);
515 if (jinitmax == NULL)
516 err(1, NULL);

--- 10 unchanged lines hidden (view full) ---

527 *initmax2++ = initmax[0];
528 initmax2[1] = '\0';
529 }
530 }
531 if (initmax2[-1] == ':')
532 strcpy(initmax2, "0");
533
534 ninitmax = sscanf(jinitmax,
530 " %lu : %lu : %lu : %i : %i : %i : %qu : %lu : %lu ",
535 " %lu : %lu : %lu : %i : %i : %i : %llu : %lu : %lu ",
531 &maxinode, &maxblock, &maxnlink, &maxuser,
532 &maxgroup, &maxflags, &maxsize, &maxlen, &maxlattr);
533 f_notabs = 1;
534 switch (ninitmax) {
535 case 0:
536 maxinode = 0;
536 &maxinode, &maxblock, &maxnlink, &maxuser,
537 &maxgroup, &maxflags, &maxsize, &maxlen, &maxlattr);
538 f_notabs = 1;
539 switch (ninitmax) {
540 case 0:
541 maxinode = 0;
542 /* fall through */
537 case 1:
538 maxblock = 0;
543 case 1:
544 maxblock = 0;
545 /* fall through */
539 case 2:
540 maxnlink = 0;
546 case 2:
547 maxnlink = 0;
548 /* fall through */
541 case 3:
542 maxuser = 0;
549 case 3:
550 maxuser = 0;
551 /* fall through */
543 case 4:
544 maxgroup = 0;
552 case 4:
553 maxgroup = 0;
554 /* fall through */
545 case 5:
546 maxflags = 0;
555 case 5:
556 maxflags = 0;
557 /* fall through */
547 case 6:
548 maxsize = 0;
558 case 6:
559 maxsize = 0;
560 /* fall through */
549 case 7:
550 maxlen = 0;
561 case 7:
562 maxlen = 0;
563 /* fall through */
551 case 8:
552 maxlattr = 0;
564 case 8:
565 maxlattr = 0;
566 /* fall through */
553#ifdef COLORLS
554 if (!f_color)
555#endif
556 f_notabs = 0;
567#ifdef COLORLS
568 if (!f_color)
569#endif
570 f_notabs = 0;
571 /* fall through */
572 default:
557 }
558 maxinode = makenines(maxinode);
559 maxblock = makenines(maxblock);
560 maxnlink = makenines(maxnlink);
561 maxsize = makenines(maxsize);
573 }
574 maxinode = makenines(maxinode);
575 maxblock = makenines(maxblock);
576 maxnlink = makenines(maxnlink);
577 maxsize = makenines(maxsize);
562 } else if (initmax == NULL || *initmax == '\0')
563 maxlattr = maxblock = maxinode = maxlen = maxnlink =
564 maxuser = maxgroup = maxflags = maxsize = 0;
578 }
565 if (f_lomac)
566 lomac_start();
567 bcfile = 0;
568 flags = NULL;
569 for (cur = list, entries = 0; cur; cur = cur->fts_link) {
570 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
571 warnx("%s: %s",
572 cur->fts_name, strerror(cur->fts_errno));

--- 57 unchanged lines hidden (view full) ---

630 if (f_flags) {
631 flags = fflagstostr(sp->st_flags);
632 if (flags != NULL && *flags == '\0') {
633 free(flags);
634 flags = strdup("-");
635 }
636 if (flags == NULL)
637 err(1, NULL);
579 if (f_lomac)
580 lomac_start();
581 bcfile = 0;
582 flags = NULL;
583 for (cur = list, entries = 0; cur; cur = cur->fts_link) {
584 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
585 warnx("%s: %s",
586 cur->fts_name, strerror(cur->fts_errno));

--- 57 unchanged lines hidden (view full) ---

644 if (f_flags) {
645 flags = fflagstostr(sp->st_flags);
646 if (flags != NULL && *flags == '\0') {
647 free(flags);
648 flags = strdup("-");
649 }
650 if (flags == NULL)
651 err(1, NULL);
638 if ((flen = strlen(flags)) > maxflags)
652 flen = strlen(flags);
653 if (flen > (size_t)maxflags)
639 maxflags = flen;
640 } else
641 flen = 0;
642 lattr = NULL;
643 if (f_lomac) {
644 lattr = get_lattr(cur);
645 lattrlen = strlen(lattr);
646 if (lattrlen > maxlattr)

--- 115 unchanged lines hidden ---
654 maxflags = flen;
655 } else
656 flen = 0;
657 lattr = NULL;
658 if (f_lomac) {
659 lattr = get_lattr(cur);
660 lattrlen = strlen(lattr);
661 if (lattrlen > maxlattr)

--- 115 unchanged lines hidden ---