From: Bill Davidson Newsgroups: comp.os.msdos.djgpp Subject: Patch for ls Date: Mon, 16 Dec 1996 21:45:48 -0400 Organization: Auracom Internet Services Lines: 322 Message-ID: <594tmp$9a8$1@thor.atcon.com> NNTP-Posting-Host: 199.45.83.31 Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------29637E28495AABDA6D4BAE2A" To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp This is a multi-part message in MIME format. --------------29637E28495AABDA6D4BAE2A Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi: I've noticed quite a few posts here recently from those unsatisfied with the default behaviour of ls, especially having to typ "--color=yes" or whatever to get color output. The version of ls on my linux system (actually older, I think, than the version with djgpp-v2) reads an environment variable called LS_OPTIONS before parsing the command line. I like this behaviour, and keep my LS_OPTIONS set to "--color=tty -F". So I changed ls.c to behave as above. I am including the patch here. I am making no claims about the quality of the code, except it works. I just create an array of char * that points to the options passed in via LS_OPTIONS, plus a count of the options, and then I copied all the code that parses the command line (that's why the patch is kind of long; I guess there would be more efficient ways of doing it, and they are left as an exercise for the reader!). Then I clear optind and let the command line parsing take it from there. Patch follows. Ok, Netscape will only "attach", not "include", so the patch is attached. Enjoy, feel free to modify, feel free to criticize or comment, all flames to /dev/nul. -- +---------------------------------------+ | Bill Davidson | | bdavidson AT ra DOT isisnet DOT com | | http://www.isisnet.com/bdavidson/ | +---------------------------------------+ --------------29637E28495AABDA6D4BAE2A Content-Type: text/plain; charset=us-ascii; name="ls.pat" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ls.pat" --- ls.old Sat Dec 14 23:35:38 1996 +++ ls.c Mon Dec 16 20:58:52 1996 @@ -793,6 +793,7 @@ static int decode_switches (int argc, char **argv) { register char *p; + char *buf; int c; int i; long int tmp_long; @@ -897,6 +898,264 @@ decode_switches (int argc, char **argv) p); } } + + /* read env var LS_OPTIONS and process before reading command line */ + + buf = getenv ("LS_OPTIONS"); + if (buf != NULL) { + char *tok; + /* worst case: almost every char in LS_OPTIONS is an option */ + char **tokv = (char **)alloca(sizeof(char *) * strlen(buf)); + int tokc=1; /* must be at least one option */ + tokv[0] = NULL; + for (tok = strtok(buf, " \t"); tok != NULL; tok = strtok(NULL, " \t")) { + tokv[tokc] = tok; + ++tokc; + } + while ((c = getopt_long (tokc, tokv, + "abcdfgiklmnopqrstuw:xABCDFGI:LNQRST:UX1", + long_options, (int *) 0)) != EOF) { + switch (c) { + case 0: + break; + + case 'a': + all_files = 1; + really_all_files = 1; + break; + + case 'b': + quote_funny_chars = 1; + qmark_funny_chars = 0; + break; + + case 'c': + time_type = time_ctime; + sort_type = sort_time; + break; + + case 'd': + immediate_dirs = 1; + break; + + case 'f': + /* Same as enabling -a -U and disabling -l -s. */ + all_files = 1; + really_all_files = 1; + sort_type = sort_none; + /* disable -l */ + if (format == long_format) + format = (isatty (1) ? many_per_line : one_per_line); + print_block_size = 0; /* disable -s */ + print_with_color = 0; /* disable --color */ + break; + + case 'g': + /* No effect. For BSD compatibility. */ +#ifdef MSDOS + msdos_long_format = 1; +#endif + break; + + case 'i': + print_inode = 1; + break; + + case 'k': + kilobyte_blocks = 1; + break; + + case 'l': + format = long_format; + break; + + case 'm': + format = with_commas; + break; + + case 'n': + numeric_users = 1; + break; + + case 'o': /* Just like -l, but don't display group info. */ + format = long_format; + inhibit_group = 1; + break; + + case 'p': + indicator_style = not_programs; + break; + + case 'q': + qmark_funny_chars = 1; + quote_funny_chars = 0; + break; + + case 'r': + sort_reverse = 1; + break; + + case 's': + print_block_size = 1; + break; + + case 't': + sort_type = sort_time; + break; + + case 'u': + time_type = time_atime; + break; + + case 'w': + if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK + || tmp_long <= 0 || tmp_long > INT_MAX) + error (1, 0, _("invalid line width: %s"), optarg); + line_length = (int) tmp_long; + break; + + case 'x': + format = horizontal; + break; + + case 'A': + all_files = 1; + break; + + case 'B': + add_ignore_pattern ("*~"); + add_ignore_pattern (".*~"); + break; + + case 'C': + format = many_per_line; + break; + + case 'D': + dired = 1; + break; + + case 'F': + indicator_style = all; + break; + + case 'G': /* inhibit display of group info */ + inhibit_group = 1; + break; + + case 'I': + add_ignore_pattern (optarg); + break; + + case 'L': + trace_links = 1; + break; + + case 'N': + quote_funny_chars = 0; + qmark_funny_chars = 0; + break; + + case 'Q': + quote_as_string = 1; + quote_funny_chars = 1; + qmark_funny_chars = 0; + break; + + case 'R': + trace_dirs = 1; + break; + + case 'S': + sort_type = sort_size; + break; + + case 'T': + if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK + || tmp_long < 0 || tmp_long > INT_MAX) + error (1, 0, _("invalid tab size: %s"), optarg); + tabsize = (int) tmp_long; + break; + + case 'U': + sort_type = sort_none; + break; + + case 'X': + sort_type = sort_extension; + break; + + case '1': + format = one_per_line; + break; + + case 10: /* --sort */ + i = argmatch (optarg, sort_args); + if (i < 0) + { + invalid_arg (_("sort type"), optarg, i); + usage (1); + } + sort_type = sort_types[i]; + break; + + case 11: /* --time */ + i = argmatch (optarg, time_args); + if (i < 0) + { + invalid_arg (_("time type"), optarg, i); + usage (1); + } + time_type = time_types[i]; + break; + + case 12: /* --format */ + i = argmatch (optarg, format_args); + if (i < 0) + { + invalid_arg (_("format type"), optarg, i); + usage (1); + } + format = formats[i]; + break; + + case 13: /* --color */ + if (optarg) + { + i = argmatch (optarg, color_args); + if (i < 0) + { + invalid_arg (_("colorization criterion"), optarg, i); + usage (1); + } + i = color_types[i]; + } + else + { + /* Using --color with no argument is equivalent to using + --color=always. */ + i = color_always; + } + + print_with_color = (i == color_always + || (i == color_if_tty + && isatty (STDOUT_FILENO))); + + if (print_with_color) + { + /* Don't use TAB characters in output. Some terminal + emulators can't handle the combination of tabs and + color codes on the same line. */ + tabsize = 0; + } + break; + + default: + usage (1); + } /* end switch */ + } /* end while */ + optind = 0; + } /* end if (buf != NULL) */ while ((c = getopt_long (argc, argv, "abcdfgiklmnopqrstuw:xABCDFGI:LNQRST:UX1", --------------29637E28495AABDA6D4BAE2A--