1 /* 'dir', 'vdir' and 'ls' directory listing programs for GNU.
2 Copyright (C) 1985-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* If ls_mode is LS_MULTI_COL,
18 the multi-column format is the default regardless
19 of the type of output device.
20 This is for the 'dir' program.
22 If ls_mode is LS_LONG_FORMAT,
23 the long format is the default regardless of the
24 type of output device.
25 This is for the 'vdir' program.
28 the output format depends on whether the output
30 This is for the 'ls' program. */
32 /* Written by Richard Stallman and David MacKenzie. */
34 /* Color support by Peter Anvin <Peter.Anvin@linux.org> and Dennis
35 Flaherty <dennisf@denix.elk.miles.com> based on original patches by
36 Greg Lee <lee@uhunix.uhcc.hawaii.edu>. */
40 #include <sys/types.h>
46 #include <sys/ioctl.h>
48 #ifdef WINSIZE_IN_PTEM
49 # include <sys/stream.h>
50 # include <sys/ptem.h>
60 #if HAVE_LANGINFO_CODESET
61 # include <langinfo.h>
64 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
67 # define SA_NOCLDSTOP 0
68 # define sigprocmask(How, Set, Oset) /* empty */
70 # if ! HAVE_SIGINTERRUPT
71 # define siginterrupt(sig, flag) /* empty */
75 /* NonStop circa 2011 lacks both SA_RESTART and siginterrupt, so don't
76 restart syscalls after a signal handler fires. This may cause
77 colors to get messed up on the screen if 'ls' is interrupted, but
78 that's the best we can do on such a platform. */
89 #include "c-strcase.h"
91 #include "filenamecat.h"
92 #include "hard-locale.h"
96 #include "filevercmp.h"
103 #include "stat-size.h"
104 #include "stat-time.h"
105 #include "strftime.h"
106 #include "xdectoint.h"
108 #include "xstrtol-error.h"
109 #include "areadlink.h"
110 #include "dircolors.h"
111 #include "xgethostname.h"
113 #include "canonicalize.h"
116 /* Include <sys/capability.h> last to avoid a clash of <sys/types.h>
117 include guards with some premature versions of libcap.
118 For more details, see <https://bugzilla.redhat.com/483548>. */
120 # include <sys/capability.h>
123 #if HAVE_LINUX_XATTR_H
124 # include <linux/xattr.h>
127 #define PROGRAM_NAME (ls_mode == LS_LS ? "ls" \
128 : (ls_mode == LS_MULTI_COL \
132 proper_name ("Richard M. Stallman"), \
133 proper_name ("David MacKenzie")
135 #define obstack_chunk_alloc malloc
136 #define obstack_chunk_free free
138 /* Unix-based readdir implementations have historically returned a dirent.d_ino
139 value that is sometimes not equal to the stat-obtained st_ino value for
140 that same entry. This error occurs for a readdir entry that refers
141 to a mount point. readdir's error is to return the inode number of
142 the underlying directory -- one that typically cannot be stat'ed, as
143 long as a file system is mounted on that directory. RELIABLE_D_INO
144 encapsulates whether we can use the more efficient approach of relying
145 on readdir-supplied d_ino values, or whether we must incur the cost of
146 calling stat or lstat to obtain each guaranteed-valid inode number. */
148 #ifndef READDIR_LIES_ABOUT_MOUNTPOINT_D_INO
149 # define READDIR_LIES_ABOUT_MOUNTPOINT_D_INO 1
152 #if READDIR_LIES_ABOUT_MOUNTPOINT_D_INO
153 # define RELIABLE_D_INO(dp) NOT_AN_INODE_NUMBER
155 # define RELIABLE_D_INO(dp) D_INO (dp)
158 #if ! HAVE_STRUCT_STAT_ST_AUTHOR
159 # define st_author st_uid
176 /* Display letters and indicators for each filetype.
177 Keep these in sync with enum filetype. */
178 static char const filetype_letter
[] = "?pcdb-lswd";
180 /* Ensure that filetype and filetype_letter have the same
181 number of elements. */
182 static_assert (sizeof filetype_letter
- 1 == arg_directory
+ 1);
184 #define FILETYPE_INDICATORS \
186 C_ORPHAN, C_FIFO, C_CHR, C_DIR, C_BLK, C_FILE, \
187 C_LINK, C_SOCK, C_FILE, C_DIR \
193 ACL_T_LSM_CONTEXT_ONLY
,
202 /* For symbolic link, name of the file linked to, otherwise zero. */
205 /* For terminal hyperlinks. */
210 enum filetype filetype
;
212 /* For symbolic link and long listing, st_mode of file linked to, otherwise
216 /* security context. */
221 /* For symbolic link and color printing, true if linked-to file
222 exists, otherwise false. */
225 /* For long listings, true if the file has an access control list,
226 or a security context. */
227 enum acl_type acl_type
;
229 /* For color listings, true if a regular file has capability info. */
232 /* Whether file name needs quoting. tri-state with -1 == unknown. */
235 /* Cached screen width (including quoting). */
239 #define LEN_STR_PAIR(s) sizeof (s) - 1, s
241 /* Null is a valid character in a color indicator (think about Epson
242 printers, for example) so we have to use a length/buffer string
247 size_t len
; /* Number of bytes */
248 char const *string
; /* Pointer to the same */
252 # define tcgetpgrp(Fd) 0
255 static size_t quote_name (char const *name
,
256 struct quoting_options
const *options
,
257 int needs_general_quoting
,
258 const struct bin_str
*color
,
259 bool allow_pad
, struct obstack
*stack
,
260 char const *absolute_name
);
261 static size_t quote_name_buf (char **inbuf
, size_t bufsize
, char *name
,
262 struct quoting_options
const *options
,
263 int needs_general_quoting
, size_t *width
,
265 static int decode_switches (int argc
, char **argv
);
266 static bool file_ignored (char const *name
);
267 static uintmax_t gobble_file (char const *name
, enum filetype type
,
268 ino_t inode
, bool command_line_arg
,
269 char const *dirname
);
270 static const struct bin_str
* get_color_indicator (const struct fileinfo
*f
,
271 bool symlink_target
);
272 static bool print_color_indicator (const struct bin_str
*ind
);
273 static void put_indicator (const struct bin_str
*ind
);
274 static void add_ignore_pattern (char const *pattern
);
275 static void attach (char *dest
, char const *dirname
, char const *name
);
276 static void clear_files (void);
277 static void extract_dirs_from_files (char const *dirname
,
278 bool command_line_arg
);
279 static void get_link_name (char const *filename
, struct fileinfo
*f
,
280 bool command_line_arg
);
281 static void indent (size_t from
, size_t to
);
282 static size_t calculate_columns (bool by_columns
);
283 static void print_current_files (void);
284 static void print_dir (char const *name
, char const *realname
,
285 bool command_line_arg
);
286 static size_t print_file_name_and_frills (const struct fileinfo
*f
,
288 static void print_horizontal (void);
289 static int format_user_width (uid_t u
);
290 static int format_group_width (gid_t g
);
291 static void print_long_format (const struct fileinfo
*f
);
292 static void print_many_per_line (void);
293 static size_t print_name_with_quoting (const struct fileinfo
*f
,
295 struct obstack
*stack
,
297 static void prep_non_filename_text (void);
298 static bool print_type_indicator (bool stat_ok
, mode_t mode
,
300 static void print_with_separator (char sep
);
301 static void queue_directory (char const *name
, char const *realname
,
302 bool command_line_arg
);
303 static void sort_files (void);
304 static void parse_ls_color (void);
306 static int getenv_quoting_style (void);
308 static size_t quote_name_width (char const *name
,
309 struct quoting_options
const *options
,
310 int needs_general_quoting
);
312 /* Initial size of hash table.
313 Most hierarchies are likely to be shallower than this. */
314 enum { INITIAL_TABLE_SIZE
= 30 };
316 /* The set of 'active' directories, from the current command-line argument
317 to the level in the hierarchy at which files are being listed.
318 A directory is represented by its device and inode numbers (struct dev_ino).
319 A directory is added to this set when ls begins listing it or its
320 entries, and it is removed from the set just after ls has finished
321 processing it. This set is used solely to detect loops, e.g., with
322 mkdir loop; cd loop; ln -s ../loop sub; ls -RL */
323 static Hash_table
*active_dir_set
;
325 #define LOOP_DETECT (!!active_dir_set)
327 /* The table of files in the current directory:
329 'cwd_file' points to a vector of 'struct fileinfo', one per file.
330 'cwd_n_alloc' is the number of elements space has been allocated for.
331 'cwd_n_used' is the number actually in use. */
333 /* Address of block containing the files that are described. */
334 static struct fileinfo
*cwd_file
;
336 /* Length of block that 'cwd_file' points to, measured in files. */
337 static size_t cwd_n_alloc
;
339 /* Index of first unused slot in 'cwd_file'. */
340 static size_t cwd_n_used
;
342 /* Whether files needs may need padding due to quoting. */
343 static bool cwd_some_quoted
;
345 /* Whether quoting style _may_ add outer quotes,
346 and whether aligning those is useful. */
347 static bool align_variable_outer_quotes
;
349 /* Vector of pointers to files, in proper sorted order, and the number
350 of entries allocated for it. */
351 static void **sorted_file
;
352 static size_t sorted_file_alloc
;
354 /* When true, in a color listing, color each symlink name according to the
355 type of file it points to. Otherwise, color them according to the 'ln'
356 directive in LS_COLORS. Dangling (orphan) symlinks are treated specially,
357 regardless. This is set when 'ln=target' appears in LS_COLORS. */
359 static bool color_symlink_as_referent
;
361 static char const *hostname
;
363 /* Mode of appropriate file for coloring. */
365 file_or_link_mode (struct fileinfo
const *file
)
367 return (color_symlink_as_referent
&& file
->linkok
368 ? file
->linkmode
: file
->stat
.st_mode
);
372 /* Record of one pending directory waiting to be listed. */
377 /* If the directory is actually the file pointed to by a symbolic link we
378 were told to list, 'realname' will contain the name of the symbolic
379 link, otherwise zero. */
381 bool command_line_arg
;
382 struct pending
*next
;
385 static struct pending
*pending_dirs
;
387 /* Current time in seconds and nanoseconds since 1970, updated as
388 needed when deciding whether a file is recent. */
390 static struct timespec current_time
;
392 static bool print_scontext
;
393 static char UNKNOWN_SECURITY_CONTEXT
[] = "?";
395 /* Whether any of the files has an ACL. This affects the width of the
398 static bool any_has_acl
;
400 /* The number of columns to use for columns containing inode numbers,
401 block sizes, link counts, owners, groups, authors, major device
402 numbers, minor device numbers, and file sizes, respectively. */
404 static int inode_number_width
;
405 static int block_size_width
;
406 static int nlink_width
;
407 static int scontext_width
;
408 static int owner_width
;
409 static int group_width
;
410 static int author_width
;
411 static int major_device_number_width
;
412 static int minor_device_number_width
;
413 static int file_size_width
;
417 /* long_format for lots of info, one per line.
418 one_per_line for just names, one per line.
419 many_per_line for just names, many per line, sorted vertically.
420 horizontal for just names, many per line, sorted horizontally.
421 with_commas for just names, many per line, separated by commas.
423 -l (and other options that imply -l), -1, -C, -x and -m control
428 long_format
, /* -l and other options that imply -l */
429 one_per_line
, /* -1 */
430 many_per_line
, /* -C */
435 static enum format format
;
437 /* 'full-iso' uses full ISO-style dates and times. 'long-iso' uses longer
438 ISO-style timestamps, though shorter than 'full-iso'. 'iso' uses shorter
439 ISO-style timestamps. 'locale' uses locale-dependent timestamps. */
442 full_iso_time_style
, /* --time-style=full-iso */
443 long_iso_time_style
, /* --time-style=long-iso */
444 iso_time_style
, /* --time-style=iso */
445 locale_time_style
/* --time-style=locale */
448 static char const *const time_style_args
[] =
450 "full-iso", "long-iso", "iso", "locale", nullptr
452 static enum time_style
const time_style_types
[] =
454 full_iso_time_style
, long_iso_time_style
, iso_time_style
,
457 ARGMATCH_VERIFY (time_style_args
, time_style_types
);
459 /* Type of time to print or sort by. Controlled by -c and -u.
460 The values of each item of this enum are important since they are
461 used as indices in the sort functions array (see sort_files()). */
465 time_mtime
= 0, /* default */
468 time_btime
, /* birth time */
469 time_numtypes
/* the number of elements of this enum */
472 static enum time_type time_type
;
473 static bool explicit_time
;
475 /* The file characteristic to sort by. Controlled by -t, -S, -U, -X, -v.
476 The values of each item of this enum are important since they are
477 used as indices in the sort functions array (see sort_files()). */
481 sort_name
= 0, /* default */
482 sort_extension
, /* -X */
485 sort_version
, /* -v */
486 sort_time
, /* -t; must be second to last */
487 sort_none
, /* -U; must be last */
488 sort_numtypes
/* the number of elements of this enum */
491 static enum sort_type sort_type
;
493 /* Direction of sort.
494 false means highest first if numeric,
495 lowest first if alphabetic;
496 these are the defaults.
497 true means the opposite order in each case. -r */
499 static bool sort_reverse
;
501 /* True means to display owner information. -g turns this off. */
503 static bool print_owner
= true;
505 /* True means to display author information. */
507 static bool print_author
;
509 /* True means to display group information. -G and -o turn this off. */
511 static bool print_group
= true;
513 /* True means print the user and group id's as numbers rather
516 static bool numeric_ids
;
518 /* True means mention the size in blocks of each file. -s */
520 static bool print_block_size
;
522 /* Human-readable options for output, when printing block counts. */
523 static int human_output_opts
;
525 /* The units to use when printing block counts. */
526 static uintmax_t output_block_size
;
528 /* Likewise, but for file sizes. */
529 static int file_human_output_opts
;
530 static uintmax_t file_output_block_size
= 1;
532 /* Follow the output with a special string. Using this format,
533 Emacs' dired mode starts up twice as fast, and can handle all
534 strange characters in file names. */
537 /* 'none' means don't mention the type of files.
538 'slash' means mention directories only, with a '/'.
539 'file_type' means mention file types.
540 'classify' means mention file types and mark executables.
542 Controlled by -F, -p, and --indicator-style. */
546 none
= 0, /* --indicator-style=none (default) */
547 slash
, /* -p, --indicator-style=slash */
548 file_type
, /* --indicator-style=file-type */
549 classify
/* -F, --indicator-style=classify */
552 static enum indicator_style indicator_style
;
554 /* Names of indicator styles. */
555 static char const *const indicator_style_args
[] =
557 "none", "slash", "file-type", "classify", nullptr
559 static enum indicator_style
const indicator_style_types
[] =
561 none
, slash
, file_type
, classify
563 ARGMATCH_VERIFY (indicator_style_args
, indicator_style_types
);
565 /* True means use colors to mark types. Also define the different
566 colors as well as the stuff for the LS_COLORS environment variable.
567 The LS_COLORS variable is now in a termcap-like format. */
569 static bool print_with_color
;
571 static bool print_hyperlink
;
573 /* Whether we used any colors in the output so far. If so, we will
574 need to restore the default color later. If not, we will need to
575 call prep_non_filename_text before using color for the first time. */
577 static bool used_color
= false;
581 when_never
, /* 0: default or --color=never */
582 when_always
, /* 1: --color=always */
583 when_if_tty
/* 2: --color=tty */
586 enum Dereference_symlink
588 DEREF_UNDEFINED
= 0, /* default */
590 DEREF_COMMAND_LINE_ARGUMENTS
, /* -H */
591 DEREF_COMMAND_LINE_SYMLINK_TO_DIR
, /* the default, in certain cases */
592 DEREF_ALWAYS
/* -L */
597 C_LEFT
, C_RIGHT
, C_END
, C_RESET
, C_NORM
, C_FILE
, C_DIR
, C_LINK
,
599 C_BLK
, C_CHR
, C_MISSING
, C_ORPHAN
, C_EXEC
, C_DOOR
, C_SETUID
, C_SETGID
,
600 C_STICKY
, C_OTHER_WRITABLE
, C_STICKY_OTHER_WRITABLE
, C_CAP
, C_MULTIHARDLINK
,
604 static char const *const indicator_name
[]=
606 "lc", "rc", "ec", "rs", "no", "fi", "di", "ln", "pi", "so",
607 "bd", "cd", "mi", "or", "ex", "do", "su", "sg", "st",
608 "ow", "tw", "ca", "mh", "cl", nullptr
611 struct color_ext_type
613 struct bin_str ext
; /* The extension we're looking for */
614 struct bin_str seq
; /* The sequence to output when we do */
615 bool exact_match
; /* Whether to compare case insensitively */
616 struct color_ext_type
*next
; /* Next in list */
619 static struct bin_str color_indicator
[] =
621 { LEN_STR_PAIR ("\033[") }, /* lc: Left of color sequence */
622 { LEN_STR_PAIR ("m") }, /* rc: Right of color sequence */
623 { 0, nullptr }, /* ec: End color (replaces lc+rs+rc) */
624 { LEN_STR_PAIR ("0") }, /* rs: Reset to ordinary colors */
625 { 0, nullptr }, /* no: Normal */
626 { 0, nullptr }, /* fi: File: default */
627 { LEN_STR_PAIR ("01;34") }, /* di: Directory: bright blue */
628 { LEN_STR_PAIR ("01;36") }, /* ln: Symlink: bright cyan */
629 { LEN_STR_PAIR ("33") }, /* pi: Pipe: yellow/brown */
630 { LEN_STR_PAIR ("01;35") }, /* so: Socket: bright magenta */
631 { LEN_STR_PAIR ("01;33") }, /* bd: Block device: bright yellow */
632 { LEN_STR_PAIR ("01;33") }, /* cd: Char device: bright yellow */
633 { 0, nullptr }, /* mi: Missing file: undefined */
634 { 0, nullptr }, /* or: Orphaned symlink: undefined */
635 { LEN_STR_PAIR ("01;32") }, /* ex: Executable: bright green */
636 { LEN_STR_PAIR ("01;35") }, /* do: Door: bright magenta */
637 { LEN_STR_PAIR ("37;41") }, /* su: setuid: white on red */
638 { LEN_STR_PAIR ("30;43") }, /* sg: setgid: black on yellow */
639 { LEN_STR_PAIR ("37;44") }, /* st: sticky: black on blue */
640 { LEN_STR_PAIR ("34;42") }, /* ow: other-writable: blue on green */
641 { LEN_STR_PAIR ("30;42") }, /* tw: ow w/ sticky: black on green */
642 { 0, nullptr }, /* ca: disabled by default */
643 { 0, nullptr }, /* mh: disabled by default */
644 { LEN_STR_PAIR ("\033[K") }, /* cl: clear to end of line */
647 /* A list mapping file extensions to corresponding display sequence. */
648 static struct color_ext_type
*color_ext_list
= nullptr;
650 /* Buffer for color sequences */
651 static char *color_buf
;
653 /* True means to check for orphaned symbolic link, for displaying
654 colors, or to group symlink to directories with other dirs. */
656 static bool check_symlink_mode
;
658 /* True means mention the inode number of each file. -i */
660 static bool print_inode
;
662 /* What to do with symbolic links. Affected by -d, -F, -H, -l (and
663 other options that imply -l), and -L. */
665 static enum Dereference_symlink dereference
;
667 /* True means when a directory is found, display info on its
670 static bool recursive
;
672 /* True means when an argument is a directory name, display info
675 static bool immediate_dirs
;
677 /* True means that directories are grouped before files. */
679 static bool directories_first
;
681 /* Which files to ignore. */
685 /* Ignore files whose names start with '.', and files specified by
686 --hide and --ignore. */
689 /* Ignore '.', '..', and files specified by --ignore. */
690 IGNORE_DOT_AND_DOTDOT
,
692 /* Ignore only files specified by --ignore. */
696 /* A linked list of shell-style globbing patterns. If a non-argument
697 file name matches any of these patterns, it is ignored.
698 Controlled by -I. Multiple -I options accumulate.
699 The -B option adds '*~' and '.*~' to this list. */
701 struct ignore_pattern
704 struct ignore_pattern
*next
;
707 static struct ignore_pattern
*ignore_patterns
;
709 /* Similar to IGNORE_PATTERNS, except that -a or -A causes this
710 variable itself to be ignored. */
711 static struct ignore_pattern
*hide_patterns
;
713 /* True means output nongraphic chars in file names as '?'.
714 (-q, --hide-control-chars)
715 qmark_funny_chars and the quoting style (-Q, --quoting-style=WORD) are
716 independent. The algorithm is: first, obey the quoting style to get a
717 string representing the file name; then, if qmark_funny_chars is set,
718 replace all nonprintable chars in that string with '?'. It's necessary
719 to replace nonprintable chars even in quoted strings, because we don't
720 want to mess up the terminal if control chars get sent to it, and some
721 quoting methods pass through control chars as-is. */
722 static bool qmark_funny_chars
;
724 /* Quoting options for file and dir name output. */
726 static struct quoting_options
*filename_quoting_options
;
727 static struct quoting_options
*dirname_quoting_options
;
729 /* The number of chars per hardware tab stop. Setting this to zero
730 inhibits the use of TAB characters for separating columns. -T */
731 static size_t tabsize
;
733 /* True means print each directory name before listing it. */
735 static bool print_dir_name
;
737 /* The line length to use for breaking lines in many-per-line format.
738 Can be set with -w. If zero, there is no limit. */
740 static size_t line_length
;
742 /* The local time zone rules, as per the TZ environment variable. */
744 static timezone_t localtz
;
746 /* If true, the file listing format requires that stat be called on
749 static bool format_needs_stat
;
751 /* Similar to 'format_needs_stat', but set if only the file type is
754 static bool format_needs_type
;
756 /* An arbitrary limit on the number of bytes in a printed timestamp.
757 This is set to a relatively small value to avoid the need to worry
758 about denial-of-service attacks on servers that run "ls" on behalf
759 of remote clients. 1000 bytes should be enough for any practical
762 enum { TIME_STAMP_LEN_MAXIMUM
= MAX (1000, INT_STRLEN_BOUND (time_t)) };
764 /* strftime formats for non-recent and recent files, respectively, in
767 static char const *long_time_format
[2] =
769 /* strftime format for non-recent files (older than 6 months), in
770 -l output. This should contain the year, month and day (at
771 least), in an order that is understood by people in your
772 locale's territory. Please try to keep the number of used
773 screen columns small, because many people work in windows with
774 only 80 columns. But make this as wide as the other string
775 below, for recent files. */
776 /* TRANSLATORS: ls output needs to be aligned for ease of reading,
777 so be wary of using variable width fields from the locale.
778 Note %b is handled specially by ls and aligned correctly.
779 Note also that specifying a width as in %5b is erroneous as strftime
780 will count bytes rather than characters in multibyte locales. */
782 /* strftime format for recent files (younger than 6 months), in -l
783 output. This should contain the month, day and time (at
784 least), in an order that is understood by people in your
785 locale's territory. Please try to keep the number of used
786 screen columns small, because many people work in windows with
787 only 80 columns. But make this as wide as the other string
788 above, for non-recent files. */
789 /* TRANSLATORS: ls output needs to be aligned for ease of reading,
790 so be wary of using variable width fields from the locale.
791 Note %b is handled specially by ls and aligned correctly.
792 Note also that specifying a width as in %5b is erroneous as strftime
793 will count bytes rather than characters in multibyte locales. */
797 /* The set of signals that are caught. */
799 static sigset_t caught_signals
;
801 /* If nonzero, the value of the pending fatal signal. */
803 static sig_atomic_t volatile interrupt_signal
;
805 /* A count of the number of pending stop signals that have been received. */
807 static sig_atomic_t volatile stop_signal_count
;
809 /* Desired exit status. */
811 static int exit_status
;
816 /* "ls" had a minor problem. E.g., while processing a directory,
817 ls obtained the name of an entry via readdir, yet was later
818 unable to stat that name. This happens when listing a directory
819 in which entries are actively being removed or renamed. */
820 LS_MINOR_PROBLEM
= 1,
822 /* "ls" had more serious trouble (e.g., memory exhausted, invalid
823 option or failure to stat a command line argument. */
827 /* For long options that have no equivalent short option, use a
828 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
831 AUTHOR_OPTION
= CHAR_MAX
+ 1,
834 DEREFERENCE_COMMAND_LINE_SYMLINK_TO_DIR_OPTION
,
835 FILE_TYPE_INDICATOR_OPTION
,
838 GROUP_DIRECTORIES_FIRST_OPTION
,
841 INDICATOR_STYLE_OPTION
,
842 QUOTING_STYLE_OPTION
,
843 SHOW_CONTROL_CHARS_OPTION
,
851 static struct option
const long_options
[] =
853 {"all", no_argument
, nullptr, 'a'},
854 {"escape", no_argument
, nullptr, 'b'},
855 {"directory", no_argument
, nullptr, 'd'},
856 {"dired", no_argument
, nullptr, 'D'},
857 {"full-time", no_argument
, nullptr, FULL_TIME_OPTION
},
858 {"group-directories-first", no_argument
, nullptr,
859 GROUP_DIRECTORIES_FIRST_OPTION
},
860 {"human-readable", no_argument
, nullptr, 'h'},
861 {"inode", no_argument
, nullptr, 'i'},
862 {"kibibytes", no_argument
, nullptr, 'k'},
863 {"numeric-uid-gid", no_argument
, nullptr, 'n'},
864 {"no-group", no_argument
, nullptr, 'G'},
865 {"hide-control-chars", no_argument
, nullptr, 'q'},
866 {"reverse", no_argument
, nullptr, 'r'},
867 {"size", no_argument
, nullptr, 's'},
868 {"width", required_argument
, nullptr, 'w'},
869 {"almost-all", no_argument
, nullptr, 'A'},
870 {"ignore-backups", no_argument
, nullptr, 'B'},
871 {"classify", optional_argument
, nullptr, 'F'},
872 {"file-type", no_argument
, nullptr, FILE_TYPE_INDICATOR_OPTION
},
873 {"si", no_argument
, nullptr, SI_OPTION
},
874 {"dereference-command-line", no_argument
, nullptr, 'H'},
875 {"dereference-command-line-symlink-to-dir", no_argument
, nullptr,
876 DEREFERENCE_COMMAND_LINE_SYMLINK_TO_DIR_OPTION
},
877 {"hide", required_argument
, nullptr, HIDE_OPTION
},
878 {"ignore", required_argument
, nullptr, 'I'},
879 {"indicator-style", required_argument
, nullptr, INDICATOR_STYLE_OPTION
},
880 {"dereference", no_argument
, nullptr, 'L'},
881 {"literal", no_argument
, nullptr, 'N'},
882 {"quote-name", no_argument
, nullptr, 'Q'},
883 {"quoting-style", required_argument
, nullptr, QUOTING_STYLE_OPTION
},
884 {"recursive", no_argument
, nullptr, 'R'},
885 {"format", required_argument
, nullptr, FORMAT_OPTION
},
886 {"show-control-chars", no_argument
, nullptr, SHOW_CONTROL_CHARS_OPTION
},
887 {"sort", required_argument
, nullptr, SORT_OPTION
},
888 {"tabsize", required_argument
, nullptr, 'T'},
889 {"time", required_argument
, nullptr, TIME_OPTION
},
890 {"time-style", required_argument
, nullptr, TIME_STYLE_OPTION
},
891 {"zero", no_argument
, nullptr, ZERO_OPTION
},
892 {"color", optional_argument
, nullptr, COLOR_OPTION
},
893 {"hyperlink", optional_argument
, nullptr, HYPERLINK_OPTION
},
894 {"block-size", required_argument
, nullptr, BLOCK_SIZE_OPTION
},
895 {"context", no_argument
, 0, 'Z'},
896 {"author", no_argument
, nullptr, AUTHOR_OPTION
},
897 {GETOPT_HELP_OPTION_DECL
},
898 {GETOPT_VERSION_OPTION_DECL
},
899 {nullptr, 0, nullptr, 0}
902 static char const *const format_args
[] =
904 "verbose", "long", "commas", "horizontal", "across",
905 "vertical", "single-column", nullptr
907 static enum format
const format_types
[] =
909 long_format
, long_format
, with_commas
, horizontal
, horizontal
,
910 many_per_line
, one_per_line
912 ARGMATCH_VERIFY (format_args
, format_types
);
914 static char const *const sort_args
[] =
916 "none", "size", "time", "version", "extension",
917 "name", "width", nullptr
919 static enum sort_type
const sort_types
[] =
921 sort_none
, sort_size
, sort_time
, sort_version
, sort_extension
,
922 sort_name
, sort_width
924 ARGMATCH_VERIFY (sort_args
, sort_types
);
926 static char const *const time_args
[] =
928 "atime", "access", "use",
930 "mtime", "modification",
934 static enum time_type
const time_types
[] =
936 time_atime
, time_atime
, time_atime
,
937 time_ctime
, time_ctime
,
938 time_mtime
, time_mtime
,
939 time_btime
, time_btime
,
941 ARGMATCH_VERIFY (time_args
, time_types
);
943 static char const *const when_args
[] =
945 /* force and none are for compatibility with another color-ls version */
946 "always", "yes", "force",
947 "never", "no", "none",
948 "auto", "tty", "if-tty", nullptr
950 static enum when_type
const when_types
[] =
952 when_always
, when_always
, when_always
,
953 when_never
, when_never
, when_never
,
954 when_if_tty
, when_if_tty
, when_if_tty
956 ARGMATCH_VERIFY (when_args
, when_types
);
958 /* Information about filling a column. */
966 /* Array with information about column fullness. */
967 static struct column_info
*column_info
;
969 /* Maximum number of columns ever possible for this display. */
970 static size_t max_idx
;
972 /* The minimum width of a column is 3: 1 character for the name and 2
973 for the separating white space. */
974 enum { MIN_COLUMN_WIDTH
= 3 };
977 /* This zero-based index is for the --dired option. It is incremented
978 for each byte of output generated by this program so that the beginning
979 and ending indices (in that output) of every file name can be recorded
980 and later output themselves. */
981 static off_t dired_pos
;
984 dired_outbyte (char c
)
990 /* Output the buffer S, of length S_LEN, and increment DIRED_POS by S_LEN. */
992 dired_outbuf (char const *s
, size_t s_len
)
995 fwrite (s
, sizeof *s
, s_len
, stdout
);
998 /* Output the string S, and increment DIRED_POS by its length. */
1000 dired_outstring (char const *s
)
1002 dired_outbuf (s
, strlen (s
));
1009 dired_outstring (" ");
1012 /* With --dired, store pairs of beginning and ending indices of file names. */
1013 static struct obstack dired_obstack
;
1015 /* With --dired, store pairs of beginning and ending indices of any
1016 directory names that appear as headers (just before 'total' line)
1017 for lists of directory entries. Such directory names are seen when
1018 listing hierarchies using -R and when a directory is listed with at
1019 least one other command line argument. */
1020 static struct obstack subdired_obstack
;
1022 /* Save the current index on the specified obstack, OBS. */
1024 push_current_dired_pos (struct obstack
*obs
)
1027 obstack_grow (obs
, &dired_pos
, sizeof dired_pos
);
1030 /* With -R, this stack is used to help detect directory cycles.
1031 The device/inode pairs on this stack mirror the pairs in the
1032 active_dir_set hash table. */
1033 static struct obstack dev_ino_obstack
;
1035 /* Push a pair onto the device/inode stack. */
1037 dev_ino_push (dev_t dev
, ino_t ino
)
1041 int dev_ino_size
= sizeof *di
;
1042 obstack_blank (&dev_ino_obstack
, dev_ino_size
);
1043 vdi
= obstack_next_free (&dev_ino_obstack
);
1050 /* Pop a dev/ino struct off the global dev_ino_obstack
1051 and return that struct. */
1052 static struct dev_ino
1057 int dev_ino_size
= sizeof *di
;
1058 affirm (dev_ino_size
<= obstack_object_size (&dev_ino_obstack
));
1059 obstack_blank_fast (&dev_ino_obstack
, -dev_ino_size
);
1060 vdi
= obstack_next_free (&dev_ino_obstack
);
1066 assert_matching_dev_ino (char const *name
, struct dev_ino di
)
1068 MAYBE_UNUSED
struct stat sb
;
1069 assure (0 <= stat (name
, &sb
));
1070 assure (sb
.st_dev
== di
.st_dev
);
1071 assure (sb
.st_ino
== di
.st_ino
);
1074 static char eolbyte
= '\n';
1076 /* Write to standard output PREFIX, followed by the quoting style and
1077 a space-separated list of the integers stored in OS all on one line. */
1080 dired_dump_obstack (char const *prefix
, struct obstack
*os
)
1084 n_pos
= obstack_object_size (os
) / sizeof (dired_pos
);
1087 off_t
*pos
= obstack_finish (os
);
1088 fputs (prefix
, stdout
);
1089 for (size_t i
= 0; i
< n_pos
; i
++)
1091 intmax_t p
= pos
[i
];
1098 /* Return the platform birthtime member of the stat structure,
1099 or fallback to the mtime member, which we have populated
1100 from the statx structure or reset to an invalid timestamp
1101 where birth time is not supported. */
1102 static struct timespec
1103 get_stat_btime (struct stat
const *st
)
1105 struct timespec btimespec
;
1107 #if HAVE_STATX && defined STATX_INO
1108 btimespec
= get_stat_mtime (st
);
1110 btimespec
= get_stat_birthtime (st
);
1116 #if HAVE_STATX && defined STATX_INO
1119 time_type_to_statx (void)
1139 calc_req_mask (void)
1141 unsigned int mask
= STATX_MODE
;
1146 if (print_block_size
)
1147 mask
|= STATX_BLOCKS
;
1149 if (format
== long_format
) {
1150 mask
|= STATX_NLINK
| STATX_SIZE
| time_type_to_statx ();
1151 if (print_owner
|| print_author
)
1162 case sort_extension
:
1166 mask
|= time_type_to_statx ();
1179 do_statx (int fd
, char const *name
, struct stat
*st
, int flags
,
1183 bool want_btime
= mask
& STATX_BTIME
;
1184 int ret
= statx (fd
, name
, flags
| AT_NO_AUTOMOUNT
, mask
, &stx
);
1187 statx_to_stat (&stx
, st
);
1188 /* Since we only need one timestamp type,
1189 store birth time in st_mtim. */
1192 if (stx
.stx_mask
& STATX_BTIME
)
1193 st
->st_mtim
= statx_timestamp_to_timespec (stx
.stx_btime
);
1195 st
->st_mtim
.tv_sec
= st
->st_mtim
.tv_nsec
= -1;
1203 do_stat (char const *name
, struct stat
*st
)
1205 return do_statx (AT_FDCWD
, name
, st
, 0, calc_req_mask ());
1209 do_lstat (char const *name
, struct stat
*st
)
1211 return do_statx (AT_FDCWD
, name
, st
, AT_SYMLINK_NOFOLLOW
, calc_req_mask ());
1215 stat_for_mode (char const *name
, struct stat
*st
)
1217 return do_statx (AT_FDCWD
, name
, st
, 0, STATX_MODE
);
1220 /* dev+ino should be static, so no need to sync with backing store */
1222 stat_for_ino (char const *name
, struct stat
*st
)
1224 return do_statx (AT_FDCWD
, name
, st
, 0, STATX_INO
);
1228 fstat_for_ino (int fd
, struct stat
*st
)
1230 return do_statx (fd
, "", st
, AT_EMPTY_PATH
, STATX_INO
);
1234 do_stat (char const *name
, struct stat
*st
)
1236 return stat (name
, st
);
1240 do_lstat (char const *name
, struct stat
*st
)
1242 return lstat (name
, st
);
1246 stat_for_mode (char const *name
, struct stat
*st
)
1248 return stat (name
, st
);
1252 stat_for_ino (char const *name
, struct stat
*st
)
1254 return stat (name
, st
);
1258 fstat_for_ino (int fd
, struct stat
*st
)
1260 return fstat (fd
, st
);
1264 /* Return the address of the first plain %b spec in FMT, or nullptr if
1265 there is no such spec. %5b etc. do not match, so that user
1266 widths/flags are honored. */
1270 first_percent_b (char const *fmt
)
1276 case 'b': return fmt
;
1277 case '%': fmt
++; break;
1282 static char RFC3986
[256];
1284 file_escape_init (void)
1286 for (int i
= 0; i
< 256; i
++)
1287 RFC3986
[i
] |= c_isalnum (i
) || i
== '~' || i
== '-' || i
== '.' || i
== '_';
1290 enum { MBSWIDTH_FLAGS
= MBSW_REJECT_INVALID
| MBSW_REJECT_UNPRINTABLE
};
1292 /* Read the abbreviated month names from the locale, to align them
1293 and to determine the max width of the field and to truncate names
1294 greater than our max allowed.
1295 Note even though this handles multibyte locales correctly
1296 it's not restricted to them as single byte locales can have
1297 variable width abbreviated months and also precomputing/caching
1298 the names was seen to increase the performance of ls significantly. */
1300 /* abformat[RECENT][MON] is the format to use for timestamps with
1301 recentness RECENT and month MON. */
1302 enum { ABFORMAT_SIZE
= 128 };
1303 static char abformat
[2][12][ABFORMAT_SIZE
];
1304 /* True if precomputed formats should be used. This can be false if
1305 nl_langinfo fails, if a format or month abbreviation is unusually
1306 long, or if a month abbreviation contains '%'. */
1307 static bool use_abformat
;
1309 /* Store into ABMON the abbreviated month names, suitably aligned.
1310 Return true if successful. */
1313 abmon_init (char abmon
[12][ABFORMAT_SIZE
])
1315 #ifndef HAVE_NL_LANGINFO
1318 int max_mon_width
= 0;
1322 for (int i
= 0; i
< 12; i
++)
1324 char const *abbr
= nl_langinfo (ABMON_1
+ i
);
1325 mon_len
[i
] = strnlen (abbr
, ABFORMAT_SIZE
);
1326 if (mon_len
[i
] == ABFORMAT_SIZE
)
1328 if (strchr (abbr
, '%'))
1330 mon_width
[i
] = mbswidth (strcpy (abmon
[i
], abbr
), MBSWIDTH_FLAGS
);
1331 if (mon_width
[i
] < 0)
1333 max_mon_width
= MAX (max_mon_width
, mon_width
[i
]);
1336 for (int i
= 0; i
< 12; i
++)
1338 int fill
= max_mon_width
- mon_width
[i
];
1339 if (ABFORMAT_SIZE
- mon_len
[i
] <= fill
)
1341 bool align_left
= !isdigit (to_uchar (abmon
[i
][0]));
1344 fill_offset
= mon_len
[i
];
1347 memmove (abmon
[i
] + fill
, abmon
[i
], mon_len
[i
]);
1350 memset (abmon
[i
] + fill_offset
, ' ', fill
);
1351 abmon
[i
][mon_len
[i
] + fill
] = '\0';
1358 /* Initialize ABFORMAT and USE_ABFORMAT. */
1361 abformat_init (void)
1364 for (int recent
= 0; recent
< 2; recent
++)
1365 pb
[recent
] = first_percent_b (long_time_format
[recent
]);
1366 if (! (pb
[0] || pb
[1]))
1369 char abmon
[12][ABFORMAT_SIZE
];
1370 if (! abmon_init (abmon
))
1373 for (int recent
= 0; recent
< 2; recent
++)
1375 char const *fmt
= long_time_format
[recent
];
1376 for (int i
= 0; i
< 12; i
++)
1378 char *nfmt
= abformat
[recent
][i
];
1382 nbytes
= snprintf (nfmt
, ABFORMAT_SIZE
, "%s", fmt
);
1385 if (! (pb
[recent
] - fmt
<= MIN (ABFORMAT_SIZE
, INT_MAX
)))
1387 int prefix_len
= pb
[recent
] - fmt
;
1388 nbytes
= snprintf (nfmt
, ABFORMAT_SIZE
, "%.*s%s%s",
1389 prefix_len
, fmt
, abmon
[i
], pb
[recent
] + 2);
1392 if (! (0 <= nbytes
&& nbytes
< ABFORMAT_SIZE
))
1397 use_abformat
= true;
1401 dev_ino_hash (void const *x
, size_t table_size
)
1403 struct dev_ino
const *p
= x
;
1404 return (uintmax_t) p
->st_ino
% table_size
;
1408 dev_ino_compare (void const *x
, void const *y
)
1410 struct dev_ino
const *a
= x
;
1411 struct dev_ino
const *b
= y
;
1412 return PSAME_INODE (a
, b
);
1416 dev_ino_free (void *x
)
1421 /* Add the device/inode pair (P->st_dev/P->st_ino) to the set of
1422 active directories. Return true if there is already a matching
1423 entry in the table. */
1426 visit_dir (dev_t dev
, ino_t ino
)
1428 struct dev_ino
*ent
;
1429 struct dev_ino
*ent_from_table
;
1432 ent
= xmalloc (sizeof *ent
);
1436 /* Attempt to insert this entry into the table. */
1437 ent_from_table
= hash_insert (active_dir_set
, ent
);
1439 if (ent_from_table
== nullptr)
1441 /* Insertion failed due to lack of memory. */
1445 found_match
= (ent_from_table
!= ent
);
1449 /* ent was not inserted, so free it. */
1457 free_pending_ent (struct pending
*p
)
1465 is_colored (enum indicator_no type
)
1467 size_t len
= color_indicator
[type
].len
;
1468 char const *s
= color_indicator
[type
].string
;
1470 || (len
== 1 && STRNCMP_LIT (s
, "0") == 0)
1471 || (len
== 2 && STRNCMP_LIT (s
, "00") == 0));
1475 restore_default_color (void)
1477 put_indicator (&color_indicator
[C_LEFT
]);
1478 put_indicator (&color_indicator
[C_RIGHT
]);
1482 set_normal_color (void)
1484 if (print_with_color
&& is_colored (C_NORM
))
1486 put_indicator (&color_indicator
[C_LEFT
]);
1487 put_indicator (&color_indicator
[C_NORM
]);
1488 put_indicator (&color_indicator
[C_RIGHT
]);
1492 /* An ordinary signal was received; arrange for the program to exit. */
1495 sighandler (int sig
)
1498 signal (sig
, SIG_IGN
);
1499 if (! interrupt_signal
)
1500 interrupt_signal
= sig
;
1503 /* A SIGTSTP was received; arrange for the program to suspend itself. */
1506 stophandler (int sig
)
1509 signal (sig
, stophandler
);
1510 if (! interrupt_signal
)
1511 stop_signal_count
++;
1514 /* Process any pending signals. If signals are caught, this function
1515 should be called periodically. Ideally there should never be an
1516 unbounded amount of time when signals are not being processed.
1517 Signal handling can restore the default colors, so callers must
1518 immediately change colors after invoking this function. */
1521 process_signals (void)
1523 while (interrupt_signal
|| stop_signal_count
)
1530 restore_default_color ();
1533 sigprocmask (SIG_BLOCK
, &caught_signals
, &oldset
);
1535 /* Reload interrupt_signal and stop_signal_count, in case a new
1536 signal was handled before sigprocmask took effect. */
1537 sig
= interrupt_signal
;
1538 stops
= stop_signal_count
;
1540 /* SIGTSTP is special, since the application can receive that signal
1541 more than once. In this case, don't set the signal handler to the
1542 default. Instead, just raise the uncatchable SIGSTOP. */
1545 stop_signal_count
= stops
- 1;
1549 signal (sig
, SIG_DFL
);
1551 /* Exit or suspend the program. */
1553 sigprocmask (SIG_SETMASK
, &oldset
, nullptr);
1555 /* If execution reaches here, then the program has been
1556 continued (after being suspended). */
1560 /* Setup signal handlers if INIT is true,
1561 otherwise restore to the default. */
1564 signal_setup (bool init
)
1566 /* The signals that are trapped, and the number of such signals. */
1567 static int const sig
[] =
1569 /* This one is handled specially. */
1572 /* The usual suspects. */
1573 SIGALRM
, SIGHUP
, SIGINT
, SIGPIPE
, SIGQUIT
, SIGTERM
,
1590 enum { nsigs
= ARRAY_CARDINALITY (sig
) };
1593 static bool caught_sig
[nsigs
];
1601 struct sigaction act
;
1603 sigemptyset (&caught_signals
);
1604 for (j
= 0; j
< nsigs
; j
++)
1606 sigaction (sig
[j
], nullptr, &act
);
1607 if (act
.sa_handler
!= SIG_IGN
)
1608 sigaddset (&caught_signals
, sig
[j
]);
1611 act
.sa_mask
= caught_signals
;
1612 act
.sa_flags
= SA_RESTART
;
1614 for (j
= 0; j
< nsigs
; j
++)
1615 if (sigismember (&caught_signals
, sig
[j
]))
1617 act
.sa_handler
= sig
[j
] == SIGTSTP
? stophandler
: sighandler
;
1618 sigaction (sig
[j
], &act
, nullptr);
1621 for (j
= 0; j
< nsigs
; j
++)
1623 caught_sig
[j
] = (signal (sig
[j
], SIG_IGN
) != SIG_IGN
);
1626 signal (sig
[j
], sig
[j
] == SIGTSTP
? stophandler
: sighandler
);
1627 siginterrupt (sig
[j
], 0);
1635 for (j
= 0; j
< nsigs
; j
++)
1636 if (sigismember (&caught_signals
, sig
[j
]))
1637 signal (sig
[j
], SIG_DFL
);
1639 for (j
= 0; j
< nsigs
; j
++)
1641 signal (sig
[j
], SIG_DFL
);
1649 signal_setup (true);
1653 signal_restore (void)
1655 signal_setup (false);
1659 main (int argc
, char **argv
)
1662 struct pending
*thispend
;
1665 initialize_main (&argc
, &argv
);
1666 set_program_name (argv
[0]);
1667 setlocale (LC_ALL
, "");
1668 bindtextdomain (PACKAGE
, LOCALEDIR
);
1669 textdomain (PACKAGE
);
1671 initialize_exit_failure (LS_FAILURE
);
1672 atexit (close_stdout
);
1674 static_assert (ARRAY_CARDINALITY (color_indicator
) + 1
1675 == ARRAY_CARDINALITY (indicator_name
));
1677 exit_status
= EXIT_SUCCESS
;
1678 print_dir_name
= true;
1679 pending_dirs
= nullptr;
1681 current_time
.tv_sec
= TYPE_MINIMUM (time_t);
1682 current_time
.tv_nsec
= -1;
1684 i
= decode_switches (argc
, argv
);
1686 if (print_with_color
)
1689 /* Test print_with_color again, because the call to parse_ls_color
1690 may have just reset it -- e.g., if LS_COLORS is invalid. */
1692 if (print_with_color
)
1694 /* Don't use TAB characters in output. Some terminal
1695 emulators can't handle the combination of tabs and
1696 color codes on the same line. */
1700 if (directories_first
)
1701 check_symlink_mode
= true;
1702 else if (print_with_color
)
1704 /* Avoid following symbolic links when possible. */
1705 if (is_colored (C_ORPHAN
)
1706 || (is_colored (C_EXEC
) && color_symlink_as_referent
)
1707 || (is_colored (C_MISSING
) && format
== long_format
))
1708 check_symlink_mode
= true;
1711 if (dereference
== DEREF_UNDEFINED
)
1712 dereference
= ((immediate_dirs
1713 || indicator_style
== classify
1714 || format
== long_format
)
1716 : DEREF_COMMAND_LINE_SYMLINK_TO_DIR
);
1718 /* When using -R, initialize a data structure we'll use to
1719 detect any directory cycles. */
1722 active_dir_set
= hash_initialize (INITIAL_TABLE_SIZE
, nullptr,
1726 if (active_dir_set
== nullptr)
1729 obstack_init (&dev_ino_obstack
);
1732 localtz
= tzalloc (getenv ("TZ"));
1734 format_needs_stat
= sort_type
== sort_time
|| sort_type
== sort_size
1735 || format
== long_format
1737 || print_block_size
;
1738 format_needs_type
= (! format_needs_stat
1741 || indicator_style
!= none
1742 || directories_first
));
1746 obstack_init (&dired_obstack
);
1747 obstack_init (&subdired_obstack
);
1750 if (print_hyperlink
)
1752 file_escape_init ();
1754 hostname
= xgethostname ();
1755 /* The hostname is generally ignored,
1756 so ignore failures obtaining it. */
1762 cwd_file
= xnmalloc (cwd_n_alloc
, sizeof *cwd_file
);
1772 gobble_file (".", directory
, NOT_AN_INODE_NUMBER
, true, "");
1774 queue_directory (".", nullptr, true);
1778 gobble_file (argv
[i
++], unknown
, NOT_AN_INODE_NUMBER
, true, "");
1784 if (!immediate_dirs
)
1785 extract_dirs_from_files (nullptr, true);
1786 /* 'cwd_n_used' might be zero now. */
1789 /* In the following if/else blocks, it is sufficient to test 'pending_dirs'
1790 (and not pending_dirs->name) because there may be no markers in the queue
1791 at this point. A marker may be enqueued when extract_dirs_from_files is
1792 called with a non-empty string or via print_dir. */
1795 print_current_files ();
1797 dired_outbyte ('\n');
1799 else if (n_files
<= 1 && pending_dirs
&& pending_dirs
->next
== 0)
1800 print_dir_name
= false;
1802 while (pending_dirs
)
1804 thispend
= pending_dirs
;
1805 pending_dirs
= pending_dirs
->next
;
1809 if (thispend
->name
== nullptr)
1811 /* thispend->name == nullptr means this is a marker entry
1812 indicating we've finished processing the directory.
1813 Use its dev/ino numbers to remove the corresponding
1814 entry from the active_dir_set hash table. */
1815 struct dev_ino di
= dev_ino_pop ();
1816 struct dev_ino
*found
= hash_remove (active_dir_set
, &di
);
1818 assert_matching_dev_ino (thispend
->realname
, di
);
1820 dev_ino_free (found
);
1821 free_pending_ent (thispend
);
1826 print_dir (thispend
->name
, thispend
->realname
,
1827 thispend
->command_line_arg
);
1829 free_pending_ent (thispend
);
1830 print_dir_name
= true;
1833 if (print_with_color
&& used_color
)
1837 /* Skip the restore when it would be a no-op, i.e.,
1838 when left is "\033[" and right is "m". */
1839 if (!(color_indicator
[C_LEFT
].len
== 2
1840 && memcmp (color_indicator
[C_LEFT
].string
, "\033[", 2) == 0
1841 && color_indicator
[C_RIGHT
].len
== 1
1842 && color_indicator
[C_RIGHT
].string
[0] == 'm'))
1843 restore_default_color ();
1849 /* Act on any signals that arrived before the default was restored.
1850 This can process signals out of order, but there doesn't seem to
1851 be an easy way to do them in order, and the order isn't that
1852 important anyway. */
1853 for (j
= stop_signal_count
; j
; j
--)
1855 j
= interrupt_signal
;
1862 /* No need to free these since we're about to exit. */
1863 dired_dump_obstack ("//DIRED//", &dired_obstack
);
1864 dired_dump_obstack ("//SUBDIRED//", &subdired_obstack
);
1865 printf ("//DIRED-OPTIONS// --quoting-style=%s\n",
1866 quoting_style_args
[get_quoting_style (filename_quoting_options
)]);
1871 assure (hash_get_n_entries (active_dir_set
) == 0);
1872 hash_free (active_dir_set
);
1878 /* Return the line length indicated by the value given by SPEC, or -1
1879 if unsuccessful. 0 means no limit on line length. */
1882 decode_line_length (char const *spec
)
1886 /* Treat too-large values as if they were 0, which is
1887 effectively infinity. */
1888 switch (xstrtoumax (spec
, nullptr, 0, &val
, ""))
1891 return val
<= MIN (PTRDIFF_MAX
, SIZE_MAX
) ? val
: 0;
1893 case LONGINT_OVERFLOW
:
1901 /* Return true if standard output is a tty, caching the result. */
1904 stdout_isatty (void)
1906 static signed char out_tty
= -1;
1908 out_tty
= isatty (STDOUT_FILENO
);
1909 assume (out_tty
== 0 || out_tty
== 1);
1913 /* Set all the option flags according to the switches specified.
1914 Return the index of the first non-option argument. */
1917 decode_switches (int argc
, char **argv
)
1919 char const *time_style_option
= nullptr;
1921 /* These variables are false or -1 unless a switch says otherwise. */
1922 bool kibibytes_specified
= false;
1923 int format_opt
= -1;
1924 int hide_control_chars_opt
= -1;
1925 int quoting_style_opt
= -1;
1927 ptrdiff_t tabsize_opt
= -1;
1928 ptrdiff_t width_opt
= -1;
1933 int c
= getopt_long (argc
, argv
,
1934 "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UXZ1",
1942 ignore_mode
= IGNORE_MINIMAL
;
1946 quoting_style_opt
= escape_quoting_style
;
1950 time_type
= time_ctime
;
1951 explicit_time
= true;
1955 immediate_dirs
= true;
1959 ignore_mode
= IGNORE_MINIMAL
; /* enable -a */
1960 sort_opt
= sort_none
; /* enable -U */
1963 case FILE_TYPE_INDICATOR_OPTION
: /* --file-type */
1964 indicator_style
= file_type
;
1968 format_opt
= long_format
;
1969 print_owner
= false;
1973 file_human_output_opts
= human_output_opts
=
1974 human_autoscale
| human_SI
| human_base_1024
;
1975 file_output_block_size
= output_block_size
= 1;
1983 kibibytes_specified
= true;
1987 format_opt
= long_format
;
1991 format_opt
= with_commas
;
1996 format_opt
= long_format
;
1999 case 'o': /* Just like -l, but don't display group info. */
2000 format_opt
= long_format
;
2001 print_group
= false;
2005 indicator_style
= slash
;
2009 hide_control_chars_opt
= true;
2013 sort_reverse
= true;
2017 print_block_size
= true;
2021 sort_opt
= sort_time
;
2025 time_type
= time_atime
;
2026 explicit_time
= true;
2030 sort_opt
= sort_version
;
2034 width_opt
= decode_line_length (optarg
);
2036 error (LS_FAILURE
, 0, "%s: %s", _("invalid line width"),
2041 format_opt
= horizontal
;
2045 ignore_mode
= IGNORE_DOT_AND_DOTDOT
;
2049 add_ignore_pattern ("*~");
2050 add_ignore_pattern (".*~");
2054 format_opt
= many_per_line
;
2058 format_opt
= long_format
;
2059 print_hyperlink
= false;
2067 i
= XARGMATCH ("--classify", optarg
, when_args
, when_types
);
2069 /* Using --classify with no argument is equivalent to using
2070 --classify=always. */
2073 if (i
== when_always
|| (i
== when_if_tty
&& stdout_isatty ()))
2074 indicator_style
= classify
;
2078 case 'G': /* inhibit display of group info */
2079 print_group
= false;
2083 dereference
= DEREF_COMMAND_LINE_ARGUMENTS
;
2086 case DEREFERENCE_COMMAND_LINE_SYMLINK_TO_DIR_OPTION
:
2087 dereference
= DEREF_COMMAND_LINE_SYMLINK_TO_DIR
;
2091 add_ignore_pattern (optarg
);
2095 dereference
= DEREF_ALWAYS
;
2099 quoting_style_opt
= literal_quoting_style
;
2103 quoting_style_opt
= c_quoting_style
;
2111 sort_opt
= sort_size
;
2115 tabsize_opt
= xnumtoumax (optarg
, 0, 0, MIN (PTRDIFF_MAX
, SIZE_MAX
),
2116 "", _("invalid tab size"), LS_FAILURE
, 0);
2120 sort_opt
= sort_none
;
2124 sort_opt
= sort_extension
;
2128 /* -1 has no effect after -l. */
2129 if (format_opt
!= long_format
)
2130 format_opt
= one_per_line
;
2134 print_author
= true;
2139 struct ignore_pattern
*hide
= xmalloc (sizeof *hide
);
2140 hide
->pattern
= optarg
;
2141 hide
->next
= hide_patterns
;
2142 hide_patterns
= hide
;
2147 sort_opt
= XARGMATCH ("--sort", optarg
, sort_args
, sort_types
);
2150 case GROUP_DIRECTORIES_FIRST_OPTION
:
2151 directories_first
= true;
2155 time_type
= XARGMATCH ("--time", optarg
, time_args
, time_types
);
2156 explicit_time
= true;
2160 format_opt
= XARGMATCH ("--format", optarg
, format_args
,
2164 case FULL_TIME_OPTION
:
2165 format_opt
= long_format
;
2166 time_style_option
= "full-iso";
2173 i
= XARGMATCH ("--color", optarg
, when_args
, when_types
);
2175 /* Using --color with no argument is equivalent to using
2179 print_with_color
= (i
== when_always
2180 || (i
== when_if_tty
&& stdout_isatty ()));
2184 case HYPERLINK_OPTION
:
2188 i
= XARGMATCH ("--hyperlink", optarg
, when_args
, when_types
);
2190 /* Using --hyperlink with no argument is equivalent to using
2191 --hyperlink=always. */
2194 print_hyperlink
= (i
== when_always
2195 || (i
== when_if_tty
&& stdout_isatty ()));
2199 case INDICATOR_STYLE_OPTION
:
2200 indicator_style
= XARGMATCH ("--indicator-style", optarg
,
2201 indicator_style_args
,
2202 indicator_style_types
);
2205 case QUOTING_STYLE_OPTION
:
2206 quoting_style_opt
= XARGMATCH ("--quoting-style", optarg
,
2208 quoting_style_vals
);
2211 case TIME_STYLE_OPTION
:
2212 time_style_option
= optarg
;
2215 case SHOW_CONTROL_CHARS_OPTION
:
2216 hide_control_chars_opt
= false;
2219 case BLOCK_SIZE_OPTION
:
2221 enum strtol_error e
= human_options (optarg
, &human_output_opts
,
2222 &output_block_size
);
2223 if (e
!= LONGINT_OK
)
2224 xstrtol_fatal (e
, oi
, 0, long_options
, optarg
);
2225 file_human_output_opts
= human_output_opts
;
2226 file_output_block_size
= output_block_size
;
2231 file_human_output_opts
= human_output_opts
=
2232 human_autoscale
| human_SI
;
2233 file_output_block_size
= output_block_size
= 1;
2237 print_scontext
= true;
2242 hide_control_chars_opt
= false;
2243 if (format_opt
!= long_format
)
2244 format_opt
= one_per_line
;
2245 print_with_color
= false;
2246 quoting_style_opt
= literal_quoting_style
;
2249 case_GETOPT_HELP_CHAR
;
2251 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
2258 if (! output_block_size
)
2260 char const *ls_block_size
= getenv ("LS_BLOCK_SIZE");
2261 human_options (ls_block_size
,
2262 &human_output_opts
, &output_block_size
);
2263 if (ls_block_size
|| getenv ("BLOCK_SIZE"))
2265 file_human_output_opts
= human_output_opts
;
2266 file_output_block_size
= output_block_size
;
2268 if (kibibytes_specified
)
2270 human_output_opts
= 0;
2271 output_block_size
= 1024;
2275 format
= (0 <= format_opt
? format_opt
2276 : ls_mode
== LS_LS
? (stdout_isatty ()
2277 ? many_per_line
: one_per_line
)
2278 : ls_mode
== LS_MULTI_COL
? many_per_line
2279 : /* ls_mode == LS_LONG_FORMAT */ long_format
);
2281 /* If the line length was not set by a switch but is needed to determine
2282 output, go to the work of obtaining it from the environment. */
2283 ptrdiff_t linelen
= width_opt
;
2284 if (format
== many_per_line
|| format
== horizontal
|| format
== with_commas
2285 || print_with_color
)
2291 if (stdout_isatty ()
2292 && 0 <= ioctl (STDOUT_FILENO
, TIOCGWINSZ
, &ws
)
2294 linelen
= ws
.ws_col
<= MIN (PTRDIFF_MAX
, SIZE_MAX
) ? ws
.ws_col
: 0;
2299 char const *p
= getenv ("COLUMNS");
2302 linelen
= decode_line_length (p
);
2305 _("ignoring invalid width"
2306 " in environment variable COLUMNS: %s"),
2312 line_length
= linelen
< 0 ? 80 : linelen
;
2314 /* Determine the max possible number of display columns. */
2315 max_idx
= line_length
/ MIN_COLUMN_WIDTH
;
2316 /* Account for first display column not having a separator,
2317 or line_lengths shorter than MIN_COLUMN_WIDTH. */
2318 max_idx
+= line_length
% MIN_COLUMN_WIDTH
!= 0;
2320 if (format
== many_per_line
|| format
== horizontal
|| format
== with_commas
)
2322 if (0 <= tabsize_opt
)
2323 tabsize
= tabsize_opt
;
2327 char const *p
= getenv ("TABSIZE");
2331 if (xstrtoumax (p
, nullptr, 0, &tmp
, "") == LONGINT_OK
2336 _("ignoring invalid tab size"
2337 " in environment variable TABSIZE: %s"),
2343 qmark_funny_chars
= (hide_control_chars_opt
< 0
2344 ? ls_mode
== LS_LS
&& stdout_isatty ()
2345 : hide_control_chars_opt
);
2347 int qs
= quoting_style_opt
;
2349 qs
= getenv_quoting_style ();
2351 qs
= (ls_mode
== LS_LS
2352 ? (stdout_isatty () ? shell_escape_quoting_style
: -1)
2353 : escape_quoting_style
);
2355 set_quoting_style (nullptr, qs
);
2356 qs
= get_quoting_style (nullptr);
2357 align_variable_outer_quotes
2358 = ((format
== long_format
2359 || ((format
== many_per_line
|| format
== horizontal
) && line_length
))
2360 && (qs
== shell_quoting_style
2361 || qs
== shell_escape_quoting_style
2362 || qs
== c_maybe_quoting_style
));
2363 filename_quoting_options
= clone_quoting_options (nullptr);
2364 if (qs
== escape_quoting_style
)
2365 set_char_quoting (filename_quoting_options
, ' ', 1);
2366 if (file_type
<= indicator_style
)
2369 for (p
= &"*=>@|"[indicator_style
- file_type
]; *p
; p
++)
2370 set_char_quoting (filename_quoting_options
, *p
, 1);
2373 dirname_quoting_options
= clone_quoting_options (nullptr);
2374 set_char_quoting (dirname_quoting_options
, ':', 1);
2376 /* --dired implies --format=long (-l) and sans --hyperlink.
2377 So ignore it if those overridden. */
2378 dired
&= (format
== long_format
) & !print_hyperlink
;
2380 if (eolbyte
< dired
)
2381 error (LS_FAILURE
, 0, _("--dired and --zero are incompatible"));
2383 /* If a time type is explicitly specified (with -c, -u, or --time=)
2384 and we're not showing a time (-l not specified), then sort by that time,
2385 rather than by name. Note this behavior is unspecified by POSIX. */
2387 sort_type
= (0 <= sort_opt
? sort_opt
2388 : (format
!= long_format
&& explicit_time
)
2389 ? sort_time
: sort_name
);
2391 if (format
== long_format
)
2393 char const *style
= time_style_option
;
2394 static char const posix_prefix
[] = "posix-";
2398 style
= getenv ("TIME_STYLE");
2403 while (STREQ_LEN (style
, posix_prefix
, sizeof posix_prefix
- 1))
2405 if (! hard_locale (LC_TIME
))
2407 style
+= sizeof posix_prefix
- 1;
2412 char const *p0
= style
+ 1;
2413 char *p0nl
= strchr (p0
, '\n');
2414 char const *p1
= p0
;
2417 if (strchr (p0nl
+ 1, '\n'))
2418 error (LS_FAILURE
, 0, _("invalid time style format %s"),
2423 long_time_format
[0] = p0
;
2424 long_time_format
[1] = p1
;
2428 ptrdiff_t res
= argmatch (style
, time_style_args
,
2429 (char const *) time_style_types
,
2430 sizeof (*time_style_types
));
2433 /* This whole block used to be a simple use of XARGMATCH.
2434 but that didn't print the "posix-"-prefixed variants or
2435 the "+"-prefixed format string option upon failure. */
2436 argmatch_invalid ("time style", style
, res
);
2438 /* The following is a manual expansion of argmatch_valid,
2439 but with the added "+ ..." description and the [posix-]
2440 prefixes prepended. Note that this simplification works
2441 only because all four existing time_style_types values
2443 fputs (_("Valid arguments are:\n"), stderr
);
2444 char const *const *p
= time_style_args
;
2446 fprintf (stderr
, " - [posix-]%s\n", *p
++);
2447 fputs (_(" - +FORMAT (e.g., +%H:%M) for a 'date'-style"
2448 " format\n"), stderr
);
2453 case full_iso_time_style
:
2454 long_time_format
[0] = long_time_format
[1] =
2455 "%Y-%m-%d %H:%M:%S.%N %z";
2458 case long_iso_time_style
:
2459 long_time_format
[0] = long_time_format
[1] = "%Y-%m-%d %H:%M";
2462 case iso_time_style
:
2463 long_time_format
[0] = "%Y-%m-%d ";
2464 long_time_format
[1] = "%m-%d %H:%M";
2467 case locale_time_style
:
2468 if (hard_locale (LC_TIME
))
2470 for (int i
= 0; i
< 2; i
++)
2471 long_time_format
[i
] =
2472 dcgettext (nullptr, long_time_format
[i
], LC_TIME
);
2483 /* Parse a string as part of the LS_COLORS variable; this may involve
2484 decoding all kinds of escape characters. If equals_end is set an
2485 unescaped equal sign ends the string, otherwise only a : or \0
2486 does. Set *OUTPUT_COUNT to the number of bytes output. Return
2489 The resulting string is *not* null-terminated, but may contain
2492 Note that both dest and src are char **; on return they point to
2493 the first free byte after the array and the character that ended
2494 the input string, respectively. */
2497 get_funky_string (char **dest
, char const **src
, bool equals_end
,
2498 size_t *output_count
)
2500 char num
; /* For numerical codes */
2501 size_t count
; /* Something to count with */
2503 ST_GND
, ST_BACKSLASH
, ST_OCTAL
, ST_HEX
, ST_CARET
, ST_END
, ST_ERROR
2508 p
= *src
; /* We don't want to double-indirect */
2509 q
= *dest
; /* the whole darn time. */
2511 count
= 0; /* No characters counted in yet. */
2514 state
= ST_GND
; /* Start in ground state. */
2515 while (state
< ST_END
)
2519 case ST_GND
: /* Ground state (no escapes) */
2524 state
= ST_END
; /* End of string */
2527 state
= ST_BACKSLASH
; /* Backslash escape sequence */
2531 state
= ST_CARET
; /* Caret escape */
2537 state
= ST_END
; /* End */
2548 case ST_BACKSLASH
: /* Backslash escaped character */
2559 state
= ST_OCTAL
; /* Octal sequence */
2564 state
= ST_HEX
; /* Hex sequence */
2567 case 'a': /* Bell */
2570 case 'b': /* Backspace */
2573 case 'e': /* Escape */
2576 case 'f': /* Form feed */
2579 case 'n': /* Newline */
2582 case 'r': /* Carriage return */
2588 case 'v': /* Vtab */
2591 case '?': /* Delete */
2594 case '_': /* Space */
2597 case '\0': /* End of string */
2598 state
= ST_ERROR
; /* Error! */
2600 default: /* Escaped character like \ ^ : = */
2604 if (state
== ST_BACKSLASH
)
2613 case ST_OCTAL
: /* Octal sequence */
2614 if (*p
< '0' || *p
> '7')
2621 num
= (num
<< 3) + (*(p
++) - '0');
2624 case ST_HEX
: /* Hex sequence */
2637 num
= (num
<< 4) + (*(p
++) - '0');
2645 num
= (num
<< 4) + (*(p
++) - 'a') + 10;
2653 num
= (num
<< 4) + (*(p
++) - 'A') + 10;
2663 case ST_CARET
: /* Caret escape */
2664 state
= ST_GND
; /* Should be the next state... */
2665 if (*p
>= '@' && *p
<= '~')
2667 *(q
++) = *(p
++) & 037;
2686 *output_count
= count
;
2688 return state
!= ST_ERROR
;
2702 /* Check if the content of TERM is a valid name in dircolors. */
2705 known_term_type (void)
2707 char const *term
= getenv ("TERM");
2708 if (! term
|| ! *term
)
2711 char const *line
= G_line
;
2712 while (line
- G_line
< sizeof (G_line
))
2714 if (STRNCMP_LIT (line
, "TERM ") == 0)
2716 if (fnmatch (line
+ 5, term
, 0) == 0)
2719 line
+= strlen (line
) + 1;
2726 parse_ls_color (void)
2728 char const *p
; /* Pointer to character being parsed */
2729 char *buf
; /* color_buf buffer pointer */
2730 int ind_no
; /* Indicator number */
2731 char label
[3]; /* Indicator label */
2732 struct color_ext_type
*ext
; /* Extension we are working on */
2734 if ((p
= getenv ("LS_COLORS")) == nullptr || *p
== '\0')
2736 /* LS_COLORS takes precedence, but if that's not set then
2737 honor the COLORTERM and TERM env variables so that
2738 we only go with the internal ANSI color codes if the
2739 former is non empty or the latter is set to a known value. */
2740 char const *colorterm
= getenv ("COLORTERM");
2741 if (! (colorterm
&& *colorterm
) && ! known_term_type ())
2742 print_with_color
= false;
2747 strcpy (label
, "??");
2749 /* This is an overly conservative estimate, but any possible
2750 LS_COLORS string will *not* generate a color_buf longer than
2751 itself, so it is a safe way of allocating a buffer in
2753 buf
= color_buf
= xstrdup (p
);
2755 enum parse_state state
= PS_START
;
2760 case PS_START
: /* First label character */
2768 /* Allocate new extension block and add to head of
2769 linked list (this way a later definition will
2770 override an earlier one, which can be useful for
2771 having terminal-specific defs override global). */
2773 ext
= xmalloc (sizeof *ext
);
2774 ext
->next
= color_ext_list
;
2775 color_ext_list
= ext
;
2776 ext
->exact_match
= false;
2779 ext
->ext
.string
= buf
;
2781 state
= (get_funky_string (&buf
, &p
, true, &ext
->ext
.len
)
2786 state
= PS_DONE
; /* Done! */
2789 default: /* Assume it is file type label */
2796 case PS_2
: /* Second label character */
2803 state
= PS_FAIL
; /* Error */
2806 case PS_3
: /* Equal sign after indicator label */
2807 state
= PS_FAIL
; /* Assume failure... */
2808 if (*(p
++) == '=')/* It *should* be... */
2810 for (ind_no
= 0; indicator_name
[ind_no
] != nullptr; ++ind_no
)
2812 if (STREQ (label
, indicator_name
[ind_no
]))
2814 color_indicator
[ind_no
].string
= buf
;
2815 state
= (get_funky_string (&buf
, &p
, false,
2816 &color_indicator
[ind_no
].len
)
2817 ? PS_START
: PS_FAIL
);
2821 if (state
== PS_FAIL
)
2822 error (0, 0, _("unrecognized prefix: %s"), quote (label
));
2826 case PS_4
: /* Equal sign after *.ext */
2829 ext
->seq
.string
= buf
;
2830 state
= (get_funky_string (&buf
, &p
, false, &ext
->seq
.len
)
2831 ? PS_START
: PS_FAIL
);
2846 if (state
== PS_FAIL
)
2848 struct color_ext_type
*e
;
2849 struct color_ext_type
*e2
;
2852 _("unparsable value for LS_COLORS environment variable"));
2854 for (e
= color_ext_list
; e
!= nullptr; /* empty */)
2860 print_with_color
= false;
2864 /* Postprocess list to set EXACT_MATCH on entries where there are
2865 different cased extensions with separate sequences defined.
2866 Also set ext.len to SIZE_MAX on any entries that can't
2867 match due to precedence, to avoid redundant string compares. */
2868 struct color_ext_type
*e1
;
2870 for (e1
= color_ext_list
; e1
!= nullptr; e1
= e1
->next
)
2872 struct color_ext_type
*e2
;
2873 bool case_ignored
= false;
2875 for (e2
= e1
->next
; e2
!= nullptr; e2
= e2
->next
)
2877 if (e2
->ext
.len
< SIZE_MAX
&& e1
->ext
.len
== e2
->ext
.len
)
2879 if (memcmp (e1
->ext
.string
, e2
->ext
.string
, e1
->ext
.len
) == 0)
2880 e2
->ext
.len
= SIZE_MAX
; /* Ignore */
2881 else if (c_strncasecmp (e1
->ext
.string
, e2
->ext
.string
,
2886 e2
->ext
.len
= SIZE_MAX
; /* Ignore */
2888 else if (e1
->seq
.len
== e2
->seq
.len
2889 && memcmp (e1
->seq
.string
, e2
->seq
.string
,
2892 e2
->ext
.len
= SIZE_MAX
; /* Ignore */
2893 case_ignored
= true; /* Ignore all subsequent */
2897 e1
->exact_match
= true;
2898 e2
->exact_match
= true;
2906 if (color_indicator
[C_LINK
].len
== 6
2907 && !STRNCMP_LIT (color_indicator
[C_LINK
].string
, "target"))
2908 color_symlink_as_referent
= true;
2911 /* Return the quoting style specified by the environment variable
2912 QUOTING_STYLE if set and valid, -1 otherwise. */
2915 getenv_quoting_style (void)
2917 char const *q_style
= getenv ("QUOTING_STYLE");
2920 int i
= ARGMATCH (q_style
, quoting_style_args
, quoting_style_vals
);
2924 _("ignoring invalid value"
2925 " of environment variable QUOTING_STYLE: %s"),
2929 return quoting_style_vals
[i
];
2932 /* Set the exit status to report a failure. If SERIOUS, it is a
2933 serious failure; otherwise, it is merely a minor problem. */
2936 set_exit_status (bool serious
)
2939 exit_status
= LS_FAILURE
;
2940 else if (exit_status
== EXIT_SUCCESS
)
2941 exit_status
= LS_MINOR_PROBLEM
;
2944 /* Assuming a failure is serious if SERIOUS, use the printf-style
2945 MESSAGE to report the failure to access a file named FILE. Assume
2946 errno is set appropriately for the failure. */
2949 file_failure (bool serious
, char const *message
, char const *file
)
2951 error (0, errno
, message
, quoteaf (file
));
2952 set_exit_status (serious
);
2955 /* Request that the directory named NAME have its contents listed later.
2956 If REALNAME is nonzero, it will be used instead of NAME when the
2957 directory name is printed. This allows symbolic links to directories
2958 to be treated as regular directories but still be listed under their
2959 real names. NAME == nullptr is used to insert a marker entry for the
2960 directory named in REALNAME.
2961 If NAME is non-null, we use its dev/ino information to save
2962 a call to stat -- when doing a recursive (-R) traversal.
2963 COMMAND_LINE_ARG means this directory was mentioned on the command line. */
2966 queue_directory (char const *name
, char const *realname
, bool command_line_arg
)
2968 struct pending
*new = xmalloc (sizeof *new);
2969 new->realname
= realname
? xstrdup (realname
) : nullptr;
2970 new->name
= name
? xstrdup (name
) : nullptr;
2971 new->command_line_arg
= command_line_arg
;
2972 new->next
= pending_dirs
;
2976 /* Read directory NAME, and list the files in it.
2977 If REALNAME is nonzero, print its name instead of NAME;
2978 this is used for symbolic links to directories.
2979 COMMAND_LINE_ARG means this directory was mentioned on the command line. */
2982 print_dir (char const *name
, char const *realname
, bool command_line_arg
)
2985 struct dirent
*next
;
2986 uintmax_t total_blocks
= 0;
2987 static bool first
= true;
2990 dirp
= opendir (name
);
2993 file_failure (command_line_arg
, _("cannot open directory %s"), name
);
2999 struct stat dir_stat
;
3000 int fd
= dirfd (dirp
);
3002 /* If dirfd failed, endure the overhead of stat'ing by path */
3004 ? fstat_for_ino (fd
, &dir_stat
)
3005 : stat_for_ino (name
, &dir_stat
)) < 0)
3007 file_failure (command_line_arg
,
3008 _("cannot determine device and inode of %s"), name
);
3013 /* If we've already visited this dev/inode pair, warn that
3014 we've found a loop, and do not process this directory. */
3015 if (visit_dir (dir_stat
.st_dev
, dir_stat
.st_ino
))
3017 error (0, 0, _("%s: not listing already-listed directory"),
3020 set_exit_status (true);
3024 dev_ino_push (dir_stat
.st_dev
, dir_stat
.st_ino
);
3029 if (recursive
|| print_dir_name
)
3032 dired_outbyte ('\n');
3036 char *absolute_name
= nullptr;
3037 if (print_hyperlink
)
3039 absolute_name
= canonicalize_filename_mode (name
, CAN_MISSING
);
3040 if (! absolute_name
)
3041 file_failure (command_line_arg
,
3042 _("error canonicalizing %s"), name
);
3044 quote_name (realname
? realname
: name
, dirname_quoting_options
, -1,
3045 nullptr, true, &subdired_obstack
, absolute_name
);
3047 free (absolute_name
);
3049 dired_outstring (":\n");
3052 /* Read the directory entries, and insert the subfiles into the 'cwd_file'
3057 /* Set errno to zero so we can distinguish between a readdir failure
3058 and when readdir simply finds that there are no more entries. */
3060 next
= readdir (dirp
);
3061 /* Some readdir()s do not absorb ENOENT (dir deleted but open). */
3062 if (errno
== ENOENT
)
3066 if (! file_ignored (next
->d_name
))
3068 enum filetype type
= unknown
;
3070 #if HAVE_STRUCT_DIRENT_D_TYPE
3071 switch (next
->d_type
)
3073 case DT_BLK
: type
= blockdev
; break;
3074 case DT_CHR
: type
= chardev
; break;
3075 case DT_DIR
: type
= directory
; break;
3076 case DT_FIFO
: type
= fifo
; break;
3077 case DT_LNK
: type
= symbolic_link
; break;
3078 case DT_REG
: type
= normal
; break;
3079 case DT_SOCK
: type
= sock
; break;
3081 case DT_WHT
: type
= whiteout
; break;
3085 total_blocks
+= gobble_file (next
->d_name
, type
,
3086 RELIABLE_D_INO (next
),
3089 /* In this narrow case, print out each name right away, so
3090 ls uses constant memory while processing the entries of
3091 this directory. Useful when there are many (millions)
3092 of entries in a directory. */
3093 if (format
== one_per_line
&& sort_type
== sort_none
3094 && !print_block_size
&& !recursive
)
3096 /* We must call sort_files in spite of
3097 "sort_type == sort_none" for its initialization
3098 of the sorted_file vector. */
3100 print_current_files ();
3105 else if (errno
!= 0)
3107 file_failure (command_line_arg
, _("reading directory %s"), name
);
3108 if (errno
!= EOVERFLOW
)
3114 /* When processing a very large directory, and since we've inhibited
3115 interrupts, this loop would take so long that ls would be annoyingly
3116 uninterruptible. This ensures that it handles signals promptly. */
3120 if (closedir (dirp
) != 0)
3122 file_failure (command_line_arg
, _("closing directory %s"), name
);
3123 /* Don't return; print whatever we got. */
3126 /* Sort the directory contents. */
3129 /* If any member files are subdirectories, perhaps they should have their
3130 contents listed rather than being mentioned here as files. */
3133 extract_dirs_from_files (name
, false);
3135 if (format
== long_format
|| print_block_size
)
3137 char buf
[LONGEST_HUMAN_READABLE
+ 3];
3138 char *p
= human_readable (total_blocks
, buf
+ 1, human_output_opts
,
3139 ST_NBLOCKSIZE
, output_block_size
);
3140 char *pend
= p
+ strlen (p
);
3144 dired_outstring (_("total"));
3145 dired_outbuf (p
, pend
- p
);
3149 print_current_files ();
3152 /* Add 'pattern' to the list of patterns for which files that match are
3156 add_ignore_pattern (char const *pattern
)
3158 struct ignore_pattern
*ignore
;
3160 ignore
= xmalloc (sizeof *ignore
);
3161 ignore
->pattern
= pattern
;
3162 /* Add it to the head of the linked list. */
3163 ignore
->next
= ignore_patterns
;
3164 ignore_patterns
= ignore
;
3167 /* Return true if one of the PATTERNS matches FILE. */
3170 patterns_match (struct ignore_pattern
const *patterns
, char const *file
)
3172 struct ignore_pattern
const *p
;
3173 for (p
= patterns
; p
; p
= p
->next
)
3174 if (fnmatch (p
->pattern
, file
, FNM_PERIOD
) == 0)
3179 /* Return true if FILE should be ignored. */
3182 file_ignored (char const *name
)
3184 return ((ignore_mode
!= IGNORE_MINIMAL
3186 && (ignore_mode
== IGNORE_DEFAULT
|| ! name
[1 + (name
[1] == '.')]))
3187 || (ignore_mode
== IGNORE_DEFAULT
3188 && patterns_match (hide_patterns
, name
))
3189 || patterns_match (ignore_patterns
, name
));
3192 /* POSIX requires that a file size be printed without a sign, even
3193 when negative. Assume the typical case where negative sizes are
3194 actually positive values that have wrapped around. */
3197 unsigned_file_size (off_t size
)
3199 return size
+ (size
< 0) * ((uintmax_t) OFF_T_MAX
- OFF_T_MIN
+ 1);
3203 /* Return true if NAME has a capability (see linux/capability.h) */
3205 has_capability (char const *name
)
3210 cap_t cap_d
= cap_get_file (name
);
3211 if (cap_d
== nullptr)
3214 result
= cap_to_text (cap_d
, nullptr);
3219 /* check if human-readable capability string is empty */
3220 has_cap
= !!*result
;
3227 has_capability (MAYBE_UNUSED
char const *name
)
3234 /* Enter and remove entries in the table 'cwd_file'. */
3237 free_ent (struct fileinfo
*f
)
3241 free (f
->absolute_name
);
3242 if (f
->scontext
!= UNKNOWN_SECURITY_CONTEXT
)
3243 aclinfo_scontext_free (f
->scontext
);
3246 /* Empty the table of files. */
3250 for (size_t i
= 0; i
< cwd_n_used
; i
++)
3252 struct fileinfo
*f
= sorted_file
[i
];
3257 cwd_some_quoted
= false;
3258 any_has_acl
= false;
3259 inode_number_width
= 0;
3260 block_size_width
= 0;
3266 major_device_number_width
= 0;
3267 minor_device_number_width
= 0;
3268 file_size_width
= 0;
3271 /* Return true if ERR implies lack-of-support failure by a
3272 getxattr-calling function like file_has_acl. */
3274 errno_unsupported (int err
)
3276 return (err
== EINVAL
|| err
== ENOSYS
|| is_ENOTSUP (err
));
3279 /* Cache file_has_aclinfo failure, when it's trivial to do.
3280 Like file_has_aclinfo, but when F's st_dev says it's on a file
3281 system lacking ACL support, return 0 with ENOTSUP immediately. */
3283 file_has_aclinfo_cache (char const *file
, struct fileinfo
*f
,
3284 struct aclinfo
*ai
, int flags
)
3286 /* st_dev of the most recently processed device for which we've
3287 found that file_has_acl fails indicating lack of support. */
3288 static dev_t unsupported_device
;
3290 if (f
->stat
.st_dev
== unsupported_device
)
3296 int n
= file_has_aclinfo (file
, &f
->stat
, ai
, flags
);
3297 if (n
<= 0 && errno_unsupported (ai
->u
.err
))
3298 unsupported_device
= f
->stat
.st_dev
;
3302 /* Cache has_capability failure, when it's trivial to do.
3303 Like has_capability, but when F's st_dev says it's on a file
3304 system lacking capability support, return 0 with ENOTSUP immediately. */
3306 has_capability_cache (char const *file
, struct fileinfo
*f
)
3308 /* st_dev of the most recently processed device for which we've
3309 found that has_capability fails indicating lack of support. */
3310 static dev_t unsupported_device
;
3312 if (f
->stat
.st_dev
== unsupported_device
)
3318 bool b
= has_capability (file
);
3319 if ( !b
&& errno_unsupported (errno
))
3320 unsupported_device
= f
->stat
.st_dev
;
3325 needs_quoting (char const *name
)
3328 size_t len
= quotearg_buffer (test
, sizeof test
, name
, -1,
3329 filename_quoting_options
);
3330 return *name
!= *test
|| strlen (name
) != len
;
3333 /* Add a file to the current table of files.
3334 Verify that the file exists, and print an error message if it does not.
3335 Return the number of blocks that the file occupies. */
3337 gobble_file (char const *name
, enum filetype type
, ino_t inode
,
3338 bool command_line_arg
, char const *dirname
)
3340 uintmax_t blocks
= 0;
3343 /* An inode value prior to gobble_file necessarily came from readdir,
3344 which is not used for command line arguments. */
3345 affirm (! command_line_arg
|| inode
== NOT_AN_INODE_NUMBER
);
3347 if (cwd_n_used
== cwd_n_alloc
)
3349 cwd_file
= xnrealloc (cwd_file
, cwd_n_alloc
, 2 * sizeof *cwd_file
);
3353 f
= &cwd_file
[cwd_n_used
];
3354 memset (f
, '\0', sizeof *f
);
3355 f
->stat
.st_ino
= inode
;
3359 if ((! cwd_some_quoted
) && align_variable_outer_quotes
)
3361 /* Determine if any quoted for padding purposes. */
3362 f
->quoted
= needs_quoting (name
);
3364 cwd_some_quoted
= 1;
3367 if (command_line_arg
3369 || format_needs_stat
3370 /* When coloring a directory (we may know the type from
3371 direct.d_type), we have to stat it in order to indicate
3372 sticky and/or other-writable attributes. */
3373 || (type
== directory
&& print_with_color
3374 && (is_colored (C_OTHER_WRITABLE
)
3375 || is_colored (C_STICKY
)
3376 || is_colored (C_STICKY_OTHER_WRITABLE
)))
3377 /* When dereferencing symlinks, the inode and type must come from
3378 stat, but readdir provides the inode and type of lstat. */
3379 || ((print_inode
|| format_needs_type
)
3380 && (type
== symbolic_link
|| type
== unknown
)
3381 && (dereference
== DEREF_ALWAYS
3382 || color_symlink_as_referent
|| check_symlink_mode
))
3383 /* Command line dereferences are already taken care of by the above
3384 assertion that the inode number is not yet known. */
3385 || (print_inode
&& inode
== NOT_AN_INODE_NUMBER
)
3386 || (format_needs_type
3387 && (type
== unknown
|| command_line_arg
3388 /* --indicator-style=classify (aka -F)
3389 requires that we stat each regular file
3390 to see if it's executable. */
3391 || (type
== normal
&& (indicator_style
== classify
3392 /* This is so that --color ends up
3393 highlighting files with these mode
3394 bits set even when options like -F are
3395 not specified. Note we do a redundant
3396 stat in the very unlikely case where
3397 C_CAP is set but not the others. */
3398 || (print_with_color
3399 && (is_colored (C_EXEC
)
3400 || is_colored (C_SETUID
)
3401 || is_colored (C_SETGID
)
3402 || is_colored (C_CAP
)))
3406 /* Absolute name of this file. */
3411 if (name
[0] == '/' || dirname
[0] == 0)
3412 full_name
= (char *) name
;
3415 full_name
= alloca (strlen (name
) + strlen (dirname
) + 2);
3416 attach (full_name
, dirname
, name
);
3419 if (print_hyperlink
)
3421 f
->absolute_name
= canonicalize_filename_mode (full_name
,
3423 if (! f
->absolute_name
)
3424 file_failure (command_line_arg
,
3425 _("error canonicalizing %s"), full_name
);
3428 switch (dereference
)
3431 err
= do_stat (full_name
, &f
->stat
);
3435 case DEREF_COMMAND_LINE_ARGUMENTS
:
3436 case DEREF_COMMAND_LINE_SYMLINK_TO_DIR
:
3437 if (command_line_arg
)
3440 err
= do_stat (full_name
, &f
->stat
);
3443 if (dereference
== DEREF_COMMAND_LINE_ARGUMENTS
)
3446 need_lstat
= (err
< 0
3447 ? (errno
== ENOENT
|| errno
== ELOOP
)
3448 : ! S_ISDIR (f
->stat
.st_mode
));
3452 /* stat failed because of ENOENT || ELOOP, maybe indicating a
3453 non-traversable symlink. Or stat succeeded,
3454 FULL_NAME does not refer to a directory,
3455 and --dereference-command-line-symlink-to-dir is in effect.
3456 Fall through so that we call lstat instead. */
3460 default: /* DEREF_NEVER */
3461 err
= do_lstat (full_name
, &f
->stat
);
3468 /* Failure to stat a command line argument leads to
3469 an exit status of 2. For other files, stat failure
3470 provokes an exit status of 1. */
3471 file_failure (command_line_arg
,
3472 _("cannot access %s"), full_name
);
3474 f
->scontext
= UNKNOWN_SECURITY_CONTEXT
;
3476 if (command_line_arg
)
3479 f
->name
= xstrdup (name
);
3487 if (format
== long_format
|| print_scontext
)
3490 int n
= file_has_aclinfo_cache (full_name
, f
, &ai
,
3491 do_deref
? ACL_SYMLINK_FOLLOW
: 0);
3492 bool have_acl
= 0 < n
;
3493 bool have_scontext
= !ai
.scontext_err
;
3494 f
->acl_type
= (!have_scontext
&& !have_acl
3496 : (have_scontext
&& !have_acl
3497 ? ACL_T_LSM_CONTEXT_ONLY
3499 any_has_acl
|= f
->acl_type
!= ACL_T_NONE
;
3501 if (format
== long_format
&& n
< 0)
3502 error (0, ai
.u
.err
, "%s", quotef (full_name
));
3505 /* When requesting security context information, don't make
3506 ls fail just because the file (even a command line argument)
3507 isn't on the right type of file system. I.e., a getfilecon
3508 failure isn't in the same class as a stat failure. */
3510 && (! (is_ENOTSUP (ai
.scontext_err
)
3511 || ai
.scontext_err
== ENODATA
)))
3512 error (0, ai
.scontext_err
, "%s", quotef (full_name
));
3515 /* has_capability adds around 30% runtime to 'ls --color',
3516 so call it only if really needed. */
3518 && (type
== normal
|| S_ISREG (f
->stat
.st_mode
))
3519 && print_with_color
&& is_colored (C_CAP
)
3520 && aclinfo_has_xattr (&ai
, XATTR_NAME_CAPS
))
3521 f
->has_capability
= has_capability_cache (full_name
, f
);
3523 f
->scontext
= ai
.scontext
;
3524 ai
.scontext
= nullptr;
3528 if (S_ISLNK (f
->stat
.st_mode
)
3529 && (format
== long_format
|| check_symlink_mode
))
3531 struct stat linkstats
;
3533 get_link_name (full_name
, f
, command_line_arg
);
3535 /* Use the slower quoting path for this entry, though
3536 don't update CWD_SOME_QUOTED since alignment not affected. */
3537 if (f
->linkname
&& f
->quoted
== 0 && needs_quoting (f
->linkname
))
3540 /* Avoid following symbolic links when possible, i.e., when
3541 they won't be traced and when no indicator is needed. */
3543 && (file_type
<= indicator_style
|| check_symlink_mode
)
3544 && stat_for_mode (full_name
, &linkstats
) == 0)
3547 f
->linkmode
= linkstats
.st_mode
;
3551 if (S_ISLNK (f
->stat
.st_mode
))
3552 f
->filetype
= symbolic_link
;
3553 else if (S_ISDIR (f
->stat
.st_mode
))
3555 if (command_line_arg
&& !immediate_dirs
)
3556 f
->filetype
= arg_directory
;
3558 f
->filetype
= directory
;
3561 f
->filetype
= normal
;
3563 blocks
= STP_NBLOCKS (&f
->stat
);
3564 if (format
== long_format
|| print_block_size
)
3566 char buf
[LONGEST_HUMAN_READABLE
+ 1];
3567 int len
= mbswidth (human_readable (blocks
, buf
, human_output_opts
,
3568 ST_NBLOCKSIZE
, output_block_size
),
3570 if (block_size_width
< len
)
3571 block_size_width
= len
;
3574 if (format
== long_format
)
3578 int len
= format_user_width (f
->stat
.st_uid
);
3579 if (owner_width
< len
)
3585 int len
= format_group_width (f
->stat
.st_gid
);
3586 if (group_width
< len
)
3592 int len
= format_user_width (f
->stat
.st_author
);
3593 if (author_width
< len
)
3600 int len
= strlen (f
->scontext
);
3601 if (scontext_width
< len
)
3602 scontext_width
= len
;
3605 if (format
== long_format
)
3607 char b
[INT_BUFSIZE_BOUND (uintmax_t)];
3608 int b_len
= strlen (umaxtostr (f
->stat
.st_nlink
, b
));
3609 if (nlink_width
< b_len
)
3610 nlink_width
= b_len
;
3612 if (S_ISCHR (f
->stat
.st_mode
) || S_ISBLK (f
->stat
.st_mode
))
3614 char buf
[INT_BUFSIZE_BOUND (uintmax_t)];
3615 int len
= strlen (umaxtostr (major (f
->stat
.st_rdev
), buf
));
3616 if (major_device_number_width
< len
)
3617 major_device_number_width
= len
;
3618 len
= strlen (umaxtostr (minor (f
->stat
.st_rdev
), buf
));
3619 if (minor_device_number_width
< len
)
3620 minor_device_number_width
= len
;
3621 len
= major_device_number_width
+ 2 + minor_device_number_width
;
3622 if (file_size_width
< len
)
3623 file_size_width
= len
;
3627 char buf
[LONGEST_HUMAN_READABLE
+ 1];
3628 uintmax_t size
= unsigned_file_size (f
->stat
.st_size
);
3629 int len
= mbswidth (human_readable (size
, buf
,
3630 file_human_output_opts
,
3631 1, file_output_block_size
),
3633 if (file_size_width
< len
)
3634 file_size_width
= len
;
3641 char buf
[INT_BUFSIZE_BOUND (uintmax_t)];
3642 int len
= strlen (umaxtostr (f
->stat
.st_ino
, buf
));
3643 if (inode_number_width
< len
)
3644 inode_number_width
= len
;
3647 f
->name
= xstrdup (name
);
3653 /* Return true if F refers to a directory. */
3655 is_directory (const struct fileinfo
*f
)
3657 return f
->filetype
== directory
|| f
->filetype
== arg_directory
;
3660 /* Return true if F refers to a (symlinked) directory. */
3662 is_linked_directory (const struct fileinfo
*f
)
3664 return f
->filetype
== directory
|| f
->filetype
== arg_directory
3665 || S_ISDIR (f
->linkmode
);
3668 /* Put the name of the file that FILENAME is a symbolic link to
3669 into the LINKNAME field of 'f'. COMMAND_LINE_ARG indicates whether
3670 FILENAME is a command-line argument. */
3673 get_link_name (char const *filename
, struct fileinfo
*f
, bool command_line_arg
)
3675 f
->linkname
= areadlink_with_size (filename
, f
->stat
.st_size
);
3676 if (f
->linkname
== nullptr)
3677 file_failure (command_line_arg
, _("cannot read symbolic link %s"),
3681 /* Return true if the last component of NAME is '.' or '..'
3682 This is so we don't try to recurse on '././././. ...' */
3685 basename_is_dot_or_dotdot (char const *name
)
3687 char const *base
= last_component (name
);
3688 return dot_or_dotdot (base
);
3691 /* Remove any entries from CWD_FILE that are for directories,
3692 and queue them to be listed as directories instead.
3693 DIRNAME is the prefix to prepend to each dirname
3694 to make it correct relative to ls's working dir;
3695 if it is null, no prefix is needed and "." and ".." should not be ignored.
3696 If COMMAND_LINE_ARG is true, this directory was mentioned at the top level,
3697 This is desirable when processing directories recursively. */
3700 extract_dirs_from_files (char const *dirname
, bool command_line_arg
)
3704 bool ignore_dot_and_dot_dot
= (dirname
!= nullptr);
3706 if (dirname
&& LOOP_DETECT
)
3708 /* Insert a marker entry first. When we dequeue this marker entry,
3709 we'll know that DIRNAME has been processed and may be removed
3710 from the set of active directories. */
3711 queue_directory (nullptr, dirname
, false);
3714 /* Queue the directories last one first, because queueing reverses the
3716 for (i
= cwd_n_used
; i
-- != 0; )
3718 struct fileinfo
*f
= sorted_file
[i
];
3720 if (is_directory (f
)
3721 && (! ignore_dot_and_dot_dot
3722 || ! basename_is_dot_or_dotdot (f
->name
)))
3724 if (!dirname
|| f
->name
[0] == '/')
3725 queue_directory (f
->name
, f
->linkname
, command_line_arg
);
3728 char *name
= file_name_concat (dirname
, f
->name
, nullptr);
3729 queue_directory (name
, f
->linkname
, command_line_arg
);
3732 if (f
->filetype
== arg_directory
)
3737 /* Now delete the directories from the table, compacting all the remaining
3740 for (i
= 0, j
= 0; i
< cwd_n_used
; i
++)
3742 struct fileinfo
*f
= sorted_file
[i
];
3744 j
+= (f
->filetype
!= arg_directory
);
3749 /* Use strcoll to compare strings in this locale. If an error occurs,
3750 report an error and longjmp to failed_strcoll. */
3752 static jmp_buf failed_strcoll
;
3755 xstrcoll (char const *a
, char const *b
)
3759 diff
= strcoll (a
, b
);
3762 error (0, errno
, _("cannot compare file names %s and %s"),
3763 quote_n (0, a
), quote_n (1, b
));
3764 set_exit_status (false);
3765 longjmp (failed_strcoll
, 1);
3770 /* Comparison routines for sorting the files. */
3772 typedef void const *V
;
3773 typedef int (*qsortFunc
)(V a
, V b
);
3775 /* Used below in DEFINE_SORT_FUNCTIONS for _df_ sort function variants. */
3777 dirfirst_check (struct fileinfo
const *a
, struct fileinfo
const *b
,
3780 int diff
= is_linked_directory (b
) - is_linked_directory (a
);
3781 return diff
? diff
: cmp (a
, b
);
3784 /* Define the 8 different sort function variants required for each sortkey.
3785 KEY_NAME is a token describing the sort key, e.g., ctime, atime, size.
3786 KEY_CMP_FUNC is a function to compare records based on that key, e.g.,
3787 ctime_cmp, atime_cmp, size_cmp. Append KEY_NAME to the string,
3788 '[rev_][x]str{cmp|coll}[_df]_', to create each function name. */
3789 #define DEFINE_SORT_FUNCTIONS(key_name, key_cmp_func) \
3790 /* direct, non-dirfirst versions */ \
3791 static int xstrcoll_##key_name (V a, V b) \
3792 { return key_cmp_func (a, b, xstrcoll); } \
3793 ATTRIBUTE_PURE static int strcmp_##key_name (V a, V b) \
3794 { return key_cmp_func (a, b, strcmp); } \
3796 /* reverse, non-dirfirst versions */ \
3797 static int rev_xstrcoll_##key_name (V a, V b) \
3798 { return key_cmp_func (b, a, xstrcoll); } \
3799 ATTRIBUTE_PURE static int rev_strcmp_##key_name (V a, V b) \
3800 { return key_cmp_func (b, a, strcmp); } \
3802 /* direct, dirfirst versions */ \
3803 static int xstrcoll_df_##key_name (V a, V b) \
3804 { return dirfirst_check (a, b, xstrcoll_##key_name); } \
3805 ATTRIBUTE_PURE static int strcmp_df_##key_name (V a, V b) \
3806 { return dirfirst_check (a, b, strcmp_##key_name); } \
3808 /* reverse, dirfirst versions */ \
3809 static int rev_xstrcoll_df_##key_name (V a, V b) \
3810 { return dirfirst_check (a, b, rev_xstrcoll_##key_name); } \
3811 ATTRIBUTE_PURE static int rev_strcmp_df_##key_name (V a, V b) \
3812 { return dirfirst_check (a, b, rev_strcmp_##key_name); }
3815 cmp_ctime (struct fileinfo
const *a
, struct fileinfo
const *b
,
3816 int (*cmp
) (char const *, char const *))
3818 int diff
= timespec_cmp (get_stat_ctime (&b
->stat
),
3819 get_stat_ctime (&a
->stat
));
3820 return diff
? diff
: cmp (a
->name
, b
->name
);
3824 cmp_mtime (struct fileinfo
const *a
, struct fileinfo
const *b
,
3825 int (*cmp
) (char const *, char const *))
3827 int diff
= timespec_cmp (get_stat_mtime (&b
->stat
),
3828 get_stat_mtime (&a
->stat
));
3829 return diff
? diff
: cmp (a
->name
, b
->name
);
3833 cmp_atime (struct fileinfo
const *a
, struct fileinfo
const *b
,
3834 int (*cmp
) (char const *, char const *))
3836 int diff
= timespec_cmp (get_stat_atime (&b
->stat
),
3837 get_stat_atime (&a
->stat
));
3838 return diff
? diff
: cmp (a
->name
, b
->name
);
3842 cmp_btime (struct fileinfo
const *a
, struct fileinfo
const *b
,
3843 int (*cmp
) (char const *, char const *))
3845 int diff
= timespec_cmp (get_stat_btime (&b
->stat
),
3846 get_stat_btime (&a
->stat
));
3847 return diff
? diff
: cmp (a
->name
, b
->name
);
3851 off_cmp (off_t a
, off_t b
)
3853 return (a
> b
) - (a
< b
);
3857 cmp_size (struct fileinfo
const *a
, struct fileinfo
const *b
,
3858 int (*cmp
) (char const *, char const *))
3860 int diff
= off_cmp (b
->stat
.st_size
, a
->stat
.st_size
);
3861 return diff
? diff
: cmp (a
->name
, b
->name
);
3865 cmp_name (struct fileinfo
const *a
, struct fileinfo
const *b
,
3866 int (*cmp
) (char const *, char const *))
3868 return cmp (a
->name
, b
->name
);
3871 /* Compare file extensions. Files with no extension are 'smallest'.
3872 If extensions are the same, compare by file names instead. */
3875 cmp_extension (struct fileinfo
const *a
, struct fileinfo
const *b
,
3876 int (*cmp
) (char const *, char const *))
3878 char const *base1
= strrchr (a
->name
, '.');
3879 char const *base2
= strrchr (b
->name
, '.');
3880 int diff
= cmp (base1
? base1
: "", base2
? base2
: "");
3881 return diff
? diff
: cmp (a
->name
, b
->name
);
3884 /* Return the (cached) screen width,
3885 for the NAME associated with the passed fileinfo F. */
3888 fileinfo_name_width (struct fileinfo
const *f
)
3892 : quote_name_width (f
->name
, filename_quoting_options
, f
->quoted
);
3896 cmp_width (struct fileinfo
const *a
, struct fileinfo
const *b
,
3897 int (*cmp
) (char const *, char const *))
3899 int diff
= fileinfo_name_width (a
) - fileinfo_name_width (b
);
3900 return diff
? diff
: cmp (a
->name
, b
->name
);
3903 DEFINE_SORT_FUNCTIONS (ctime
, cmp_ctime
)
3904 DEFINE_SORT_FUNCTIONS (mtime
, cmp_mtime
)
3905 DEFINE_SORT_FUNCTIONS (atime
, cmp_atime
)
3906 DEFINE_SORT_FUNCTIONS (btime
, cmp_btime
)
3907 DEFINE_SORT_FUNCTIONS (size
, cmp_size
)
3908 DEFINE_SORT_FUNCTIONS (name
, cmp_name
)
3909 DEFINE_SORT_FUNCTIONS (extension
, cmp_extension
)
3910 DEFINE_SORT_FUNCTIONS (width
, cmp_width
)
3912 /* Compare file versions.
3913 Unlike the other compare functions, cmp_version does not fail
3914 because filevercmp and strcmp do not fail; cmp_version uses strcmp
3915 instead of xstrcoll because filevercmp is locale-independent so
3916 strcmp is its appropriate secondary.
3918 All the other sort options need xstrcoll and strcmp variants,
3919 because they all use xstrcoll (either as the primary or secondary
3920 sort key), and xstrcoll has the ability to do a longjmp if strcoll fails for
3923 cmp_version (struct fileinfo
const *a
, struct fileinfo
const *b
)
3925 int diff
= filevercmp (a
->name
, b
->name
);
3926 return diff
? diff
: strcmp (a
->name
, b
->name
);
3930 xstrcoll_version (V a
, V b
)
3932 return cmp_version (a
, b
);
3935 rev_xstrcoll_version (V a
, V b
)
3937 return cmp_version (b
, a
);
3940 xstrcoll_df_version (V a
, V b
)
3942 return dirfirst_check (a
, b
, xstrcoll_version
);
3945 rev_xstrcoll_df_version (V a
, V b
)
3947 return dirfirst_check (a
, b
, rev_xstrcoll_version
);
3951 /* We have 2^3 different variants for each sort-key function
3952 (for 3 independent sort modes).
3953 The function pointers stored in this array must be dereferenced as:
3955 sort_variants[sort_key][use_strcmp][reverse][dirs_first]
3957 Note that the order in which sort keys are listed in the function pointer
3958 array below is defined by the order of the elements in the time_type and
3961 #define LIST_SORTFUNCTION_VARIANTS(key_name) \
3964 { xstrcoll_##key_name, xstrcoll_df_##key_name }, \
3965 { rev_xstrcoll_##key_name, rev_xstrcoll_df_##key_name }, \
3968 { strcmp_##key_name, strcmp_df_##key_name }, \
3969 { rev_strcmp_##key_name, rev_strcmp_df_##key_name }, \
3973 static qsortFunc
const sort_functions
[][2][2][2] =
3975 LIST_SORTFUNCTION_VARIANTS (name
),
3976 LIST_SORTFUNCTION_VARIANTS (extension
),
3977 LIST_SORTFUNCTION_VARIANTS (width
),
3978 LIST_SORTFUNCTION_VARIANTS (size
),
3982 { xstrcoll_version
, xstrcoll_df_version
},
3983 { rev_xstrcoll_version
, rev_xstrcoll_df_version
},
3986 /* We use nullptr for the strcmp variants of version comparison
3987 since as explained in cmp_version definition, version comparison
3988 does not rely on xstrcoll, so it will never longjmp, and never
3989 need to try the strcmp fallback. */
3991 { nullptr, nullptr },
3992 { nullptr, nullptr },
3996 /* last are time sort functions */
3997 LIST_SORTFUNCTION_VARIANTS (mtime
),
3998 LIST_SORTFUNCTION_VARIANTS (ctime
),
3999 LIST_SORTFUNCTION_VARIANTS (atime
),
4000 LIST_SORTFUNCTION_VARIANTS (btime
)
4003 /* The number of sort keys is calculated as the sum of
4004 the number of elements in the sort_type enum (i.e., sort_numtypes)
4005 -2 because neither sort_time nor sort_none use entries themselves
4006 the number of elements in the time_type enum (i.e., time_numtypes)
4007 This is because when sort_type==sort_time, we have up to
4008 time_numtypes possible sort keys.
4010 This line verifies at compile-time that the array of sort functions has been
4011 initialized for all possible sort keys. */
4012 static_assert (ARRAY_CARDINALITY (sort_functions
)
4013 == sort_numtypes
- 2 + time_numtypes
);
4015 /* Set up SORTED_FILE to point to the in-use entries in CWD_FILE, in order. */
4018 initialize_ordering_vector (void)
4020 for (size_t i
= 0; i
< cwd_n_used
; i
++)
4021 sorted_file
[i
] = &cwd_file
[i
];
4024 /* Cache values based on attributes global to all files. */
4027 update_current_files_info (void)
4029 /* Cache screen width of name, if needed multiple times. */
4030 if (sort_type
== sort_width
4031 || (line_length
&& (format
== many_per_line
|| format
== horizontal
)))
4034 for (i
= 0; i
< cwd_n_used
; i
++)
4036 struct fileinfo
*f
= sorted_file
[i
];
4037 f
->width
= fileinfo_name_width (f
);
4042 /* Sort the files now in the table. */
4049 if (sorted_file_alloc
< cwd_n_used
+ cwd_n_used
/ 2)
4052 sorted_file
= xnmalloc (cwd_n_used
, 3 * sizeof *sorted_file
);
4053 sorted_file_alloc
= 3 * cwd_n_used
;
4056 initialize_ordering_vector ();
4058 update_current_files_info ();
4060 if (sort_type
== sort_none
)
4063 /* Try strcoll. If it fails, fall back on strcmp. We can't safely
4064 ignore strcoll failures, as a failing strcoll might be a
4065 comparison function that is not a total order, and if we ignored
4066 the failure this might cause qsort to dump core. */
4068 if (! setjmp (failed_strcoll
))
4069 use_strcmp
= false; /* strcoll() succeeded */
4073 affirm (sort_type
!= sort_version
);
4074 initialize_ordering_vector ();
4077 /* When sort_type == sort_time, use time_type as subindex. */
4078 mpsort ((void const **) sorted_file
, cwd_n_used
,
4079 sort_functions
[sort_type
+ (sort_type
== sort_time
? time_type
: 0)]
4080 [use_strcmp
][sort_reverse
]
4081 [directories_first
]);
4084 /* List all the files now in the table. */
4087 print_current_files (void)
4094 for (i
= 0; i
< cwd_n_used
; i
++)
4096 print_file_name_and_frills (sorted_file
[i
], 0);
4103 print_with_separator (' ');
4105 print_many_per_line ();
4110 print_with_separator (' ');
4112 print_horizontal ();
4116 print_with_separator (',');
4120 for (i
= 0; i
< cwd_n_used
; i
++)
4122 set_normal_color ();
4123 print_long_format (sorted_file
[i
]);
4124 dired_outbyte (eolbyte
);
4130 /* Replace the first %b with precomputed aligned month names.
4131 Note on glibc-2.7 at least, this speeds up the whole 'ls -lU'
4132 process by around 17%, compared to letting strftime() handle the %b. */
4135 align_nstrftime (char *buf
, size_t size
, bool recent
, struct tm
const *tm
,
4136 timezone_t tz
, int ns
)
4138 char const *nfmt
= (use_abformat
4139 ? abformat
[recent
][tm
->tm_mon
]
4140 : long_time_format
[recent
]);
4141 return nstrftime (buf
, size
, nfmt
, tm
, tz
, ns
);
4144 /* Return the expected number of columns in a long-format timestamp,
4145 or zero if it cannot be calculated. */
4148 long_time_expected_width (void)
4150 static int width
= -1;
4156 char buf
[TIME_STAMP_LEN_MAXIMUM
+ 1];
4158 /* In case you're wondering if localtime_rz can fail with an input time_t
4159 value of 0, let's just say it's very unlikely, but not inconceivable.
4160 The TZ environment variable would have to specify a time zone that
4161 is 2**31-1900 years or more ahead of UTC. This could happen only on
4162 a 64-bit system that blindly accepts e.g., TZ=UTC+20000000000000.
4163 However, this is not possible with Solaris 10 or glibc-2.3.5, since
4164 their implementations limit the offset to 167:59 and 24:00, resp. */
4165 if (localtime_rz (localtz
, &epoch
, &tm
))
4167 size_t len
= align_nstrftime (buf
, sizeof buf
, false,
4170 width
= mbsnwidth (buf
, len
, MBSWIDTH_FLAGS
);
4180 /* Print the user or group name NAME, with numeric id ID, using a
4181 print width of WIDTH columns. */
4184 format_user_or_group (char const *name
, uintmax_t id
, int width
)
4188 int name_width
= mbswidth (name
, MBSWIDTH_FLAGS
);
4189 int width_gap
= name_width
< 0 ? 0 : width
- name_width
;
4190 int pad
= MAX (0, width_gap
);
4191 dired_outstring (name
);
4194 dired_outbyte (' ');
4198 dired_pos
+= printf ("%*ju ", width
, id
);
4201 /* Print the name or id of the user with id U, using a print width of
4205 format_user (uid_t u
, int width
, bool stat_ok
)
4207 format_user_or_group (! stat_ok
? "?" :
4208 (numeric_ids
? nullptr : getuser (u
)), u
, width
);
4211 /* Likewise, for groups. */
4214 format_group (gid_t g
, int width
, bool stat_ok
)
4216 format_user_or_group (! stat_ok
? "?" :
4217 (numeric_ids
? nullptr : getgroup (g
)), g
, width
);
4220 /* Return the number of columns that format_user_or_group will print,
4221 or -1 if unknown. */
4224 format_user_or_group_width (char const *name
, uintmax_t id
)
4227 ? mbswidth (name
, MBSWIDTH_FLAGS
)
4228 : snprintf (nullptr, 0, "%ju", id
));
4231 /* Return the number of columns that format_user will print,
4232 or -1 if unknown. */
4235 format_user_width (uid_t u
)
4237 return format_user_or_group_width (numeric_ids
? nullptr : getuser (u
), u
);
4240 /* Likewise, for groups. */
4243 format_group_width (gid_t g
)
4245 return format_user_or_group_width (numeric_ids
? nullptr : getgroup (g
), g
);
4248 /* Return a pointer to a formatted version of F->stat.st_ino,
4249 possibly using buffer, which must be at least
4250 INT_BUFSIZE_BOUND (uintmax_t) bytes. */
4252 format_inode (char buf
[INT_BUFSIZE_BOUND (uintmax_t)],
4253 const struct fileinfo
*f
)
4255 return (f
->stat_ok
&& f
->stat
.st_ino
!= NOT_AN_INODE_NUMBER
4256 ? umaxtostr (f
->stat
.st_ino
, buf
)
4260 /* Print information about F in long format. */
4262 print_long_format (const struct fileinfo
*f
)
4266 [LONGEST_HUMAN_READABLE
+ 1 /* inode */
4267 + LONGEST_HUMAN_READABLE
+ 1 /* size in blocks */
4268 + sizeof (modebuf
) - 1 + 1 /* mode string */
4269 + INT_BUFSIZE_BOUND (uintmax_t) /* st_nlink */
4270 + LONGEST_HUMAN_READABLE
+ 2 /* major device number */
4271 + LONGEST_HUMAN_READABLE
+ 1 /* minor device number */
4272 + TIME_STAMP_LEN_MAXIMUM
+ 1 /* max length of time/date */
4276 struct timespec when_timespec
;
4277 struct tm when_local
;
4278 bool btime_ok
= true;
4280 /* Compute the mode string, except remove the trailing space if no
4281 file in this directory has an ACL or security context. */
4283 filemodestring (&f
->stat
, modebuf
);
4286 modebuf
[0] = filetype_letter
[f
->filetype
];
4287 memset (modebuf
+ 1, '?', 10);
4292 else if (f
->acl_type
== ACL_T_LSM_CONTEXT_ONLY
)
4294 else if (f
->acl_type
== ACL_T_YES
)
4300 when_timespec
= get_stat_ctime (&f
->stat
);
4303 when_timespec
= get_stat_mtime (&f
->stat
);
4306 when_timespec
= get_stat_atime (&f
->stat
);
4309 when_timespec
= get_stat_btime (&f
->stat
);
4310 if (when_timespec
.tv_sec
== -1 && when_timespec
.tv_nsec
== -1)
4321 char hbuf
[INT_BUFSIZE_BOUND (uintmax_t)];
4322 p
+= sprintf (p
, "%*s ", inode_number_width
, format_inode (hbuf
, f
));
4325 if (print_block_size
)
4327 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
4328 char const *blocks
=
4331 : human_readable (STP_NBLOCKS (&f
->stat
), hbuf
, human_output_opts
,
4332 ST_NBLOCKSIZE
, output_block_size
));
4333 int blocks_width
= mbswidth (blocks
, MBSWIDTH_FLAGS
);
4334 for (int pad
= blocks_width
< 0 ? 0 : block_size_width
- blocks_width
;
4337 while ((*p
++ = *blocks
++))
4342 /* The last byte of the mode string is the POSIX
4343 "optional alternate access method flag". */
4345 char hbuf
[INT_BUFSIZE_BOUND (uintmax_t)];
4346 p
+= sprintf (p
, "%s %*s ", modebuf
, nlink_width
,
4347 ! f
->stat_ok
? "?" : umaxtostr (f
->stat
.st_nlink
, hbuf
));
4352 if (print_owner
|| print_group
|| print_author
|| print_scontext
)
4354 dired_outbuf (buf
, p
- buf
);
4357 format_user (f
->stat
.st_uid
, owner_width
, f
->stat_ok
);
4360 format_group (f
->stat
.st_gid
, group_width
, f
->stat_ok
);
4363 format_user (f
->stat
.st_author
, author_width
, f
->stat_ok
);
4366 format_user_or_group (f
->scontext
, 0, scontext_width
);
4372 && (S_ISCHR (f
->stat
.st_mode
) || S_ISBLK (f
->stat
.st_mode
)))
4374 char majorbuf
[INT_BUFSIZE_BOUND (uintmax_t)];
4375 char minorbuf
[INT_BUFSIZE_BOUND (uintmax_t)];
4376 int blanks_width
= (file_size_width
4377 - (major_device_number_width
+ 2
4378 + minor_device_number_width
));
4379 p
+= sprintf (p
, "%*s, %*s ",
4380 major_device_number_width
+ MAX (0, blanks_width
),
4381 umaxtostr (major (f
->stat
.st_rdev
), majorbuf
),
4382 minor_device_number_width
,
4383 umaxtostr (minor (f
->stat
.st_rdev
), minorbuf
));
4387 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
4391 : human_readable (unsigned_file_size (f
->stat
.st_size
),
4392 hbuf
, file_human_output_opts
, 1,
4393 file_output_block_size
));
4394 int size_width
= mbswidth (size
, MBSWIDTH_FLAGS
);
4395 for (int pad
= size_width
< 0 ? 0 : file_size_width
- size_width
;
4398 while ((*p
++ = *size
++))
4406 if (f
->stat_ok
&& btime_ok
4407 && localtime_rz (localtz
, &when_timespec
.tv_sec
, &when_local
))
4409 struct timespec six_months_ago
;
4412 /* If the file appears to be in the future, update the current
4413 time, in case the file happens to have been modified since
4414 the last time we checked the clock. */
4415 if (timespec_cmp (current_time
, when_timespec
) < 0)
4416 gettime (¤t_time
);
4418 /* Consider a time to be recent if it is within the past six months.
4419 A Gregorian year has 365.2425 * 24 * 60 * 60 == 31556952 seconds
4420 on the average. Write this value as an integer constant to
4421 avoid floating point hassles. */
4422 six_months_ago
.tv_sec
= current_time
.tv_sec
- 31556952 / 2;
4423 six_months_ago
.tv_nsec
= current_time
.tv_nsec
;
4425 recent
= (timespec_cmp (six_months_ago
, when_timespec
) < 0
4426 && timespec_cmp (when_timespec
, current_time
) < 0);
4428 /* We assume here that all time zones are offset from UTC by a
4429 whole number of seconds. */
4430 s
= align_nstrftime (p
, TIME_STAMP_LEN_MAXIMUM
+ 1, recent
,
4431 &when_local
, localtz
, when_timespec
.tv_nsec
);
4441 /* The time cannot be converted using the desired format, so
4442 print it as a huge integer number of seconds. */
4443 char hbuf
[INT_BUFSIZE_BOUND (intmax_t)];
4444 p
+= sprintf (p
, "%*s ", long_time_expected_width (),
4445 (! f
->stat_ok
|| ! btime_ok
4447 : timetostr (when_timespec
.tv_sec
, hbuf
)));
4448 /* FIXME: (maybe) We discarded when_timespec.tv_nsec. */
4451 dired_outbuf (buf
, p
- buf
);
4452 size_t w
= print_name_with_quoting (f
, false, &dired_obstack
, p
- buf
);
4454 if (f
->filetype
== symbolic_link
)
4458 dired_outstring (" -> ");
4459 print_name_with_quoting (f
, true, nullptr, (p
- buf
) + w
+ 4);
4460 if (indicator_style
!= none
)
4461 print_type_indicator (true, f
->linkmode
, unknown
);
4464 else if (indicator_style
!= none
)
4465 print_type_indicator (f
->stat_ok
, f
->stat
.st_mode
, f
->filetype
);
4468 /* Write to *BUF a quoted representation of the file name NAME, if non-null,
4469 using OPTIONS to control quoting. *BUF is set to NAME if no quoting
4470 is required. *BUF is allocated if more space required (and the original
4471 *BUF is not deallocated).
4472 Store the number of screen columns occupied by NAME's quoted
4473 representation into WIDTH, if non-null.
4474 Store into PAD whether an initial space is needed for padding.
4475 Return the number of bytes in *BUF. */
4478 quote_name_buf (char **inbuf
, size_t bufsize
, char *name
,
4479 struct quoting_options
const *options
,
4480 int needs_general_quoting
, size_t *width
, bool *pad
)
4483 size_t displayed_width
IF_LINT ( = 0);
4487 enum quoting_style qs
= get_quoting_style (options
);
4488 bool needs_further_quoting
= qmark_funny_chars
4489 && (qs
== shell_quoting_style
4490 || qs
== shell_always_quoting_style
4491 || qs
== literal_quoting_style
);
4493 if (needs_general_quoting
!= 0)
4495 len
= quotearg_buffer (buf
, bufsize
, name
, -1, options
);
4498 buf
= xmalloc (len
+ 1);
4499 quotearg_buffer (buf
, len
+ 1, name
, -1, options
);
4502 quoted
= (*name
!= *buf
) || strlen (name
) != len
;
4504 else if (needs_further_quoting
)
4506 len
= strlen (name
);
4508 buf
= xmalloc (len
+ 1);
4509 memcpy (buf
, name
, len
+ 1);
4515 len
= strlen (name
);
4520 if (needs_further_quoting
)
4524 char const *p
= buf
;
4525 char const *plimit
= buf
+ len
;
4527 displayed_width
= 0;
4532 case ' ': case '!': case '"': case '#': case '%':
4533 case '&': case '\'': case '(': case ')': case '*':
4534 case '+': case ',': case '-': case '.': case '/':
4535 case '0': case '1': case '2': case '3': case '4':
4536 case '5': case '6': case '7': case '8': case '9':
4537 case ':': case ';': case '<': case '=': case '>':
4539 case 'A': case 'B': case 'C': case 'D': case 'E':
4540 case 'F': case 'G': case 'H': case 'I': case 'J':
4541 case 'K': case 'L': case 'M': case 'N': case 'O':
4542 case 'P': case 'Q': case 'R': case 'S': case 'T':
4543 case 'U': case 'V': case 'W': case 'X': case 'Y':
4545 case '[': case '\\': case ']': case '^': case '_':
4546 case 'a': case 'b': case 'c': case 'd': case 'e':
4547 case 'f': case 'g': case 'h': case 'i': case 'j':
4548 case 'k': case 'l': case 'm': case 'n': case 'o':
4549 case 'p': case 'q': case 'r': case 's': case 't':
4550 case 'u': case 'v': case 'w': case 'x': case 'y':
4551 case 'z': case '{': case '|': case '}': case '~':
4552 /* These characters are printable ASCII characters. */
4554 displayed_width
+= 1;
4557 /* If we have a multibyte sequence, copy it until we
4558 reach its end, replacing each non-printable multibyte
4559 character with a single question mark. */
4561 mbstate_t mbstate
; mbszero (&mbstate
);
4568 bytes
= mbrtoc32 (&wc
, p
, plimit
- p
, &mbstate
);
4570 if (bytes
== (size_t) -1)
4572 /* An invalid multibyte sequence was
4573 encountered. Skip one input byte, and
4574 put a question mark. */
4577 displayed_width
+= 1;
4581 if (bytes
== (size_t) -2)
4583 /* An incomplete multibyte character
4584 at the end. Replace it entirely with
4588 displayed_width
+= 1;
4593 /* A null wide character was encountered. */
4599 /* A printable multibyte character.
4601 for (; bytes
> 0; --bytes
)
4603 displayed_width
+= w
;
4607 /* An nonprintable multibyte character.
4608 Replace it entirely with a question
4612 displayed_width
+= 1;
4615 while (! mbsinit (&mbstate
));
4620 /* The buffer may have shrunk. */
4626 char const *plimit
= buf
+ len
;
4630 if (! isprint (to_uchar (*p
)))
4634 displayed_width
= len
;
4637 else if (width
!= nullptr)
4641 displayed_width
= mbsnwidth (buf
, len
, MBSWIDTH_FLAGS
);
4642 displayed_width
= MAX (0, displayed_width
);
4646 char const *p
= buf
;
4647 char const *plimit
= buf
+ len
;
4649 displayed_width
= 0;
4652 if (isprint (to_uchar (*p
)))
4659 /* Set padding to better align quoted items,
4660 and also give a visual indication that quotes are
4661 not actually part of the name. */
4662 *pad
= (align_variable_outer_quotes
&& cwd_some_quoted
&& ! quoted
);
4664 if (width
!= nullptr)
4665 *width
= displayed_width
;
4673 quote_name_width (char const *name
, struct quoting_options
const *options
,
4674 int needs_general_quoting
)
4676 char smallbuf
[BUFSIZ
];
4677 char *buf
= smallbuf
;
4681 quote_name_buf (&buf
, sizeof smallbuf
, (char *) name
, options
,
4682 needs_general_quoting
, &width
, &pad
);
4684 if (buf
!= smallbuf
&& buf
!= name
)
4692 /* %XX escape any input out of range as defined in RFC3986,
4693 and also if PATH, convert all path separators to '/'. */
4695 file_escape (char const *str
, bool path
)
4697 char *esc
= xnmalloc (3, strlen (str
) + 1);
4701 if (path
&& ISSLASH (*str
))
4706 else if (RFC3986
[to_uchar (*str
)])
4709 p
+= sprintf (p
, "%%%02x", to_uchar (*str
++));
4716 quote_name (char const *name
, struct quoting_options
const *options
,
4717 int needs_general_quoting
, const struct bin_str
*color
,
4718 bool allow_pad
, struct obstack
*stack
, char const *absolute_name
)
4720 char smallbuf
[BUFSIZ
];
4721 char *buf
= smallbuf
;
4725 len
= quote_name_buf (&buf
, sizeof smallbuf
, (char *) name
, options
,
4726 needs_general_quoting
, nullptr, &pad
);
4728 if (pad
&& allow_pad
)
4729 dired_outbyte (' ');
4732 print_color_indicator (color
);
4734 /* If we're padding, then don't include the outer quotes in
4735 the --hyperlink, to improve the alignment of those links. */
4736 bool skip_quotes
= false;
4740 if (align_variable_outer_quotes
&& cwd_some_quoted
&& ! pad
)
4745 char *h
= file_escape (hostname
, /* path= */ false);
4746 char *n
= file_escape (absolute_name
, /* path= */ true);
4747 /* TODO: It would be good to be able to define parameters
4748 to give hints to the terminal as how best to render the URI.
4749 For example since ls is outputting a dense block of URIs
4750 it would be best to not underline by default, and only
4751 do so upon hover etc. */
4752 printf ("\033]8;;file://%s%s%s\a", h
, *n
== '/' ? "" : "/", n
);
4758 push_current_dired_pos (stack
);
4760 fwrite (buf
+ skip_quotes
, 1, len
- (skip_quotes
* 2), stdout
);
4765 push_current_dired_pos (stack
);
4769 fputs ("\033]8;;\a", stdout
);
4771 putchar (*(buf
+ len
- 1));
4774 if (buf
!= smallbuf
&& buf
!= name
)
4781 print_name_with_quoting (const struct fileinfo
*f
,
4782 bool symlink_target
,
4783 struct obstack
*stack
,
4786 char const *name
= symlink_target
? f
->linkname
: f
->name
;
4788 const struct bin_str
*color
4789 = print_with_color
? get_color_indicator (f
, symlink_target
) : nullptr;
4791 bool used_color_this_time
= (print_with_color
4792 && (color
|| is_colored (C_NORM
)));
4794 size_t len
= quote_name (name
, filename_quoting_options
, f
->quoted
,
4795 color
, !symlink_target
, stack
, f
->absolute_name
);
4798 if (used_color_this_time
)
4800 prep_non_filename_text ();
4802 /* We use the byte length rather than display width here as
4803 an optimization to avoid accurately calculating the width,
4804 because we only output the clear to EOL sequence if the name
4805 _might_ wrap to the next line. This may output a sequence
4806 unnecessarily in multi-byte locales for example,
4807 but in that case it's inconsequential to the output. */
4809 && (start_col
/ line_length
!= (start_col
+ len
- 1) / line_length
))
4810 put_indicator (&color_indicator
[C_CLR_TO_EOL
]);
4817 prep_non_filename_text (void)
4819 if (color_indicator
[C_END
].string
!= nullptr)
4820 put_indicator (&color_indicator
[C_END
]);
4823 put_indicator (&color_indicator
[C_LEFT
]);
4824 put_indicator (&color_indicator
[C_RESET
]);
4825 put_indicator (&color_indicator
[C_RIGHT
]);
4829 /* Print the file name of 'f' with appropriate quoting.
4830 Also print file size, inode number, and filetype indicator character,
4831 as requested by switches. */
4834 print_file_name_and_frills (const struct fileinfo
*f
, size_t start_col
)
4836 char buf
[MAX (LONGEST_HUMAN_READABLE
+ 1, INT_BUFSIZE_BOUND (uintmax_t))];
4838 set_normal_color ();
4841 printf ("%*s ", format
== with_commas
? 0 : inode_number_width
,
4842 format_inode (buf
, f
));
4844 if (print_block_size
)
4845 printf ("%*s ", format
== with_commas
? 0 : block_size_width
,
4847 : human_readable (STP_NBLOCKS (&f
->stat
), buf
, human_output_opts
,
4848 ST_NBLOCKSIZE
, output_block_size
));
4851 printf ("%*s ", format
== with_commas
? 0 : scontext_width
, f
->scontext
);
4853 size_t width
= print_name_with_quoting (f
, false, nullptr, start_col
);
4855 if (indicator_style
!= none
)
4856 width
+= print_type_indicator (f
->stat_ok
, f
->stat
.st_mode
, f
->filetype
);
4861 /* Given these arguments describing a file, return the single-byte
4862 type indicator, or 0. */
4864 get_type_indicator (bool stat_ok
, mode_t mode
, enum filetype type
)
4868 if (stat_ok
? S_ISREG (mode
) : type
== normal
)
4870 if (stat_ok
&& indicator_style
== classify
&& (mode
& S_IXUGO
))
4877 if (stat_ok
? S_ISDIR (mode
) : type
== directory
|| type
== arg_directory
)
4879 else if (indicator_style
== slash
)
4881 else if (stat_ok
? S_ISLNK (mode
) : type
== symbolic_link
)
4883 else if (stat_ok
? S_ISFIFO (mode
) : type
== fifo
)
4885 else if (stat_ok
? S_ISSOCK (mode
) : type
== sock
)
4887 else if (stat_ok
&& S_ISDOOR (mode
))
4896 print_type_indicator (bool stat_ok
, mode_t mode
, enum filetype type
)
4898 char c
= get_type_indicator (stat_ok
, mode
, type
);
4904 /* Returns if color sequence was printed. */
4906 print_color_indicator (const struct bin_str
*ind
)
4910 /* Need to reset so not dealing with attribute combinations */
4911 if (is_colored (C_NORM
))
4912 restore_default_color ();
4913 put_indicator (&color_indicator
[C_LEFT
]);
4914 put_indicator (ind
);
4915 put_indicator (&color_indicator
[C_RIGHT
]);
4918 return ind
!= nullptr;
4921 /* Returns color indicator or nullptr if none. */
4923 static const struct bin_str
*
4924 get_color_indicator (const struct fileinfo
*f
, bool symlink_target
)
4926 enum indicator_no type
;
4927 struct color_ext_type
*ext
; /* Color extension */
4928 size_t len
; /* Length of name */
4937 linkok
= f
->linkok
? 0 : -1;
4942 mode
= file_or_link_mode (f
);
4946 /* Is this a nonexistent file? If so, linkok == -1. */
4948 if (linkok
== -1 && is_colored (C_MISSING
))
4950 else if (!f
->stat_ok
)
4952 static enum indicator_no filetype_indicator
[] = FILETYPE_INDICATORS
;
4953 type
= filetype_indicator
[f
->filetype
];
4961 if ((mode
& S_ISUID
) != 0 && is_colored (C_SETUID
))
4963 else if ((mode
& S_ISGID
) != 0 && is_colored (C_SETGID
))
4965 else if (is_colored (C_CAP
) && f
->has_capability
)
4967 else if ((mode
& S_IXUGO
) != 0 && is_colored (C_EXEC
))
4969 else if ((1 < f
->stat
.st_nlink
) && is_colored (C_MULTIHARDLINK
))
4970 type
= C_MULTIHARDLINK
;
4972 else if (S_ISDIR (mode
))
4976 if ((mode
& S_ISVTX
) && (mode
& S_IWOTH
)
4977 && is_colored (C_STICKY_OTHER_WRITABLE
))
4978 type
= C_STICKY_OTHER_WRITABLE
;
4979 else if ((mode
& S_IWOTH
) != 0 && is_colored (C_OTHER_WRITABLE
))
4980 type
= C_OTHER_WRITABLE
;
4981 else if ((mode
& S_ISVTX
) != 0 && is_colored (C_STICKY
))
4984 else if (S_ISLNK (mode
))
4986 else if (S_ISFIFO (mode
))
4988 else if (S_ISSOCK (mode
))
4990 else if (S_ISBLK (mode
))
4992 else if (S_ISCHR (mode
))
4994 else if (S_ISDOOR (mode
))
4998 /* Classify a file of some other type as C_ORPHAN. */
5003 /* Check the file's suffix only if still classified as C_FILE. */
5007 /* Test if NAME has a recognized suffix. */
5009 len
= strlen (name
);
5010 name
+= len
; /* Pointer to final \0. */
5011 for (ext
= color_ext_list
; ext
!= nullptr; ext
= ext
->next
)
5013 if (ext
->ext
.len
<= len
)
5015 if (ext
->exact_match
)
5017 if (STREQ_LEN (name
- ext
->ext
.len
, ext
->ext
.string
,
5023 if (c_strncasecmp (name
- ext
->ext
.len
, ext
->ext
.string
,
5031 /* Adjust the color for orphaned symlinks. */
5032 if (type
== C_LINK
&& !linkok
)
5034 if (color_symlink_as_referent
|| is_colored (C_ORPHAN
))
5038 const struct bin_str
*const s
5039 = ext
? &(ext
->seq
) : &color_indicator
[type
];
5041 return s
->string
? s
: nullptr;
5044 /* Output a color indicator (which may contain nulls). */
5046 put_indicator (const struct bin_str
*ind
)
5052 /* If the standard output is a controlling terminal, watch out
5053 for signals, so that the colors can be restored to the
5054 default state if "ls" is suspended or interrupted. */
5056 if (0 <= tcgetpgrp (STDOUT_FILENO
))
5059 prep_non_filename_text ();
5062 fwrite (ind
->string
, ind
->len
, 1, stdout
);
5066 length_of_file_name_and_frills (const struct fileinfo
*f
)
5069 char buf
[MAX (LONGEST_HUMAN_READABLE
+ 1, INT_BUFSIZE_BOUND (uintmax_t))];
5072 len
+= 1 + (format
== with_commas
5073 ? strlen (umaxtostr (f
->stat
.st_ino
, buf
))
5074 : inode_number_width
);
5076 if (print_block_size
)
5077 len
+= 1 + (format
== with_commas
5078 ? strlen (! f
->stat_ok
? "?"
5079 : human_readable (STP_NBLOCKS (&f
->stat
), buf
,
5080 human_output_opts
, ST_NBLOCKSIZE
,
5082 : block_size_width
);
5085 len
+= 1 + (format
== with_commas
? strlen (f
->scontext
) : scontext_width
);
5087 len
+= fileinfo_name_width (f
);
5089 if (indicator_style
!= none
)
5091 char c
= get_type_indicator (f
->stat_ok
, f
->stat
.st_mode
, f
->filetype
);
5099 print_many_per_line (void)
5101 size_t row
; /* Current row. */
5102 size_t cols
= calculate_columns (true);
5103 struct column_info
const *line_fmt
= &column_info
[cols
- 1];
5105 /* Calculate the number of rows that will be in each column except possibly
5106 for a short column on the right. */
5107 size_t rows
= cwd_n_used
/ cols
+ (cwd_n_used
% cols
!= 0);
5109 for (row
= 0; row
< rows
; row
++)
5112 size_t filesno
= row
;
5115 /* Print the next row. */
5118 struct fileinfo
const *f
= sorted_file
[filesno
];
5119 size_t name_length
= length_of_file_name_and_frills (f
);
5120 size_t max_name_length
= line_fmt
->col_arr
[col
++];
5121 print_file_name_and_frills (f
, pos
);
5124 if (filesno
>= cwd_n_used
)
5127 indent (pos
+ name_length
, pos
+ max_name_length
);
5128 pos
+= max_name_length
;
5135 print_horizontal (void)
5139 size_t cols
= calculate_columns (false);
5140 struct column_info
const *line_fmt
= &column_info
[cols
- 1];
5141 struct fileinfo
const *f
= sorted_file
[0];
5142 size_t name_length
= length_of_file_name_and_frills (f
);
5143 size_t max_name_length
= line_fmt
->col_arr
[0];
5145 /* Print first entry. */
5146 print_file_name_and_frills (f
, 0);
5149 for (filesno
= 1; filesno
< cwd_n_used
; ++filesno
)
5151 size_t col
= filesno
% cols
;
5160 indent (pos
+ name_length
, pos
+ max_name_length
);
5161 pos
+= max_name_length
;
5164 f
= sorted_file
[filesno
];
5165 print_file_name_and_frills (f
, pos
);
5167 name_length
= length_of_file_name_and_frills (f
);
5168 max_name_length
= line_fmt
->col_arr
[col
];
5173 /* Output name + SEP + ' '. */
5176 print_with_separator (char sep
)
5181 for (filesno
= 0; filesno
< cwd_n_used
; filesno
++)
5183 struct fileinfo
const *f
= sorted_file
[filesno
];
5184 size_t len
= line_length
? length_of_file_name_and_frills (f
) : 0;
5191 || ((pos
+ len
+ 2 < line_length
)
5192 && (pos
<= SIZE_MAX
- len
- 2)))
5200 separator
= eolbyte
;
5204 putchar (separator
);
5207 print_file_name_and_frills (f
, pos
);
5213 /* Assuming cursor is at position FROM, indent up to position TO.
5214 Use a TAB character instead of two or more spaces whenever possible. */
5217 indent (size_t from
, size_t to
)
5221 if (tabsize
!= 0 && to
/ tabsize
> (from
+ 1) / tabsize
)
5224 from
+= tabsize
- from
% tabsize
;
5234 /* Put DIRNAME/NAME into DEST, handling '.' and '/' properly. */
5235 /* FIXME: maybe remove this function someday. See about using a
5236 non-malloc'ing version of file_name_concat. */
5239 attach (char *dest
, char const *dirname
, char const *name
)
5241 char const *dirnamep
= dirname
;
5243 /* Copy dirname if it is not ".". */
5244 if (dirname
[0] != '.' || dirname
[1] != 0)
5247 *dest
++ = *dirnamep
++;
5248 /* Add '/' if 'dirname' doesn't already end with it. */
5249 if (dirnamep
> dirname
&& dirnamep
[-1] != '/')
5257 /* Allocate enough column info suitable for the current number of
5258 files and display columns, and initialize the info to represent the
5259 narrowest possible columns. */
5262 init_column_info (size_t max_cols
)
5266 /* Currently allocated columns in column_info. */
5267 static size_t column_info_alloc
;
5269 if (column_info_alloc
< max_cols
)
5271 size_t new_column_info_alloc
;
5274 if (!max_idx
|| max_cols
< max_idx
/ 2)
5276 /* The number of columns is far less than the display width
5277 allows. Grow the allocation, but only so that it's
5278 double the current requirements. If the display is
5279 extremely wide, this avoids allocating a lot of memory
5280 that is never needed. */
5281 column_info
= xnrealloc (column_info
, max_cols
,
5282 2 * sizeof *column_info
);
5283 new_column_info_alloc
= 2 * max_cols
;
5287 column_info
= xnrealloc (column_info
, max_idx
, sizeof *column_info
);
5288 new_column_info_alloc
= max_idx
;
5291 /* Allocate the new size_t objects by computing the triangle
5292 formula n * (n + 1) / 2, except that we don't need to
5293 allocate the part of the triangle that we've already
5294 allocated. Check for address arithmetic overflow. */
5296 size_t column_info_growth
= new_column_info_alloc
- column_info_alloc
;
5297 size_t s
= column_info_alloc
+ 1 + new_column_info_alloc
;
5298 size_t t
= s
* column_info_growth
;
5299 if (s
< new_column_info_alloc
|| t
/ column_info_growth
!= s
)
5301 p
= xnmalloc (t
/ 2, sizeof *p
);
5304 /* Grow the triangle by parceling out the cells just allocated. */
5305 for (i
= column_info_alloc
; i
< new_column_info_alloc
; i
++)
5307 column_info
[i
].col_arr
= p
;
5311 column_info_alloc
= new_column_info_alloc
;
5314 for (i
= 0; i
< max_cols
; ++i
)
5318 column_info
[i
].valid_len
= true;
5319 column_info
[i
].line_len
= (i
+ 1) * MIN_COLUMN_WIDTH
;
5320 for (j
= 0; j
<= i
; ++j
)
5321 column_info
[i
].col_arr
[j
] = MIN_COLUMN_WIDTH
;
5325 /* Calculate the number of columns needed to represent the current set
5326 of files in the current display width. */
5329 calculate_columns (bool by_columns
)
5331 size_t filesno
; /* Index into cwd_file. */
5332 size_t cols
; /* Number of files across. */
5334 /* Normally the maximum number of columns is determined by the
5335 screen width. But if few files are available this might limit it
5337 size_t max_cols
= 0 < max_idx
&& max_idx
< cwd_n_used
? max_idx
: cwd_n_used
;
5339 init_column_info (max_cols
);
5341 /* Compute the maximum number of possible columns. */
5342 for (filesno
= 0; filesno
< cwd_n_used
; ++filesno
)
5344 struct fileinfo
const *f
= sorted_file
[filesno
];
5345 size_t name_length
= length_of_file_name_and_frills (f
);
5347 for (size_t i
= 0; i
< max_cols
; ++i
)
5349 if (column_info
[i
].valid_len
)
5351 size_t idx
= (by_columns
5352 ? filesno
/ ((cwd_n_used
+ i
) / (i
+ 1))
5353 : filesno
% (i
+ 1));
5354 size_t real_length
= name_length
+ (idx
== i
? 0 : 2);
5356 if (column_info
[i
].col_arr
[idx
] < real_length
)
5358 column_info
[i
].line_len
+= (real_length
5359 - column_info
[i
].col_arr
[idx
]);
5360 column_info
[i
].col_arr
[idx
] = real_length
;
5361 column_info
[i
].valid_len
= (column_info
[i
].line_len
5368 /* Find maximum allowed columns. */
5369 for (cols
= max_cols
; 1 < cols
; --cols
)
5371 if (column_info
[cols
- 1].valid_len
)
5381 if (status
!= EXIT_SUCCESS
)
5385 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name
);
5387 List information about the FILEs (the current directory by default).\n\
5388 Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.\n\
5391 emit_mandatory_arg_note ();
5394 -a, --all do not ignore entries starting with .\n\
5395 -A, --almost-all do not list implied . and ..\n\
5396 --author with -l, print the author of each file\n\
5397 -b, --escape print C-style escapes for nongraphic characters\n\
5400 --block-size=SIZE with -l, scale sizes by SIZE when printing them;\n\
5401 e.g., '--block-size=M'; see SIZE format below\n\
5405 -B, --ignore-backups do not list implied entries ending with ~\n\
5408 -c with -lt: sort by, and show, ctime (time of last\n\
5409 change of file status information);\n\
5410 with -l: show ctime and sort by name;\n\
5411 otherwise: sort by ctime, newest first\n\
5415 -C list entries by columns\n\
5416 --color[=WHEN] color the output WHEN; more info below\n\
5417 -d, --directory list directories themselves, not their contents\n\
5418 -D, --dired generate output designed for Emacs' dired mode\n\
5422 -F, --classify[=WHEN] append indicator (one of */=>@|) to entries WHEN\n\
5423 --file-type likewise, except do not append '*'\n\
5426 --format=WORD across -x, commas -m, horizontal -x, long -l,\n\
5427 single-column -1, verbose -l, vertical -C\n\
5431 --full-time like -l --time-style=full-iso\n\
5434 -g like -l, but do not list owner\n\
5437 --group-directories-first\n\
5438 group directories before files\n\
5441 -G, --no-group in a long listing, don't print group names\n\
5444 -h, --human-readable with -l and -s, print sizes like 1K 234M 2G etc.\n\
5445 --si likewise, but use powers of 1000 not 1024\n\
5448 -H, --dereference-command-line\n\
5449 follow symbolic links listed on the command line\n\
5452 --dereference-command-line-symlink-to-dir\n\
5453 follow each command line symbolic link\n\
5454 that points to a directory\n\
5458 --hide=PATTERN do not list implied entries matching shell PATTERN\
5460 (overridden by -a or -A)\n\
5464 --hyperlink[=WHEN] hyperlink file names WHEN\n\
5467 --indicator-style=WORD\n\
5468 append indicator with style WORD to entry names:\n\
5469 none (default), slash (-p),\n\
5470 file-type (--file-type), classify (-F)\n\
5474 -i, --inode print the index number of each file\n\
5475 -I, --ignore=PATTERN do not list implied entries matching shell PATTERN\
5479 -k, --kibibytes default to 1024-byte blocks for file system usage;\
5481 used only with -s and per directory totals\n\
5485 -l use a long listing format\n\
5488 -L, --dereference when showing file information for a symbolic\n\
5489 link, show information for the file the link\n\
5490 references rather than for the link itself\n\
5494 -m fill width with a comma separated list of entries\
5498 -n, --numeric-uid-gid like -l, but list numeric user and group IDs\n\
5499 -N, --literal print entry names without quoting\n\
5500 -o like -l, but do not list group information\n\
5501 -p, --indicator-style=slash\n\
5502 append / indicator to directories\n\
5505 -q, --hide-control-chars print ? instead of nongraphic characters\n\
5508 --show-control-chars show nongraphic characters as-is (the default,\n\
5509 unless program is 'ls' and output is a terminal)\
5514 -Q, --quote-name enclose entry names in double quotes\n\
5517 --quoting-style=WORD use quoting style WORD for entry names:\n\
5518 literal, locale, shell, shell-always,\n\
5519 shell-escape, shell-escape-always, c, escape\n\
5520 (overrides QUOTING_STYLE environment variable)\n\
5524 -r, --reverse reverse order while sorting\n\
5525 -R, --recursive list subdirectories recursively\n\
5526 -s, --size print the allocated size of each file, in blocks\n\
5529 -S sort by file size, largest first\n\
5532 --sort=WORD change default 'name' sort to WORD:\n\
5533 none (-U), size (-S), time (-t),\n\
5534 version (-v), extension (-X), name, width\n\
5538 --time=WORD select which timestamp used to display or sort;\n\
5539 access time (-u): atime, access, use;\n\
5540 metadata change time (-c): ctime, status;\n\
5541 modified time (default): mtime, modification;\n\
5542 birth time: birth, creation;\n\
5543 with -l, WORD determines which time to show;\n\
5544 with --sort=time, sort by WORD (newest first)\n\
5548 --time-style=TIME_STYLE\n\
5549 time/date format with -l; see TIME_STYLE below\n\
5552 -t sort by time, newest first; see --time\n\
5553 -T, --tabsize=COLS assume tab stops at each COLS instead of 8\n\
5556 -u with -lt: sort by, and show, access time;\n\
5557 with -l: show access time and sort by name;\n\
5558 otherwise: sort by access time, newest first\n\
5562 -U do not sort directory entries\n\
5565 -v natural sort of (version) numbers within text\n\
5568 -w, --width=COLS set output width to COLS. 0 means no limit\n\
5569 -x list entries by lines instead of by columns\n\
5570 -X sort alphabetically by entry extension\n\
5571 -Z, --context print any security context of each file\n\
5572 --zero end each output line with NUL, not newline\n\
5573 -1 list one file per line\n\
5575 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
5576 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
5580 The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.\n\
5581 FORMAT is interpreted like in date(1). If FORMAT is FORMAT1<newline>FORMAT2,\n\
5582 then FORMAT1 applies to non-recent files and FORMAT2 to recent files.\n\
5583 TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.\n\
5584 Also the TIME_STYLE environment variable sets the default style to use.\n\
5588 The WHEN argument defaults to 'always' and can also be 'auto' or 'never'.\n\
5592 Using color to distinguish file types is disabled both by default and\n\
5593 with --color=never. With --color=auto, ls emits color codes only when\n\
5594 standard output is connected to a terminal. The LS_COLORS environment\n\
5595 variable can change the settings. Use the dircolors(1) command to set it.\n\
5601 1 if minor problems (e.g., cannot access subdirectory),\n\
5602 2 if serious trouble (e.g., cannot access command-line argument).\n\
5604 emit_ancillary_info (PROGRAM_NAME
);