diff options
author | Elliott Hughes <enh@google.com> | 2019-02-04 17:43:27 -0800 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-02-04 21:03:17 -0600 |
commit | d5ad47d000635aa69f2016bf641958c89fba26cc (patch) | |
tree | 411e4c75299ba9baba7724fb2183f6f2a9cf8d4b /toys/posix | |
parent | 02f220a9a6a8b358d18b26824ed3434cbdbbd75b (diff) | |
download | toybox-d5ad47d000635aa69f2016bf641958c89fba26cc.tar.gz |
sort -o: fix behavior when output file is one of the input files.
Bug: http://b/123902291
Diffstat (limited to 'toys/posix')
-rw-r--r-- | toys/posix/sort.c | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/toys/posix/sort.c b/toys/posix/sort.c index 6f5467e1..9433aef2 100644 --- a/toys/posix/sort.c +++ b/toys/posix/sort.c @@ -304,9 +304,6 @@ void sort_main(void) { int idx, fd = 1; - // Open output file if necessary. - if (TT.o) fd = xcreate(TT.o, O_CREAT|O_TRUNC|O_WRONLY, 0666); - // Parse -k sort keys. if (TT.k) { struct arg_list *arg; @@ -384,6 +381,10 @@ void sort_main(void) if (TT.linecount) TT.linecount = jdx+1; } + // Open output file if necessary. We can't do this until we've finished + // reading in case the output file is one of the input files. + if (TT.o) fd = xcreate(TT.o, O_CREAT|O_TRUNC|O_WRONLY, 0666); + // Output result for (idx = 0; idx<TT.linecount; idx++) { char *s = TT.lines[idx]; |