~vcs-imports/busybox/trunk

« back to all changes in this revision

Viewing changes to coreutils/ls.c

  • Committer: Denys Vlasenko
  • Author(s): Christian Franke
  • Date: 2023-11-13 10:32:35 UTC
  • Revision ID: git-v1:a63b60bdd6fa26b867c80d44074118babbae7ffd
Cygwin: regenerate defconfig

Signed-off-by: Christian Franke <christian.franke@t-online.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
109
109
//usage:#define ls_full_usage "\n\n"
110
110
//usage:       "List directory contents\n"
111
111
//usage:     "\n        -1      One column output"
112
 
//usage:     "\n        -a      Include entries which start with ."
 
112
//usage:     "\n        -a      Include names starting with ."
113
113
//usage:     "\n        -A      Like -a, but exclude . and .."
114
114
////usage:     "\n      -C      List by columns" - don't show, this is a default anyway
115
115
//usage:     "\n        -x      List by lines"
116
 
//usage:     "\n        -d      List directory entries instead of contents"
 
116
//usage:     "\n        -d      List directory names, not contents"
117
117
//usage:        IF_FEATURE_LS_FOLLOWLINKS(
118
118
//usage:     "\n        -L      Follow symlinks"
119
119
//usage:     "\n        -H      Follow symlinks on command line"
122
122
//usage:     "\n        -R      Recurse"
123
123
//usage:        )
124
124
//usage:        IF_FEATURE_LS_FILETYPES(
125
 
//usage:     "\n        -p      Append / to dir entries"
126
 
//usage:     "\n        -F      Append indicator (one of */=@|) to entries"
 
125
//usage:     "\n        -p      Append / to directory names"
 
126
//usage:     "\n        -F      Append indicator (one of */=@|) to names"
127
127
//usage:        )
128
 
//usage:     "\n        -l      Long listing format"
 
128
//usage:     "\n        -l      Long format"
129
129
//usage:     "\n        -i      List inode numbers"
130
130
//usage:     "\n        -n      List numeric UIDs and GIDs instead of names"
131
131
//usage:     "\n        -s      List allocated blocks"
134
134
//usage:     "\n        -lu     List atime"
135
135
//usage:        )
136
136
//usage:        IF_FEATURE_LS_TIMESTAMPS(IF_LONG_OPTS(
137
 
//usage:     "\n        --full-time     List full date and time"
 
137
//usage:     "\n        --full-time     List full date/time"
138
138
//usage:        ))
139
139
//usage:        IF_FEATURE_HUMAN_READABLE(
140
140
//usage:     "\n        -h      Human readable sizes (1K 243M 2G)"
160
160
//usage:     "\n        -w N    Format N columns wide"
161
161
//usage:        )
162
162
//usage:        IF_FEATURE_LS_COLOR(
163
 
//usage:     "\n        --color[={always,never,auto}]   Control coloring"
 
163
//usage:     "\n        --color[={always,never,auto}]"
164
164
//usage:        )
165
165
 
166
166
#include "libbb.h"
187
187
 
188
188
 
189
189
enum {
190
 
TERMINAL_WIDTH  = 80,           /* use 79 if terminal has linefold bug */
 
190
TERMINAL_WIDTH  = 80, /* use 79 if terminal has linefold bug */
191
191
 
192
192
SPLIT_FILE      = 0,
193
193
SPLIT_DIR       = 1,
298
298
// but there are invisible fields as well
299
299
// (such as nanosecond-resolution timespamps)
300
300
// and padding, which we also don't want to store.
301
 
// We also can pre-parse dev_t dn_rdev (in glibc, it's huge).
 
301
// We also pre-parse dev_t dn_rdev (in glibc, it's huge).
302
302
// On 32-bit uclibc: dnode size went from 112 to 84 bytes.
303
303
//
304
304
        /* Same names as in struct stat, but with dn_ instead of st_ pfx: */
453
453
        name = printable_string2(&uni_stat, name);
454
454
 
455
455
        if (!(option_mask32 & OPT_Q)) {
456
 
                fputs(name, stdout);
 
456
                fputs_stdout(name);
457
457
                return uni_stat.unicode_width;
458
458
        }
459
459
 
498
498
 
499
499
        if (opt & OPT_i) /* show inode# */
500
500
                column += printf("%7llu ", (long long) dn->dn_ino);
501
 
//TODO: -h should affect -s too:
502
 
        if (opt & OPT_s) /* show allocated blocks */
503
 
                column += printf("%6"OFF_FMT"u ", (off_t) (dn->dn_blocks >> 1));
 
501
        if (opt & OPT_s) { /* show allocated blocks */
 
502
                if (opt & OPT_h) {
 
503
                        column += printf("%"HUMAN_READABLE_MAX_WIDTH_STR"s ",
 
504
                                /* print size, show one fractional, use suffixes */
 
505
                                make_human_readable_str((off_t)dn->dn_blocks << 9, 1, 0)
 
506
                        );
 
507
                } else {
 
508
                        column += printf("%6"OFF_FMT"u ", (off_t)(dn->dn_blocks >> 1));
 
509
                }
 
510
        }
504
511
        if (opt & OPT_l) {
505
512
                /* long listing: show mode */
506
 
                column += printf("%-10s ", (char *) bb_mode_string(dn->dn_mode));
 
513
                char modestr[12];
 
514
                column += printf("%-10s ", bb_mode_string(modestr, dn->dn_mode));
507
515
                /* long listing: show number of links */
508
516
                column += printf("%4lu ", (long) dn->dn_nlink);
509
517
                /* long listing: show user/group */
1145
1153
 
1146
1154
#if ENABLE_FEATURE_LS_COLOR
1147
1155
        /* set G_show_color = 1/0 */
1148
 
        if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) {
 
1156
        if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && !is_TERM_dumb()) {
1149
1157
                char *p = getenv("LS_COLORS");
1150
1158
                /* LS_COLORS is unset, or (not empty && not "none") ? */
1151
 
                if (!p || (p[0] && strcmp(p, "none") != 0))
1152
 
                        G_show_color = 1;
 
1159
                if (!p || (p[0] && strcmp(p, "none") != 0)) {
 
1160
                        if (isatty(STDOUT_FILENO)) {
 
1161
                                /* check isatty() last because it's expensive (syscall) */
 
1162
                                G_show_color = 1;
 
1163
                        }
 
1164
                }
1153
1165
        }
1154
1166
        if (opt & OPT_color) {
1155
1167
                if (color_opt[0] == 'n')
1158
1170
                case 3:
1159
1171
                case 4:
1160
1172
                case 5:
1161
 
                        if (isatty(STDOUT_FILENO)) {
 
1173
                        if (!is_TERM_dumb() && isatty(STDOUT_FILENO)) {
1162
1174
                case 0:
1163
1175
                case 1:
1164
1176
                case 2: