Don't include "gtod.h"
[platform/upstream/coreutils.git] / src / ls.c
1 /* `dir', `vdir' and `ls' directory listing programs for GNU.
2    Copyright (C) 85, 88, 90, 91, 1995-2001 Free Software Foundation, Inc.
3
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 2, or (at your option)
7    any later version.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* If ls_mode is LS_MULTI_COL,
19    the multi-column format is the default regardless
20    of the type of output device.
21    This is for the `dir' program.
22
23    If ls_mode is LS_LONG_FORMAT,
24    the long format is the default regardless of the
25    type of output device.
26    This is for the `vdir' program.
27
28    If ls_mode is LS_LS,
29    the output format depends on whether the output
30    device is a terminal.
31    This is for the `ls' program. */
32
33 /* Written by Richard Stallman and David MacKenzie.  */
34
35 /* Color support by Peter Anvin <Peter.Anvin@linux.org> and Dennis
36    Flaherty <dennisf@denix.elk.miles.com> based on original patches by
37    Greg Lee <lee@uhunix.uhcc.hawaii.edu>.  */
38
39 #ifdef _AIX
40  #pragma alloca
41 #endif
42
43 #include <config.h>
44 #include <sys/types.h>
45
46 #if HAVE_INTTYPES_H
47 # include <inttypes.h>
48 #endif
49
50 #if HAVE_TERMIOS_H
51 # include <termios.h>
52 #endif
53
54 #ifdef GWINSZ_IN_SYS_IOCTL
55 # include <sys/ioctl.h>
56 #endif
57
58 #ifdef WINSIZE_IN_PTEM
59 # include <sys/stream.h>
60 # include <sys/ptem.h>
61 #endif
62
63 #if HAVE_SYS_ACL_H
64 # include <sys/acl.h>
65 #endif
66
67 #include <stdio.h>
68 #include <assert.h>
69 #include <grp.h>
70 #include <pwd.h>
71 #include <getopt.h>
72
73 /* Get MB_CUR_MAX.  */
74 #if HAVE_STDLIB_H
75 # include <stdlib.h>
76 #endif
77
78 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth().  */
79 #if HAVE_WCHAR_H
80 # include <wchar.h>
81 #endif
82
83 /* Get iswprint().  */
84 #if HAVE_WCTYPE_H
85 # include <wctype.h>
86 #endif
87 #if !defined iswprint && !HAVE_ISWPRINT
88 # define iswprint(wc) 1
89 #endif
90
91 #ifndef HAVE_DECL_WCWIDTH
92 "this configure-time declaration test was not run"
93 #endif
94 #if !HAVE_DECL_WCWIDTH
95 int wcwidth ();
96 #endif
97
98 /* If wcwidth() doesn't exist, assume all printable characters have
99    width 1.  */
100 #ifndef wcwidth
101 # if !HAVE_WCWIDTH
102 #  define wcwidth(wc) ((wc) == 0 ? 0 : iswprint (wc) ? 1 : -1)
103 # endif
104 #endif
105
106 #include "system.h"
107 #include <fnmatch.h>
108
109 #include "argmatch.h"
110 #include "dirname.h"
111 #include "dirfd.h"
112 #include "error.h"
113 #include "hard-locale.h"
114 #include "hash.h"
115 #include "human.h"
116 #include "filemode.h"
117 #include "ls.h"
118 #include "mbswidth.h"
119 #include "obstack.h"
120 #include "path-concat.h"
121 #include "quotearg.h"
122 #include "same.h"
123 #include "strverscmp.h"
124 #include "xstrtol.h"
125 #include "xalloc.h"
126 #include "xreadlink.h"
127
128 /* Use access control lists only under all the following conditions.
129    Some systems (OSF4, Irix5, Irix6) have the acl function, but not
130    sys/acl.h or don't define the GETACLCNT macro.  */
131 #if HAVE_SYS_ACL_H && HAVE_ACL && defined GETACLCNT
132 # define USE_ACL 1
133 #endif
134
135 #define PROGRAM_NAME (ls_mode == LS_LS ? "ls" \
136                       : (ls_mode == LS_MULTI_COL \
137                          ? "dir" : "vdir"))
138
139 #define AUTHORS N_ ("Richard Stallman and David MacKenzie")
140
141 #define obstack_chunk_alloc malloc
142 #define obstack_chunk_free free
143
144 /* Return an int indicating the result of comparing two integers.
145    Subtracting doesn't always work, due to overflow.  */
146 #define longdiff(a, b) ((a) < (b) ? -1 : (a) > (b))
147
148 /* The field width for inode numbers.  On some hosts inode numbers are
149    64 bits, so columns won't line up exactly when a huge inode number
150    is encountered, but in practice 7 digits is usually enough.  */
151 #ifndef INODE_DIGITS
152 # define INODE_DIGITS 7
153 #endif
154
155 #ifdef S_ISLNK
156 # define HAVE_SYMLINKS 1
157 #else
158 # define HAVE_SYMLINKS 0
159 #endif
160
161 /* If any of the S_* macros are undefined, define them here so each
162    use doesn't have to be guarded with e.g., #ifdef S_ISLNK.  */
163 #ifndef S_ISLNK
164 # define S_ISLNK(Mode) 0
165 #endif
166
167 #ifndef S_ISFIFO
168 # define S_ISFIFO(Mode) 0
169 #endif
170
171 #ifndef S_ISSOCK
172 # define S_ISSOCK(Mode) 0
173 #endif
174
175 #ifndef S_ISCHR
176 # define S_ISCHR(Mode) 0
177 #endif
178
179 #ifndef S_ISBLK
180 # define S_ISBLK(Mode) 0
181 #endif
182
183 #ifndef S_ISDOOR
184 # define S_ISDOOR(Mode) 0
185 #endif
186
187 /* Arrange to make lstat calls go through the wrapper function
188    on systems with an lstat function that does not dereference symlinks
189    that are specified with a trailing slash.  */
190 #if ! LSTAT_FOLLOWS_SLASHED_SYMLINK
191 int rpl_lstat PARAMS((const char *, struct stat *));
192 # undef lstat
193 # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
194 #endif
195
196 #if D_TYPE_IN_DIRENT && defined DTTOIF
197 # define HAVE_STRUCT_DIRENT_D_TYPE 1
198 # define DT_INIT(Val) = Val
199 #else
200 # define HAVE_STRUCT_DIRENT_D_TYPE 0
201 # define DT_INIT(Val) /* empty */
202 #endif
203
204 #ifdef ST_MTIM_NSEC
205 # define TIMESPEC_NS(timespec) ((timespec).ST_MTIM_NSEC)
206 #else
207 # define TIMESPEC_NS(timespec) 0
208 #endif
209
210 enum filetype
211   {
212     unknown DT_INIT (DT_UNKNOWN),
213     fifo DT_INIT (DT_FIFO),
214     chardev DT_INIT (DT_CHR),
215     directory DT_INIT (DT_DIR),
216     blockdev DT_INIT (DT_BLK),
217     normal DT_INIT (DT_REG),
218     symbolic_link DT_INIT (DT_LNK),
219     sock DT_INIT (DT_SOCK),
220     arg_directory DT_INIT (2 * (DT_UNKNOWN | DT_FIFO | DT_CHR | DT_DIR | DT_BLK
221                                 | DT_REG | DT_LNK | DT_SOCK))
222   };
223
224 struct fileinfo
225   {
226     /* The file name. */
227     char *name;
228
229     struct stat stat;
230
231     /* For symbolic link, name of the file linked to, otherwise zero. */
232     char *linkname;
233
234     /* For symbolic link and long listing, st_mode of file linked to, otherwise
235        zero. */
236     mode_t linkmode;
237
238     /* For symbolic link and color printing, 1 if linked-to file
239        exists, otherwise 0.  */
240     int linkok;
241
242     enum filetype filetype;
243
244 #if USE_ACL
245     /* For long listings, nonzero if the file has an access control list,
246        otherwise zero.  */
247     int have_acl;
248 #endif
249   };
250
251 #if USE_ACL
252 # define FILE_HAS_ACL(F) ((F)->have_acl)
253 #else
254 # define FILE_HAS_ACL(F) 0
255 #endif
256
257 #define LEN_STR_PAIR(s) sizeof (s) - 1, s
258
259 /* Null is a valid character in a color indicator (think about Epson
260    printers, for example) so we have to use a length/buffer string
261    type.  */
262
263 struct bin_str
264   {
265     int len;                    /* Number of bytes */
266     const char *string;         /* Pointer to the same */
267   };
268
269 #ifndef STDC_HEADERS
270 time_t time ();
271 #endif
272
273 size_t nstrftime PARAMS ((char *, size_t, char const *,
274                           struct tm const *, int, int));
275 char *getgroup ();
276 char *getuser ();
277
278 static size_t quote_name PARAMS ((FILE *out, const char *name,
279                                   struct quoting_options const *options));
280 static char *make_link_path PARAMS ((const char *path, const char *linkname));
281 static int compare_atime PARAMS ((const struct fileinfo *file1,
282                                   const struct fileinfo *file2));
283 static int rev_cmp_atime PARAMS ((const struct fileinfo *file2,
284                                   const struct fileinfo *file1));
285 static int compare_ctime PARAMS ((const struct fileinfo *file1,
286                                   const struct fileinfo *file2));
287 static int rev_cmp_ctime PARAMS ((const struct fileinfo *file2,
288                                   const struct fileinfo *file1));
289 static int compare_mtime PARAMS ((const struct fileinfo *file1,
290                                   const struct fileinfo *file2));
291 static int rev_cmp_mtime PARAMS ((const struct fileinfo *file2,
292                                   const struct fileinfo *file1));
293 static int compare_size PARAMS ((const struct fileinfo *file1,
294                                  const struct fileinfo *file2));
295 static int rev_cmp_size PARAMS ((const struct fileinfo *file2,
296                                  const struct fileinfo *file1));
297 static int compare_name PARAMS ((const struct fileinfo *file1,
298                                  const struct fileinfo *file2));
299 static int rev_cmp_name PARAMS ((const struct fileinfo *file2,
300                                  const struct fileinfo *file1));
301 static int compare_extension PARAMS ((const struct fileinfo *file1,
302                                       const struct fileinfo *file2));
303 static int rev_cmp_extension PARAMS ((const struct fileinfo *file2,
304                                       const struct fileinfo *file1));
305 static int compare_version PARAMS ((const struct fileinfo *file1,
306                                     const struct fileinfo *file2));
307 static int rev_cmp_version PARAMS ((const struct fileinfo *file2,
308                                     const struct fileinfo *file1));
309 static int decode_switches PARAMS ((int argc, char **argv));
310 static int file_interesting PARAMS ((const struct dirent *next));
311 static uintmax_t gobble_file PARAMS ((const char *name, enum filetype type,
312                                       int explicit_arg, const char *dirname));
313 static void print_color_indicator PARAMS ((const char *name, mode_t mode,
314                                            int linkok));
315 static void put_indicator PARAMS ((const struct bin_str *ind));
316 static int length_of_file_name_and_frills PARAMS ((const struct fileinfo *f));
317 static void add_ignore_pattern PARAMS ((const char *pattern));
318 static void attach PARAMS ((char *dest, const char *dirname, const char *name));
319 static void clear_files PARAMS ((void));
320 static void extract_dirs_from_files PARAMS ((const char *dirname,
321                                              int ignore_dot_and_dot_dot));
322 static void get_link_name PARAMS ((const char *filename, struct fileinfo *f));
323 static void indent PARAMS ((int from, int to));
324 static void init_column_info PARAMS ((void));
325 static void print_current_files PARAMS ((void));
326 static void print_dir PARAMS ((const char *name, const char *realname));
327 static void print_file_name_and_frills PARAMS ((const struct fileinfo *f));
328 static void print_horizontal PARAMS ((void));
329 static void print_long_format PARAMS ((const struct fileinfo *f));
330 static void print_many_per_line PARAMS ((void));
331 static void print_name_with_quoting PARAMS ((const char *p, mode_t mode,
332                                              int linkok,
333                                              struct obstack *stack));
334 static void prep_non_filename_text PARAMS ((void));
335 static void print_type_indicator PARAMS ((mode_t mode));
336 static void print_with_commas PARAMS ((void));
337 static void queue_directory PARAMS ((const char *name, const char *realname));
338 static void sort_files PARAMS ((void));
339 static void parse_ls_color PARAMS ((void));
340 void usage PARAMS ((int status));
341
342 /* The name the program was run with, stripped of any leading path. */
343 char *program_name;
344
345 /* Initial size of hash table.
346    Most hierarchies are likely to be shallower than this.  */
347 #define INITIAL_TABLE_SIZE 30
348
349 /* The set of `active' directories, from the current command-line argument
350    to the level in the hierarchy at which files are being listed.
351    A directory is represented by its device and inode numbers.
352    A directory is added to this set when ls begins listing it or its
353    entries, and it is removed from the set just after ls has finished
354    processing it.  This set is used solely to detect loops, e.g., with
355    mkdir loop; cd loop; ln -s ../loop sub; ls -RL  */
356 static Hash_table *active_dir_set;
357
358 #define LOOP_DETECT (!!active_dir_set)
359
360 /* An entry in the active_dir_set.  */
361 struct dev_ino
362 {
363   dev_t st_dev;
364   ino_t st_ino;
365 };
366
367 /* The table of files in the current directory:
368
369    `files' points to a vector of `struct fileinfo', one per file.
370    `nfiles' is the number of elements space has been allocated for.
371    `files_index' is the number actually in use.  */
372
373 /* Address of block containing the files that are described.  */
374 static struct fileinfo *files;  /* FIXME: rename this to e.g. cwd_file */
375
376 /* Length of block that `files' points to, measured in files.  */
377 static int nfiles;  /* FIXME: rename this to e.g. cwd_n_alloc */
378
379 /* Index of first unused in `files'.  */
380 static int files_index;  /* FIXME: rename this to e.g. cwd_n_used */
381
382 /* When nonzero, in a color listing, color each symlink name according to the
383    type of file it points to.  Otherwise, color them according to the `ln'
384    directive in LS_COLORS.  Dangling (orphan) symlinks are treated specially,
385    regardless.  This is set when `ln=target' appears in LS_COLORS.  */
386
387 static int color_symlink_as_referent;
388
389 /* mode of appropriate file for colorization */
390 #define FILE_OR_LINK_MODE(File) \
391     ((color_symlink_as_referent && (File)->linkok) \
392      ? (File)->linkmode : (File)->stat.st_mode)
393
394
395 /* Record of one pending directory waiting to be listed.  */
396
397 struct pending
398   {
399     char *name;
400     /* If the directory is actually the file pointed to by a symbolic link we
401        were told to list, `realname' will contain the name of the symbolic
402        link, otherwise zero. */
403     char *realname;
404     struct pending *next;
405   };
406
407 static struct pending *pending_dirs;
408
409 /* Current time in seconds and nanoseconds since 1970, updated as
410    needed when deciding whether a file is recent.  */
411
412 static time_t current_time = TYPE_MINIMUM (time_t);
413 static int current_time_ns = -1;
414
415 /* The number of digits to use for block sizes.
416    4, or more if needed for bigger numbers.  */
417
418 static int block_size_size;
419
420 /* Option flags */
421
422 /* long_format for lots of info, one per line.
423    one_per_line for just names, one per line.
424    many_per_line for just names, many per line, sorted vertically.
425    horizontal for just names, many per line, sorted horizontally.
426    with_commas for just names, many per line, separated by commas.
427
428    -l (and other options that imply -l), -1, -C, -x and -m control
429    this parameter.  */
430
431 enum format
432   {
433     long_format,                /* -l and other options that imply -l */
434     one_per_line,               /* -1 */
435     many_per_line,              /* -C */
436     horizontal,                 /* -x */
437     with_commas                 /* -m */
438   };
439
440 static enum format format;
441
442 /* `full-iso' uses full ISO-style dates and times.  `iso' uses shorter
443    ISO-style time stamps.  `locale' uses locale-dependent time stamps.
444    `posix-iso' uses traditional POSIX-locale-style dates where
445    POSIX requires it, ISO-style dates otherwise.  */
446 enum time_style
447   {
448     full_iso_time_style,        /* --time-style=full-iso */
449     iso_time_style,             /* --time-style=iso */
450     locale_time_style,          /* --time-style=locale */
451     posix_iso_time_style        /* --time-style=posix-iso (default) */
452   };
453
454 static char const *const time_style_args[] =
455 {
456   "full-iso", "iso", "locale", "posix-iso", 0
457 };
458
459 static enum time_style const time_style_types[] =
460 {
461   full_iso_time_style, iso_time_style,
462   locale_time_style, posix_iso_time_style, 0
463 };
464
465 /* Type of time to print or sort by.  Controlled by -c and -u.  */
466
467 enum time_type
468   {
469     time_mtime,                 /* default */
470     time_ctime,                 /* -c */
471     time_atime                  /* -u */
472   };
473
474 static enum time_type time_type;
475
476 /* The file characteristic to sort by.  Controlled by -t, -S, -U, -X, -v. */
477
478 enum sort_type
479   {
480     sort_none,                  /* -U */
481     sort_name,                  /* default */
482     sort_extension,             /* -X */
483     sort_time,                  /* -t */
484     sort_size,                  /* -S */
485     sort_version                /* -v */
486   };
487
488 static enum sort_type sort_type;
489
490 /* Direction of sort.
491    0 means highest first if numeric,
492    lowest first if alphabetic;
493    these are the defaults.
494    1 means the opposite order in each case.  -r  */
495
496 static int sort_reverse;
497
498 /* Nonzero means to display owner information.  -g turns this off.  */
499
500 static int print_owner = 1;
501
502 /* Nonzero means to display group information.  -G and -o turn this off.  */
503
504 static int print_group = 1;
505
506 /* Nonzero means print the user and group id's as numbers rather
507    than as names.  -n  */
508
509 static int numeric_ids;
510
511 /* Nonzero means mention the size in blocks of each file.  -s  */
512
513 static int print_block_size;
514
515 /* If positive, the units to use when printing sizes;
516    if negative, the human-readable base.  */
517 static int output_block_size;
518
519 /* Precede each line of long output (per file) with a string like `m,n:'
520    where M is the number of characters after the `:' and before the
521    filename and N is the length of the filename.  Using this format,
522    Emacs' dired mode starts up twice as fast, and can handle all
523    strange characters in file names.  */
524 static int dired;
525
526 /* `none' means don't mention the type of files.
527    `classify' means mention file types and mark executables.
528    `file_type' means mention only file types.
529
530    Controlled by -F, -p, and --indicator-style.  */
531
532 enum indicator_style
533   {
534     none,       /*     --indicator-style=none */
535     classify,   /* -F, --indicator-style=classify */
536     file_type   /* -p, --indicator-style=file-type */
537   };
538
539 static enum indicator_style indicator_style;
540
541 /* Names of indicator styles.  */
542 static char const *const indicator_style_args[] =
543 {
544   "none", "classify", "file-type", 0
545 };
546
547 static enum indicator_style const indicator_style_types[]=
548 {
549   none, classify, file_type
550 };
551
552 /* Nonzero means use colors to mark types.  Also define the different
553    colors as well as the stuff for the LS_COLORS environment variable.
554    The LS_COLORS variable is now in a termcap-like format.  */
555
556 static int print_with_color;
557
558 enum color_type
559   {
560     color_never,                /* 0: default or --color=never */
561     color_always,               /* 1: --color=always */
562     color_if_tty                /* 2: --color=tty */
563   };
564
565 enum Dereference_symlink
566   {
567     DEREF_UNDEFINED = 1,
568     DEREF_NEVER,
569     DEREF_COMMAND_LINE_ARGUMENTS,       /* -H */
570     DEREF_ALWAYS                        /* -L */
571   };
572
573 enum indicator_no
574   {
575     C_LEFT, C_RIGHT, C_END, C_NORM, C_FILE, C_DIR, C_LINK, C_FIFO, C_SOCK,
576     C_BLK, C_CHR, C_MISSING, C_ORPHAN, C_EXEC, C_DOOR
577   };
578
579 static const char *const indicator_name[]=
580   {
581     "lc", "rc", "ec", "no", "fi", "di", "ln", "pi", "so",
582     "bd", "cd", "mi", "or", "ex", "do", NULL
583   };
584
585 struct color_ext_type
586   {
587     struct bin_str ext;         /* The extension we're looking for */
588     struct bin_str seq;         /* The sequence to output when we do */
589     struct color_ext_type *next;        /* Next in list */
590   };
591
592 static struct bin_str color_indicator[] =
593   {
594     { LEN_STR_PAIR ("\033[") },         /* lc: Left of color sequence */
595     { LEN_STR_PAIR ("m") },             /* rc: Right of color sequence */
596     { 0, NULL },                        /* ec: End color (replaces lc+no+rc) */
597     { LEN_STR_PAIR ("0") },             /* no: Normal */
598     { LEN_STR_PAIR ("0") },             /* fi: File: default */
599     { LEN_STR_PAIR ("01;34") },         /* di: Directory: bright blue */
600     { LEN_STR_PAIR ("01;36") },         /* ln: Symlink: bright cyan */
601     { LEN_STR_PAIR ("33") },            /* pi: Pipe: yellow/brown */
602     { LEN_STR_PAIR ("01;35") },         /* so: Socket: bright magenta */
603     { LEN_STR_PAIR ("01;33") },         /* bd: Block device: bright yellow */
604     { LEN_STR_PAIR ("01;33") },         /* cd: Char device: bright yellow */
605     { 0, NULL },                        /* mi: Missing file: undefined */
606     { 0, NULL },                        /* or: Orphanned symlink: undefined */
607     { LEN_STR_PAIR ("01;32") },         /* ex: Executable: bright green */
608     { LEN_STR_PAIR ("01;35") }          /* do: Door: bright magenta */
609   };
610
611 /* FIXME: comment  */
612 static struct color_ext_type *color_ext_list = NULL;
613
614 /* Buffer for color sequences */
615 static char *color_buf;
616
617 /* Nonzero means to check for orphaned symbolic link, for displaying
618    colors.  */
619
620 static int check_symlink_color;
621
622 /* Nonzero means mention the inode number of each file.  -i  */
623
624 static int print_inode;
625
626 /* What to do with symbolic links.  Affected by -d, -F, -H, -l (and
627    other options that imply -l), and -L.  */
628
629 static enum Dereference_symlink dereference;
630
631 /* Nonzero means when a directory is found, display info on its
632    contents.  -R  */
633
634 static int recursive;
635
636 /* Nonzero means when an argument is a directory name, display info
637    on it itself.  -d  */
638
639 static int immediate_dirs;
640
641 /* Nonzero means don't omit files whose names start with `.'.  -A */
642
643 static int all_files;
644
645 /* Nonzero means don't omit files `.' and `..'
646    This flag implies `all_files'.  -a  */
647
648 static int really_all_files;
649
650 /* A linked list of shell-style globbing patterns.  If a non-argument
651    file name matches any of these patterns, it is omitted.
652    Controlled by -I.  Multiple -I options accumulate.
653    The -B option adds `*~' and `.*~' to this list.  */
654
655 struct ignore_pattern
656   {
657     const char *pattern;
658     struct ignore_pattern *next;
659   };
660
661 static struct ignore_pattern *ignore_patterns;
662
663 /* Nonzero means output nongraphic chars in file names as `?'.
664    (-q, --hide-control-chars)
665    qmark_funny_chars and the quoting style (-Q, --quoting-style=WORD) are
666    independent.  The algorithm is: first, obey the quoting style to get a
667    string representing the file name;  then, if qmark_funny_chars is set,
668    replace all nonprintable chars in that string with `?'.  It's necessary
669    to replace nonprintable chars even in quoted strings, because we don't
670    want to mess up the terminal if control chars get sent to it, and some
671    quoting methods pass through control chars as-is.  */
672 static int qmark_funny_chars;
673
674 /* Quoting options for file and dir name output.  */
675
676 static struct quoting_options *filename_quoting_options;
677 static struct quoting_options *dirname_quoting_options;
678
679 /* The number of chars per hardware tab stop.  Setting this to zero
680    inhibits the use of TAB characters for separating columns.  -T */
681 static int tabsize;
682
683 /* Nonzero means we are listing the working directory because no
684    non-option arguments were given. */
685
686 static int dir_defaulted;
687
688 /* Nonzero means print each directory name before listing it. */
689
690 static int print_dir_name;
691
692 /* The line length to use for breaking lines in many-per-line format.
693    Can be set with -w.  */
694
695 static int line_length;
696
697 /* If nonzero, the file listing format requires that stat be called on
698    each file. */
699
700 static int format_needs_stat;
701
702 /* Similar to `format_needs_stat', but set if only the file type is
703    needed.  */
704
705 static int format_needs_type;
706
707 /* strftime formats for non-recent and recent files, respectively, in
708    -l output.  */
709
710 static char const *long_time_format[2] = { N_("%b %e  %Y"),
711                                            N_("%b %e %H:%M") };
712
713 /* The exit status to use if we don't get any fatal errors. */
714
715 static int exit_status;
716
717 /* For long options that have no equivalent short option, use a
718    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
719 enum
720 {
721   BLOCK_SIZE_OPTION = CHAR_MAX + 1,
722   COLOR_OPTION,
723   FORMAT_OPTION,
724   FULL_TIME_OPTION,
725   INDICATOR_STYLE_OPTION,
726   QUOTING_STYLE_OPTION,
727   SHOW_CONTROL_CHARS_OPTION,
728   SI_OPTION,
729   SORT_OPTION,
730   TIME_OPTION,
731   TIME_STYLE_OPTION
732 };
733
734 static struct option const long_options[] =
735 {
736   {"all", no_argument, 0, 'a'},
737   {"escape", no_argument, 0, 'b'},
738   {"directory", no_argument, 0, 'd'},
739   {"dired", no_argument, 0, 'D'},
740   {"full-time", no_argument, 0, FULL_TIME_OPTION},
741   {"human-readable", no_argument, 0, 'h'},
742   {"inode", no_argument, 0, 'i'},
743   {"kilobytes", no_argument, 0, 'k'},
744   {"numeric-uid-gid", no_argument, 0, 'n'},
745   {"no-group", no_argument, 0, 'G'},
746   {"hide-control-chars", no_argument, 0, 'q'},
747   {"reverse", no_argument, 0, 'r'},
748   {"size", no_argument, 0, 's'},
749   {"width", required_argument, 0, 'w'},
750   {"almost-all", no_argument, 0, 'A'},
751   {"ignore-backups", no_argument, 0, 'B'},
752   {"classify", no_argument, 0, 'F'},
753   {"file-type", no_argument, 0, 'p'},
754   {"si", no_argument, 0, SI_OPTION},
755   {"dereference-command-line", no_argument, 0, 'H'},
756   {"ignore", required_argument, 0, 'I'},
757   {"indicator-style", required_argument, 0, INDICATOR_STYLE_OPTION},
758   {"dereference", no_argument, 0, 'L'},
759   {"literal", no_argument, 0, 'N'},
760   {"quote-name", no_argument, 0, 'Q'},
761   {"quoting-style", required_argument, 0, QUOTING_STYLE_OPTION},
762   {"recursive", no_argument, 0, 'R'},
763   {"format", required_argument, 0, FORMAT_OPTION},
764   {"show-control-chars", no_argument, 0, SHOW_CONTROL_CHARS_OPTION},
765   {"sort", required_argument, 0, SORT_OPTION},
766   {"tabsize", required_argument, 0, 'T'},
767   {"time", required_argument, 0, TIME_OPTION},
768   {"time-style", required_argument, 0, TIME_STYLE_OPTION},
769   {"color", optional_argument, 0, COLOR_OPTION},
770   {"block-size", required_argument, 0, BLOCK_SIZE_OPTION},
771   {GETOPT_HELP_OPTION_DECL},
772   {GETOPT_VERSION_OPTION_DECL},
773   {NULL, 0, NULL, 0}
774 };
775
776 static char const *const format_args[] =
777 {
778   "verbose", "long", "commas", "horizontal", "across",
779   "vertical", "single-column", 0
780 };
781
782 static enum format const format_types[] =
783 {
784   long_format, long_format, with_commas, horizontal, horizontal,
785   many_per_line, one_per_line
786 };
787
788 static char const *const sort_args[] =
789 {
790   "none", "time", "size", "extension", "version", 0
791 };
792
793 static enum sort_type const sort_types[] =
794 {
795   sort_none, sort_time, sort_size, sort_extension, sort_version
796 };
797
798 static char const *const time_args[] =
799 {
800   "atime", "access", "use", "ctime", "status", 0
801 };
802
803 static enum time_type const time_types[] =
804 {
805   time_atime, time_atime, time_atime, time_ctime, time_ctime
806 };
807
808 static char const *const color_args[] =
809 {
810   /* force and none are for compatibility with another color-ls version */
811   "always", "yes", "force",
812   "never", "no", "none",
813   "auto", "tty", "if-tty", 0
814 };
815
816 static enum color_type const color_types[] =
817 {
818   color_always, color_always, color_always,
819   color_never, color_never, color_never,
820   color_if_tty, color_if_tty, color_if_tty
821 };
822
823 /* Information about filling a column.  */
824 struct column_info
825 {
826   int valid_len;
827   int line_len;
828   int *col_arr;
829 };
830
831 /* Array with information about column filledness.  */
832 static struct column_info *column_info;
833
834 /* Maximum number of columns ever possible for this display.  */
835 static int max_idx;
836
837 /* The minimum width of a colum is 3: 1 character for the name and 2
838    for the separating white space.  */
839 #define MIN_COLUMN_WIDTH        3
840
841
842 /* This zero-based index is used solely with the --dired option.
843    When that option is in effect, this counter is incremented for each
844    character of output generated by this program so that the beginning
845    and ending indices (in that output) of every file name can be recorded
846    and later output themselves.  */
847 static size_t dired_pos;
848
849 #define DIRED_PUTCHAR(c) do {putchar ((c)); ++dired_pos;} while (0)
850
851 /* Write S to STREAM and increment DIRED_POS by S_LEN.  */
852 #define DIRED_FPUTS(s, stream, s_len) \
853     do {fputs ((s), (stream)); dired_pos += s_len;} while (0)
854
855 /* Like DIRED_FPUTS, but for use when S is a literal string.  */
856 #define DIRED_FPUTS_LITERAL(s, stream) \
857     do {fputs ((s), (stream)); dired_pos += sizeof((s)) - 1;} while (0)
858
859 #define DIRED_INDENT()                                                  \
860     do                                                                  \
861       {                                                                 \
862         /* FIXME: remove the `&& format == long_format' clause.  */     \
863         if (dired && format == long_format)                             \
864           DIRED_FPUTS_LITERAL ("  ", stdout);                           \
865       }                                                                 \
866     while (0)
867
868 /* With --dired, store pairs of beginning and ending indices of filenames.  */
869 static struct obstack dired_obstack;
870
871 /* With --dired, store pairs of beginning and ending indices of any
872    directory names that appear as headers (just before `total' line)
873    for lists of directory entries.  Such directory names are seen when
874    listing hierarchies using -R and when a directory is listed with at
875    least one other command line argument.  */
876 static struct obstack subdired_obstack;
877
878 /* Save the current index on the specified obstack, OBS.  */
879 #define PUSH_CURRENT_DIRED_POS(obs)                                     \
880   do                                                                    \
881     {                                                                   \
882       /* FIXME: remove the `&& format == long_format' clause.  */       \
883       if (dired && format == long_format)                               \
884         obstack_grow ((obs), &dired_pos, sizeof (dired_pos));           \
885     }                                                                   \
886   while (0)
887
888 /* With -R, this stack is used to help detect directory cycles.
889    The device/inode pairs on this stack mirror the pairs in the
890    active_dir_set hash table.  */
891 static struct obstack dev_ino_obstack;
892
893 /* Push a pair onto the device/inode stack.  */
894 #define DEV_INO_PUSH(Dev, Ino)                                          \
895   do                                                                    \
896     {                                                                   \
897       struct dev_ino *di;                                               \
898       obstack_blank (&dev_ino_obstack, sizeof (struct dev_ino));        \
899       di = -1 + (struct dev_ino *) obstack_next_free (&dev_ino_obstack); \
900       di->st_dev = (Dev);                                               \
901       di->st_ino = (Ino);                                               \
902     }                                                                   \
903   while (0)
904
905 /* Pop a dev/ino struct off the global dev_ino_obstack
906    and return that struct.  */
907 static struct dev_ino
908 dev_ino_pop (void)
909 {
910   assert (sizeof (struct dev_ino) <= obstack_object_size (&dev_ino_obstack));
911   obstack_blank (&dev_ino_obstack, -(int) (sizeof (struct dev_ino)));
912   return *(struct dev_ino*) obstack_next_free (&dev_ino_obstack);
913 }
914
915 #define ASSERT_MATCHING_DEV_INO(Name, Di)       \
916   do                                            \
917     {                                           \
918       struct stat sb;                           \
919       assert (Name);                            \
920       assert (0 <= stat (Name, &sb));           \
921       assert (sb.st_dev == Di.st_dev);          \
922       assert (sb.st_ino == Di.st_ino);          \
923     }                                           \
924   while (0)
925
926
927 /* Write to standard output PREFIX, followed by the quoting style and
928    a space-separated list of the integers stored in OS all on one line.  */
929
930 static void
931 dired_dump_obstack (const char *prefix, struct obstack *os)
932 {
933   int n_pos;
934
935   n_pos = obstack_object_size (os) / sizeof (dired_pos);
936   if (n_pos > 0)
937     {
938       int i;
939       size_t *pos;
940
941       pos = (size_t *) obstack_finish (os);
942       fputs (prefix, stdout);
943       for (i = 0; i < n_pos; i++)
944         printf (" %lu", (unsigned long) pos[i]);
945       fputs ("\n", stdout);
946     }
947 }
948
949 static unsigned int
950 dev_ino_hash (void const *x, unsigned int table_size)
951 {
952   struct dev_ino const *p = x;
953   return (uintmax_t) p->st_ino % table_size;
954 }
955
956 static bool
957 dev_ino_compare (void const *x, void const *y)
958 {
959   struct dev_ino const *a = x;
960   struct dev_ino const *b = y;
961   return SAME_INODE (*a, *b) ? true : false;
962 }
963
964 static void
965 dev_ino_free (void *x)
966 {
967   free (x);
968 }
969
970 /* Add the device/inode pair (P->st_dev/P->st_ino) to the set of
971    active directories.  Return nonzero if there is already a matching
972    entry in the table.  Otherwise, return zero.  */
973
974 static int
975 visit_dir (dev_t dev, ino_t ino)
976 {
977   struct dev_ino *ent;
978   struct dev_ino *ent_from_table;
979   int found_match;
980
981   ent = XMALLOC (struct dev_ino, 1);
982   ent->st_ino = ino;
983   ent->st_dev = dev;
984
985   /* Attempt to insert this entry into the table.  */
986   ent_from_table = hash_insert (active_dir_set, ent);
987
988   if (ent_from_table == NULL)
989     {
990       /* Insertion failed due to lack of memory.  */
991       xalloc_die ();
992     }
993
994   found_match = (ent_from_table != ent);
995
996   if (found_match)
997     {
998       /* ent was not inserted, so free it.  */
999       free (ent);
1000     }
1001
1002   return found_match;
1003 }
1004
1005 static void
1006 free_pending_ent (struct pending *p)
1007 {
1008   if (p->name)
1009     free (p->name);
1010   if (p->realname)
1011     free (p->realname);
1012   free (p);
1013 }
1014
1015 int
1016 main (int argc, char **argv)
1017 {
1018   register int i;
1019   register struct pending *thispend;
1020   unsigned int n_files;
1021
1022   program_name = argv[0];
1023   setlocale (LC_ALL, "");
1024   bindtextdomain (PACKAGE, LOCALEDIR);
1025   textdomain (PACKAGE);
1026
1027   atexit (close_stdout);
1028
1029 #define N_ENTRIES(Array) (sizeof Array / sizeof *(Array))
1030   assert (N_ENTRIES (color_indicator) + 1 == N_ENTRIES (indicator_name));
1031
1032   exit_status = 0;
1033   dir_defaulted = 1;
1034   print_dir_name = 1;
1035   pending_dirs = 0;
1036
1037   i = decode_switches (argc, argv);
1038
1039   if (print_with_color)
1040     parse_ls_color ();
1041
1042   /* Test print_with_color again, because the call to parse_ls_color
1043      may have just reset it -- e.g., if LS_COLORS is invalid.  */
1044   if (print_with_color)
1045     {
1046       prep_non_filename_text ();
1047       /* Avoid following symbolic links when possible.  */
1048       if (color_indicator[C_ORPHAN].string != NULL
1049           || (color_indicator[C_MISSING].string != NULL
1050               && format == long_format))
1051         check_symlink_color = 1;
1052     }
1053
1054   if (dereference == DEREF_UNDEFINED)
1055     dereference = ((immediate_dirs
1056                     || indicator_style == classify
1057                     || format == long_format)
1058                    ? DEREF_NEVER
1059                    : DEREF_COMMAND_LINE_ARGUMENTS);
1060
1061   /* When using -R, initialize a data structure we'll use to
1062      detect any directory cycles.  */
1063   if (recursive)
1064     {
1065       active_dir_set = hash_initialize (INITIAL_TABLE_SIZE, NULL,
1066                                         dev_ino_hash,
1067                                         dev_ino_compare,
1068                                         dev_ino_free);
1069       if (active_dir_set == NULL)
1070         xalloc_die ();
1071
1072       obstack_init (&dev_ino_obstack);
1073     }
1074
1075   format_needs_stat = sort_type == sort_time || sort_type == sort_size
1076     || format == long_format
1077     || dereference == DEREF_ALWAYS
1078     || recursive || print_block_size || print_inode;
1079   format_needs_type = (format_needs_stat == 0
1080                        && (print_with_color || indicator_style != none));
1081
1082   if (dired && format == long_format)
1083     {
1084       obstack_init (&dired_obstack);
1085       obstack_init (&subdired_obstack);
1086     }
1087
1088   nfiles = 100;
1089   files = XMALLOC (struct fileinfo, nfiles);
1090   files_index = 0;
1091
1092   clear_files ();
1093
1094   n_files = argc - i;
1095   if (0 < n_files)
1096     dir_defaulted = 0;
1097
1098   for (; i < argc; i++)
1099     {
1100       gobble_file (argv[i], unknown, 1, "");
1101     }
1102
1103   if (dir_defaulted)
1104     {
1105       if (immediate_dirs)
1106         gobble_file (".", directory, 1, "");
1107       else
1108         queue_directory (".", 0);
1109     }
1110
1111   if (files_index)
1112     {
1113       sort_files ();
1114       if (!immediate_dirs)
1115         extract_dirs_from_files ("", 0);
1116       /* `files_index' might be zero now.  */
1117     }
1118
1119   if (files_index)
1120     {
1121       print_current_files ();
1122       if (pending_dirs && pending_dirs->name)
1123         DIRED_PUTCHAR ('\n');
1124     }
1125   else if (n_files <= 1 && pending_dirs && pending_dirs->next == 0)
1126     print_dir_name = 0;
1127
1128   while (pending_dirs)
1129     {
1130       thispend = pending_dirs;
1131       pending_dirs = pending_dirs->next;
1132
1133       if (LOOP_DETECT)
1134         {
1135           if (thispend->name == NULL)
1136             {
1137               /* thispend->name == NULL means this is a marker entry
1138                  indicating we've finished processing the directory.
1139                  Use its dev/ino numbers to remove the corresponding
1140                  entry from the active_dir_set hash table.  */
1141               struct dev_ino di = dev_ino_pop ();
1142               struct dev_ino *found = hash_delete (active_dir_set, &di);
1143               /* ASSERT_MATCHING_DEV_INO (thispend->realname, di); */
1144               assert (found);
1145               dev_ino_free (found);
1146               free_pending_ent (thispend);
1147               continue;
1148             }
1149         }
1150
1151       print_dir (thispend->name, thispend->realname);
1152
1153       free_pending_ent (thispend);
1154       print_dir_name = 1;
1155     }
1156
1157   if (dired && format == long_format)
1158     {
1159       /* No need to free these since we're about to exit.  */
1160       dired_dump_obstack ("//DIRED//", &dired_obstack);
1161       dired_dump_obstack ("//SUBDIRED//", &subdired_obstack);
1162       printf ("//DIRED-OPTIONS// --quoting-style=%s\n",
1163               quoting_style_args[get_quoting_style (filename_quoting_options)]);
1164     }
1165
1166   /* Restore default color before exiting */
1167   if (print_with_color)
1168     {
1169       put_indicator (&color_indicator[C_LEFT]);
1170       put_indicator (&color_indicator[C_RIGHT]);
1171     }
1172
1173   if (LOOP_DETECT)
1174     {
1175       assert (hash_get_n_entries (active_dir_set) == 0);
1176       hash_free (active_dir_set);
1177     }
1178
1179   exit (exit_status);
1180 }
1181
1182 /* Set all the option flags according to the switches specified.
1183    Return the index of the first non-option argument.  */
1184
1185 static int
1186 decode_switches (int argc, char **argv)
1187 {
1188   int c;
1189   char const *time_style_option = 0;
1190
1191   /* Record whether there is an option specifying sort type.  */
1192   int sort_type_specified = 0;
1193
1194   qmark_funny_chars = 0;
1195
1196   /* initialize all switches to default settings */
1197
1198   switch (ls_mode)
1199     {
1200     case LS_MULTI_COL:
1201       /* This is for the `dir' program.  */
1202       format = many_per_line;
1203       set_quoting_style (NULL, escape_quoting_style);
1204       break;
1205
1206     case LS_LONG_FORMAT:
1207       /* This is for the `vdir' program.  */
1208       format = long_format;
1209       set_quoting_style (NULL, escape_quoting_style);
1210       break;
1211
1212     case LS_LS:
1213       /* This is for the `ls' program.  */
1214       if (isatty (STDOUT_FILENO))
1215         {
1216           format = many_per_line;
1217           /* See description of qmark_funny_chars, above.  */
1218           qmark_funny_chars = 1;
1219         }
1220       else
1221         {
1222           format = one_per_line;
1223           qmark_funny_chars = 0;
1224         }
1225       break;
1226
1227     default:
1228       abort ();
1229     }
1230
1231   time_type = time_mtime;
1232   sort_type = sort_name;
1233   sort_reverse = 0;
1234   numeric_ids = 0;
1235   print_block_size = 0;
1236   indicator_style = none;
1237   print_inode = 0;
1238   dereference = DEREF_UNDEFINED;
1239   recursive = 0;
1240   immediate_dirs = 0;
1241   all_files = 0;
1242   really_all_files = 0;
1243   ignore_patterns = 0;
1244
1245   /* FIXME: put this in a function.  */
1246   {
1247     char const *q_style = getenv ("QUOTING_STYLE");
1248     if (q_style)
1249       {
1250         int i = ARGCASEMATCH (q_style, quoting_style_args, quoting_style_vals);
1251         if (0 <= i)
1252           set_quoting_style (NULL, quoting_style_vals[i]);
1253         else
1254           error (0, 0,
1255          _("ignoring invalid value of environment variable QUOTING_STYLE: %s"),
1256                  quotearg (q_style));
1257       }
1258   }
1259
1260   human_block_size (getenv ("LS_BLOCK_SIZE"), 0, &output_block_size);
1261
1262   line_length = 80;
1263   {
1264     char const *p = getenv ("COLUMNS");
1265     if (p && *p)
1266       {
1267         long int tmp_long;
1268         if (xstrtol (p, NULL, 0, &tmp_long, NULL) == LONGINT_OK
1269             && 0 < tmp_long && tmp_long <= INT_MAX)
1270           {
1271             line_length = (int) tmp_long;
1272           }
1273         else
1274           {
1275             error (0, 0,
1276                _("ignoring invalid width in environment variable COLUMNS: %s"),
1277                    quotearg (p));
1278           }
1279       }
1280   }
1281
1282 #ifdef TIOCGWINSZ
1283   {
1284     struct winsize ws;
1285
1286     if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0)
1287       line_length = ws.ws_col;
1288   }
1289 #endif
1290
1291   /* Using the TABSIZE environment variable is not POSIX-approved.
1292      Ignore it when POSIXLY_CORRECT is set.  */
1293   {
1294     char const *p;
1295     tabsize = 8;
1296     if (!getenv ("POSIXLY_CORRECT") && (p = getenv ("TABSIZE")))
1297       {
1298         long int tmp_long;
1299         if (xstrtol (p, NULL, 0, &tmp_long, NULL) == LONGINT_OK
1300             && 0 <= tmp_long && tmp_long <= INT_MAX)
1301           {
1302             tabsize = (int) tmp_long;
1303           }
1304         else
1305           {
1306             error (0, 0,
1307              _("ignoring invalid tab size in environment variable TABSIZE: %s"),
1308                    quotearg (p));
1309           }
1310       }
1311   }
1312
1313   while ((c = getopt_long (argc, argv,
1314                            "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UX1",
1315                            long_options, NULL)) != -1)
1316     {
1317       switch (c)
1318         {
1319         case 0:
1320           break;
1321
1322         case 'a':
1323           all_files = 1;
1324           really_all_files = 1;
1325           break;
1326
1327         case 'b':
1328           set_quoting_style (NULL, escape_quoting_style);
1329           break;
1330
1331         case 'c':
1332           time_type = time_ctime;
1333           break;
1334
1335         case 'd':
1336           immediate_dirs = 1;
1337           break;
1338
1339         case 'f':
1340           /* Same as enabling -a -U and disabling -l -s.  */
1341           all_files = 1;
1342           really_all_files = 1;
1343           sort_type = sort_none;
1344           sort_type_specified = 1;
1345           /* disable -l */
1346           if (format == long_format)
1347             format = (isatty (STDOUT_FILENO) ? many_per_line : one_per_line);
1348           print_block_size = 0; /* disable -s */
1349           print_with_color = 0; /* disable --color */
1350           break;
1351
1352         case 'g':
1353           format = long_format;
1354           print_owner = 0;
1355           break;
1356
1357         case 'h':
1358           output_block_size = -1024;
1359           break;
1360
1361         case 'i':
1362           print_inode = 1;
1363           break;
1364
1365         case 'k':
1366           output_block_size = 1024;
1367           break;
1368
1369         case 'l':
1370           format = long_format;
1371           break;
1372
1373         case 'm':
1374           format = with_commas;
1375           break;
1376
1377         case 'n':
1378           numeric_ids = 1;
1379           format = long_format;
1380           break;
1381
1382         case 'o':  /* Just like -l, but don't display group info.  */
1383           format = long_format;
1384           print_group = 0;
1385           break;
1386
1387         case 'p':
1388           indicator_style = file_type;
1389           break;
1390
1391         case 'q':
1392           qmark_funny_chars = 1;
1393           break;
1394
1395         case 'r':
1396           sort_reverse = 1;
1397           break;
1398
1399         case 's':
1400           print_block_size = 1;
1401           break;
1402
1403         case 't':
1404           sort_type = sort_time;
1405           sort_type_specified = 1;
1406           break;
1407
1408         case 'u':
1409           time_type = time_atime;
1410           break;
1411
1412         case 'v':
1413           sort_type = sort_version;
1414           sort_type_specified = 1;
1415           break;
1416
1417         case 'w':
1418           {
1419             long int tmp_long;
1420             if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK
1421                 || tmp_long <= 0 || tmp_long > INT_MAX)
1422               error (EXIT_FAILURE, 0, _("invalid line width: %s"),
1423                      quotearg (optarg));
1424             line_length = (int) tmp_long;
1425             break;
1426           }
1427
1428         case 'x':
1429           format = horizontal;
1430           break;
1431
1432         case 'A':
1433           really_all_files = 0;
1434           all_files = 1;
1435           break;
1436
1437         case 'B':
1438           add_ignore_pattern ("*~");
1439           add_ignore_pattern (".*~");
1440           break;
1441
1442         case 'C':
1443           format = many_per_line;
1444           break;
1445
1446         case 'D':
1447           dired = 1;
1448           break;
1449
1450         case 'F':
1451           indicator_style = classify;
1452           break;
1453
1454         case 'G':               /* inhibit display of group info */
1455           print_group = 0;
1456           break;
1457
1458         case 'H':
1459           dereference = DEREF_COMMAND_LINE_ARGUMENTS;
1460           break;
1461
1462         case 'I':
1463           add_ignore_pattern (optarg);
1464           break;
1465
1466         case 'L':
1467           dereference = DEREF_ALWAYS;
1468           break;
1469
1470         case 'N':
1471           set_quoting_style (NULL, literal_quoting_style);
1472           break;
1473
1474         case 'Q':
1475           set_quoting_style (NULL, c_quoting_style);
1476           break;
1477
1478         case 'R':
1479           recursive = 1;
1480           break;
1481
1482         case 'S':
1483           sort_type = sort_size;
1484           sort_type_specified = 1;
1485           break;
1486
1487         case 'T':
1488           {
1489             long int tmp_long;
1490             if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK
1491                 || tmp_long < 0 || tmp_long > INT_MAX)
1492               error (EXIT_FAILURE, 0, _("invalid tab size: %s"),
1493                      quotearg (optarg));
1494             tabsize = (int) tmp_long;
1495             break;
1496           }
1497
1498         case 'U':
1499           sort_type = sort_none;
1500           sort_type_specified = 1;
1501           break;
1502
1503         case 'X':
1504           sort_type = sort_extension;
1505           sort_type_specified = 1;
1506           break;
1507
1508         case '1':
1509           /* -1 has no effect after -l.  */
1510           if (format != long_format)
1511             format = one_per_line;
1512           break;
1513
1514         case SORT_OPTION:
1515           sort_type = XARGMATCH ("--sort", optarg, sort_args, sort_types);
1516           sort_type_specified = 1;
1517           break;
1518
1519         case TIME_OPTION:
1520           time_type = XARGMATCH ("--time", optarg, time_args, time_types);
1521           break;
1522
1523         case FORMAT_OPTION:
1524           format = XARGMATCH ("--format", optarg, format_args, format_types);
1525           break;
1526
1527         case FULL_TIME_OPTION:
1528           format = long_format;
1529           time_style_option = "full-iso";
1530           break;
1531
1532         case COLOR_OPTION:
1533           {
1534             int i;
1535             if (optarg)
1536               i = XARGMATCH ("--color", optarg, color_args, color_types);
1537             else
1538               /* Using --color with no argument is equivalent to using
1539                  --color=always.  */
1540               i = color_always;
1541
1542             print_with_color = (i == color_always
1543                                 || (i == color_if_tty
1544                                     && isatty (STDOUT_FILENO)));
1545
1546             if (print_with_color)
1547               {
1548                 /* Don't use TAB characters in output.  Some terminal
1549                    emulators can't handle the combination of tabs and
1550                    color codes on the same line.  */
1551                 tabsize = 0;
1552               }
1553             break;
1554           }
1555
1556         case INDICATOR_STYLE_OPTION:
1557           indicator_style = XARGMATCH ("--indicator-style", optarg,
1558                                        indicator_style_args,
1559                                        indicator_style_types);
1560           break;
1561
1562         case QUOTING_STYLE_OPTION:
1563           set_quoting_style (NULL,
1564                              XARGMATCH ("--quoting-style", optarg,
1565                                         quoting_style_args,
1566                                         quoting_style_vals));
1567           break;
1568
1569         case TIME_STYLE_OPTION:
1570           time_style_option = optarg;
1571           break;
1572
1573         case SHOW_CONTROL_CHARS_OPTION:
1574           qmark_funny_chars = 0;
1575           break;
1576
1577         case BLOCK_SIZE_OPTION:
1578           human_block_size (optarg, 1, &output_block_size);
1579           break;
1580
1581         case SI_OPTION:
1582           output_block_size = -1000;
1583           break;
1584
1585         case_GETOPT_HELP_CHAR;
1586
1587         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1588
1589         default:
1590           usage (EXIT_FAILURE);
1591         }
1592     }
1593
1594   filename_quoting_options = clone_quoting_options (NULL);
1595   if (get_quoting_style (filename_quoting_options) == escape_quoting_style)
1596     set_char_quoting (filename_quoting_options, ' ', 1);
1597   if (indicator_style != none)
1598     {
1599       char const *p;
1600       for (p = "*=@|" + (int) indicator_style - 1;  *p;  p++)
1601         set_char_quoting (filename_quoting_options, *p, 1);
1602     }
1603
1604   dirname_quoting_options = clone_quoting_options (NULL);
1605   set_char_quoting (dirname_quoting_options, ':', 1);
1606
1607   /* If -c or -u is specified and not -l (or any other option that implies -l),
1608      and no sort-type was specified, then sort by the ctime (-c) or atime (-u).
1609      The behavior of ls when using either -c or -u but with neither -l nor -t
1610      appears to be unspecified by POSIX.  So, with GNU ls, `-u' alone means
1611      sort by atime (this is the one that's not specified by the POSIX spec),
1612      -lu means show atime and sort by name, -lut means show atime and sort
1613      by atime.  */
1614
1615   if ((time_type == time_ctime || time_type == time_atime)
1616       && !sort_type_specified && format != long_format)
1617     {
1618       sort_type = sort_time;
1619     }
1620
1621   if (format == long_format)
1622     {
1623       if (! time_style_option)
1624         time_style_option = getenv ("TIME_STYLE");
1625
1626       switch (time_style_option
1627               ? XARGMATCH ("time style", time_style_option,
1628                            time_style_args,
1629                            time_style_types)
1630               : posix_iso_time_style)
1631         {
1632         case full_iso_time_style:
1633           long_time_format[0] = long_time_format[1] =
1634             "%Y-%m-%d %H:%M:%S.%N %z";
1635           break;
1636
1637         case posix_iso_time_style:
1638           if (! hard_locale (LC_TIME))
1639             break;
1640           /* Fall through.  */
1641         case iso_time_style:
1642           long_time_format[0] = "%Y-%m-%d ";
1643           long_time_format[1] = "%m-%d %H:%M";
1644           break;
1645
1646         case locale_time_style:
1647           if (hard_locale (LC_TIME))
1648             {
1649               unsigned int i;
1650               for (i = 0; i < 2; i++)
1651                 long_time_format[i] =
1652                   dcgettext (NULL, long_time_format[i], LC_TIME);
1653             }
1654         }
1655     }
1656
1657   return optind;
1658 }
1659
1660 /* Parse a string as part of the LS_COLORS variable; this may involve
1661    decoding all kinds of escape characters.  If equals_end is set an
1662    unescaped equal sign ends the string, otherwise only a : or \0
1663    does.  Returns the number of characters output, or -1 on failure.
1664
1665    The resulting string is *not* null-terminated, but may contain
1666    embedded nulls.
1667
1668    Note that both dest and src are char **; on return they point to
1669    the first free byte after the array and the character that ended
1670    the input string, respectively.  */
1671
1672 static int
1673 get_funky_string (char **dest, const char **src, int equals_end)
1674 {
1675   int num;                      /* For numerical codes */
1676   int count;                    /* Something to count with */
1677   enum {
1678     ST_GND, ST_BACKSLASH, ST_OCTAL, ST_HEX, ST_CARET, ST_END, ST_ERROR
1679   } state;
1680   const char *p;
1681   char *q;
1682
1683   p = *src;                     /* We don't want to double-indirect */
1684   q = *dest;                    /* the whole darn time.  */
1685
1686   count = 0;                    /* No characters counted in yet.  */
1687   num = 0;
1688
1689   state = ST_GND;               /* Start in ground state.  */
1690   while (state < ST_END)
1691     {
1692       switch (state)
1693         {
1694         case ST_GND:            /* Ground state (no escapes) */
1695           switch (*p)
1696             {
1697             case ':':
1698             case '\0':
1699               state = ST_END;   /* End of string */
1700               break;
1701             case '\\':
1702               state = ST_BACKSLASH; /* Backslash scape sequence */
1703               ++p;
1704               break;
1705             case '^':
1706               state = ST_CARET; /* Caret escape */
1707               ++p;
1708               break;
1709             case '=':
1710               if (equals_end)
1711                 {
1712                   state = ST_END; /* End */
1713                   break;
1714                 }
1715               /* else fall through */
1716             default:
1717               *(q++) = *(p++);
1718               ++count;
1719               break;
1720             }
1721           break;
1722
1723         case ST_BACKSLASH:      /* Backslash escaped character */
1724           switch (*p)
1725             {
1726             case '0':
1727             case '1':
1728             case '2':
1729             case '3':
1730             case '4':
1731             case '5':
1732             case '6':
1733             case '7':
1734               state = ST_OCTAL; /* Octal sequence */
1735               num = *p - '0';
1736               break;
1737             case 'x':
1738             case 'X':
1739               state = ST_HEX;   /* Hex sequence */
1740               num = 0;
1741               break;
1742             case 'a':           /* Bell */
1743               num = 7;          /* Not all C compilers know what \a means */
1744               break;
1745             case 'b':           /* Backspace */
1746               num = '\b';
1747               break;
1748             case 'e':           /* Escape */
1749               num = 27;
1750               break;
1751             case 'f':           /* Form feed */
1752               num = '\f';
1753               break;
1754             case 'n':           /* Newline */
1755               num = '\n';
1756               break;
1757             case 'r':           /* Carriage return */
1758               num = '\r';
1759               break;
1760             case 't':           /* Tab */
1761               num = '\t';
1762               break;
1763             case 'v':           /* Vtab */
1764               num = '\v';
1765               break;
1766             case '?':           /* Delete */
1767               num = 127;
1768               break;
1769             case '_':           /* Space */
1770               num = ' ';
1771               break;
1772             case '\0':          /* End of string */
1773               state = ST_ERROR; /* Error! */
1774               break;
1775             default:            /* Escaped character like \ ^ : = */
1776               num = *p;
1777               break;
1778             }
1779           if (state == ST_BACKSLASH)
1780             {
1781               *(q++) = num;
1782               ++count;
1783               state = ST_GND;
1784             }
1785           ++p;
1786           break;
1787
1788         case ST_OCTAL:          /* Octal sequence */
1789           if (*p < '0' || *p > '7')
1790             {
1791               *(q++) = num;
1792               ++count;
1793               state = ST_GND;
1794             }
1795           else
1796             num = (num << 3) + (*(p++) - '0');
1797           break;
1798
1799         case ST_HEX:            /* Hex sequence */
1800           switch (*p)
1801             {
1802             case '0':
1803             case '1':
1804             case '2':
1805             case '3':
1806             case '4':
1807             case '5':
1808             case '6':
1809             case '7':
1810             case '8':
1811             case '9':
1812               num = (num << 4) + (*(p++) - '0');
1813               break;
1814             case 'a':
1815             case 'b':
1816             case 'c':
1817             case 'd':
1818             case 'e':
1819             case 'f':
1820               num = (num << 4) + (*(p++) - 'a') + 10;
1821               break;
1822             case 'A':
1823             case 'B':
1824             case 'C':
1825             case 'D':
1826             case 'E':
1827             case 'F':
1828               num = (num << 4) + (*(p++) - 'A') + 10;
1829               break;
1830             default:
1831               *(q++) = num;
1832               ++count;
1833               state = ST_GND;
1834               break;
1835             }
1836           break;
1837
1838         case ST_CARET:          /* Caret escape */
1839           state = ST_GND;       /* Should be the next state... */
1840           if (*p >= '@' && *p <= '~')
1841             {
1842               *(q++) = *(p++) & 037;
1843               ++count;
1844             }
1845           else if (*p == '?')
1846             {
1847               *(q++) = 127;
1848               ++count;
1849             }
1850           else
1851             state = ST_ERROR;
1852           break;
1853
1854         default:
1855           abort ();
1856         }
1857     }
1858
1859   *dest = q;
1860   *src = p;
1861
1862   return state == ST_ERROR ? -1 : count;
1863 }
1864
1865 static void
1866 parse_ls_color (void)
1867 {
1868   const char *p;                /* Pointer to character being parsed */
1869   char *buf;                    /* color_buf buffer pointer */
1870   int state;                    /* State of parser */
1871   int ind_no;                   /* Indicator number */
1872   char label[3];                /* Indicator label */
1873   struct color_ext_type *ext;   /* Extension we are working on */
1874
1875   if ((p = getenv ("LS_COLORS")) == NULL || *p == '\0')
1876     return;
1877
1878   ext = NULL;
1879   strcpy (label, "??");
1880
1881   /* This is an overly conservative estimate, but any possible
1882      LS_COLORS string will *not* generate a color_buf longer than
1883      itself, so it is a safe way of allocating a buffer in
1884      advance.  */
1885   buf = color_buf = xstrdup (p);
1886
1887   state = 1;
1888   while (state > 0)
1889     {
1890       switch (state)
1891         {
1892         case 1:         /* First label character */
1893           switch (*p)
1894             {
1895             case ':':
1896               ++p;
1897               break;
1898
1899             case '*':
1900               /* Allocate new extension block and add to head of
1901                  linked list (this way a later definition will
1902                  override an earlier one, which can be useful for
1903                  having terminal-specific defs override global).  */
1904
1905               ext = XMALLOC (struct color_ext_type, 1);
1906               ext->next = color_ext_list;
1907               color_ext_list = ext;
1908
1909               ++p;
1910               ext->ext.string = buf;
1911
1912               state = (ext->ext.len =
1913                        get_funky_string (&buf, &p, 1)) < 0 ? -1 : 4;
1914               break;
1915
1916             case '\0':
1917               state = 0;        /* Done! */
1918               break;
1919
1920             default:    /* Assume it is file type label */
1921               label[0] = *(p++);
1922               state = 2;
1923               break;
1924             }
1925           break;
1926
1927         case 2:         /* Second label character */
1928           if (*p)
1929             {
1930               label[1] = *(p++);
1931               state = 3;
1932             }
1933           else
1934             state = -1; /* Error */
1935           break;
1936
1937         case 3:         /* Equal sign after indicator label */
1938           state = -1;   /* Assume failure... */
1939           if (*(p++) == '=')/* It *should* be... */
1940             {
1941               for (ind_no = 0; indicator_name[ind_no] != NULL; ++ind_no)
1942                 {
1943                   if (STREQ (label, indicator_name[ind_no]))
1944                     {
1945                       color_indicator[ind_no].string = buf;
1946                       state = ((color_indicator[ind_no].len =
1947                                 get_funky_string (&buf, &p, 0)) < 0 ? -1 : 1);
1948                       break;
1949                     }
1950                 }
1951               if (state == -1)
1952                 error (0, 0, _("unrecognized prefix: %s"), quotearg (label));
1953             }
1954          break;
1955
1956         case 4:         /* Equal sign after *.ext */
1957           if (*(p++) == '=')
1958             {
1959               ext->seq.string = buf;
1960               state = (ext->seq.len =
1961                        get_funky_string (&buf, &p, 0)) < 0 ? -1 : 1;
1962             }
1963           else
1964             state = -1;
1965           break;
1966         }
1967     }
1968
1969   if (state < 0)
1970     {
1971       struct color_ext_type *e;
1972       struct color_ext_type *e2;
1973
1974       error (0, 0,
1975              _("unparsable value for LS_COLORS environment variable"));
1976       free (color_buf);
1977       for (e = color_ext_list; e != NULL; /* empty */)
1978         {
1979           e2 = e;
1980           e = e->next;
1981           free (e2);
1982         }
1983       print_with_color = 0;
1984     }
1985
1986   if (color_indicator[C_LINK].len == 6
1987       && !strncmp (color_indicator[C_LINK].string, "target", 6))
1988     color_symlink_as_referent = 1;
1989 }
1990
1991 /* Request that the directory named NAME have its contents listed later.
1992    If REALNAME is nonzero, it will be used instead of NAME when the
1993    directory name is printed.  This allows symbolic links to directories
1994    to be treated as regular directories but still be listed under their
1995    real names.  NAME == NULL is used to insert a marker entry for the
1996    directory named in REALNAME.
1997    If F is non-NULL, we use its dev/ino information to save
1998    a call to stat -- when doing a recursive (-R) traversal.  */
1999
2000 static void
2001 queue_directory (const char *name, const char *realname)
2002 {
2003   struct pending *new;
2004
2005   new = XMALLOC (struct pending, 1);
2006   new->realname = realname ? xstrdup (realname) : NULL;
2007   new->name = name ? xstrdup (name) : NULL;
2008   new->next = pending_dirs;
2009   pending_dirs = new;
2010 }
2011
2012 /* Read directory `name', and list the files in it.
2013    If `realname' is nonzero, print its name instead of `name';
2014    this is used for symbolic links to directories. */
2015
2016 static void
2017 print_dir (const char *name, const char *realname)
2018 {
2019   register DIR *reading;
2020   register struct dirent *next;
2021   register uintmax_t total_blocks = 0;
2022
2023   errno = 0;
2024   reading = opendir (name);
2025   if (!reading)
2026     {
2027       error (0, errno, "%s", quotearg_colon (name));
2028       exit_status = 1;
2029       return;
2030     }
2031
2032   if (LOOP_DETECT)
2033     {
2034       struct stat dir_stat;
2035       int fd = dirfd (reading);
2036
2037       /* If dirfd failed, endure the overhead of using stat.  */
2038       if ((0 <= fd
2039            ? fstat (fd, &dir_stat)
2040            : stat (name, &dir_stat)) < 0)
2041         {
2042           error (0, errno, _("cannot determine device and inode of %s"),
2043                  quotearg_colon (name));
2044           exit_status = 1;
2045           return;
2046         }
2047
2048       /* If we've already visited this dev/inode pair, warn that
2049          we've found a loop, and do not process this directory.  */
2050       if (visit_dir (dir_stat.st_dev, dir_stat.st_ino))
2051         {
2052           error (0, 0, _("not listing already-listed directory: %s"),
2053                  quotearg_colon (name));
2054           return;
2055         }
2056
2057       DEV_INO_PUSH (dir_stat.st_dev, dir_stat.st_ino);
2058     }
2059
2060   /* Read the directory entries, and insert the subfiles into the `files'
2061      table.  */
2062
2063   clear_files ();
2064
2065   while ((next = readdir (reading)) != NULL)
2066     if (file_interesting (next))
2067       {
2068         enum filetype type = unknown;
2069
2070 #if HAVE_STRUCT_DIRENT_D_TYPE
2071         if (next->d_type == DT_DIR || next->d_type == DT_CHR
2072             || next->d_type == DT_BLK || next->d_type == DT_SOCK
2073             || next->d_type == DT_FIFO)
2074           type = next->d_type;
2075 #endif
2076         total_blocks += gobble_file (next->d_name, type, 0, name);
2077       }
2078
2079   if (CLOSEDIR (reading))
2080     {
2081       error (0, errno, "%s", quotearg_colon (name));
2082       exit_status = 1;
2083       /* Don't return; print whatever we got. */
2084     }
2085
2086   /* Sort the directory contents.  */
2087   sort_files ();
2088
2089   /* If any member files are subdirectories, perhaps they should have their
2090      contents listed rather than being mentioned here as files.  */
2091
2092   if (recursive)
2093     extract_dirs_from_files (name, 1);
2094
2095   if (recursive || print_dir_name)
2096     {
2097       DIRED_INDENT ();
2098       PUSH_CURRENT_DIRED_POS (&subdired_obstack);
2099       dired_pos += quote_name (stdout, realname ? realname : name,
2100                                dirname_quoting_options);
2101       PUSH_CURRENT_DIRED_POS (&subdired_obstack);
2102       DIRED_FPUTS_LITERAL (":\n", stdout);
2103     }
2104
2105   if (format == long_format || print_block_size)
2106     {
2107       const char *p;
2108       char buf[LONGEST_HUMAN_READABLE + 1];
2109
2110       DIRED_INDENT ();
2111       p = _("total");
2112       DIRED_FPUTS (p, stdout, strlen (p));
2113       DIRED_PUTCHAR (' ');
2114       p = human_readable_inexact (total_blocks, buf, ST_NBLOCKSIZE,
2115                                   output_block_size, human_ceiling);
2116       DIRED_FPUTS (p, stdout, strlen (p));
2117       DIRED_PUTCHAR ('\n');
2118     }
2119
2120   if (files_index)
2121     print_current_files ();
2122
2123   if (pending_dirs && pending_dirs->name)
2124     DIRED_PUTCHAR ('\n');
2125 }
2126
2127 /* Add `pattern' to the list of patterns for which files that match are
2128    not listed.  */
2129
2130 static void
2131 add_ignore_pattern (const char *pattern)
2132 {
2133   register struct ignore_pattern *ignore;
2134
2135   ignore = XMALLOC (struct ignore_pattern, 1);
2136   ignore->pattern = pattern;
2137   /* Add it to the head of the linked list. */
2138   ignore->next = ignore_patterns;
2139   ignore_patterns = ignore;
2140 }
2141
2142 /* Return nonzero if the file in `next' should be listed. */
2143
2144 static int
2145 file_interesting (const struct dirent *next)
2146 {
2147   register struct ignore_pattern *ignore;
2148
2149   for (ignore = ignore_patterns; ignore; ignore = ignore->next)
2150     if (fnmatch (ignore->pattern, next->d_name, FNM_PERIOD) == 0)
2151       return 0;
2152
2153   if (really_all_files
2154       || next->d_name[0] != '.'
2155       || (all_files
2156           && next->d_name[1] != '\0'
2157           && (next->d_name[1] != '.' || next->d_name[2] != '\0')))
2158     return 1;
2159
2160   return 0;
2161 }
2162
2163 /* Enter and remove entries in the table `files'.  */
2164
2165 /* Empty the table of files. */
2166
2167 static void
2168 clear_files (void)
2169 {
2170   register int i;
2171
2172   for (i = 0; i < files_index; i++)
2173     {
2174       free (files[i].name);
2175       if (files[i].linkname)
2176         free (files[i].linkname);
2177     }
2178
2179   files_index = 0;
2180   block_size_size = 4;
2181 }
2182
2183 /* Add a file to the current table of files.
2184    Verify that the file exists, and print an error message if it does not.
2185    Return the number of blocks that the file occupies.  */
2186
2187 static uintmax_t
2188 gobble_file (const char *name, enum filetype type, int explicit_arg,
2189              const char *dirname)
2190 {
2191   register uintmax_t blocks;
2192   register char *path;
2193
2194   if (files_index == nfiles)
2195     {
2196       nfiles *= 2;
2197       files = XREALLOC (files, struct fileinfo, nfiles);
2198     }
2199
2200   files[files_index].linkname = 0;
2201   files[files_index].linkmode = 0;
2202   files[files_index].linkok = 0;
2203
2204   /* FIXME: this use of ls: `mkdir a; touch a/{b,c,d}; ls -R a'
2205      shouldn't require that ls stat b, c, and d -- at least
2206      not on systems with usable d_type.  The problem is that
2207      format_needs_stat is set, because of the -R.  */
2208   if (explicit_arg || format_needs_stat
2209       || (format_needs_type && type == unknown))
2210     {
2211       /* `path' is the absolute pathname of this file. */
2212       int val;
2213
2214       if (name[0] == '/' || dirname[0] == 0)
2215         path = (char *) name;
2216       else
2217         {
2218           path = (char *) alloca (strlen (name) + strlen (dirname) + 2);
2219           attach (path, dirname, name);
2220         }
2221
2222       val = (DEREF_ALWAYS <= dereference + explicit_arg
2223              ? stat (path, &files[files_index].stat)
2224              : lstat (path, &files[files_index].stat));
2225
2226       if (val < 0)
2227         {
2228           error (0, errno, "%s", quotearg_colon (path));
2229           exit_status = 1;
2230           return 0;
2231         }
2232
2233 #if USE_ACL
2234       if (format == long_format)
2235         files[files_index].have_acl =
2236           (! S_ISLNK (files[files_index].stat.st_mode)
2237            && 4 < acl (path, GETACLCNT, 0, NULL));
2238 #endif
2239
2240       if (S_ISLNK (files[files_index].stat.st_mode)
2241           && (format == long_format || check_symlink_color))
2242         {
2243           char *linkpath;
2244           struct stat linkstats;
2245
2246           get_link_name (path, &files[files_index]);
2247           linkpath = make_link_path (path, files[files_index].linkname);
2248
2249           /* Avoid following symbolic links when possible, ie, when
2250              they won't be traced and when no indicator is needed. */
2251           if (linkpath
2252               && (indicator_style != none || check_symlink_color)
2253               && stat (linkpath, &linkstats) == 0)
2254             {
2255               files[files_index].linkok = 1;
2256
2257               /* Symbolic links to directories that are mentioned on the
2258                  command line are automatically traced if not being
2259                  listed as files.  */
2260               if (explicit_arg && format != long_format
2261                   && S_ISDIR (linkstats.st_mode))
2262                 {
2263                   /* Substitute the linked-to directory's name, but
2264                      save the real name in `linkname' for printing.  */
2265                   if (!immediate_dirs)
2266                     {
2267                       const char *tempname = name;
2268                       name = linkpath;
2269                       linkpath = files[files_index].linkname;
2270                       files[files_index].linkname = (char *) tempname;
2271                     }
2272                   files[files_index].stat = linkstats;
2273                 }
2274               else
2275                 {
2276                   /* Get the linked-to file's mode for the filetype indicator
2277                      in long listings.  */
2278                   files[files_index].linkmode = linkstats.st_mode;
2279                   files[files_index].linkok = 1;
2280                 }
2281             }
2282           if (linkpath)
2283             free (linkpath);
2284         }
2285
2286       if (S_ISLNK (files[files_index].stat.st_mode))
2287         files[files_index].filetype = symbolic_link;
2288       else if (S_ISDIR (files[files_index].stat.st_mode))
2289         {
2290           if (explicit_arg && !immediate_dirs)
2291             files[files_index].filetype = arg_directory;
2292           else
2293             files[files_index].filetype = directory;
2294         }
2295       else
2296         files[files_index].filetype = normal;
2297
2298       blocks = ST_NBLOCKS (files[files_index].stat);
2299       {
2300         char buf[LONGEST_HUMAN_READABLE + 1];
2301         int len = strlen (human_readable_inexact (blocks, buf, ST_NBLOCKSIZE,
2302                                                   output_block_size,
2303                                                   human_ceiling));
2304         if (block_size_size < len)
2305           block_size_size = len < 7 ? len : 7;
2306       }
2307     }
2308   else
2309     {
2310       files[files_index].filetype = type;
2311 #if HAVE_STRUCT_DIRENT_D_TYPE
2312       files[files_index].stat.st_mode = DTTOIF (type);
2313 #endif
2314       blocks = 0;
2315     }
2316
2317   files[files_index].name = xstrdup (name);
2318   files_index++;
2319
2320   return blocks;
2321 }
2322
2323 #if HAVE_SYMLINKS
2324
2325 /* Put the name of the file that `filename' is a symbolic link to
2326    into the `linkname' field of `f'. */
2327
2328 static void
2329 get_link_name (const char *filename, struct fileinfo *f)
2330 {
2331   f->linkname = xreadlink (filename);
2332   if (f->linkname == NULL)
2333     {
2334       error (0, errno, _("cannot read symbolic link %s"),
2335              quotearg_colon (filename));
2336       exit_status = 1;
2337     }
2338 }
2339
2340 /* If `linkname' is a relative path and `path' contains one or more
2341    leading directories, return `linkname' with those directories
2342    prepended; otherwise, return a copy of `linkname'.
2343    If `linkname' is zero, return zero. */
2344
2345 static char *
2346 make_link_path (const char *path, const char *linkname)
2347 {
2348   char *linkbuf;
2349   size_t bufsiz;
2350
2351   if (linkname == 0)
2352     return 0;
2353
2354   if (*linkname == '/')
2355     return xstrdup (linkname);
2356
2357   /* The link is to a relative path.  Prepend any leading path
2358      in `path' to the link name. */
2359   linkbuf = strrchr (path, '/');
2360   if (linkbuf == 0)
2361     return xstrdup (linkname);
2362
2363   bufsiz = linkbuf - path + 1;
2364   linkbuf = xmalloc (bufsiz + strlen (linkname) + 1);
2365   strncpy (linkbuf, path, bufsiz);
2366   strcpy (linkbuf + bufsiz, linkname);
2367   return linkbuf;
2368 }
2369 #endif
2370
2371 /* Return nonzero if base_name (NAME) ends in `.' or `..'
2372    This is so we don't try to recurse on `././././. ...' */
2373
2374 static int
2375 basename_is_dot_or_dotdot (const char *name)
2376 {
2377   char const *base = base_name (name);
2378   return DOT_OR_DOTDOT (base);
2379 }
2380
2381 /* Remove any entries from `files' that are for directories,
2382    and queue them to be listed as directories instead.
2383    `dirname' is the prefix to prepend to each dirname
2384    to make it correct relative to ls's working dir.
2385    If IGNORE_DOT_AND_DOT_DOT is nonzero don't treat `.' and `..' as dirs.
2386    This is desirable when processing directories recursively.  */
2387
2388 static void
2389 extract_dirs_from_files (const char *dirname, int ignore_dot_and_dot_dot)
2390 {
2391   register int i, j;
2392
2393   if (*dirname && LOOP_DETECT)
2394     {
2395       /* Insert a marker entry first.  When we dequeue this marker entry,
2396          we'll know that DIRNAME has been processed and may be removed
2397          from the set of active directories.  */
2398       queue_directory (NULL, dirname);
2399     }
2400
2401   /* Queue the directories last one first, because queueing reverses the
2402      order.  */
2403   for (i = files_index - 1; i >= 0; i--)
2404     if ((files[i].filetype == directory || files[i].filetype == arg_directory)
2405         && (!ignore_dot_and_dot_dot
2406             || !basename_is_dot_or_dotdot (files[i].name)))
2407       {
2408         if (files[i].name[0] == '/' || dirname[0] == 0)
2409           {
2410             queue_directory (files[i].name, files[i].linkname);
2411           }
2412         else
2413           {
2414             char *path = path_concat (dirname, files[i].name, NULL);
2415             queue_directory (path, files[i].linkname);
2416             free (path);
2417           }
2418         if (files[i].filetype == arg_directory)
2419           free (files[i].name);
2420       }
2421
2422   /* Now delete the directories from the table, compacting all the remaining
2423      entries.  */
2424
2425   for (i = 0, j = 0; i < files_index; i++)
2426     if (files[i].filetype != arg_directory)
2427       files[j++] = files[i];
2428   files_index = j;
2429 }
2430
2431 /* Sort the files now in the table.  */
2432
2433 static void
2434 sort_files (void)
2435 {
2436   int (*func) ();
2437
2438   switch (sort_type)
2439     {
2440     case sort_none:
2441       return;
2442     case sort_time:
2443       switch (time_type)
2444         {
2445         case time_ctime:
2446           func = sort_reverse ? rev_cmp_ctime : compare_ctime;
2447           break;
2448         case time_mtime:
2449           func = sort_reverse ? rev_cmp_mtime : compare_mtime;
2450           break;
2451         case time_atime:
2452           func = sort_reverse ? rev_cmp_atime : compare_atime;
2453           break;
2454         default:
2455           abort ();
2456         }
2457       break;
2458     case sort_name:
2459       func = sort_reverse ? rev_cmp_name : compare_name;
2460       break;
2461     case sort_extension:
2462       func = sort_reverse ? rev_cmp_extension : compare_extension;
2463       break;
2464     case sort_size:
2465       func = sort_reverse ? rev_cmp_size : compare_size;
2466       break;
2467     case sort_version:
2468       func = sort_reverse ? rev_cmp_version : compare_version;
2469       break;
2470     default:
2471       abort ();
2472     }
2473
2474   qsort (files, files_index, sizeof (struct fileinfo), func);
2475 }
2476
2477 /* Comparison routines for sorting the files. */
2478
2479 static int
2480 compare_ctime (const struct fileinfo *file1, const struct fileinfo *file2)
2481 {
2482   int diff = CTIME_CMP (file2->stat, file1->stat);
2483   if (diff == 0)
2484     diff = strcoll (file1->name, file2->name);
2485   return diff;
2486 }
2487
2488 static int
2489 rev_cmp_ctime (const struct fileinfo *file2, const struct fileinfo *file1)
2490 {
2491   int diff = CTIME_CMP (file2->stat, file1->stat);
2492   if (diff == 0)
2493     diff = strcoll (file1->name, file2->name);
2494   return diff;
2495 }
2496
2497 static int
2498 compare_mtime (const struct fileinfo *file1, const struct fileinfo *file2)
2499 {
2500   int diff = MTIME_CMP (file2->stat, file1->stat);
2501   if (diff == 0)
2502     diff = strcoll (file1->name, file2->name);
2503   return diff;
2504 }
2505
2506 static int
2507 rev_cmp_mtime (const struct fileinfo *file2, const struct fileinfo *file1)
2508 {
2509   int diff = MTIME_CMP (file2->stat, file1->stat);
2510   if (diff == 0)
2511     diff = strcoll (file1->name, file2->name);
2512   return diff;
2513 }
2514
2515 static int
2516 compare_atime (const struct fileinfo *file1, const struct fileinfo *file2)
2517 {
2518   int diff = ATIME_CMP (file2->stat, file1->stat);
2519   if (diff == 0)
2520     diff = strcoll (file1->name, file2->name);
2521   return diff;
2522 }
2523
2524 static int
2525 rev_cmp_atime (const struct fileinfo *file2, const struct fileinfo *file1)
2526 {
2527   int diff = ATIME_CMP (file2->stat, file1->stat);
2528   if (diff == 0)
2529     diff = strcoll (file1->name, file2->name);
2530   return diff;
2531 }
2532
2533 static int
2534 compare_size (const struct fileinfo *file1, const struct fileinfo *file2)
2535 {
2536   int diff = longdiff (file2->stat.st_size, file1->stat.st_size);
2537   if (diff == 0)
2538     diff = strcoll (file1->name, file2->name);
2539   return diff;
2540 }
2541
2542 static int
2543 rev_cmp_size (const struct fileinfo *file2, const struct fileinfo *file1)
2544 {
2545   int diff = longdiff (file2->stat.st_size, file1->stat.st_size);
2546   if (diff == 0)
2547     diff = strcoll (file1->name, file2->name);
2548   return diff;
2549 }
2550
2551 static int
2552 compare_version (const struct fileinfo *file1, const struct fileinfo *file2)
2553 {
2554   return strverscmp (file1->name, file2->name);
2555 }
2556
2557 static int
2558 rev_cmp_version (const struct fileinfo *file2, const struct fileinfo *file1)
2559 {
2560   return strverscmp (file1->name, file2->name);
2561 }
2562
2563 static int
2564 compare_name (const struct fileinfo *file1, const struct fileinfo *file2)
2565 {
2566   return strcoll (file1->name, file2->name);
2567 }
2568
2569 static int
2570 rev_cmp_name (const struct fileinfo *file2, const struct fileinfo *file1)
2571 {
2572   return strcoll (file1->name, file2->name);
2573 }
2574
2575 /* Compare file extensions.  Files with no extension are `smallest'.
2576    If extensions are the same, compare by filenames instead. */
2577
2578 static int
2579 compare_extension (const struct fileinfo *file1, const struct fileinfo *file2)
2580 {
2581   int cmp;
2582   char const *base1 = strrchr (file1->name, '.');
2583   char const *base2 = strrchr (file2->name, '.');
2584   if (base1 == 0 && base2 == 0)
2585     return strcoll (file1->name, file2->name);
2586   if (base1 == 0)
2587     return -1;
2588   if (base2 == 0)
2589     return 1;
2590   cmp = strcoll (base1, base2);
2591   if (cmp == 0)
2592     return strcoll (file1->name, file2->name);
2593   return cmp;
2594 }
2595
2596 static int
2597 rev_cmp_extension (const struct fileinfo *file2, const struct fileinfo *file1)
2598 {
2599   int cmp;
2600   char const *base1 = strrchr (file1->name, '.');
2601   char const *base2 = strrchr (file2->name, '.');
2602   if (base1 == 0 && base2 == 0)
2603     return strcoll (file1->name, file2->name);
2604   if (base1 == 0)
2605     return -1;
2606   if (base2 == 0)
2607     return 1;
2608   cmp = strcoll (base1, base2);
2609   if (cmp == 0)
2610     return strcoll (file1->name, file2->name);
2611   return cmp;
2612 }
2613
2614 /* List all the files now in the table.  */
2615
2616 static void
2617 print_current_files (void)
2618 {
2619   register int i;
2620
2621   switch (format)
2622     {
2623     case one_per_line:
2624       for (i = 0; i < files_index; i++)
2625         {
2626           print_file_name_and_frills (files + i);
2627           putchar ('\n');
2628         }
2629       break;
2630
2631     case many_per_line:
2632       init_column_info ();
2633       print_many_per_line ();
2634       break;
2635
2636     case horizontal:
2637       init_column_info ();
2638       print_horizontal ();
2639       break;
2640
2641     case with_commas:
2642       print_with_commas ();
2643       break;
2644
2645     case long_format:
2646       for (i = 0; i < files_index; i++)
2647         {
2648           print_long_format (files + i);
2649           DIRED_PUTCHAR ('\n');
2650         }
2651       break;
2652     }
2653 }
2654
2655 /* Return the expected number of columns in a long-format time stamp,
2656    or zero if it cannot be calculated.  */
2657
2658 static int
2659 long_time_expected_width (void)
2660 {
2661   static int width = -1;
2662
2663   if (width < 0)
2664     {
2665       time_t epoch = 0;
2666       struct tm const *tm = localtime (&epoch);
2667       char const *fmt = long_time_format[0];
2668       char initbuf[100];
2669       char *buf = initbuf;
2670       size_t bufsize = sizeof initbuf;
2671       size_t len;
2672
2673       for (;;)
2674         {
2675           *buf = '\1';
2676           len = nstrftime (buf, bufsize, fmt, tm, 0, 0);
2677           if (len || ! *buf)
2678             break;
2679           buf = alloca (bufsize *= 2);
2680         }
2681
2682       width = mbsnwidth (buf, len, 0);
2683       if (width < 0)
2684         width = 0;
2685     }
2686
2687   return width;
2688 }
2689
2690 /* Get the current time.  */
2691
2692 static void
2693 get_current_time (void)
2694 {
2695 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
2696   {
2697     struct timespec timespec;
2698     if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
2699       {
2700         current_time = timespec.tv_sec;
2701         current_time_ns = timespec.tv_nsec;
2702         return;
2703       }
2704   }
2705 #endif
2706
2707   /* The clock does not have nanosecond resolution, so get the maximum
2708      possible value for the current time that is consistent with the
2709      reported clock.  That way, files are not considered to be in the
2710      future merely because their time stamps have higher resolution
2711      than the clock resolution.  */
2712
2713 #if HAVE_GETTIMEOFDAY
2714   {
2715     struct timeval timeval;
2716     if (gettimeofday (&timeval, NULL) == 0)
2717       {
2718         current_time = timeval.tv_sec;
2719         current_time_ns = timeval.tv_usec * 1000 + 999;
2720         return;
2721       }
2722   }
2723 #endif
2724
2725   current_time = time (NULL);
2726   current_time_ns = 999999999;
2727 }
2728
2729 static void
2730 print_long_format (const struct fileinfo *f)
2731 {
2732   char modebuf[12];
2733
2734   /* 7 fields that may require LONGEST_HUMAN_READABLE bytes,
2735      1 10-byte mode string,
2736      1 35-byte time string (may be longer in some locales -- see below)
2737        or LONGEST_HUMAN_READABLE integer,
2738      9 spaces, one following each of these fields, and
2739      1 trailing NUL byte.  */
2740   char init_bigbuf[7 * LONGEST_HUMAN_READABLE + 10
2741                    + MAX (35, LONGEST_HUMAN_READABLE)
2742                    + 9 + 1];
2743   char *buf = init_bigbuf;
2744   size_t bufsize = sizeof (init_bigbuf);
2745   size_t s;
2746   char *p;
2747   time_t when;
2748   int when_ns IF_LINT (= 0);
2749   struct tm *when_local;
2750
2751 #if HAVE_ST_DM_MODE
2752   /* Cray DMF: look at the file's migrated, not real, status */
2753   mode_string (f->stat.st_dm_mode, modebuf);
2754 #else
2755   mode_string (f->stat.st_mode, modebuf);
2756 #endif
2757
2758   modebuf[10] = (FILE_HAS_ACL (f) ? '+' : ' ');
2759   modebuf[11] = '\0';
2760
2761   switch (time_type)
2762     {
2763     case time_ctime:
2764       when = f->stat.st_ctime;
2765       when_ns = TIMESPEC_NS (f->stat.st_ctim);
2766       break;
2767     case time_mtime:
2768       when = f->stat.st_mtime;
2769       when_ns = TIMESPEC_NS (f->stat.st_mtim);
2770       break;
2771     case time_atime:
2772       when = f->stat.st_atime;
2773       when_ns = TIMESPEC_NS (f->stat.st_atim);
2774       break;
2775     }
2776
2777   p = buf;
2778
2779   if (print_inode)
2780     {
2781       char hbuf[LONGEST_HUMAN_READABLE + 1];
2782       sprintf (p, "%*s ", INODE_DIGITS,
2783                human_readable ((uintmax_t) f->stat.st_ino, hbuf, 1, 1));
2784       p += strlen (p);
2785     }
2786
2787   if (print_block_size)
2788     {
2789       char hbuf[LONGEST_HUMAN_READABLE + 1];
2790       sprintf (p, "%*s ", block_size_size,
2791                human_readable_inexact ((uintmax_t) ST_NBLOCKS (f->stat), hbuf,
2792                                        ST_NBLOCKSIZE, output_block_size,
2793                                        human_ceiling));
2794       p += strlen (p);
2795     }
2796
2797   /* The last byte of the mode string is the POSIX
2798      "optional alternate access method flag".  */
2799   sprintf (p, "%s %3lu ", modebuf, (unsigned long) f->stat.st_nlink);
2800   p += strlen (p);
2801
2802   if (print_owner)
2803     {
2804       char const *user_name = (numeric_ids ? NULL : getuser (f->stat.st_uid));
2805       if (user_name)
2806         sprintf (p, "%-8s ", user_name);
2807       else
2808         sprintf (p, "%-8lu ", (unsigned long) f->stat.st_uid);
2809       p += strlen (p);
2810     }
2811
2812   if (print_group)
2813     {
2814       char const *group_name = (numeric_ids ? NULL : getgroup (f->stat.st_gid));
2815       if (group_name)
2816         sprintf (p, "%-8s ", group_name);
2817       else
2818         sprintf (p, "%-8lu ", (unsigned long) f->stat.st_gid);
2819       p += strlen (p);
2820     }
2821
2822   if (S_ISCHR (f->stat.st_mode) || S_ISBLK (f->stat.st_mode))
2823     sprintf (p, "%3lu, %3lu ",
2824              (unsigned long) major (f->stat.st_rdev),
2825              (unsigned long) minor (f->stat.st_rdev));
2826   else
2827     {
2828       char hbuf[LONGEST_HUMAN_READABLE + 1];
2829       uintmax_t size = f->stat.st_size;
2830
2831       /* POSIX requires that the size be printed without a sign, even
2832          when negative.  Assume the typical case where negative sizes
2833          are actually positive values that have wrapped around.  */
2834       size += (f->stat.st_size < 0) * ((uintmax_t) OFF_T_MAX - OFF_T_MIN + 1);
2835
2836       sprintf (p, "%8s ",
2837                human_readable (size, hbuf, 1,
2838                                output_block_size < 0 ? output_block_size : 1));
2839     }
2840
2841   p += strlen (p);
2842
2843   if ((when_local = localtime (&when)))
2844     {
2845       time_t six_months_ago;
2846       int recent;
2847       char const *fmt;
2848
2849       /* If the file appears to be in the future, update the current
2850          time, in case the file happens to have been modified since
2851          the last time we checked the clock.  */
2852       if (current_time < when
2853           || (current_time == when && current_time_ns < when_ns))
2854         {
2855           /* Note that get_current_time calls gettimeofday which, on some non-
2856              compliant systems, clobbers the buffer used for localtime's result.
2857              But it's ok here, because we use a gettimeofday wrapper that
2858              saves and restores the buffer around the gettimeofday call.  */
2859           get_current_time ();
2860         }
2861
2862       /* Consider a time to be recent if it is within the past six
2863          months.  A Gregorian year has 365.2425 * 24 * 60 * 60 ==
2864          31556952 seconds on the average.  Write this value as an
2865          integer constant to avoid floating point hassles.  */
2866       six_months_ago = current_time - 31556952 / 2;
2867       recent = (six_months_ago <= when
2868                 && (when < current_time
2869                     || (when == current_time && when_ns <= current_time_ns)));
2870       fmt = long_time_format[recent];
2871
2872       for (;;)
2873         {
2874           char *newbuf;
2875           *p = '\1';
2876           s = nstrftime (p, buf + bufsize - p - 1, fmt,
2877                          when_local, 0, when_ns);
2878           if (s || ! *p)
2879             break;
2880           newbuf = alloca (bufsize *= 2);
2881           memcpy (newbuf, buf, p - buf);
2882           p = newbuf + (p - buf);
2883           buf = newbuf;
2884         }
2885
2886       p += s;
2887       *p++ = ' ';
2888
2889       /* NUL-terminate the string -- fputs (via DIRED_FPUTS) requires it.  */
2890       *p = '\0';
2891     }
2892   else
2893     {
2894       /* The time cannot be represented as a local time;
2895          print it as a huge integer number of seconds.  */
2896       char hbuf[LONGEST_HUMAN_READABLE + 1];
2897       int width = long_time_expected_width ();
2898
2899       if (when < 0)
2900         {
2901           const char *num = human_readable (- (uintmax_t) when, hbuf, 1, 1);
2902           int sign_width = width - strlen (num);
2903           sprintf (p, "%*s%s ", sign_width < 0 ? 0 : sign_width, "-", num);
2904         }
2905       else
2906         sprintf (p, "%*s ", width,
2907                  human_readable ((uintmax_t) when, hbuf, 1, 1));
2908
2909       p += strlen (p);
2910     }
2911
2912   DIRED_INDENT ();
2913   DIRED_FPUTS (buf, stdout, p - buf);
2914   print_name_with_quoting (f->name, FILE_OR_LINK_MODE (f), f->linkok,
2915                            &dired_obstack);
2916
2917   if (f->filetype == symbolic_link)
2918     {
2919       if (f->linkname)
2920         {
2921           DIRED_FPUTS_LITERAL (" -> ", stdout);
2922           print_name_with_quoting (f->linkname, f->linkmode, f->linkok - 1,
2923                                    NULL);
2924           if (indicator_style != none)
2925             print_type_indicator (f->linkmode);
2926         }
2927     }
2928   else if (indicator_style != none)
2929     print_type_indicator (f->stat.st_mode);
2930 }
2931
2932 /* Output to OUT a quoted representation of the file name NAME,
2933    using OPTIONS to control quoting.  Produce no output if OUT is NULL.
2934    Return the number of screen columns occupied by NAME's quoted
2935    representation.  */
2936
2937 static size_t
2938 quote_name (FILE *out, const char *name, struct quoting_options const *options)
2939 {
2940   char smallbuf[BUFSIZ];
2941   size_t len = quotearg_buffer (smallbuf, sizeof smallbuf, name, -1, options);
2942   char *buf;
2943   int displayed_width;
2944
2945   if (len < sizeof smallbuf)
2946     buf = smallbuf;
2947   else
2948     {
2949       buf = (char *) alloca (len + 1);
2950       quotearg_buffer (buf, len + 1, name, -1, options);
2951     }
2952
2953   if (qmark_funny_chars)
2954     {
2955 #if HAVE_MBRTOWC
2956       if (MB_CUR_MAX > 1)
2957         {
2958           char const *p = buf;
2959           char const *plimit = buf + len;
2960           char *q = buf;
2961           displayed_width = 0;
2962
2963           while (p < plimit)
2964             switch (*p)
2965               {
2966                 case ' ': case '!': case '"': case '#': case '%':
2967                 case '&': case '\'': case '(': case ')': case '*':
2968                 case '+': case ',': case '-': case '.': case '/':
2969                 case '0': case '1': case '2': case '3': case '4':
2970                 case '5': case '6': case '7': case '8': case '9':
2971                 case ':': case ';': case '<': case '=': case '>':
2972                 case '?':
2973                 case 'A': case 'B': case 'C': case 'D': case 'E':
2974                 case 'F': case 'G': case 'H': case 'I': case 'J':
2975                 case 'K': case 'L': case 'M': case 'N': case 'O':
2976                 case 'P': case 'Q': case 'R': case 'S': case 'T':
2977                 case 'U': case 'V': case 'W': case 'X': case 'Y':
2978                 case 'Z':
2979                 case '[': case '\\': case ']': case '^': case '_':
2980                 case 'a': case 'b': case 'c': case 'd': case 'e':
2981                 case 'f': case 'g': case 'h': case 'i': case 'j':
2982                 case 'k': case 'l': case 'm': case 'n': case 'o':
2983                 case 'p': case 'q': case 'r': case 's': case 't':
2984                 case 'u': case 'v': case 'w': case 'x': case 'y':
2985                 case 'z': case '{': case '|': case '}': case '~':
2986                   /* These characters are printable ASCII characters.  */
2987                   *q++ = *p++;
2988                   displayed_width += 1;
2989                   break;
2990                 default:
2991                   /* If we have a multibyte sequence, copy it until we
2992                      reach its end, replacing each non-printable multibyte
2993                      character with a single question mark.  */
2994                   {
2995                     mbstate_t mbstate;
2996                     memset (&mbstate, 0, sizeof mbstate);
2997                     do
2998                       {
2999                         wchar_t wc;
3000                         size_t bytes;
3001                         int w;
3002
3003                         bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
3004
3005                         if (bytes == (size_t) -1)
3006                           {
3007                             /* An invalid multibyte sequence was
3008                                encountered.  Skip one input byte, and
3009                                put a question mark.  */
3010                             p++;
3011                             *q++ = '?';
3012                             displayed_width += 1;
3013                             break;
3014                           }
3015
3016                         if (bytes == (size_t) -2)
3017                           {
3018                             /* An incomplete multibyte character
3019                                at the end.  Replace it entirely with
3020                                a question mark.  */
3021                             p = plimit;
3022                             *q++ = '?';
3023                             displayed_width += 1;
3024                             break;
3025                           }
3026
3027                         if (bytes == 0)
3028                           /* A null wide character was encountered.  */
3029                           bytes = 1;
3030
3031                         w = wcwidth (wc);
3032                         if (w >= 0)
3033                           {
3034                             /* A printable multibyte character.
3035                                Keep it.  */
3036                             for (; bytes > 0; --bytes)
3037                               *q++ = *p++;
3038                             displayed_width += w;
3039                           }
3040                         else
3041                           {
3042                             /* An unprintable multibyte character.
3043                                Replace it entirely with a question
3044                                mark.  */
3045                             p += bytes;
3046                             *q++ = '?';
3047                             displayed_width += 1;
3048                           }
3049                       }
3050                     while (! mbsinit (&mbstate));
3051                   }
3052                   break;
3053               }
3054
3055           /* The buffer may have shrunk.  */
3056           len = q - buf;
3057         }
3058       else
3059 #endif
3060         {
3061           char *p = buf;
3062           char const *plimit = buf + len;
3063
3064           while (p < plimit)
3065             {
3066               if (! ISPRINT ((unsigned char) *p))
3067                 *p = '?';
3068               p++;
3069             }
3070           displayed_width = len;
3071         }
3072     }
3073   else
3074     {
3075       /* Assume unprintable characters have a displayed_width of 1.  */
3076 #if HAVE_MBRTOWC
3077       if (MB_CUR_MAX > 1)
3078         displayed_width = mbsnwidth (buf, len, 0);
3079       else
3080 #endif
3081         displayed_width = len;
3082     }
3083
3084   if (out != NULL)
3085     fwrite (buf, 1, len, out);
3086   return displayed_width;
3087 }
3088
3089 static void
3090 print_name_with_quoting (const char *p, mode_t mode, int linkok,
3091                          struct obstack *stack)
3092 {
3093   if (print_with_color)
3094     print_color_indicator (p, mode, linkok);
3095
3096   if (stack)
3097     PUSH_CURRENT_DIRED_POS (stack);
3098
3099   dired_pos += quote_name (stdout, p, filename_quoting_options);
3100
3101   if (stack)
3102     PUSH_CURRENT_DIRED_POS (stack);
3103
3104   if (print_with_color)
3105     prep_non_filename_text ();
3106 }
3107
3108 static void
3109 prep_non_filename_text (void)
3110 {
3111   if (color_indicator[C_END].string != NULL)
3112     put_indicator (&color_indicator[C_END]);
3113   else
3114     {
3115       put_indicator (&color_indicator[C_LEFT]);
3116       put_indicator (&color_indicator[C_NORM]);
3117       put_indicator (&color_indicator[C_RIGHT]);
3118     }
3119 }
3120
3121 /* Print the file name of `f' with appropriate quoting.
3122    Also print file size, inode number, and filetype indicator character,
3123    as requested by switches.  */
3124
3125 static void
3126 print_file_name_and_frills (const struct fileinfo *f)
3127 {
3128   char buf[LONGEST_HUMAN_READABLE + 1];
3129
3130   if (print_inode)
3131     printf ("%*s ", INODE_DIGITS,
3132             human_readable ((uintmax_t) f->stat.st_ino, buf, 1, 1));
3133
3134   if (print_block_size)
3135     printf ("%*s ", block_size_size,
3136             human_readable_inexact ((uintmax_t) ST_NBLOCKS (f->stat), buf,
3137                                     ST_NBLOCKSIZE, output_block_size,
3138                                     human_ceiling));
3139
3140   print_name_with_quoting (f->name, FILE_OR_LINK_MODE (f), f->linkok, NULL);
3141
3142   if (indicator_style != none)
3143     print_type_indicator (f->stat.st_mode);
3144 }
3145
3146 static void
3147 print_type_indicator (mode_t mode)
3148 {
3149   int c;
3150
3151   if (S_ISREG (mode))
3152     {
3153       if (indicator_style == classify && (mode & S_IXUGO))
3154         c ='*';
3155       else
3156         c = 0;
3157     }
3158   else
3159     {
3160       if (S_ISDIR (mode))
3161         c = '/';
3162       else if (S_ISLNK (mode))
3163         c = '@';
3164       else if (S_ISFIFO (mode))
3165         c = '|';
3166       else if (S_ISSOCK (mode))
3167         c = '=';
3168       else if (S_ISDOOR (mode))
3169         c = '>';
3170       else
3171         c = 0;
3172     }
3173
3174   if (c)
3175     DIRED_PUTCHAR (c);
3176 }
3177
3178 static void
3179 print_color_indicator (const char *name, mode_t mode, int linkok)
3180 {
3181   int type = C_FILE;
3182   struct color_ext_type *ext;   /* Color extension */
3183   size_t len;                   /* Length of name */
3184
3185   /* Is this a nonexistent file?  If so, linkok == -1.  */
3186
3187   if (linkok == -1 && color_indicator[C_MISSING].string != NULL)
3188     {
3189       ext = NULL;
3190       type = C_MISSING;
3191     }
3192   else
3193     {
3194       if (S_ISDIR (mode))
3195         type = C_DIR;
3196       else if (S_ISLNK (mode))
3197         type = ((!linkok && color_indicator[C_ORPHAN].string)
3198                 ? C_ORPHAN : C_LINK);
3199       else if (S_ISFIFO (mode))
3200         type = C_FIFO;
3201       else if (S_ISSOCK (mode))
3202         type = C_SOCK;
3203       else if (S_ISBLK (mode))
3204         type = C_BLK;
3205       else if (S_ISCHR (mode))
3206         type = C_CHR;
3207       else if (S_ISDOOR (mode))
3208         type = C_DOOR;
3209
3210       if (type == C_FILE && (mode & S_IXUGO) != 0)
3211         type = C_EXEC;
3212
3213       /* Check the file's suffix only if still classified as C_FILE.  */
3214       ext = NULL;
3215       if (type == C_FILE)
3216         {
3217           /* Test if NAME has a recognized suffix.  */
3218
3219           len = strlen (name);
3220           name += len;          /* Pointer to final \0.  */
3221           for (ext = color_ext_list; ext != NULL; ext = ext->next)
3222             {
3223               if ((size_t) ext->ext.len <= len
3224                   && strncmp (name - ext->ext.len, ext->ext.string,
3225                               ext->ext.len) == 0)
3226                 break;
3227             }
3228         }
3229     }
3230
3231   put_indicator (&color_indicator[C_LEFT]);
3232   put_indicator (ext ? &(ext->seq) : &color_indicator[type]);
3233   put_indicator (&color_indicator[C_RIGHT]);
3234 }
3235
3236 /* Output a color indicator (which may contain nulls).  */
3237 static void
3238 put_indicator (const struct bin_str *ind)
3239 {
3240   register int i;
3241   register const char *p;
3242
3243   p = ind->string;
3244
3245   for (i = ind->len; i > 0; --i)
3246     putchar (*(p++));
3247 }
3248
3249 static int
3250 length_of_file_name_and_frills (const struct fileinfo *f)
3251 {
3252   register int len = 0;
3253
3254   if (print_inode)
3255     len += INODE_DIGITS + 1;
3256
3257   if (print_block_size)
3258     len += 1 + block_size_size;
3259
3260   len += quote_name (NULL, f->name, filename_quoting_options);
3261
3262   if (indicator_style != none)
3263     {
3264       mode_t filetype = f->stat.st_mode;
3265
3266       if (S_ISREG (filetype))
3267         {
3268           if (indicator_style == classify
3269               && (f->stat.st_mode & S_IXUGO))
3270             len += 1;
3271         }
3272       else if (S_ISDIR (filetype)
3273                || S_ISLNK (filetype)
3274                || S_ISFIFO (filetype)
3275                || S_ISSOCK (filetype)
3276                || S_ISDOOR (filetype)
3277                )
3278         len += 1;
3279     }
3280
3281   return len;
3282 }
3283
3284 static void
3285 print_many_per_line (void)
3286 {
3287   struct column_info *line_fmt;
3288   int filesno;                  /* Index into files. */
3289   int row;                      /* Current row. */
3290   int max_name_length;          /* Length of longest file name + frills. */
3291   int name_length;              /* Length of each file name + frills. */
3292   int pos;                      /* Current character column. */
3293   int cols;                     /* Number of files across. */
3294   int rows;                     /* Maximum number of files down. */
3295   int max_cols;
3296
3297   /* Normally the maximum number of columns is determined by the
3298      screen width.  But if few files are available this might limit it
3299      as well.  */
3300   max_cols = max_idx > files_index ? files_index : max_idx;
3301
3302   /* Compute the maximum number of possible columns.  */
3303   for (filesno = 0; filesno < files_index; ++filesno)
3304     {
3305       int i;
3306
3307       name_length = length_of_file_name_and_frills (files + filesno);
3308
3309       for (i = 0; i < max_cols; ++i)
3310         {
3311           if (column_info[i].valid_len)
3312             {
3313               int idx = filesno / ((files_index + i) / (i + 1));
3314               int real_length = name_length + (idx == i ? 0 : 2);
3315
3316               if (real_length > column_info[i].col_arr[idx])
3317                 {
3318                   column_info[i].line_len += (real_length
3319                                            - column_info[i].col_arr[idx]);
3320                   column_info[i].col_arr[idx] = real_length;
3321                   column_info[i].valid_len = column_info[i].line_len < line_length;
3322                 }
3323             }
3324         }
3325     }
3326
3327   /* Find maximum allowed columns.  */
3328   for (cols = max_cols; cols > 1; --cols)
3329     {
3330       if (column_info[cols - 1].valid_len)
3331         break;
3332     }
3333
3334   line_fmt = &column_info[cols - 1];
3335
3336   /* Calculate the number of rows that will be in each column except possibly
3337      for a short column on the right. */
3338   rows = files_index / cols + (files_index % cols != 0);
3339
3340   for (row = 0; row < rows; row++)
3341     {
3342       int col = 0;
3343       filesno = row;
3344       pos = 0;
3345       /* Print the next row.  */
3346       while (1)
3347         {
3348           print_file_name_and_frills (files + filesno);
3349           name_length = length_of_file_name_and_frills (files + filesno);
3350           max_name_length = line_fmt->col_arr[col++];
3351
3352           filesno += rows;
3353           if (filesno >= files_index)
3354             break;
3355
3356           indent (pos + name_length, pos + max_name_length);
3357           pos += max_name_length;
3358         }
3359       putchar ('\n');
3360     }
3361 }
3362
3363 static void
3364 print_horizontal (void)
3365 {
3366   struct column_info *line_fmt;
3367   int filesno;
3368   int max_name_length;
3369   int name_length;
3370   int cols;
3371   int pos;
3372   int max_cols;
3373
3374   /* Normally the maximum number of columns is determined by the
3375      screen width.  But if few files are available this might limit it
3376      as well.  */
3377   max_cols = max_idx > files_index ? files_index : max_idx;
3378
3379   /* Compute the maximum file name length.  */
3380   max_name_length = 0;
3381   for (filesno = 0; filesno < files_index; ++filesno)
3382     {
3383       int i;
3384
3385       name_length = length_of_file_name_and_frills (files + filesno);
3386
3387       for (i = 0; i < max_cols; ++i)
3388         {
3389           if (column_info[i].valid_len)
3390             {
3391               int idx = filesno % (i + 1);
3392               int real_length = name_length + (idx == i ? 0 : 2);
3393
3394               if (real_length > column_info[i].col_arr[idx])
3395                 {
3396                   column_info[i].line_len += (real_length
3397                                            - column_info[i].col_arr[idx]);
3398                   column_info[i].col_arr[idx] = real_length;
3399                   column_info[i].valid_len = column_info[i].line_len < line_length;
3400                 }
3401             }
3402         }
3403     }
3404
3405   /* Find maximum allowed columns.  */
3406   for (cols = max_cols; cols > 1; --cols)
3407     {
3408       if (column_info[cols - 1].valid_len)
3409         break;
3410     }
3411
3412   line_fmt = &column_info[cols - 1];
3413
3414   pos = 0;
3415
3416   /* Print first entry.  */
3417   print_file_name_and_frills (files);
3418   name_length = length_of_file_name_and_frills (files);
3419   max_name_length = line_fmt->col_arr[0];
3420
3421   /* Now the rest.  */
3422   for (filesno = 1; filesno < files_index; ++filesno)
3423     {
3424       int col = filesno % cols;
3425
3426       if (col == 0)
3427         {
3428           putchar ('\n');
3429           pos = 0;
3430         }
3431       else
3432         {
3433           indent (pos + name_length, pos + max_name_length);
3434           pos += max_name_length;
3435         }
3436
3437       print_file_name_and_frills (files + filesno);
3438
3439       name_length = length_of_file_name_and_frills (files + filesno);
3440       max_name_length = line_fmt->col_arr[col];
3441     }
3442   putchar ('\n');
3443 }
3444
3445 static void
3446 print_with_commas (void)
3447 {
3448   int filesno;
3449   int pos, old_pos;
3450
3451   pos = 0;
3452
3453   for (filesno = 0; filesno < files_index; filesno++)
3454     {
3455       old_pos = pos;
3456
3457       pos += length_of_file_name_and_frills (files + filesno);
3458       if (filesno + 1 < files_index)
3459         pos += 2;               /* For the comma and space */
3460
3461       if (old_pos != 0 && pos >= line_length)
3462         {
3463           putchar ('\n');
3464           pos -= old_pos;
3465         }
3466
3467       print_file_name_and_frills (files + filesno);
3468       if (filesno + 1 < files_index)
3469         {
3470           putchar (',');
3471           putchar (' ');
3472         }
3473     }
3474   putchar ('\n');
3475 }
3476
3477 /* Assuming cursor is at position FROM, indent up to position TO.
3478    Use a TAB character instead of two or more spaces whenever possible.  */
3479
3480 static void
3481 indent (int from, int to)
3482 {
3483   while (from < to)
3484     {
3485       if (tabsize > 0 && to / tabsize > (from + 1) / tabsize)
3486         {
3487           putchar ('\t');
3488           from += tabsize - from % tabsize;
3489         }
3490       else
3491         {
3492           putchar (' ');
3493           from++;
3494         }
3495     }
3496 }
3497
3498 /* Put DIRNAME/NAME into DEST, handling `.' and `/' properly. */
3499 /* FIXME: maybe remove this function someday.  See about using a
3500    non-malloc'ing version of path_concat.  */
3501
3502 static void
3503 attach (char *dest, const char *dirname, const char *name)
3504 {
3505   const char *dirnamep = dirname;
3506
3507   /* Copy dirname if it is not ".". */
3508   if (dirname[0] != '.' || dirname[1] != 0)
3509     {
3510       while (*dirnamep)
3511         *dest++ = *dirnamep++;
3512       /* Add '/' if `dirname' doesn't already end with it. */
3513       if (dirnamep > dirname && dirnamep[-1] != '/')
3514         *dest++ = '/';
3515     }
3516   while (*name)
3517     *dest++ = *name++;
3518   *dest = 0;
3519 }
3520
3521 static void
3522 init_column_info (void)
3523 {
3524   int i;
3525   int allocate = 0;
3526
3527   max_idx = line_length / MIN_COLUMN_WIDTH;
3528   if (max_idx == 0)
3529     max_idx = 1;
3530
3531   if (column_info == NULL)
3532     {
3533       column_info = XMALLOC (struct column_info, max_idx);
3534       allocate = 1;
3535     }
3536
3537   for (i = 0; i < max_idx; ++i)
3538     {
3539       int j;
3540
3541       column_info[i].valid_len = 1;
3542       column_info[i].line_len = (i + 1) * MIN_COLUMN_WIDTH;
3543
3544       if (allocate)
3545         column_info[i].col_arr = XMALLOC (int, i + 1);
3546
3547       for (j = 0; j <= i; ++j)
3548         column_info[i].col_arr[j] = MIN_COLUMN_WIDTH;
3549     }
3550 }
3551
3552 void
3553 usage (int status)
3554 {
3555   if (status != 0)
3556     fprintf (stderr, _("Try `%s --help' for more information.\n"),
3557              program_name);
3558   else
3559     {
3560       printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
3561       fputs (_("\
3562 List information about the FILEs (the current directory by default).\n\
3563 Sort entries alphabetically if none of -cftuSUX nor --sort.\n\
3564 \n\
3565 "), stdout);
3566       fputs (_("\
3567 Mandatory arguments to long options are mandatory for short options too.\n\
3568 "), stdout);
3569       fputs (_("\
3570   -a, --all                  do not hide entries starting with .\n\
3571   -A, --almost-all           do not list implied . and ..\n\
3572   -b, --escape               print octal escapes for nongraphic characters\n\
3573 "), stdout);
3574       fputs (_("\
3575       --block-size=SIZE      use SIZE-byte blocks\n\
3576   -B, --ignore-backups       do not list implied entries ending with ~\n\
3577   -c                         with -lt: sort by, and show, ctime (time of last\n\
3578                                modification of file status information)\n\
3579                                with -l: show ctime and sort by name\n\
3580                                otherwise: sort by ctime\n\
3581 "), stdout);
3582       fputs (_("\
3583   -C                         list entries by columns\n\
3584       --color[=WHEN]         control whether color is used to distinguish file\n\
3585                                types.  WHEN may be `never', `always', or `auto'\n\
3586   -d, --directory            list directory entries instead of contents\n\
3587   -D, --dired                generate output designed for Emacs' dired mode\n\
3588 "), stdout);
3589       fputs (_("\
3590   -f                         do not sort, enable -aU, disable -lst\n\
3591   -F, --classify             append indicator (one of */=@|) to entries\n\
3592       --format=WORD          across -x, commas -m, horizontal -x, long -l,\n\
3593                                single-column -1, verbose -l, vertical -C\n\
3594       --full-time            like -l --time-style=full-iso\n\
3595 "), stdout);
3596       fputs (_("\
3597   -g                         like -l, but do not list owner\n\
3598   -G, --no-group             inhibit display of group information\n\
3599   -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)\n\
3600       --si                   likewise, but use powers of 1000 not 1024\n\
3601   -H, --dereference-command-line  follow symbolic links on the command line\n\
3602 "), stdout);
3603       fputs (_("\
3604       --indicator-style=WORD append indicator with style WORD to entry names:\n\
3605                                none (default), classify (-F), file-type (-p)\n\
3606   -i, --inode                print index number of each file\n\
3607   -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN\n\
3608   -k, --kilobytes            like --block-size=1024\n\
3609 "), stdout);
3610       fputs (_("\
3611   -l                         use a long listing format\n\
3612   -L, --dereference          when showing file information for a symbolic\n\
3613                                link, show information for the file the link\n\
3614                                references rather than for the link itself\n\
3615   -m                         fill width with a comma separated list of entries\n\
3616 "), stdout);
3617       fputs (_("\
3618   -n, --numeric-uid-gid      like -l, but list numeric UIDs and GIDs\n\
3619   -N, --literal              print raw entry names (don't treat e.g. control\n\
3620                                characters specially)\n\
3621   -o                         like -l, but do not list group information\n\
3622   -p, --file-type            append indicator (one of /=@|) to entries\n\
3623 "), stdout);
3624       fputs (_("\
3625   -q, --hide-control-chars   print ? instead of non graphic characters\n\
3626       --show-control-chars   show non graphic characters as-is (default\n\
3627                              unless program is `ls' and output is a terminal)\n\
3628   -Q, --quote-name           enclose entry names in double quotes\n\
3629       --quoting-style=WORD   use quoting style WORD for entry names:\n\
3630                                literal, locale, shell, shell-always, c, escape\n\
3631 "), stdout);
3632       fputs (_("\
3633   -r, --reverse              reverse order while sorting\n\
3634   -R, --recursive            list subdirectories recursively\n\
3635   -s, --size                 print size of each file, in blocks\n\
3636 "), stdout);
3637       fputs (_("\
3638   -S                         sort by file size\n\
3639       --sort=WORD            extension -X, none -U, size -S, time -t,\n\
3640                                version -v\n\
3641                              status -c, time -t, atime -u, access -u, use -u\n\
3642       --time=WORD            show time as WORD instead of modification time:\n\
3643                                atime, access, use, ctime or status; use\n\
3644                                specified time as sort key if --sort=time\n\
3645 "), stdout);
3646       fputs (_("\
3647       --time-style=WORD      show times using style WORD:\n\
3648                                full-iso, iso, locale, posix-iso\n\
3649   -t                         sort by modification time\n\
3650   -T, --tabsize=COLS         assume tab stops at each COLS instead of 8\n\
3651 "), stdout);
3652       fputs (_("\
3653   -u                         with -lt: sort by, and show, access time\n\
3654                                with -l: show access time and sort by name\n\
3655                                otherwise: sort by access time\n\
3656   -U                         do not sort; list entries in directory order\n\
3657   -v                         sort by version\n\
3658 "), stdout);
3659       fputs (_("\
3660   -w, --width=COLS           assume screen width instead of current value\n\
3661   -x                         list entries by lines instead of by columns\n\
3662   -X                         sort alphabetically by entry extension\n\
3663   -1                         list one file per line\n\
3664 "), stdout);
3665       fputs (_("\
3666       --help                 display this help and exit\n\
3667       --version              output version information and exit\n\
3668 "), stdout);
3669       fputs (_("\
3670 \n\
3671 By default, color is not used to distinguish types of files.  That is\n\
3672 equivalent to using --color=none.  Using the --color option without the\n\
3673 optional WHEN argument is equivalent to using --color=always.  With\n\
3674 --color=auto, color codes are output only if standard output is connected\n\
3675 to a terminal (tty).\n\
3676 "), stdout);
3677       puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
3678     }
3679   exit (status);
3680 }