]> git.lizzy.rs Git - plan9front.git/commitdiff
awk(1): fix append operator to avoid truncating file
authorAlex Musolino <alex@musolino.id.au>
Wed, 31 Oct 2018 06:19:02 +0000 (16:49 +1030)
committerAlex Musolino <alex@musolino.id.au>
Wed, 31 Oct 2018 06:19:02 +0000 (16:49 +1030)
sys/src/cmd/awk/run.c

index c23efb36b71be0b5b347daf9e458016c9dcbfb9e..9af1b4b01f0a8a37279aa058eb04f694fda90143 100644 (file)
@@ -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 */