1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1992 Diomidis Spinellis.
5 * Copyright (c) 1992, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Diomidis Spinellis of Imperial College, University of London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <sys/uio.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <limits.h>
48 #include <regex.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <wchar.h>
54 #include <wctype.h>
55
56 #include "defs.h"
57 #include "extern.h"
58
59 static SPACE HS, PS, SS, YS;
60 #define pd PS.deleted
61 #define ps PS.space
62 #define psl PS.len
63 #define psanl PS.append_newline
64 #define hs HS.space
65 #define hsl HS.len
66
67 static inline int applies(struct s_command *);
68 static void do_tr(struct s_tr *);
69 static void flush_appends(void);
70 static void lputs(char *, size_t);
71 static int regexec_e(regex_t *, const char *, int, int, size_t,
72 size_t);
73 static void regsub(SPACE *, char *, char *);
74 static int substitute(struct s_command *);
75
76 struct s_appends *appends; /* Array of pointers to strings to append. */
77 static unsigned int appendx; /* Index into appends array. */
78 unsigned int appendnum; /* Size of appends array. */
79
80 static int lastaddr; /* Set by applies if last address of a range. */
81 static int sdone; /* If any substitutes since last line input. */
82 /* Iov structure for 'w' commands. */
83 static regex_t *defpreg;
84 size_t maxnsub;
85 regmatch_t *match;
86
87 #define OUT() do { \
88 fwrite(ps, 1, psl, outfile); \
89 if (psanl) fputc('\n', outfile); \
90 } while (0)
91
92 void
process(void)93 process(void)
94 {
95 struct s_command *cp;
96 SPACE tspace;
97 size_t oldpsl;
98 char *p;
99 int oldpsanl;
100
101 p = NULL;
102 oldpsanl = oldpsl = 0;
103
104 for (linenum = 0; mf_fgets(&PS, REPLACE);) {
105 pd = 0;
106 top:
107 cp = prog;
108 redirect:
109 while (cp != NULL) {
110 if (!applies(cp)) {
111 cp = cp->next;
112 continue;
113 }
114 switch (cp->code) {
115 case '{':
116 cp = cp->u.c;
117 goto redirect;
118 case 'a':
119 if (appendx >= appendnum)
120 if ((appends = realloc(appends,
121 sizeof(struct s_appends) *
122 (appendnum *= 2))) == NULL)
123 err(1, "realloc");
124 appends[appendx].type = AP_STRING;
125 appends[appendx].s = cp->t;
126 appends[appendx].len = strlen(cp->t);
127 appendx++;
128 break;
129 case 'b':
130 cp = cp->u.c;
131 goto redirect;
132 case 'c':
133 pd = 1;
134 psl = 0;
135 if (cp->a2 == NULL || lastaddr || lastline())
136 (void)fprintf(outfile, "%s", cp->t);
137 goto new;
138 case 'd':
139 pd = 1;
140 goto new;
141 case 'D':
142 if (pd)
143 goto new;
144 if (psl == 0 ||
145 (p = memchr(ps, '\n', psl)) == NULL) {
146 pd = 1;
147 goto new;
148 } else {
149 psl -= (p + 1) - ps;
150 memmove(ps, p + 1, psl);
151 goto top;
152 }
153 case 'g':
154 cspace(&PS, hs, hsl, REPLACE);
155 break;
156 case 'G':
157 cspace(&PS, "\n", 1, APPEND);
158 cspace(&PS, hs, hsl, APPEND);
159 break;
160 case 'h':
161 cspace(&HS, ps, psl, REPLACE);
162 break;
163 case 'H':
164 cspace(&HS, "\n", 1, APPEND);
165 cspace(&HS, ps, psl, APPEND);
166 break;
167 case 'i':
168 (void)fprintf(outfile, "%s", cp->t);
169 break;
170 case 'l':
171 lputs(ps, psl);
172 break;
173 case 'n':
174 if (!nflag && !pd)
175 OUT();
176 flush_appends();
177 if (!mf_fgets(&PS, REPLACE))
178 exit(0);
179 pd = 0;
180 break;
181 case 'N':
182 flush_appends();
183 cspace(&PS, "\n", 1, APPEND);
184 if (!mf_fgets(&PS, APPEND))
185 exit(0);
186 break;
187 case 'p':
188 if (pd)
189 break;
190 OUT();
191 break;
192 case 'P':
193 if (pd)
194 break;
195 if ((p = memchr(ps, '\n', psl)) != NULL) {
196 oldpsl = psl;
197 oldpsanl = psanl;
198 psl = p - ps;
199 psanl = 1;
200 }
201 OUT();
202 if (p != NULL) {
203 psl = oldpsl;
204 psanl = oldpsanl;
205 }
206 break;
207 case 'q':
208 if (inplace == NULL) {
209 if (!nflag && !pd)
210 OUT();
211 flush_appends();
212 exit(0);
213 }
214 quit = 1;
215 break;
216 case 'r':
217 if (appendx >= appendnum)
218 if ((appends = realloc(appends,
219 sizeof(struct s_appends) *
220 (appendnum *= 2))) == NULL)
221 err(1, "realloc");
222 appends[appendx].type = AP_FILE;
223 appends[appendx].s = cp->t;
224 appends[appendx].len = strlen(cp->t);
225 appendx++;
226 break;
227 case 's':
228 sdone |= substitute(cp);
229 break;
230 case 't':
231 if (sdone) {
232 sdone = 0;
233 cp = cp->u.c;
234 goto redirect;
235 }
236 break;
237 case 'w':
238 if (pd)
239 break;
240 if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
241 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
242 DEFFILEMODE)) == -1)
243 err(1, "%s", cp->t);
244 if (write(cp->u.fd, ps, psl) != (ssize_t)psl ||
245 write(cp->u.fd, "\n", 1) != 1)
246 err(1, "%s", cp->t);
247 break;
248 case 'x':
249 /*
250 * If the hold space is null, make it empty
251 * but not null. Otherwise the pattern space
252 * will become null after the swap, which is
253 * an abnormal condition.
254 */
255 if (hs == NULL)
256 cspace(&HS, "", 0, REPLACE);
257 tspace = PS;
258 PS = HS;
259 psanl = tspace.append_newline;
260 HS = tspace;
261 break;
262 case 'y':
263 if (pd || psl == 0)
264 break;
265 do_tr(cp->u.y);
266 break;
267 case ':':
268 case '}':
269 break;
270 case '=':
271 (void)fprintf(outfile, "%lu\n", linenum);
272 }
273 cp = cp->next;
274 } /* for all cp */
275
276 new: if (!nflag && !pd)
277 OUT();
278 flush_appends();
279 } /* for all lines */
280 }
281
282 /*
283 * TRUE if the address passed matches the current program state
284 * (lastline, linenumber, ps).
285 */
286 #define MATCH(a) \
287 ((a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, 0, psl) : \
288 (a)->type == AT_LINE ? linenum == (a)->u.l : lastline())
289
290 /*
291 * Return TRUE if the command applies to the current line. Sets the start
292 * line for process ranges. Interprets the non-select (``!'') flag.
293 */
294 static inline int
applies(struct s_command * cp)295 applies(struct s_command *cp)
296 {
297 int r;
298
299 lastaddr = 0;
300 if (cp->a1 == NULL && cp->a2 == NULL)
301 r = 1;
302 else if (cp->a2)
303 if (cp->startline > 0) {
304 switch (cp->a2->type) {
305 case AT_RELLINE:
306 if (linenum - cp->startline <= cp->a2->u.l)
307 r = 1;
308 else {
309 cp->startline = 0;
310 r = 0;
311 }
312 break;
313 default:
314 if (MATCH(cp->a2)) {
315 cp->startline = 0;
316 lastaddr = 1;
317 r = 1;
318 } else if (cp->a2->type == AT_LINE &&
319 linenum > cp->a2->u.l) {
320 /*
321 * We missed the 2nd address due to a
322 * branch, so just close the range and
323 * return false.
324 */
325 cp->startline = 0;
326 r = 0;
327 } else
328 r = 1;
329 }
330 } else if (cp->a1 && MATCH(cp->a1)) {
331 /*
332 * If the second address is a number less than or
333 * equal to the line number first selected, only
334 * one line shall be selected.
335 * -- POSIX 1003.2
336 * Likewise if the relative second line address is zero.
337 */
338 if ((cp->a2->type == AT_LINE &&
339 linenum >= cp->a2->u.l) ||
340 (cp->a2->type == AT_RELLINE && cp->a2->u.l == 0))
341 lastaddr = 1;
342 else {
343 cp->startline = linenum;
344 }
345 r = 1;
346 } else
347 r = 0;
348 else
349 r = MATCH(cp->a1);
350 return (cp->nonsel ? ! r : r);
351 }
352
353 /*
354 * Reset the sed processor to its initial state.
355 */
356 void
resetstate(void)357 resetstate(void)
358 {
359 struct s_command *cp;
360
361 /*
362 * Reset all in-range markers.
363 */
364 for (cp = prog; cp; cp = cp->code == '{' ? cp->u.c : cp->next)
365 if (cp->a2)
366 cp->startline = 0;
367
368 /*
369 * Clear out the hold space.
370 */
371 cspace(&HS, "", 0, REPLACE);
372 }
373
374 /*
375 * substitute --
376 * Do substitutions in the pattern space. Currently, we build a
377 * copy of the new pattern space in the substitute space structure
378 * and then swap them.
379 */
380 static int
substitute(struct s_command * cp)381 substitute(struct s_command *cp)
382 {
383 SPACE tspace;
384 regex_t *re;
385 regoff_t slen;
386 int lastempty, n;
387 regoff_t le = 0;
388 char *s;
389
390 s = ps;
391 re = cp->u.s->re;
392 if (re == NULL) {
393 if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
394 linenum = cp->u.s->linenum;
395 errx(1, "%lu: %s: \\%u not defined in the RE",
396 linenum, fname, cp->u.s->maxbref);
397 }
398 }
399 if (!regexec_e(re, ps, 0, 0, 0, psl))
400 return (0);
401
402 SS.len = 0; /* Clean substitute space. */
403 slen = psl;
404 n = cp->u.s->n;
405 lastempty = 1;
406
407 do {
408 /* Copy the leading retained string. */
409 if (n <= 1 && (match[0].rm_so > le))
410 cspace(&SS, s, match[0].rm_so - le, APPEND);
411
412 /* Skip zero-length matches right after other matches. */
413 if (lastempty || (match[0].rm_so - le) ||
414 match[0].rm_so != match[0].rm_eo) {
415 if (n <= 1) {
416 /* Want this match: append replacement. */
417 regsub(&SS, ps, cp->u.s->new);
418 if (n == 1)
419 n = -1;
420 } else {
421 /* Want a later match: append original. */
422 if (match[0].rm_eo - le)
423 cspace(&SS, s, match[0].rm_eo - le,
424 APPEND);
425 n--;
426 }
427 }
428
429 /* Move past this match. */
430 s = ps + match[0].rm_eo;
431 slen = psl - match[0].rm_eo;
432 le = match[0].rm_eo;
433
434 /*
435 * After a zero-length match, advance one byte,
436 * and at the end of the line, terminate.
437 */
438 if (match[0].rm_so == match[0].rm_eo) {
439 if (slen > 0) {
440 cspace(&SS, s++, 1, APPEND);
441 slen--;
442 le++;
443 } else
444 slen = -1;
445 lastempty = 1;
446 } else
447 lastempty = 0;
448
449 } while (n >= 0 && slen >= 0 &&
450 regexec_e(re, ps, REG_NOTBOL, 0, le, psl));
451
452 /* Did not find the requested number of matches. */
453 if (n > 0)
454 return (0);
455
456 /* Copy the trailing retained string. */
457 if (slen > 0)
458 cspace(&SS, s, slen, APPEND);
459
460 /*
461 * Swap the substitute space and the pattern space, and make sure
462 * that any leftover pointers into stdio memory get lost.
463 */
464 tspace = PS;
465 PS = SS;
466 psanl = tspace.append_newline;
467 SS = tspace;
468 SS.space = SS.back;
469
470 /* Handle the 'p' flag. */
471 if (cp->u.s->p)
472 OUT();
473
474 /* Handle the 'w' flag. */
475 if (cp->u.s->wfile && !pd) {
476 if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
477 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
478 err(1, "%s", cp->u.s->wfile);
479 if (write(cp->u.s->wfd, ps, psl) != (ssize_t)psl ||
480 write(cp->u.s->wfd, "\n", 1) != 1)
481 err(1, "%s", cp->u.s->wfile);
482 }
483 return (1);
484 }
485
486 /*
487 * do_tr --
488 * Perform translation ('y' command) in the pattern space.
489 */
490 static void
do_tr(struct s_tr * y)491 do_tr(struct s_tr *y)
492 {
493 SPACE tmp;
494 char c, *p;
495 size_t clen, left;
496 int i;
497
498 if (MB_CUR_MAX == 1) {
499 /*
500 * Single-byte encoding: perform in-place translation
501 * of the pattern space.
502 */
503 for (p = ps; p < &ps[psl]; p++)
504 *p = y->bytetab[(u_char)*p];
505 } else {
506 /*
507 * Multi-byte encoding: perform translation into the
508 * translation space, then swap the translation and
509 * pattern spaces.
510 */
511 /* Clean translation space. */
512 YS.len = 0;
513 for (p = ps, left = psl; left > 0; p += clen, left -= clen) {
514 if ((c = y->bytetab[(u_char)*p]) != '\0') {
515 cspace(&YS, &c, 1, APPEND);
516 clen = 1;
517 continue;
518 }
519 for (i = 0; i < y->nmultis; i++)
520 if (left >= y->multis[i].fromlen &&
521 memcmp(p, y->multis[i].from,
522 y->multis[i].fromlen) == 0)
523 break;
524 if (i < y->nmultis) {
525 cspace(&YS, y->multis[i].to,
526 y->multis[i].tolen, APPEND);
527 clen = y->multis[i].fromlen;
528 } else {
529 cspace(&YS, p, 1, APPEND);
530 clen = 1;
531 }
532 }
533 /* Swap the translation space and the pattern space. */
534 tmp = PS;
535 PS = YS;
536 psanl = tmp.append_newline;
537 YS = tmp;
538 YS.space = YS.back;
539 }
540 }
541
542 /*
543 * Flush append requests. Always called before reading a line,
544 * therefore it also resets the substitution done (sdone) flag.
545 */
546 static void
flush_appends(void)547 flush_appends(void)
548 {
549 FILE *f;
550 unsigned int count, idx;
551 char buf[8 * 1024];
552
553 for (idx = 0; idx < appendx; idx++)
554 switch (appends[idx].type) {
555 case AP_STRING:
556 fwrite(appends[idx].s, sizeof(char), appends[idx].len,
557 outfile);
558 break;
559 case AP_FILE:
560 /*
561 * Read files probably shouldn't be cached. Since
562 * it's not an error to read a non-existent file,
563 * it's possible that another program is interacting
564 * with the sed script through the filesystem. It
565 * would be truly bizarre, but possible. It's probably
566 * not that big a performance win, anyhow.
567 */
568 if ((f = fopen(appends[idx].s, "r")) == NULL)
569 break;
570 while ((count = fread(buf, sizeof(char), sizeof(buf), f)))
571 (void)fwrite(buf, sizeof(char), count, outfile);
572 (void)fclose(f);
573 break;
574 }
575 if (ferror(outfile))
576 errx(1, "%s: %s", outfname, strerror(errno ? errno : EIO));
577 appendx = sdone = 0;
578 }
579
580 static void
lputs(char * s,size_t len)581 lputs(char *s, size_t len)
582 {
583 static const char escapes[] = "\\\a\b\f\r\t\v";
584 int c, col, width;
585 const char *p;
586 struct winsize win;
587 static int termwidth = -1;
588 size_t clen, i;
589 wchar_t wc;
590 mbstate_t mbs;
591
592 if (outfile != stdout)
593 termwidth = 60;
594 if (termwidth == -1) {
595 if ((p = getenv("COLUMNS")) && *p != '\0')
596 termwidth = atoi(p);
597 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
598 win.ws_col > 0)
599 termwidth = win.ws_col;
600 else
601 termwidth = 60;
602 }
603 if (termwidth <= 0)
604 termwidth = 1;
605
606 memset(&mbs, 0, sizeof(mbs));
607 col = 0;
608 while (len != 0) {
609 clen = mbrtowc(&wc, s, len, &mbs);
610 if (clen == 0)
611 clen = 1;
612 if (clen == (size_t)-1 || clen == (size_t)-2) {
613 wc = (unsigned char)*s;
614 clen = 1;
615 memset(&mbs, 0, sizeof(mbs));
616 }
617 if (wc == '\n') {
618 if (col + 1 >= termwidth)
619 fprintf(outfile, "\\\n");
620 fputc('$', outfile);
621 fputc('\n', outfile);
622 col = 0;
623 } else if (iswprint(wc)) {
624 width = wcwidth(wc);
625 if (col + width >= termwidth) {
626 fprintf(outfile, "\\\n");
627 col = 0;
628 }
629 fwrite(s, 1, clen, outfile);
630 col += width;
631 } else if (wc != L'\0' && (c = wctob(wc)) != EOF &&
632 (p = strchr(escapes, c)) != NULL) {
633 if (col + 2 >= termwidth) {
634 fprintf(outfile, "\\\n");
635 col = 0;
636 }
637 fprintf(outfile, "\\%c", "\\abfrtv"[p - escapes]);
638 col += 2;
639 } else {
640 if (col + 4 * clen >= (unsigned)termwidth) {
641 fprintf(outfile, "\\\n");
642 col = 0;
643 }
644 for (i = 0; i < clen; i++)
645 fprintf(outfile, "\\%03o",
646 (int)(unsigned char)s[i]);
647 col += 4 * clen;
648 }
649 s += clen;
650 len -= clen;
651 }
652 if (col + 1 >= termwidth)
653 fprintf(outfile, "\\\n");
654 (void)fputc('$', outfile);
655 (void)fputc('\n', outfile);
656 if (ferror(outfile))
657 errx(1, "%s: %s", outfname, strerror(errno ? errno : EIO));
658 }
659
660 static int
regexec_e(regex_t * preg,const char * string,int eflags,int nomatch,size_t start,size_t stop)661 regexec_e(regex_t *preg, const char *string, int eflags, int nomatch,
662 size_t start, size_t stop)
663 {
664 int eval;
665
666 if (preg == NULL) {
667 if (defpreg == NULL)
668 errx(1, "first RE may not be empty");
669 } else
670 defpreg = preg;
671
672 /* Set anchors */
673 match[0].rm_so = start;
674 match[0].rm_eo = stop;
675
676 eval = regexec(defpreg, string,
677 nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
678 switch(eval) {
679 case 0:
680 return (1);
681 case REG_NOMATCH:
682 return (0);
683 }
684 errx(1, "RE error: %s", strregerror(eval, defpreg));
685 /* NOTREACHED */
686 }
687
688 /*
689 * regsub - perform substitutions after a regexp match
690 * Based on a routine by Henry Spencer
691 */
692 static void
regsub(SPACE * sp,char * string,char * src)693 regsub(SPACE *sp, char *string, char *src)
694 {
695 int len, no;
696 char c, *dst;
697
698 #define NEEDSP(reqlen) \
699 /* XXX What is the +1 for? */ \
700 if (sp->len + (reqlen) + 1 >= sp->blen) { \
701 sp->blen += (reqlen) + 1024; \
702 if ((sp->space = sp->back = realloc(sp->back, sp->blen)) \
703 == NULL) \
704 err(1, "realloc"); \
705 dst = sp->space + sp->len; \
706 }
707
708 dst = sp->space + sp->len;
709 while ((c = *src++) != '\0') {
710 if (c == '&')
711 no = 0;
712 else if (c == '\\' && isdigit((unsigned char)*src))
713 no = *src++ - '0';
714 else
715 no = -1;
716 if (no < 0) { /* Ordinary character. */
717 if (c == '\\' && (*src == '\\' || *src == '&'))
718 c = *src++;
719 NEEDSP(1);
720 *dst++ = c;
721 ++sp->len;
722 } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
723 len = match[no].rm_eo - match[no].rm_so;
724 NEEDSP(len);
725 memmove(dst, string + match[no].rm_so, len);
726 dst += len;
727 sp->len += len;
728 }
729 }
730 NEEDSP(1);
731 *dst = '\0';
732 }
733
734 /*
735 * cspace --
736 * Concatenate space: append the source space to the destination space,
737 * allocating new space as necessary.
738 */
739 void
cspace(SPACE * sp,const char * p,size_t len,enum e_spflag spflag)740 cspace(SPACE *sp, const char *p, size_t len, enum e_spflag spflag)
741 {
742 size_t tlen;
743
744 /* Make sure SPACE has enough memory and ramp up quickly. */
745 tlen = sp->len + len + 1;
746 if (tlen > sp->blen) {
747 sp->blen = tlen + 1024;
748 if ((sp->space = sp->back = realloc(sp->back, sp->blen)) ==
749 NULL)
750 err(1, "realloc");
751 }
752
753 if (spflag == REPLACE)
754 sp->len = 0;
755
756 memmove(sp->space + sp->len, p, len);
757
758 sp->space[sp->len += len] = '\0';
759 }
760
761 /*
762 * Close all cached opened files and report any errors
763 */
764 void
cfclose(struct s_command * cp,struct s_command * end)765 cfclose(struct s_command *cp, struct s_command *end)
766 {
767
768 for (; cp != end; cp = cp->next)
769 switch(cp->code) {
770 case 's':
771 if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
772 err(1, "%s", cp->u.s->wfile);
773 cp->u.s->wfd = -1;
774 break;
775 case 'w':
776 if (cp->u.fd != -1 && close(cp->u.fd))
777 err(1, "%s", cp->t);
778 cp->u.fd = -1;
779 break;
780 case '{':
781 cfclose(cp->u.c, cp->next);
782 break;
783 }
784 }
785