1 /* `dir', `vdir' and `ls' directory listing programs for GNU.
2 Copyright (C) 1985, 1988, 1990, 1991, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* If the macro MULTI_COL is defined,
19 the multi-column format is the default regardless
20 of the type of output device.
21 This is for the `dir' program.
23 If the macro LONG_FORMAT is defined,
24 the long format is the default regardless of the
25 type of output device.
26 This is for the `vdir' program.
28 If neither is defined,
29 the output format depends on whether the output
31 This is for the `ls' program. */
33 /* Written by Richard Stallman and David MacKenzie. */
40 #include <sys/types.h>
41 #if !defined(_POSIX_SOURCE) || defined(_AIX)
42 #include <sys/ioctl.h>
49 /* limits.h must come before system.h because limits.h on some systems
50 undefs PATH_MAX, whereas system.h includes pathmax.h which sets
63 #define obstack_chunk_alloc xmalloc
64 #define obstack_chunk_free free
67 #define INT_MAX 2147483647
70 /* Return an int indicating the result of comparing two longs. */
71 #if (INT_MAX <= 65535)
72 #define longdiff(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
74 #define longdiff(a, b) ((a) - (b))
77 /* The maximum number of digits required to print an inode number
78 in an unsigned format. */
80 #define INODE_DIGITS 7
100 static char *make_link_path ();
101 static int compare_atime ();
102 static int rev_cmp_atime ();
103 static int compare_ctime ();
104 static int rev_cmp_ctime ();
105 static int compare_mtime ();
106 static int rev_cmp_mtime ();
107 static int compare_size ();
108 static int rev_cmp_size ();
109 static int compare_name ();
110 static int rev_cmp_name ();
111 static int compare_extension ();
112 static int rev_cmp_extension ();
113 static int decode_switches ();
114 static void parse_ls_color ();
115 static int file_interesting ();
116 static int gobble_file ();
117 static int is_not_dot_or_dotdot ();
118 static int length_of_file_name_and_frills ();
119 static void add_ignore_pattern ();
120 static void attach ();
121 static void clear_files ();
122 static void extract_dirs_from_files ();
123 static void get_link_name ();
124 static void indent ();
125 static void print_current_files ();
126 static void print_dir ();
127 static void print_file_name_and_frills ();
128 static void print_horizontal ();
129 static void print_long_format ();
130 static void print_many_per_line ();
131 static void print_name_with_quoting ();
132 static void print_type_indicator ();
133 static void print_color_indicator ();
134 static void put_indicator ();
135 static void print_with_commas ();
136 static void queue_directory ();
137 static void sort_files ();
138 static void usage ();
140 /* The name the program was run with, stripped of any leading path. */
147 arg_directory
, /* Directory given as command line arg. */
148 normal
/* All others. */
158 /* For symbolic link, name of the file linked to, otherwise zero. */
161 /* For symbolic link and long listing, st_mode of file linked to, otherwise
163 unsigned int linkmode
;
165 enum filetype filetype
;
168 /* The table of files in the current directory:
170 `files' points to a vector of `struct fileinfo', one per file.
171 `nfiles' is the number of elements space has been allocated for.
172 `files_index' is the number actually in use. */
174 /* Address of block containing the files that are described. */
176 static struct fileinfo
*files
;
178 /* Length of block that `files' points to, measured in files. */
182 /* Index of first unused in `files'. */
184 static int files_index
;
186 /* Record of one pending directory waiting to be listed. */
191 /* If the directory is actually the file pointed to by a symbolic link we
192 were told to list, `realname' will contain the name of the symbolic
193 link, otherwise zero. */
195 struct pending
*next
;
198 static struct pending
*pending_dirs
;
200 /* Current time (seconds since 1970). When we are printing a file's time,
201 include the year if it is more than 6 months before this time. */
203 static time_t current_time
;
205 /* The number of digits to use for block sizes.
206 4, or more if needed for bigger numbers. */
208 static int block_size_size
;
212 /* long_format for lots of info, one per line.
213 one_per_line for just names, one per line.
214 many_per_line for just names, many per line, sorted vertically.
215 horizontal for just names, many per line, sorted horizontally.
216 with_commas for just names, many per line, separated by commas.
218 -l, -1, -C, -x and -m control this parameter. */
222 long_format
, /* -l */
223 one_per_line
, /* -1 */
224 many_per_line
, /* -C */
229 static enum format format
;
231 /* Type of time to print or sort by. Controlled by -c and -u. */
235 time_mtime
, /* default */
240 static enum time_type time_type
;
242 /* print the full time, otherwise the standard unix heuristics. */
246 /* The file characteristic to sort by. Controlled by -t, -S, -U, -X. */
251 sort_name
, /* default */
252 sort_extension
, /* -X */
257 static enum sort_type sort_type
;
259 /* Direction of sort.
260 0 means highest first if numeric,
261 lowest first if alphabetic;
262 these are the defaults.
263 1 means the opposite order in each case. -r */
265 static int sort_reverse
;
267 /* Nonzero means to NOT display group information. -G */
271 /* Nonzero means print the user and group id's as numbers rather
274 static int numeric_users
;
276 /* Nonzero means mention the size in 512 byte blocks of each file. -s */
278 static int print_block_size
;
280 /* Nonzero means show file sizes in kilobytes instead of blocks
281 (the size of which is system-dependent). -k */
283 static int kilobyte_blocks
;
285 /* Precede each line of long output (per file) with a string like `m,n:'
286 where M is the number of characters after the `:' and before the
287 filename and N is the length of the filename. Using this format,
288 Emacs' dired mode starts up twice as fast, and can handle all
289 strange characters in file names. */
292 /* none means don't mention the type of files.
293 all means mention the types of all files.
294 not_programs means do so except for executables.
296 Controlled by -F and -p. */
302 not_programs
/* -p */
305 static enum indicator_style indicator_style
;
307 /* Nonzero means use colors to mark types. Also define the different
308 colors as well as the stuff for the LS_COLORS environment variable.
309 The LS_COLORS variable is now in a termcap-like format. -o or
312 static int print_with_color
;
316 color_no
, /* 0: default or --color=no */
317 color_yes
, /* 1: -o or --color=yes */
318 color_if_tty
/* 2: --color=tty */
321 /* Note that color_no and color_yes equals boolean values; they will
322 be assigned to print_with_color which is a boolean variable */
324 #define MAXCOLORLEN 16 /* Max # of characters in a color sequence */
328 C_LEFT
, C_RIGHT
, C_END
, C_FILE
, C_DIR
, C_LINK
, C_FIFO
, C_SOCK
,
332 const char *const indicator_name
[] =
334 "lc", "rc", "ec", "fi", "di", "ln", "pi", "so", "bd", "cd", "ex", NULL
337 struct indicator_type
340 char string
[MAXCOLORLEN
];
343 #define LEN_STR_PAIR(s) sizeof (s) - 1, s
345 static struct indicator_type color_indicator
[] =
347 {LEN_STR_PAIR ("\033[")}, /* lc: Left of color sequence */
348 {LEN_STR_PAIR ("m")}, /* rc: Right of color sequence */
349 {LEN_STR_PAIR ("\033[0m")}, /* ec: End color */
350 {LEN_STR_PAIR ("0")}, /* fi: File: default */
351 {LEN_STR_PAIR ("32")}, /* di: Directory: green */
352 {LEN_STR_PAIR ("36")}, /* ln: Symlink: cyan */
353 {LEN_STR_PAIR ("31")}, /* pi: Pipe: red */
354 {LEN_STR_PAIR ("33")}, /* so: Socket: yellow/brown */
355 {LEN_STR_PAIR ("44;37")}, /* bd: Block device: white on blue */
356 {LEN_STR_PAIR ("44;37")}, /* cd: Char device: white on blue */
357 {LEN_STR_PAIR ("35")}, /* ex: Executable: purple */
360 /* Nonzero means print using ISO 8859 characters. The default is specified
361 here as well. -8 enables, -7 disables. */
363 static int print_iso8859
;
364 #ifndef DEFAULT_ISO8859
365 #define DEFAULT_ISO8859 1
368 /* Nonzero means mention the inode number of each file. -i */
370 static int print_inode
;
372 /* Nonzero means when a symbolic link is found, display info on
373 the file linked to. -L */
375 static int trace_links
;
377 /* Nonzero means when a directory is found, display info on its
380 static int trace_dirs
;
382 /* Nonzero means when an argument is a directory name, display info
385 static int immediate_dirs
;
387 /* Nonzero means don't omit files whose names start with `.'. -A */
389 static int all_files
;
391 /* Nonzero means don't omit files `.' and `..'
392 This flag implies `all_files'. -a */
394 static int really_all_files
;
396 /* A linked list of shell-style globbing patterns. If a non-argument
397 file name matches any of these patterns, it is omitted.
398 Controlled by -I. Multiple -I options accumulate.
399 The -B option adds `*~' and `.*~' to this list. */
401 struct ignore_pattern
404 struct ignore_pattern
*next
;
407 static struct ignore_pattern
*ignore_patterns
;
409 /* Nonzero means quote nongraphic chars in file names. -b */
411 static int quote_funny_chars
;
413 /* Nonzero means output nongraphic chars in file names as `?'. -q */
415 static int qmark_funny_chars
;
417 /* Nonzero means output each file name using C syntax for a string.
418 Always accompanied by `quote_funny_chars'.
419 This mode, together with -x or -C or -m,
420 and without such frills as -F or -s,
421 is guaranteed to make it possible for a program receiving
422 the output to tell exactly what file names are present. -Q */
424 static int quote_as_string
;
426 /* The number of chars per hardware tab stop. -T */
429 /* Nonzero means we are listing the working directory because no
430 non-option arguments were given. */
432 static int dir_defaulted
;
434 /* Nonzero means print each directory name before listing it. */
436 static int print_dir_name
;
438 /* The line length to use for breaking lines in many-per-line format.
439 Can be set with -w. */
441 static int line_length
;
443 /* If nonzero, the file listing format requires that stat be called on
446 static int format_needs_stat
;
448 /* The exit status to use if we don't get any fatal errors. */
450 static int exit_status
;
452 /* If non-zero, display usage information and exit. */
453 static int show_help
;
455 /* If non-zero, print the version on standard output and exit. */
456 static int show_version
;
458 static struct option
const long_options
[] =
460 {"all", no_argument
, 0, 'a'},
461 {"escape", no_argument
, 0, 'b'},
462 {"directory", no_argument
, 0, 'd'},
463 {"dired", no_argument
, 0, 'D'},
464 {"full-time", no_argument
, &full_time
, 1},
465 {"inode", no_argument
, 0, 'i'},
466 {"kilobytes", no_argument
, 0, 'k'},
467 {"numeric-uid-gid", no_argument
, 0, 'n'},
468 {"no-group", no_argument
, 0, 'G'},
469 {"hide-control-chars", no_argument
, 0, 'q'},
470 {"reverse", no_argument
, 0, 'r'},
471 {"size", no_argument
, 0, 's'},
472 {"width", required_argument
, 0, 'w'},
473 {"almost-all", no_argument
, 0, 'A'},
474 {"ignore-backups", no_argument
, 0, 'B'},
475 {"classify", no_argument
, 0, 'F'},
476 {"file-type", no_argument
, 0, 'F'},
477 {"ignore", required_argument
, 0, 'I'},
478 {"dereference", no_argument
, 0, 'L'},
479 {"literal", no_argument
, 0, 'N'},
480 {"quote-name", no_argument
, 0, 'Q'},
481 {"recursive", no_argument
, 0, 'R'},
482 {"format", required_argument
, 0, 12},
483 {"sort", required_argument
, 0, 10},
484 {"tabsize", required_argument
, 0, 'T'},
485 {"time", required_argument
, 0, 11},
486 {"help", no_argument
, &show_help
, 1},
487 {"version", no_argument
, &show_version
, 1},
488 {"color", optional_argument
, 0, 'o'},
489 {"colour", optional_argument
, 0, 'o'},
490 {"7bit", no_argument
, 0, '7'},
491 {"8bit", no_argument
, 0, '8'},
495 static char const *const format_args
[] =
497 "verbose", "long", "commas", "horizontal", "across",
498 "vertical", "single-column", 0
501 static enum format
const formats
[] =
503 long_format
, long_format
, with_commas
, horizontal
, horizontal
,
504 many_per_line
, one_per_line
507 static char const *const sort_args
[] =
509 "none", "time", "size", "extension", 0
512 static enum sort_type
const sort_types
[] =
514 sort_none
, sort_time
, sort_size
, sort_extension
517 static char const *const time_args
[] =
519 "atime", "access", "use", "ctime", "status", 0
522 /* This zero-based index is used solely with the --dired option.
523 When that option is in effect, this counter is incremented for each
524 character of output generated by this program so that the beginning
525 and ending indices (in that output) of every file name can be recorded
526 and later output themselves. */
527 static size_t dired_pos
;
529 #define PUTCHAR(c) do {putchar ((c)); ++dired_pos;} while (0)
531 /* Write S to STREAM and increment DIRED_POS by S_LEN. */
532 #define FPUTS(s, stream, s_len) \
533 do {fputs ((s), (stream)); dired_pos += s_len;} while (0)
535 /* Like FPUTS, but for use when S is a literal string. */
536 #define FPUTS_LITERAL(s, stream) \
537 do {fputs ((s), (stream)); dired_pos += sizeof((s)) - 1;} while (0)
539 #define DIRED_INDENT() \
542 /* FIXME: remove the `&& format == long_format' clause. */ \
543 if (dired && format == long_format) \
544 FPUTS_LITERAL (" ", stdout); \
548 /* With --dired, store pairs of beginning and ending indices of filenames. */
549 static struct obstack dired_obstack
;
551 /* With --dired and --recursive, store pairs of beginning and ending
552 indices of directory names. */
553 static struct obstack subdired_obstack
;
555 /* Save the current index on the specified obstack, OBS. */
556 #define PUSH_CURRENT_DIRED_POS(obs) \
559 /* FIXME: remove the `&& format == long_format' clause. */ \
560 if (dired && format == long_format) \
561 obstack_grow ((obs), &dired_pos, sizeof (dired_pos)); \
565 static enum time_type
const time_types
[] =
567 time_atime
, time_atime
, time_atime
, time_ctime
, time_ctime
570 static char const *const color_args
[] =
572 /* Note: "no" is a prefix of "none" so we don't include it */
573 "yes", "force", "none", "tty", "if-tty"
576 static enum color_type
const color_types
[] =
578 color_yes
, color_yes
, color_no
, color_if_tty
, color_if_tty
581 /* Write to standard output the string PREFIX followed by a space-separated
582 list of the integers stored in OS all on one line. */
585 dired_dump_obstack (prefix
, os
)
592 fputs (prefix
, stdout
);
593 n_pos
= obstack_object_size (os
) / sizeof (size_t);
594 pos
= (size_t *) obstack_finish (os
);
595 for (i
= 0; i
< n_pos
; i
++)
596 printf (" %d", (int) pos
[i
]);
597 fputs ("\n", stdout
);
606 register struct pending
*thispend
;
612 current_time
= time ((time_t *) 0);
614 program_name
= argv
[0];
615 i
= decode_switches (argc
, argv
);
619 printf ("ls - %s\n", version_string
);
626 format_needs_stat
= sort_type
== sort_time
|| sort_type
== sort_size
627 || format
== long_format
628 || trace_links
|| trace_dirs
|| indicator_style
!= none
629 || print_block_size
|| print_inode
|| print_with_color
;
631 if (dired
&& format
== long_format
)
633 obstack_init (&dired_obstack
);
635 obstack_init (&subdired_obstack
);
639 files
= (struct fileinfo
*) xmalloc (sizeof (struct fileinfo
) * nfiles
);
646 for (; i
< argc
; i
++)
647 gobble_file (argv
[i
], 1, "");
652 gobble_file (".", 1, "");
654 queue_directory (".", 0);
661 extract_dirs_from_files ("", 0);
662 /* `files_index' might be zero now. */
666 print_current_files ();
670 else if (pending_dirs
&& pending_dirs
->next
== 0)
675 thispend
= pending_dirs
;
676 pending_dirs
= pending_dirs
->next
;
677 print_dir (thispend
->name
, thispend
->realname
);
678 free (thispend
->name
);
679 if (thispend
->realname
)
680 free (thispend
->realname
);
685 if (dired
&& format
== long_format
)
687 /* No need to free these since we're about to exit. */
688 dired_dump_obstack ("//DIRED//", &dired_obstack
);
690 dired_dump_obstack ("//SUBDIRED//", &subdired_obstack
);
696 /* Set all the option flags according to the switches specified.
697 Return the index of the first non-option argument. */
700 decode_switches (argc
, argv
)
708 qmark_funny_chars
= 0;
709 quote_funny_chars
= 0;
711 /* initialize all switches to default settings */
716 /* This is for the `dir' program. */
717 format
= many_per_line
;
718 quote_funny_chars
= 1;
722 /* This is for the `vdir' program. */
723 format
= long_format
;
724 quote_funny_chars
= 1;
728 /* This is for the `ls' program. */
731 format
= many_per_line
;
732 qmark_funny_chars
= 1;
736 format
= one_per_line
;
737 qmark_funny_chars
= 0;
745 time_type
= time_mtime
;
747 sort_type
= sort_name
;
750 print_block_size
= 0;
751 kilobyte_blocks
= getenv ("POSIXLY_CORRECT") == 0;
752 indicator_style
= none
;
758 really_all_files
= 0;
761 print_with_color
= 0;
762 print_iso8859
= DEFAULT_ISO8859
;
764 p
= getenv ("COLUMNS");
765 line_length
= p
? atoi (p
) : 80;
771 if (ioctl (1, TIOCGWINSZ
, &ws
) != -1 && ws
.ws_col
!= 0)
772 line_length
= ws
.ws_col
;
776 /* FIXME: reference TABSIZE iff !posix_pedantic. */
777 p
= getenv ("TABSIZE");
778 /* FIXME: use strtol here! */
779 tabsize
= p
? atoi (p
) : 8;
783 "ignoring invalid tab size in enironment variable TABSIZE: %s",
788 while ((c
= getopt_long (argc
, argv
,
789 "abcdfgiklmnopqrstuw:xABCDFGI:LNQRST:UX178",
790 long_options
, (int *) 0)) != EOF
)
799 really_all_files
= 1;
803 quote_funny_chars
= 1;
804 qmark_funny_chars
= 0;
808 time_type
= time_ctime
;
816 /* Same as enabling -a -U and disabling -l -s. */
818 really_all_files
= 1;
819 sort_type
= sort_none
;
821 if (format
== long_format
)
822 format
= (isatty (1) ? many_per_line
: one_per_line
);
823 print_block_size
= 0; /* disable -s */
827 /* No effect. For BSD compatibility. */
839 format
= long_format
;
843 format
= with_commas
;
853 i
= argmatch (optarg
, color_args
);
856 invalid_arg ("colorization criterion", optarg
, i
);
862 i
= color_yes
; /* -o or --color -> do colorize */
864 if (i
== color_if_tty
)
865 print_with_color
= isatty (1) ? 1 : 0;
867 print_with_color
= i
;
869 if (print_with_color
)
871 /* Don't use TAB characters in output. Some terminal
872 emulators can't handle the combination of tabs and
873 color codes on the same line. */
874 tabsize
= line_length
;
879 indicator_style
= not_programs
;
883 qmark_funny_chars
= 1;
884 quote_funny_chars
= 0;
892 print_block_size
= 1;
896 sort_type
= sort_time
;
900 time_type
= time_atime
;
904 line_length
= atoi (optarg
);
906 error (1, 0, "invalid line width: %s", optarg
);
918 add_ignore_pattern ("*~");
919 add_ignore_pattern (".*~");
923 format
= many_per_line
;
931 indicator_style
= all
;
934 case 'G': /* inhibit display of group info */
939 add_ignore_pattern (optarg
);
947 quote_funny_chars
= 0;
948 qmark_funny_chars
= 0;
953 quote_funny_chars
= 1;
954 qmark_funny_chars
= 0;
962 sort_type
= sort_size
;
966 tabsize
= atoi (optarg
);
968 error (1, 0, "invalid tab size: %s", optarg
);
972 sort_type
= sort_none
;
976 sort_type
= sort_extension
;
980 format
= one_per_line
;
991 case 10: /* --sort */
992 i
= argmatch (optarg
, sort_args
);
995 invalid_arg ("sort type", optarg
, i
);
998 sort_type
= sort_types
[i
];
1001 case 11: /* --time */
1002 i
= argmatch (optarg
, time_args
);
1005 invalid_arg ("time type", optarg
, i
);
1008 time_type
= time_types
[i
];
1011 case 12: /* --format */
1012 i
= argmatch (optarg
, format_args
);
1015 invalid_arg ("format type", optarg
, i
);
1018 format
= formats
[i
];
1026 if (print_with_color
)
1034 /* Parse the LS_COLORS/LS_COLOURS variable */
1039 register char *p
; /* Pointer to character being parsed */
1040 char *whichvar
; /* LS_COLORS or LS_COLOURS? */
1041 int state
; /* State of parser */
1042 int ind_no
; /* Indicator number */
1043 int ccount
; /* Character count */
1044 int num
; /* Escape char numeral */
1045 char label
[3] = "??"; /* Indicator label */
1047 if ((p
= getenv (whichvar
= "LS_COLORS")) ||
1048 (p
= getenv (whichvar
= "LS_COLOURS")))
1055 case 1: /* First label character */
1062 state
= 0; /* Done */
1065 case 2: /* Second label character */
1072 state
= -1; /* Error */
1075 case 3: /* Should be equal sign */
1077 state
= -1; /* Error state */
1081 state
= -1; /* In case we fail */
1082 while (indicator_name
[ind_no
] != NULL
)
1084 if (strcmp (label
, indicator_name
[ind_no
]) == 0)
1086 state
= 4; /* We found it */
1087 ccount
= 0; /* Nothing stored yet */
1096 case 4: /* Character to store */
1100 color_indicator
[ind_no
].len
= ccount
;
1104 /* The escape sequence will always generate a character,
1105 so enter error state if the buffer is full */
1106 state
= (ccount
>= MAXCOLORLEN
) ? -1 : 5;
1109 /* Control character in the ^X notation */
1110 state
= (ccount
>= MAXCOLORLEN
) ? -1 : 8;
1113 color_indicator
[ind_no
].len
= ccount
;
1114 state
= 0; /* Done */
1117 if (ccount
>= MAXCOLORLEN
)
1118 state
= -1; /* Too long */
1120 color_indicator
[ind_no
].string
[ccount
++] = *p
;
1125 case 5: /* Escape character */
1137 state
= 6; /* Octal numeral */
1142 state
= 7; /* Hex numeral */
1145 case 'a': /* Bell */
1148 case 'b': /* Backspace */
1151 case 'e': /* Escape */
1154 case 'f': /* Formfeed */
1157 case 'n': /* Newline */
1160 case 'r': /* Return */
1166 case 'v': /* Vtab */
1169 case '?': /* Delete */
1172 case '\0': /* End of string */
1173 state
= -1; /* Error */
1175 default: /* Escaped character */
1181 color_indicator
[ind_no
].string
[ccount
++] = num
;
1187 case 6: /* Octal numeral */
1192 color_indicator
[ind_no
].string
[ccount
++] = num
;
1193 color_indicator
[ind_no
].len
= ccount
;
1194 state
= (*(p
++) == ':') ? 1 : 0;
1205 num
= (num
<< 3) + (*(p
++) - '0');
1208 color_indicator
[ind_no
].string
[ccount
++] = num
;
1214 case 7: /* Hex numeral */
1219 color_indicator
[ind_no
].string
[ccount
++] = num
;
1220 color_indicator
[ind_no
].len
= ccount
;
1221 state
= (*(p
++) == ':') ? 1 : 0;
1233 num
= (num
<< 4) + (*(p
++) - '0');
1241 num
= (num
<< 4) + (*(p
++) - 'A' + 10);
1249 num
= (num
<< 4) + (*(p
++) - 'a' + 10);
1252 color_indicator
[ind_no
].string
[ccount
++] = num
;
1258 case 8: /* ^ notation */
1259 state
= 4; /* Usually the next state */
1260 if (*p
>= '@' && *p
<= '~')
1261 color_indicator
[ind_no
].string
[ccount
++] = *p
& 037;
1263 color_indicator
[ind_no
].string
[ccount
++] = 127;
1265 state
= -1; /* Error */
1273 error (0, 0, "bad %s variable\n", whichvar
);
1274 print_with_color
= 0;
1279 /* Request that the directory named `name' have its contents listed later.
1280 If `realname' is nonzero, it will be used instead of `name' when the
1281 directory name is printed. This allows symbolic links to directories
1282 to be treated as regular directories but still be listed under their
1286 queue_directory (name
, realname
)
1290 struct pending
*new;
1292 new = (struct pending
*) xmalloc (sizeof (struct pending
));
1293 new->next
= pending_dirs
;
1295 new->name
= xstrdup (name
);
1297 new->realname
= xstrdup (realname
);
1302 /* Read directory `name', and list the files in it.
1303 If `realname' is nonzero, print its name instead of `name';
1304 this is used for symbolic links to directories. */
1307 print_dir (name
, realname
)
1309 const char *realname
;
1311 register DIR *reading
;
1312 register struct dirent
*next
;
1313 register int total_blocks
= 0;
1316 reading
= opendir (name
);
1319 error (0, errno
, "%s", name
);
1324 /* Read the directory entries, and insert the subfiles into the `files'
1329 while ((next
= readdir (reading
)) != NULL
)
1330 if (file_interesting (next
))
1331 total_blocks
+= gobble_file (next
->d_name
, 0, name
);
1333 if (CLOSEDIR (reading
))
1335 error (0, errno
, "%s", name
);
1337 /* Don't return; print whatever we got. */
1340 /* Sort the directory contents. */
1343 /* If any member files are subdirectories, perhaps they should have their
1344 contents listed rather than being mentioned here as files. */
1347 extract_dirs_from_files (name
, 1);
1354 dir
= (realname
? realname
: name
);
1355 PUSH_CURRENT_DIRED_POS (&subdired_obstack
);
1356 FPUTS (dir
, stdout
, strlen (dir
));
1357 PUSH_CURRENT_DIRED_POS (&subdired_obstack
);
1358 FPUTS_LITERAL (":\n", stdout
);
1361 if (format
== long_format
|| print_block_size
)
1363 char buf
[6 + 20 + 1 + 1];
1366 sprintf (buf
, "total %u\n", total_blocks
);
1367 FPUTS (buf
, stdout
, strlen (buf
));
1371 print_current_files ();
1377 /* Add `pattern' to the list of patterns for which files that match are
1381 add_ignore_pattern (pattern
)
1384 register struct ignore_pattern
*ignore
;
1386 ignore
= (struct ignore_pattern
*) xmalloc (sizeof (struct ignore_pattern
));
1387 ignore
->pattern
= pattern
;
1388 /* Add it to the head of the linked list. */
1389 ignore
->next
= ignore_patterns
;
1390 ignore_patterns
= ignore
;
1393 /* Return nonzero if the file in `next' should be listed. */
1396 file_interesting (next
)
1397 register struct dirent
*next
;
1399 register struct ignore_pattern
*ignore
;
1401 for (ignore
= ignore_patterns
; ignore
; ignore
= ignore
->next
)
1402 if (fnmatch (ignore
->pattern
, next
->d_name
, FNM_PERIOD
) == 0)
1405 if (really_all_files
1406 || next
->d_name
[0] != '.'
1408 && next
->d_name
[1] != '\0'
1409 && (next
->d_name
[1] != '.' || next
->d_name
[2] != '\0')))
1415 /* Enter and remove entries in the table `files'. */
1417 /* Empty the table of files. */
1424 for (i
= 0; i
< files_index
; i
++)
1426 free (files
[i
].name
);
1427 if (files
[i
].linkname
)
1428 free (files
[i
].linkname
);
1432 block_size_size
= 4;
1435 /* Add a file to the current table of files.
1436 Verify that the file exists, and print an error message if it does not.
1437 Return the number of blocks that the file occupies. */
1440 gobble_file (name
, explicit_arg
, dirname
)
1445 register int blocks
;
1447 register char *path
;
1449 if (files_index
== nfiles
)
1452 files
= (struct fileinfo
*) xrealloc (files
, sizeof (*files
) * nfiles
);
1455 files
[files_index
].linkname
= 0;
1456 files
[files_index
].linkmode
= 0;
1458 if (explicit_arg
|| format_needs_stat
)
1460 /* `path' is the absolute pathname of this file. */
1462 if (name
[0] == '/' || dirname
[0] == 0)
1466 path
= (char *) alloca (strlen (name
) + strlen (dirname
) + 2);
1467 attach (path
, dirname
, name
);
1472 val
= stat (path
, &files
[files_index
].stat
);
1474 /* Perhaps a symbolically-linked to file doesn't exist; stat
1475 the link instead. */
1476 val
= lstat (path
, &files
[files_index
].stat
);
1479 val
= lstat (path
, &files
[files_index
].stat
);
1482 error (0, errno
, "%s", path
);
1488 if (S_ISLNK (files
[files_index
].stat
.st_mode
)
1489 && (explicit_arg
|| format
== long_format
))
1492 struct stat linkstats
;
1494 get_link_name (path
, &files
[files_index
]);
1495 linkpath
= make_link_path (path
, files
[files_index
].linkname
);
1497 /* Avoid following symbolic links when possible, ie, when
1498 they won't be traced and when no indicator is needed. */
1500 && ((explicit_arg
&& format
!= long_format
)
1501 || indicator_style
!= none
|| print_with_color
)
1502 && stat (linkpath
, &linkstats
) == 0)
1504 /* Symbolic links to directories that are mentioned on the
1505 command line are automatically traced if not being
1507 if (explicit_arg
&& format
!= long_format
1508 && S_ISDIR (linkstats
.st_mode
))
1510 /* Substitute the linked-to directory's name, but
1511 save the real name in `linkname' for printing. */
1512 if (!immediate_dirs
)
1514 char *tempname
= name
;
1516 linkpath
= files
[files_index
].linkname
;
1517 files
[files_index
].linkname
= tempname
;
1519 files
[files_index
].stat
= linkstats
;
1522 /* Get the linked-to file's mode for the filetype indicator
1523 in long listings. */
1524 files
[files_index
].linkmode
= linkstats
.st_mode
;
1532 if (S_ISLNK (files
[files_index
].stat
.st_mode
))
1533 files
[files_index
].filetype
= symbolic_link
;
1536 if (S_ISDIR (files
[files_index
].stat
.st_mode
))
1538 if (explicit_arg
&& !immediate_dirs
)
1539 files
[files_index
].filetype
= arg_directory
;
1541 files
[files_index
].filetype
= directory
;
1544 files
[files_index
].filetype
= normal
;
1546 blocks
= convert_blocks (ST_NBLOCKS (files
[files_index
].stat
),
1548 if (blocks
>= 10000 && block_size_size
< 5)
1549 block_size_size
= 5;
1550 if (blocks
>= 100000 && block_size_size
< 6)
1551 block_size_size
= 6;
1552 if (blocks
>= 1000000 && block_size_size
< 7)
1553 block_size_size
= 7;
1558 files
[files_index
].name
= xstrdup (name
);
1566 /* Put the name of the file that `filename' is a symbolic link to
1567 into the `linkname' field of `f'. */
1570 get_link_name (filename
, f
)
1575 register int linksize
;
1577 linkbuf
= (char *) alloca (PATH_MAX
+ 2);
1578 /* Some automounters give incorrect st_size for mount points.
1579 I can't think of a good workaround for it, though. */
1580 linksize
= readlink (filename
, linkbuf
, PATH_MAX
+ 1);
1583 error (0, errno
, "%s", filename
);
1588 linkbuf
[linksize
] = '\0';
1589 f
->linkname
= xstrdup (linkbuf
);
1593 /* If `linkname' is a relative path and `path' contains one or more
1594 leading directories, return `linkname' with those directories
1595 prepended; otherwise, return a copy of `linkname'.
1596 If `linkname' is zero, return zero. */
1599 make_link_path (path
, linkname
)
1609 if (*linkname
== '/')
1610 return xstrdup (linkname
);
1612 /* The link is to a relative path. Prepend any leading path
1613 in `path' to the link name. */
1614 linkbuf
= strrchr (path
, '/');
1616 return xstrdup (linkname
);
1618 bufsiz
= linkbuf
- path
+ 1;
1619 linkbuf
= xmalloc (bufsiz
+ strlen (linkname
) + 1);
1620 strncpy (linkbuf
, path
, bufsiz
);
1621 strcpy (linkbuf
+ bufsiz
, linkname
);
1626 /* Remove any entries from `files' that are for directories,
1627 and queue them to be listed as directories instead.
1628 `dirname' is the prefix to prepend to each dirname
1629 to make it correct relative to ls's working dir.
1630 `recursive' is nonzero if we should not treat `.' and `..' as dirs.
1631 This is desirable when processing directories recursively. */
1634 extract_dirs_from_files (dirname
, recursive
)
1639 register char *path
;
1642 dirlen
= strlen (dirname
) + 2;
1643 /* Queue the directories last one first, because queueing reverses the
1645 for (i
= files_index
- 1; i
>= 0; i
--)
1646 if ((files
[i
].filetype
== directory
|| files
[i
].filetype
== arg_directory
)
1647 && (!recursive
|| is_not_dot_or_dotdot (files
[i
].name
)))
1649 if (files
[i
].name
[0] == '/' || dirname
[0] == 0)
1651 queue_directory (files
[i
].name
, files
[i
].linkname
);
1655 path
= (char *) xmalloc (strlen (files
[i
].name
) + dirlen
);
1656 attach (path
, dirname
, files
[i
].name
);
1657 queue_directory (path
, files
[i
].linkname
);
1660 if (files
[i
].filetype
== arg_directory
)
1661 free (files
[i
].name
);
1664 /* Now delete the directories from the table, compacting all the remaining
1667 for (i
= 0, j
= 0; i
< files_index
; i
++)
1668 if (files
[i
].filetype
!= arg_directory
)
1669 files
[j
++] = files
[i
];
1673 /* Return non-zero if `name' doesn't end in `.' or `..'
1674 This is so we don't try to recurse on `././././. ...' */
1677 is_not_dot_or_dotdot (name
)
1682 t
= strrchr (name
, '/');
1688 || (name
[1] == '.' && name
[2] == '\0')))
1694 /* Sort the files now in the table. */
1709 func
= sort_reverse
? rev_cmp_ctime
: compare_ctime
;
1712 func
= sort_reverse
? rev_cmp_mtime
: compare_mtime
;
1715 func
= sort_reverse
? rev_cmp_atime
: compare_atime
;
1722 func
= sort_reverse
? rev_cmp_name
: compare_name
;
1724 case sort_extension
:
1725 func
= sort_reverse
? rev_cmp_extension
: compare_extension
;
1728 func
= sort_reverse
? rev_cmp_size
: compare_size
;
1734 qsort (files
, files_index
, sizeof (struct fileinfo
), func
);
1737 /* Comparison routines for sorting the files. */
1740 compare_ctime (file1
, file2
)
1741 struct fileinfo
*file1
, *file2
;
1743 return longdiff (file2
->stat
.st_ctime
, file1
->stat
.st_ctime
);
1747 rev_cmp_ctime (file2
, file1
)
1748 struct fileinfo
*file1
, *file2
;
1750 return longdiff (file2
->stat
.st_ctime
, file1
->stat
.st_ctime
);
1754 compare_mtime (file1
, file2
)
1755 struct fileinfo
*file1
, *file2
;
1757 return longdiff (file2
->stat
.st_mtime
, file1
->stat
.st_mtime
);
1761 rev_cmp_mtime (file2
, file1
)
1762 struct fileinfo
*file1
, *file2
;
1764 return longdiff (file2
->stat
.st_mtime
, file1
->stat
.st_mtime
);
1768 compare_atime (file1
, file2
)
1769 struct fileinfo
*file1
, *file2
;
1771 return longdiff (file2
->stat
.st_atime
, file1
->stat
.st_atime
);
1775 rev_cmp_atime (file2
, file1
)
1776 struct fileinfo
*file1
, *file2
;
1778 return longdiff (file2
->stat
.st_atime
, file1
->stat
.st_atime
);
1782 compare_size (file1
, file2
)
1783 struct fileinfo
*file1
, *file2
;
1785 return longdiff (file2
->stat
.st_size
, file1
->stat
.st_size
);
1789 rev_cmp_size (file2
, file1
)
1790 struct fileinfo
*file1
, *file2
;
1792 return longdiff (file2
->stat
.st_size
, file1
->stat
.st_size
);
1796 compare_name (file1
, file2
)
1797 struct fileinfo
*file1
, *file2
;
1799 return strcmp (file1
->name
, file2
->name
);
1803 rev_cmp_name (file2
, file1
)
1804 struct fileinfo
*file1
, *file2
;
1806 return strcmp (file1
->name
, file2
->name
);
1809 /* Compare file extensions. Files with no extension are `smallest'.
1810 If extensions are the same, compare by filenames instead. */
1813 compare_extension (file1
, file2
)
1814 struct fileinfo
*file1
, *file2
;
1816 register char *base1
, *base2
;
1819 base1
= strrchr (file1
->name
, '.');
1820 base2
= strrchr (file2
->name
, '.');
1821 if (base1
== 0 && base2
== 0)
1822 return strcmp (file1
->name
, file2
->name
);
1827 cmp
= strcmp (base1
, base2
);
1829 return strcmp (file1
->name
, file2
->name
);
1834 rev_cmp_extension (file2
, file1
)
1835 struct fileinfo
*file1
, *file2
;
1837 register char *base1
, *base2
;
1840 base1
= strrchr (file1
->name
, '.');
1841 base2
= strrchr (file2
->name
, '.');
1842 if (base1
== 0 && base2
== 0)
1843 return strcmp (file1
->name
, file2
->name
);
1848 cmp
= strcmp (base1
, base2
);
1850 return strcmp (file1
->name
, file2
->name
);
1854 /* List all the files now in the table. */
1857 print_current_files ()
1864 for (i
= 0; i
< files_index
; i
++)
1866 print_file_name_and_frills (files
+ i
);
1872 print_many_per_line ();
1876 print_horizontal ();
1880 print_with_commas ();
1884 for (i
= 0; i
< files_index
; i
++)
1886 print_long_format (files
+ i
);
1894 print_long_format (f
)
1900 /* 7 fields that may (worst case be 64-bit integral values) require 20 bytes,
1901 10 character mode field,
1902 24 characters for the time,
1903 9 spaces following each of these fields,
1904 and 1 trailing NUL byte. */
1905 char bigbuf
[7 * 20 + 10 + 24 + 9 + 1];
1909 mode_string (f
->stat
.st_mode
, modebuf
);
1915 when
= f
->stat
.st_ctime
;
1918 when
= f
->stat
.st_mtime
;
1921 when
= f
->stat
.st_atime
;
1925 strcpy (timebuf
, ctime (&when
));
1931 if (current_time
> when
+ 6L * 30L * 24L * 60L * 60L /* Old. */
1932 || current_time
< when
- 60L * 60L) /* In the future. */
1934 /* The file is fairly old or in the future.
1935 POSIX says the cutoff is 6 months old;
1936 approximate this by 6*30 days.
1937 Allow a 1 hour slop factor for what is considered "the future",
1938 to allow for NFS server/client clock disagreement.
1939 Show the year instead of the time of day. */
1940 strcpy (timebuf
+ 11, timebuf
+ 19);
1949 sprintf (p
, "%*lu ", INODE_DIGITS
, (unsigned long) f
->stat
.st_ino
);
1953 if (print_block_size
)
1955 sprintf (p
, "%*u ", block_size_size
,
1956 (unsigned) convert_blocks (ST_NBLOCKS (f
->stat
),
1961 /* The space between the mode and the number of links is the POSIX
1962 "optional alternate access method flag". */
1963 sprintf (p
, "%s %3u ", modebuf
, (unsigned int) f
->stat
.st_nlink
);
1967 sprintf (p
, "%-8u ", (unsigned int) f
->stat
.st_uid
);
1969 sprintf (p
, "%-8.8s ", getuser (f
->stat
.st_uid
));
1975 sprintf (p
, "%-8u ", (unsigned int) f
->stat
.st_gid
);
1977 sprintf (p
, "%-8.8s ", getgroup (f
->stat
.st_gid
));
1981 if (S_ISCHR (f
->stat
.st_mode
) || S_ISBLK (f
->stat
.st_mode
))
1982 sprintf (p
, "%3u, %3u ", (unsigned) major (f
->stat
.st_rdev
),
1983 (unsigned) minor (f
->stat
.st_rdev
));
1985 sprintf (p
, "%8lu ", (unsigned long) f
->stat
.st_size
);
1988 sprintf (p
, "%s ", full_time
? timebuf
: timebuf
+ 4);
1992 FPUTS (bigbuf
, stdout
, p
- bigbuf
);
1993 PUSH_CURRENT_DIRED_POS (&dired_obstack
);
1994 if (print_with_color
)
1995 print_color_indicator (f
->stat
.st_mode
);
1996 print_name_with_quoting (f
->name
);
1997 if (print_with_color
)
1998 put_indicator (C_END
);
1999 PUSH_CURRENT_DIRED_POS (&dired_obstack
);
2001 if (f
->filetype
== symbolic_link
)
2005 FPUTS_LITERAL (" -> ", stdout
);
2006 if (print_with_color
)
2007 print_color_indicator (f
->linkmode
);
2008 print_name_with_quoting (f
->linkname
);
2009 if (print_with_color
)
2010 put_indicator (C_END
);
2011 if (indicator_style
!= none
)
2012 print_type_indicator (f
->linkmode
);
2015 else if (indicator_style
!= none
)
2016 print_type_indicator (f
->stat
.st_mode
);
2019 /* Set QUOTED_LENGTH to strlen(P) and return NULL if P == quoted(P).
2020 Otherwise, return xmalloc'd storage containing the quoted version
2021 of P and set QUOTED_LENGTH to the length of the quoted P. */
2024 quote_filename (p
, quoted_length
)
2025 register const char *p
;
2026 size_t *quoted_length
;
2028 register unsigned char c
;
2033 if (!quote_as_string
&& !quote_funny_chars
&& !qmark_funny_chars
)
2035 *quoted_length
= strlen (p
);
2040 for (c
= *p
; c
; c
= *++p
)
2042 if (quote_funny_chars
)
2058 /* FIXME: why not just use the ISPRINT macro here? */
2059 if (!((c
> 040 && c
< 0177)
2060 || (print_iso8859
&& c
>= 0200 && c
<= 0377)))
2067 if (!((c
>= 040 && c
< 0177)
2068 || (print_iso8859
&& c
>= 0xA1 && c
<= 0xFF))
2069 && qmark_funny_chars
)
2076 if (!found_quotable
&& !quote_as_string
)
2078 *quoted_length
= p
- p0
;
2083 quoted
= xmalloc (4 * strlen (p
) + 1);
2086 #define SAVECHAR(c) *q++ = (c)
2087 #define SAVE_2_CHARS(c12) \
2088 do { *q++ = ((c12)[0]); \
2089 *q++ = ((c12)[1]); } while (0)
2091 if (quote_as_string
)
2096 if (quote_funny_chars
)
2101 SAVE_2_CHARS ("\\\\");
2105 SAVE_2_CHARS ("\\n");
2109 SAVE_2_CHARS ("\\b");
2113 SAVE_2_CHARS ("\\r");
2117 SAVE_2_CHARS ("\\t");
2121 SAVE_2_CHARS ("\\f");
2125 SAVE_2_CHARS ("\\ ");
2129 SAVE_2_CHARS ("\\\"");
2133 if ((c
> 040 && c
< 0177)
2134 || (print_iso8859
&& c
>= 0200 && c
<= 0377))
2139 sprintf (buf
, "\\%03o", (unsigned int) c
);
2140 q
= stpcpy (q
, buf
);
2146 if ((c
>= 040 && c
< 0177)
2147 || (print_iso8859
&& c
>= 0200 && c
<= 0377))
2149 else if (!qmark_funny_chars
)
2156 if (quote_as_string
)
2159 *quoted_length
= q
- quoted
;
2167 print_name_with_quoting (p
)
2171 size_t quoted_length
;
2173 quoted
= quote_filename (p
, "ed_length
);
2174 FPUTS (quoted
!= NULL
? quoted
: p
, stdout
, quoted_length
);
2179 /* Print the file name of `f' with appropriate quoting.
2180 Also print file size, inode number, and filetype indicator character,
2181 as requested by switches. */
2184 print_file_name_and_frills (f
)
2188 printf ("%*lu ", INODE_DIGITS
, (unsigned long) f
->stat
.st_ino
);
2190 if (print_block_size
)
2191 printf ("%*u ", block_size_size
,
2192 (unsigned) convert_blocks (ST_NBLOCKS (f
->stat
),
2195 if (print_with_color
)
2196 print_color_indicator (f
->stat
.st_mode
);
2197 print_name_with_quoting (f
->name
);
2198 if (print_with_color
)
2199 put_indicator (C_END
);
2201 if (indicator_style
!= none
)
2202 print_type_indicator (f
->stat
.st_mode
);
2206 print_type_indicator (mode
)
2218 if (S_ISFIFO (mode
))
2223 if (S_ISSOCK (mode
))
2227 if (S_ISREG (mode
) && indicator_style
== all
2228 && (mode
& (S_IEXEC
| S_IXGRP
| S_IXOTH
)))
2233 print_color_indicator (mode
)
2242 else if (S_ISLNK (mode
))
2247 else if (S_ISFIFO (mode
))
2252 else if (S_ISSOCK (mode
))
2257 else if (S_ISBLK (mode
))
2262 else if (S_ISCHR (mode
))
2266 if (type
== C_FILE
&& (mode
& (S_IEXEC
| S_IEXEC
>> 3 | S_IEXEC
>> 6)))
2269 put_indicator (C_LEFT
);
2270 put_indicator (type
);
2271 put_indicator (C_RIGHT
);
2274 /* Output a color indicator (which may contain nulls) */
2282 p
= color_indicator
[n
].string
;
2284 for (i
= color_indicator
[n
].len
; i
; i
--)
2291 length_of_file_name_and_frills (f
)
2294 register char *p
= f
->name
;
2295 register unsigned char c
;
2296 register int len
= 0;
2299 len
+= INODE_DIGITS
+ 1;
2301 if (print_block_size
)
2302 len
+= 1 + block_size_size
;
2304 if (quote_as_string
)
2309 if (quote_funny_chars
)
2324 if (quote_as_string
)
2331 if ((c
>= 040 && c
< 0177)
2332 || (print_iso8859
&& c
>= 0200 && c
<= 0377))
2342 if (indicator_style
!= none
)
2344 unsigned filetype
= f
->stat
.st_mode
;
2346 if (S_ISREG (filetype
))
2348 if (indicator_style
== all
2349 && (f
->stat
.st_mode
& (S_IEXEC
| S_IEXEC
>> 3 | S_IEXEC
>> 6)))
2352 else if (S_ISDIR (filetype
)
2354 || S_ISLNK (filetype
)
2357 || S_ISFIFO (filetype
)
2360 || S_ISSOCK (filetype
)
2370 print_many_per_line ()
2372 int filesno
; /* Index into files. */
2373 int row
; /* Current row. */
2374 int max_name_length
; /* Length of longest file name + frills. */
2375 int name_length
; /* Length of each file name + frills. */
2376 int pos
; /* Current character column. */
2377 int cols
; /* Number of files across. */
2378 int rows
; /* Maximum number of files down. */
2380 /* Compute the maximum file name length. */
2381 max_name_length
= 0;
2382 for (filesno
= 0; filesno
< files_index
; filesno
++)
2384 name_length
= length_of_file_name_and_frills (files
+ filesno
);
2385 if (name_length
> max_name_length
)
2386 max_name_length
= name_length
;
2389 /* Allow at least two spaces between names. */
2390 max_name_length
+= 2;
2392 /* Calculate the maximum number of columns that will fit. */
2393 cols
= line_length
/ max_name_length
;
2396 /* Calculate the number of rows that will be in each column except possibly
2397 for a short column on the right. */
2398 rows
= files_index
/ cols
+ (files_index
% cols
!= 0);
2399 /* Recalculate columns based on rows. */
2400 cols
= files_index
/ rows
+ (files_index
% rows
!= 0);
2402 for (row
= 0; row
< rows
; row
++)
2406 /* Print the next row. */
2409 print_file_name_and_frills (files
+ filesno
);
2410 name_length
= length_of_file_name_and_frills (files
+ filesno
);
2413 if (filesno
>= files_index
)
2416 indent (pos
+ name_length
, pos
+ max_name_length
);
2417 pos
+= max_name_length
;
2427 int max_name_length
;
2432 /* Compute the maximum file name length. */
2433 max_name_length
= 0;
2434 for (filesno
= 0; filesno
< files_index
; filesno
++)
2436 name_length
= length_of_file_name_and_frills (files
+ filesno
);
2437 if (name_length
> max_name_length
)
2438 max_name_length
= name_length
;
2441 /* Allow two spaces between names. */
2442 max_name_length
+= 2;
2444 cols
= line_length
/ max_name_length
;
2451 for (filesno
= 0; filesno
< files_index
; filesno
++)
2455 if (filesno
% cols
== 0)
2462 indent (pos
+ name_length
, pos
+ max_name_length
);
2463 pos
+= max_name_length
;
2467 print_file_name_and_frills (files
+ filesno
);
2469 name_length
= length_of_file_name_and_frills (files
+ filesno
);
2475 print_with_commas ()
2482 for (filesno
= 0; filesno
< files_index
; filesno
++)
2486 pos
+= length_of_file_name_and_frills (files
+ filesno
);
2487 if (filesno
+ 1 < files_index
)
2488 pos
+= 2; /* For the comma and space */
2490 if (old_pos
!= 0 && pos
>= line_length
)
2496 print_file_name_and_frills (files
+ filesno
);
2497 if (filesno
+ 1 < files_index
)
2506 /* Assuming cursor is at position FROM, indent up to position TO.
2507 Use a TAB character instead of two or more spaces whenever possible. */
2515 if (to
/ tabsize
> (from
+ 1) / tabsize
)
2518 from
+= tabsize
- from
% tabsize
;
2528 /* Put DIRNAME/NAME into DEST, handling `.' and `/' properly. */
2531 attach (dest
, dirname
, name
)
2532 char *dest
, *dirname
, *name
;
2534 char *dirnamep
= dirname
;
2536 /* Copy dirname if it is not ".". */
2537 if (dirname
[0] != '.' || dirname
[1] != 0)
2540 *dest
++ = *dirnamep
++;
2541 /* Add '/' if `dirname' doesn't already end with it. */
2542 if (dirnamep
> dirname
&& dirnamep
[-1] != '/')
2555 fprintf (stderr
, "Try `%s --help' for more information.\n",
2559 printf ("Usage: %s [OPTION]... [FILE]...\n", program_name
);
2561 List information about the FILEs (the current directory by default).\n\
2562 Sort entries alphabetically if none of -cftuSUX nor --sort.\n\
2564 -A, --almost-all do not list implied . and ..\n\
2565 -a, --all do not hide entries starting with .\n\
2566 -B, --ignore-backups do not list implied entries ending with ~\n\
2567 -b, --escape print octal escapes for nongraphic characters\n\
2568 -C list entries by columns\n\
2569 -c sort by change time; with -l: show ctime\n\
2570 -D, --dired generate output well suited to Emacs' dired mode\n\
2571 -d, --directory list directory entries instead of contents\n\
2572 -F, --classify append a character for typing each entry\n\
2573 -f do not sort, enable -aU, disable -lst\n\
2574 --format=WORD across -x, commas -m, horizontal -x, long -l,\n\
2575 single-column -1, verbose -l, vertical -C\n\
2576 --full-time list both full date and full time\n");
2579 -G, --no-group inhibit display of group information\n\
2581 -I, --ignore=PATTERN do not list implied entries matching shell PATTERN\n\
2582 -i, --inode print index number of each file\n\
2583 -k, --kilobytes use 1024 blocks, not 512 despite POSIXLY_CORRECT\n\
2584 -L, --dereference list entries pointed to by symbolic links\n\
2585 -l use a long listing format\n\
2586 -m fill width with a comma separated list of entries\n\
2587 -N, --literal do not quote entry names\n\
2588 -n, --numeric-uid-gid list numeric UIDs and GIDs instead of names\n\
2589 -o, --color, --colour colorize entries according to type\n\
2590 --colo(u)r=WORD yes -o, no, tty (if output is a terminal)\n\
2591 -p append a character for typing each entry\n\
2592 -Q, --quote-name enclose entry names in double quotes\n\
2593 -q, --hide-control-chars print ? instead of non graphic characters\n\
2594 -R, --recursive list subdirectories recursively\n\
2595 -r, --reverse reverse order while sorting\n\
2596 -S sort by file size\n");
2599 -s, --size print block size of each file\n\
2600 --sort=WORD ctime -c, extension -X, none -U, size -S,\n\
2601 status -c, time -t\n\
2602 --time=WORD atime -u, access -u, use -u\n\
2603 -T, --tabsize=COLS assume tab stops at each COLS instead of 8\n\
2604 -t sort by modification time; with -l: show mtime\n\
2605 -U do not sort; list entries in directory order\n\
2606 -u sort by last access time; with -l: show atime\n\
2607 -w, --width=COLS assume screen width instead of current value\n\
2608 -x list entries by lines instead of by columns\n\
2609 -X sort alphabetically by entry extension\n\
2610 -1 list one file per line\n\
2611 -7, --7bit allow only 7-bit ASCII characters to be printed\n\
2612 -8, --8bit allow 8-bit ISO 8859 characters to be printed\n\
2613 --help display this help and exit\n\
2614 --version output version information and exit");