diff options
author | Alex Musolino <alex@musolino.id.au> | 2018-10-31 16:49:02 +1030 |
---|---|---|
committer | Alex Musolino <alex@musolino.id.au> | 2018-10-31 16:49:02 +1030 |
commit | 913be4e74affe04a2ceb3ab75cb8056dd920a5c8 (patch) | |
tree | 9fdddf366bddaf5298c83f8c289a9756a13da5b6 /sys/src/cmd/awk | |
parent | 8e9d2434e97fe6cf4b0d3e7f43e3e35d7ae1033f (diff) |
awk(1): fix append operator to avoid truncating file
Diffstat (limited to 'sys/src/cmd/awk')
-rw-r--r-- | sys/src/cmd/awk/run.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/sys/src/cmd/awk/run.c b/sys/src/cmd/awk/run.c index c23efb36b..9af1b4b01 100644 --- a/sys/src/cmd/awk/run.c +++ b/sys/src/cmd/awk/run.c @@ -1710,7 +1710,7 @@ void stdinit(void) /* in case stdin, etc., are not constants */ Biobuf *openfile(int a, char *us) { char *s = us; - int i, m; + int i, m, fd; Biobuf *fp = nil; if (*s == '\0') @@ -1735,9 +1735,15 @@ Biobuf *openfile(int a, char *us) if (a == GT) { fp = Bopen(s, OWRITE); } else if (a == APPEND) { - fp = Bopen(s, OWRITE); - Bseek(fp, 0LL, 2); - m = GT; /* so can mix > and >> */ + fd = open(s, OWRITE); + if (fd < 0) + fd = create(s, OWRITE, 0666); + if (fd >= 0) { + fp = Bfdopen(fd, OWRITE); + if (fp != nil) + Bseek(fp, 0LL, 2); + m = GT; /* so can mix > and >> */ + } } else if (a == '|') { /* output pipe */ fp = popen(s, OWRITE); } else if (a == LE) { /* input pipe */ |