diff options
author | Dag-Erling Smørgrav <des@FreeBSD.org> | 2003-11-04 13:09:16 +0000 |
---|---|---|
committer | Dag-Erling Smørgrav <des@FreeBSD.org> | 2003-11-04 13:09:16 +0000 |
commit | f54cda1467567b0bb2591162af68bc0769051c8d (patch) | |
tree | fddafe53ce6d23db8b371f7ca372c795650eca05 /usr.bin/sed/process.c | |
parent | 95755cc99b8c434aa31eaf96b61217bb8c3a7b88 (diff) | |
download | src-f54cda1467567b0bb2591162af68bc0769051c8d.tar.gz src-f54cda1467567b0bb2591162af68bc0769051c8d.zip |
Reimplement in-place editing in a slightly less disgusting manner. Also,
make an effort to preserve the ownership and mode of the file we are
editing.
Sponsored by: Registrar AS
Notes
Notes:
svn path=/head/; revision=122049
Diffstat (limited to 'usr.bin/sed/process.c')
-rw-r--r-- | usr.bin/sed/process.c | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/usr.bin/sed/process.c b/usr.bin/sed/process.c index 5baed7a44b0c..772fd0b92893 100644 --- a/usr.bin/sed/process.c +++ b/usr.bin/sed/process.c @@ -86,7 +86,7 @@ static regex_t *defpreg; size_t maxnsub; regmatch_t *match; -#define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); putchar('\n'); } +#define OUT(s) { fwrite(s, sizeof(u_char), psl, outfile); fputc('\n', outfile); } void process(void) @@ -130,7 +130,7 @@ redirect: pd = 1; psl = 0; if (cp->a2 == NULL || lastaddr) - (void)printf("%s", cp->t); + (void)fprintf(outfile, "%s", cp->t); break; case 'd': pd = 1; @@ -162,7 +162,7 @@ redirect: cspace(&HS, ps, psl, 0); break; case 'i': - (void)printf("%s", cp->t); + (void)fprintf(outfile, "%s", cp->t); break; case 'l': lputs(ps); @@ -252,7 +252,7 @@ redirect: case '}': break; case '=': - (void)printf("%lu\n", linenum); + (void)fprintf(outfile, "%lu\n", linenum); } cp = cp->next; } /* for all cp */ @@ -438,7 +438,7 @@ flush_appends(void) switch (appends[i].type) { case AP_STRING: fwrite(appends[i].s, sizeof(char), appends[i].len, - stdout); + outfile); break; case AP_FILE: /* @@ -452,12 +452,12 @@ flush_appends(void) if ((f = fopen(appends[i].s, "r")) == NULL) break; while ((count = fread(buf, sizeof(char), sizeof(buf), f))) - (void)fwrite(buf, sizeof(char), count, stdout); + (void)fwrite(buf, sizeof(char), count, outfile); (void)fclose(f); break; } - if (ferror(stdout)) - errx(1, "stdout: %s", strerror(errno ? errno : EIO)); + if (ferror(outfile)) + errx(1, "%s: %s", outfname, strerror(errno ? errno : EIO)); appendx = sdone = 0; } @@ -470,6 +470,8 @@ lputs(char *s) struct winsize win; static int termwidth = -1; + if (outfile != stdout) + termwidth = 60; if (termwidth == -1) { if ((p = getenv("COLUMNS")) && *p != '\0') termwidth = atoi(p); @@ -482,32 +484,32 @@ lputs(char *s) for (count = 0; *s; ++s) { if (count + 5 >= termwidth) { - (void)printf("\\\n"); + (void)fprintf(outfile, "\\\n"); count = 0; } if (isprint((unsigned char)*s) && *s != '\\') { - (void)putchar(*s); + (void)fputc(*s, outfile); count++; } else if (*s == '\n') { - (void)putchar('$'); - (void)putchar('\n'); + (void)fputc('$', outfile); + (void)fputc('\n', outfile); count = 0; } else { escapes = "\\\a\b\f\r\t\v"; - (void)putchar('\\'); + (void)fputc('\\', outfile); if ((p = strchr(escapes, *s))) { - (void)putchar("\\abfrtv"[p - escapes]); + (void)fputc("\\abfrtv"[p - escapes], outfile); count += 2; } else { - (void)printf("%03o", *(u_char *)s); + (void)fprintf(outfile, "%03o", *(u_char *)s); count += 4; } } } - (void)putchar('$'); - (void)putchar('\n'); - if (ferror(stdout)) - errx(1, "stdout: %s", strerror(errno ? errno : EIO)); + (void)fputc('$', outfile); + (void)fputc('\n', outfile); + if (ferror(outfile)) + errx(1, "%s: %s", outfname, strerror(errno ? errno : EIO)); } static __inline int |