ref: 869300591ad0bd8765a7e789b178677b31b5464a
parent: c7a0b7febc865a429ac0bf2cb8ed7e14347f4a71
author: Alex Musolino <alex@musolino.id.au>
date: Wed Oct 31 12:49:02 EDT 2018
awk(1): fix append operator to avoid truncating file
--- a/sys/src/cmd/awk/run.c
+++ b/sys/src/cmd/awk/run.c
@@ -1710,7 +1710,7 @@
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 @@
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 */--
⑨