check the return status of Bputc, Bputrune, Bterm, Bprint, Bopen. in the case of arout(), a compromise was made to ensure that old scripts don't break. just emit a diagnostic. Reference: /n/sources/patch/maybe/sederrors Date: Tue Aug 18 17:32:56 CES 2009 Signed-off-by: quanstro@quanstro.net --- /sys/src/cmd/sed.c Tue Aug 18 17:29:40 2009 +++ /sys/src/cmd/sed.c Tue Aug 18 17:29:37 2009 @@ -199,6 +199,10 @@ int ycomp(SedCom *); char * trans(int c); void putline(Biobuf *bp, Rune *buf, int n); +void ebputc(Biobufhdr*, int); +void ebputrune(Biobufhdr*, int); + +#define ebprint(bp, ...) if(Bprint(bp, __VA_ARGS__) < 0) quit("Bprint: %r"); else {} void main(int argc, char **argv) @@ -735,7 +739,8 @@ } else c = -1; } else if ((c = Bgetrune(prog.bp)) < 0) - Bterm(prog.bp); + if(Bterm(prog.bp) < 0) + quit("write: %r"); return c; } @@ -1132,8 +1137,8 @@ delflag = 1; if(ipc->active == 1) { for(rp = ipc->text; *rp; rp++) - Bputrune(&fout, *rp); - Bputc(&fout, '\n'); + ebputrune(&fout, *rp); + ebputc(&fout, '\n'); } break; case DCOM: @@ -1154,7 +1159,7 @@ jflag++; break; case EQCOM: - Bprint(&fout, "%ld\n", lnum); + ebprint(&fout, "%ld\n", lnum) ; break; case GCOM: p1 = linebuf; @@ -1189,8 +1194,8 @@ break; case ICOM: for(rp = ipc->text; *rp; rp++) - Bputrune(&fout, *rp); - Bputc(&fout, '\n'); + ebputrune(&fout, *rp); + ebputc(&fout, '\n'); break; case BCOM: jflag = 1; @@ -1200,25 +1205,25 @@ for (i = 0, rp = linebuf; *rp; rp++) { c = *rp; if(c >= 0x20 && c < 0x7F && c != '\\') { - Bputc(&fout, c); + ebputc(&fout, c); if(i++ > 71) { - Bprint(&fout, "\\\n"); + ebprint(&fout, "\\\n"); i = 0; } } else { for (ucp = trans(*rp); *ucp; ucp++){ c = *ucp; - Bputc(&fout, c); + ebputc(&fout, c); if(i++ > 71) { - Bprint(&fout, "\\\n"); + ebprint(&fout, "\\\n"); i = 0; } } } } if(c == ' ') - Bprint(&fout, "\\n"); - Bputc(&fout, '\n'); + ebprint(&fout, "\\n"); + ebputc(&fout, '\n'); break; case NCOM: if(!nflag) @@ -1248,8 +1253,8 @@ case CPCOM: cpcom: for(rp = linebuf; *rp && *rp != '\n'; rp++) - Bputc(&fout, *rp); - Bputc(&fout, '\n'); + ebputc(&fout, *rp); + ebputc(&fout, '\n'); break; case QCOM: if(!nflag) @@ -1316,8 +1321,8 @@ putline(Biobuf *bp, Rune *buf, int n) { while (n--) - Bputrune(bp, *buf++); - Bputc(bp, '\n'); + ebputrune(bp, *buf++); + ebputc(bp, '\n'); } ecmp(Rune *a, Rune *b, int count) { @@ -1339,17 +1344,25 @@ for (aptr = abuf; *aptr; aptr++) { if((*aptr)->command == ACOM) { for(p1 = (*aptr)->text; *p1; p1++ ) - Bputrune(&fout, *p1); - Bputc(&fout, '\n'); + ebputrune(&fout, *p1); + ebputc(&fout, '\n'); } else { for(s = buf, p1 = (*aptr)->text; *p1; p1++) s += runetochar(s, p1); *s = '\0'; - if((fi = Bopen(buf, OREAD)) == 0) + if((fi = Bopen(buf, OREAD)) == nil){ + /* + * this is a botch. we should quit. + * but i'm worried about breaking + * old scripts. — quanstro + */ + fprint(2, "Bopen: %r\n"); continue; + } while((c = Bgetc(fi)) >= 0) - Bputc(&fout, c); - Bterm(fi); + ebputc(&fout, c); + if(Bterm(fi) < 0) + quit("Bterm: %r"); } } aptr = abuf; @@ -1379,6 +1392,20 @@ errexit(); } +void +ebputc(Biobufhdr *bp, int c) +{ + if(Bputc(bp, c) < 0) + quit("Bputc: %r"); +} + +void +ebputrune(Biobufhdr *bp, int r) +{ + if(Bputrune(bp, r) < 0) + quit("Bputrune: %r"); +} + Rune * gline(Rune *addr) { @@ -1412,7 +1439,8 @@ return p; } peekc = 0; - Bterm(f); + if(Bterm(f) < 0) + quit("write: %r"); } while (opendata() > 0); /* Switch to next stream */ f = 0; return 0;