[PATCH] ls: adding --zero/-z option, including tests (original) (raw)


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


From: Anthon van der Neut
Subject: [PATCH] ls: adding --zero/-z option, including tests
Date: Mon, 03 Feb 2014 16:15:51 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0

| A recent Q&A thread on unix.stackexchange.com on the reason why ls has no option like find to generate NUL separated output triggered the making of this patch. ( http://unix.stackexchange.com/q/112125/33055 ).

Quite often questions pop up on that site where questioners, unaware of the fact that newlines and other special characters can be part of filenames do something like: ls -rt | xargs ... Invariably someone points out that it is not safe to process the output of ls in this way in general.

The included patch adds an option --zero/-z to ls so that the filenames are separated by the NUL character instead of a newline when doing the -1 (which is in principle indistinguishable from a newline that is part of the filename as you can have "abc", "def" and "abc\ndef" as filenames in one directory).

The patch allows one to do: ls -rt --zero | xargs -0 ... and ||| ls -rtz | xargs -0 ... | One file with basic tests has been added, another extended.

Please let me know if this is not acceptable, or if changes are required before this can be processed.

Regards Anthon

Attachements:


From 6413d5e2a488ecadb8b988c802fe0a5e5cb7d8f4 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut <address@hidden> Date: Mon, 3 Feb 2014 15:33:50 +0100 Subject: [PATCH] ls: adding --zero/-z option, including tests

This patch was inspired by numerous questions on unix.stackexchange.com where the output of ls was piped into some other program, invariably resulting in someone pointing out that is an unsafe practise because of possible newlines and other characters in the filenames.

src/ls.c | 31 +++++++++++++++++++++++++------ tests/ls/no-arg.sh | 7 ++++++- tests/ls/rt-zero.sh | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) create mode 100755 tests/ls/rt-zero.sh

diff --git a/src/ls.c b/src/ls.c index 5d87dd3..962e6bb 100644 --- a/src/ls.c +++ b/src/ls.c @@ -381,6 +381,7 @@ static int file_size_width; many_per_line for just names, many per line, sorted vertically. horizontal for just names, many per line, sorted horizontally. with_commas for just names, many per line, separated by commas.

-l (and other options that imply -l), -1, -C, -x and -m control

this parameter.  */

@@ -391,7 +392,8 @@ enum format one_per_line, /* -1 / many_per_line, / -C / horizontal, / -x */

};

static enum format format;

@@ -842,6 +844,7 @@ static struct option const long_options[] = {"block-size", required_argument, NULL, BLOCK_SIZE_OPTION}, {"context", no_argument, 0, 'Z'}, {"author", no_argument, NULL, AUTHOR_OPTION},

@@ -1645,7 +1648,7 @@ decode_switches (int argc, char **argv)

 {
   int oi = -1;
   int c = getopt_long (argc, argv,

@@ -1852,6 +1855,10 @@ decode_switches (int argc, char **argv) format = one_per_line; break;

+ case 'z':

@@ -2607,7 +2614,8 @@ print_dir (char const *name, char const *realname, bool command_line_arg) ls uses constant memory while processing the entries of this directory. Useful when there are many (millions) of entries in a directory. */

@@ -3598,6 +3606,14 @@ print_current_files (void) } break;

+ case with_zero:

@@ -4490,6 +4506,7 @@ print_many_per_line (void) indent (pos + name_length, pos + max_name_length); pos += max_name_length; }

"), stdout); fputs (_("
@@ -4888,6 +4906,7 @@ Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.\n
-X sort alphabetically by entry extension\n
-Z, --context print any security context of each file\n
-1 list one file per line\n\

diff --git a/tests/ls/no-arg.sh b/tests/ls/no-arg.sh index e356a29..da28b96 100755 --- a/tests/ls/no-arg.sh +++ b/tests/ls/no-arg.sh @@ -30,11 +30,16 @@ out symlink EOF

-

ls -1 > out || fail=1

compare exp out || fail=1 +/bin/echo -en "dir\00exp\00out\00symlink\00" > exp || framework_failure_

1.7.9.5


    Developer's Certificate of Origin 1.1

    By making a contribution to this project, I certify that:

    (a) The contribution was created in whole or in part by me and I
        have the right to submit it under the open source license
        indicated in the file; or

    (b) The contribution is based upon previous work that, to the best
        of my knowledge, is covered under an appropriate open source
        license and I have the right under that license to submit that
        work with modifications, whether created in whole or in part
        by me, under the same open source license (unless I am
        permitted to submit under a different license), as indicated
        in the file; or

    (c) The contribution was provided directly to me by some other
        person who certified (a), (b) or (c) and I have not modified
        it.

    (d) I understand and agree that this project and the contribution
        are public and that a record of the contribution (including all
        personal information I submit with it, including my sign-off) is
        maintained indefinitely and may be redistributed consistent with
        this project or the open source license(s) involved.


    Signed-off-by: Anthon van der Neut <address@hidden>

|