2 * io.c --- routines for dealing with input and output and records
6 * Copyright (C) 1976, 1988, 1989, 1991-2000 the Free Software Foundation, Inc.
8 * This file is part of GAWK, the GNU implementation of the
9 * AWK Programming Language.
11 * GAWK is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GAWK is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
25 * $FreeBSD: src/contrib/awk/io.c,v 1.4.2.1 2001/01/23 22:08:31 asmodai Exp $
26 * $DragonFly: src/contrib/awk/Attic/io.c,v 1.2 2003/06/17 04:23:58 dillon Exp $
30 #undef HAVE_MMAP /* for now, probably forever */
32 #ifdef HAVE_SYS_PARAM_H
33 #undef RE_DUP_MAX /* avoid spurious conflict w/regex.h */
34 #include <sys/param.h>
35 #endif /* HAVE_SYS_PARAM_H */
37 #ifdef HAVE_SYS_WAIT_H
39 #endif /* HAVE_SYS_WAIT_H */
44 #define MAP_FAILED ((caddr_t) -1)
45 #endif /* ! defined (MAP_FAILED) */
46 #endif /* HAVE_MMAP */
52 #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
55 #if ! defined(S_ISREG) && defined(S_IFREG)
56 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
59 #if ! defined(S_ISDIR) && defined(S_IFDIR)
60 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
71 #if defined(MSDOS) || defined(OS2) || defined(WIN32)
72 #define PIPES_SIMULATED
75 static IOBUF *nextfile P((int skipping));
76 static int inrec P((IOBUF *iop));
77 static int iop_close P((IOBUF *iop));
78 struct redirect *redirect P((NODE *tree, int *errflg));
79 static void close_one P((void));
80 static int close_redir P((struct redirect *rp, int exitwarn));
81 #ifndef PIPES_SIMULATED
82 static int wait_any P((int interesting));
84 static IOBUF *gawk_popen P((char *cmd, struct redirect *rp));
85 static IOBUF *iop_open P((const char *file, const char *how, IOBUF *buf));
86 static IOBUF *iop_alloc P((int fd, const char *name, IOBUF *buf));
87 static int gawk_pclose P((struct redirect *rp));
88 static int do_pathopen P((const char *file));
89 static int get_a_record P((char **out, IOBUF *iop, int rs, Regexp *RSre, int *errcode));
91 static int mmap_get_record P((char **out, IOBUF *iop, int rs, Regexp *RSre, int *errcode));
92 #endif /* HAVE_MMAP */
93 static int str2mode P((const char *mode));
94 static void spec_setup P((IOBUF *iop, int len, int allocate));
95 static int specfdopen P((IOBUF *iop, const char *name, const char *mode));
96 static int pidopen P((IOBUF *iop, const char *name, const char *mode));
97 static int useropen P((IOBUF *iop, const char *name, const char *mode));
99 #if defined (HAVE_POPEN_H)
103 static struct redirect *red_head = NULL;
105 static Regexp *RS_regexp;
109 extern int output_is_tty;
110 extern NODE *ARGC_node;
111 extern NODE *ARGV_node;
112 extern NODE *ARGIND_node;
113 extern NODE *ERRNO_node;
114 extern NODE **fields_arr;
116 static jmp_buf filebuf; /* for do_nextfile() */
119 /* File pointers have an extra level of indirection, and there are cases where
120 `stdin' can be null. That can crash gawk if fileno() is used as-is. */
121 static int vmsrtl_fileno P((FILE *));
122 static int vmsrtl_fileno(fp) FILE *fp; { return fileno(fp); }
124 #define fileno(FP) (((FP) && *(FP)) ? vmsrtl_fileno(FP) : -1)
127 /* do_nextfile --- implement gawk "nextfile" extension */
132 (void) nextfile(TRUE);
136 /* nextfile --- move to the next input data file */
143 static int files = 0;
145 static IOBUF *curfile = NULL;
155 if (curfile != NULL) {
156 if (curfile->cnt == EOF) {
157 (void) iop_close(curfile);
162 for (; i < (long) (ARGC_node->lnode->numbr); i++) {
163 arg = *assoc_lookup(ARGV_node, tmp_number((AWKNUM) i));
166 arg->stptr[arg->stlen] = '\0';
167 if (! do_traditional) {
168 unref(ARGIND_node->var_value);
169 ARGIND_node->var_value = make_number((AWKNUM) i);
171 if (! arg_assign(arg->stptr)) {
174 curfile = iop_open(fname, "r", &mybuf);
177 curfile->flag |= IOP_NOFREE_OBJ;
178 /* This is a kludge. */
179 unref(FILENAME_node->var_value);
180 FILENAME_node->var_value = dupnode(arg);
188 /* no args. -- use stdin */
189 /* FNR is init'ed to 0 */
190 FILENAME_node->var_value = make_string("-", 1);
192 curfile = iop_open(fname, "r", &mybuf);
195 curfile->flag |= IOP_NOFREE_OBJ;
200 fatal("cannot open file `%s' for reading (%s)",
201 fname, strerror(errno));
206 /* set_FNR --- update internal FNR from awk variable */
211 FNR = (long) FNR_node->var_value->numbr;
214 /* set_NR --- update internal NR from awk variable */
219 NR = (long) NR_node->var_value->numbr;
222 /* inrec --- This reads in a record from the input file */
232 if ((cnt = iop->cnt) != EOF)
233 cnt = (*(iop->getrec))
234 (&begin, iop, RS->stptr[0], RS_regexp, NULL);
241 set_record(begin, cnt, TRUE);
247 /* iop_close --- close an open IOP */
260 /* Work around bug in UNICOS popen */
265 /* save these for re-use; don't free the storage */
266 if ((iop->flag & IOP_IS_INTERNAL) != 0) {
268 iop->end = iop->buf + strlen(iop->buf);
274 /* Don't close standard files or else crufty code elsewhere will lose */
275 if (iop->fd == fileno(stdin)
276 || iop->fd == fileno(stdout)
277 || iop->fd == fileno(stderr)
278 || (iop->flag & IOP_MMAPPED) != 0)
281 ret = close(iop->fd);
284 warning("close of fd %d (`%s') failed (%s)", iop->fd,
285 iop->name, strerror(errno));
286 if ((iop->flag & IOP_NO_FREE) == 0) {
288 * Be careful -- $0 may still reference the buffer even though
289 * an explicit close is being done; in the future, maybe we
290 * can do this a bit better.
293 if ((fields_arr[0]->stptr >= iop->buf)
294 && (fields_arr[0]->stptr < (iop->buf + iop->secsiz + iop->size))) {
297 t = make_string(fields_arr[0]->stptr,
298 fields_arr[0]->stlen);
299 unref(fields_arr[0]);
303 if ((iop->flag & IOP_MMAPPED) == 0)
307 (void) munmap(iop->buf, iop->size);
310 if ((iop->flag & IOP_NOFREE_OBJ) == 0)
313 return ret == -1 ? 1 : 0;
316 /* do_input --- the main input processing loop */
324 (void) setjmp(filebuf); /* for `nextfile' */
326 while ((iop = nextfile(FALSE)) != NULL) {
328 while (interpret(expression_value) && inrec(iop) == 0)
331 /* recover any space from C based alloca */
339 /* redirect --- Redirection for printf and print commands */
342 redirect(tree, errflg)
347 register struct redirect *rp;
351 const char *direction = "to";
354 const char *what = NULL;
356 switch (tree->type) {
357 case Node_redirect_append:
360 case Node_redirect_output:
361 outflag = (RED_FILE|RED_WRITE);
363 if (tree->type == Node_redirect_output)
368 case Node_redirect_pipe:
369 tflag = (RED_PIPE|RED_WRITE);
372 case Node_redirect_pipein:
373 tflag = (RED_PIPE|RED_READ);
376 case Node_redirect_input:
377 tflag = (RED_FILE|RED_READ);
381 fatal("invalid tree type %d in redirect()", tree->type);
384 tmp = tree_eval(tree->subnode);
385 if (do_lint && (tmp->flags & STR) == 0)
386 warning("expression in `%s' redirection only has numeric value",
388 tmp = force_string(tmp);
391 if (str == NULL || *str == '\0')
392 fatal("expression for `%s' redirection has null string value",
396 && (STREQN(str, "0", tmp->stlen) || STREQN(str, "1", tmp->stlen)))
397 warning("filename `%s' for `%s' redirection may be result of logical expression", str, what);
398 for (rp = red_head; rp != NULL; rp = rp->next)
399 if (strlen(rp->value) == tmp->stlen
400 && STREQN(rp->value, str, tmp->stlen)
401 && ((rp->flag & ~(RED_NOBUF|RED_EOF)) == tflag
403 && (rp->flag & (RED_FILE|RED_WRITE)) == outflag)))
406 emalloc(rp, struct redirect *, sizeof(struct redirect),
408 emalloc(str, char *, tmp->stlen+1, "redirect");
409 memcpy(str, tmp->stptr, tmp->stlen);
410 str[tmp->stlen] = '\0';
415 rp->pid = 0; /* unlikely that we're worried about init */
417 /* maintain list in most-recently-used first order */
418 if (red_head != NULL)
424 str = rp->value; /* get \0 terminated string */
425 while (rp->fp == NULL && rp->iop == NULL) {
426 if (rp->flag & RED_EOF)
428 * encountered EOF on file or pipe -- must be cleared
429 * by explicit close() before reading more
434 switch (tree->type) {
435 case Node_redirect_output:
437 if ((rp->flag & RED_USED) != 0)
440 case Node_redirect_append:
443 case Node_redirect_pipe:
444 /* synchronize output before new pipe */
447 if ((rp->fp = popen(str, "w")) == NULL)
448 fatal("can't open pipe (\"%s\") for output (%s)",
449 str, strerror(errno));
450 rp->flag |= RED_NOBUF;
452 case Node_redirect_pipein:
454 if (gawk_popen(str, rp) == NULL)
455 fatal("can't open pipe (\"%s\") for input (%s)",
456 str, strerror(errno));
458 case Node_redirect_input:
460 rp->iop = iop_open(str, "r", NULL);
467 fd = devopen(str, mode);
468 if (fd > INVALID_HANDLE) {
469 if (fd == fileno(stdin))
471 else if (fd == fileno(stdout))
473 else if (fd == fileno(stderr))
476 rp->fp = fdopen(fd, (char *) mode);
477 /* don't leak file descriptors */
481 if (rp->fp != NULL && isatty(fd))
482 rp->flag |= RED_NOBUF;
483 /* Move rp to the head of the list. */
484 if (red_head != rp) {
485 if ((rp->prev->next = rp->next) != NULL)
486 rp->next->prev = rp->prev;
494 if (rp->fp == NULL && rp->iop == NULL) {
495 /* too many files open -- close one and try again */
496 if (errno == EMFILE || errno == ENFILE)
498 #if defined __MINGW32__ || defined HAVE_MMAP
499 /* this works for solaris 2.5, not sunos */
500 /* it is also needed for MINGW32 */
501 else if (errno == 0) /* HACK! */
505 /* Alpha/VMS V7.1's C RTL is returning this instead
506 of EMFILE (haven't tried other post-V6.2 systems) */
507 #define SS$_EXQUOTA 0x001C
508 else if (errno == EIO && vaxc$errno == SS$_EXQUOTA)
513 * Some other reason for failure.
515 * On redirection of input from a file,
516 * just return an error, so e.g. getline
517 * can return -1. For output to file,
518 * complain. The shell will complain on
519 * a bad command to a pipe.
523 if (tree->type == Node_redirect_output
524 || tree->type == Node_redirect_append)
525 fatal("can't redirect %s `%s' (%s)",
526 direction, str, strerror(errno));
538 /* getredirect --- find the struct redirect for this file or pipe */
541 getredirect(str, len)
547 for (rp = red_head; rp != NULL; rp = rp->next)
548 if (strlen(rp->value) == len && STREQN(rp->value, str, len))
554 /* close_one --- temporarily close an open file to re-use the fd */
559 register struct redirect *rp;
560 register struct redirect *rplast = NULL;
562 /* go to end of list first, to pick up least recently used entry */
563 for (rp = red_head; rp != NULL; rp = rp->next)
565 /* now work back up through the list */
566 for (rp = rplast; rp != NULL; rp = rp->prev)
567 if (rp->fp != NULL && (rp->flag & RED_FILE) != 0) {
568 rp->flag |= RED_USED;
570 if (/* do_lint && */ fclose(rp->fp) != 0)
571 warning("close of \"%s\" failed (%s).",
572 rp->value, strerror(errno));
577 /* surely this is the only reason ??? */
578 fatal("too many pipes or input files open");
581 /* do_close --- completely close an open file or pipe */
588 register struct redirect *rp;
590 tmp = force_string(tree_eval(tree->subnode));
592 for (rp = red_head; rp != NULL; rp = rp->next) {
593 if (strlen(rp->value) == tmp->stlen
594 && STREQN(rp->value, tmp->stptr, tmp->stlen))
598 if (rp == NULL) { /* no match */
599 /* icky special case: close(FILENAME) called. */
600 if (tree->subnode == FILENAME_node
601 || (tmp->stlen == FILENAME_node->var_value->stlen
602 && STREQN(tmp->stptr, FILENAME_node->var_value->stptr, tmp->stlen))) {
603 (void) nextfile(TRUE);
605 warning("close: `%.*s' is not an open file or pipe",
606 tmp->stlen, tmp->stptr);
609 return tmp_number((AWKNUM) 0.0);
612 fflush(stdout); /* synchronize regular output */
613 tmp = tmp_number((AWKNUM) close_redir(rp, FALSE));
618 /* close_redir --- close an open file or pipe */
621 close_redir(rp, exitwarn)
622 register struct redirect *rp;
630 if (rp->fp == stdout || rp->fp == stderr)
633 if ((rp->flag & (RED_PIPE|RED_WRITE)) == (RED_PIPE|RED_WRITE))
634 status = pclose(rp->fp);
635 else if (rp->fp != NULL)
636 status = fclose(rp->fp);
637 else if (rp->iop != NULL) {
638 if ((rp->flag & RED_PIPE) != 0)
639 status = gawk_pclose(rp);
641 status = iop_close(rp->iop);
646 what = ((rp->flag & RED_PIPE) != 0) ? "pipe" : "file";
648 /* SVR4 awk checks and warns about status of close */
650 char *s = strerror(errno);
653 * Too many people have complained about this.
654 * As of 2.15.6, it is now under lint control.
657 warning("failure status (%d) on %s close of \"%s\" (%s)",
658 status, what, rp->value, s);
660 if (! do_traditional) {
661 /* set ERRNO too so that program can get at it */
662 unref(ERRNO_node->var_value);
663 ERRNO_node->var_value = make_string(s, strlen(s));
668 warning("no explicit close of %s `%s' provided",
671 if (rp->next != NULL)
672 rp->next->prev = rp->prev;
673 if (rp->prev != NULL)
674 rp->prev->next = rp->next;
682 /* flush_io --- flush all open output files */
687 register struct redirect *rp;
691 if (fflush(stdout)) {
692 warning("error writing standard output (%s)", strerror(errno));
695 if (fflush(stderr)) {
696 warning("error writing standard error (%s)", strerror(errno));
699 for (rp = red_head; rp != NULL; rp = rp->next)
700 /* flush both files and pipes, what the heck */
701 if ((rp->flag & RED_WRITE) && rp->fp != NULL) {
702 if (fflush(rp->fp)) {
703 warning("%s flush of \"%s\" failed (%s).",
704 (rp->flag & RED_PIPE) ? "pipe" :
705 "file", rp->value, strerror(errno));
712 /* close_io --- close all open files, called when exiting */
717 register struct redirect *rp;
718 register struct redirect *next;
722 for (rp = red_head; rp != NULL; rp = next) {
725 * close_redir() will print a message if needed
726 * if do_lint, warn about lack of explicit close
728 if (close_redir(rp, do_lint))
733 * Some of the non-Unix os's have problems doing an fclose
734 * on stdout and stderr. Since we don't really need to close
735 * them, we just flush them, and do that across the board.
737 if (fflush(stdout)) {
738 warning("error writing standard output (%s)", strerror(errno));
741 if (fflush(stderr)) {
742 warning("error writing standard error (%s)", strerror(errno));
748 /* str2mode --- convert a string mode to an integer mode */
762 ret = O_WRONLY|O_CREAT|O_TRUNC;
766 ret = O_WRONLY|O_APPEND|O_CREAT;
776 /* devopen --- handle /dev/std{in,out,err}, /dev/fd/N, regular files */
779 * This separate version is still needed for output, since file and pipe
780 * output is done with stdio. iop_open() handles input with IOBUFs of
781 * more "special" files. Those files are not handled here since it makes
782 * no sense to use them for output.
787 const char *name, *mode;
794 extern double strtod();
796 flag = str2mode(mode);
798 if (STREQ(name, "-"))
799 openfd = fileno(stdin);
801 openfd = INVALID_HANDLE;
806 if ((openfd = os_devopen(name, flag)) >= 0)
809 if (STREQN(name, "/dev/", 5) && stat((char *) name, &buf) == -1) {
812 if (STREQ(cp, "stdin") && (flag & O_ACCMODE) == O_RDONLY)
813 openfd = fileno(stdin);
814 else if (STREQ(cp, "stdout") && (flag & O_ACCMODE) == O_WRONLY)
815 openfd = fileno(stdout);
816 else if (STREQ(cp, "stderr") && (flag & O_ACCMODE) == O_WRONLY)
817 openfd = fileno(stderr);
818 else if (STREQN(cp, "fd/", 3)) {
820 openfd = (int) strtod(cp, &ptr);
821 if (openfd <= INVALID_HANDLE || ptr == cp)
822 openfd = INVALID_HANDLE;
827 if (openfd == INVALID_HANDLE)
828 openfd = open(name, flag, 0666);
829 if (openfd != INVALID_HANDLE && fstat(openfd, &buf) > 0)
830 if (S_ISDIR(buf.st_mode))
831 fatal("file `%s' is a directory", name);
836 /* spec_setup --- setup an IOBUF for a special internal file */
839 spec_setup(iop, len, allocate)
847 emalloc(cp, char *, len+2, "spec_setup");
850 len = strlen(iop->buf);
851 iop->buf[len++] = '\n'; /* get_a_record clobbered it */
852 iop->buf[len] = '\0'; /* just in case */
858 iop->end = iop->buf + len;
860 iop->flag = IOP_IS_INTERNAL;
861 iop->getrec = get_a_record;
864 /* specfdopen --- open an fd special file */
867 specfdopen(iop, name, mode)
869 const char *name, *mode;
874 fd = devopen(name, mode);
875 if (fd == INVALID_HANDLE)
876 return INVALID_HANDLE;
877 tp = iop_alloc(fd, name, NULL);
879 /* don't leak fd's */
881 return INVALID_HANDLE;
884 iop->flag |= IOP_NO_FREE;
890 #define getpgrp_arg() /* nothing */
892 #define getpgrp_arg() getpid()
895 /* pidopen --- "open" /dev/pid, /dev/ppid, and /dev/pgrpid */
898 pidopen(iop, name, mode)
900 const char *name, *mode;
906 sprintf(tbuf, "%d\n", (int) getpgrp(getpgrp_arg()));
907 else if (name[6] == 'i')
908 sprintf(tbuf, "%d\n", (int) getpid());
910 sprintf(tbuf, "%d\n", (int) getppid());
912 spec_setup(iop, i, TRUE);
913 strcpy(iop->buf, tbuf);
917 /* useropen --- "open" /dev/user */
920 * /dev/user creates a record as follows:
925 * If multiple groups are supported, then $5 through $NF are the
926 * supplementary group set.
930 useropen(iop, name, mode)
932 const char *name, *mode;
934 char tbuf[BUFSIZ], *cp;
936 #if defined(NGROUPS_MAX) && NGROUPS_MAX > 0
937 GETGROUPS_T groupset[NGROUPS_MAX];
941 sprintf(tbuf, "%d %d %d %d", (int) getuid(), (int) geteuid(), (int) getgid(), (int) getegid());
943 cp = tbuf + strlen(tbuf);
944 #if defined(NGROUPS_MAX) && NGROUPS_MAX > 0
945 ngroups = getgroups(NGROUPS_MAX, groupset);
947 fatal("could not find groups: %s", strerror(errno));
949 for (i = 0; i < ngroups; i++) {
951 sprintf(cp, "%d", (int) groupset[i]);
959 spec_setup(iop, i, TRUE);
960 strcpy(iop->buf, tbuf);
964 /* iop_open --- handle special and regular files for input */
967 iop_open(name, mode, iop)
968 const char *name, *mode;
971 int openfd = INVALID_HANDLE;
974 static struct internal {
977 int (*fp) P((IOBUF *, const char *, const char *));
980 { "/dev/fd/", 8, specfdopen },
981 { "/dev/stdin", 10, specfdopen },
982 { "/dev/stdout", 11, specfdopen },
983 { "/dev/stderr", 11, specfdopen },
984 { "/dev/pid", 8, pidopen },
985 { "/dev/ppid", 9, pidopen },
986 { "/dev/pgrpid", 11, pidopen },
987 { "/dev/user", 9, useropen },
989 int devcount = sizeof(table) / sizeof(table[0]);
991 flag = str2mode(mode);
994 * FIXME: remove the stat call, and always process these files
997 if (STREQ(name, "-"))
998 openfd = fileno(stdin);
999 else if (do_traditional)
1001 else if (STREQN(name, "/dev/", 5) && stat((char *) name, &buf) == -1) {
1004 for (i = 0; i < devcount; i++) {
1005 if (STREQN(name, table[i].name, table[i].compare)) {
1006 iop = & table[i].iob;
1008 if (iop->buf != NULL) {
1009 spec_setup(iop, 0, FALSE);
1011 } else if ((*table[i].fp)(iop, name, mode) == 0)
1014 warning("could not open %s, mode `%s'",
1023 if (openfd == INVALID_HANDLE)
1024 openfd = open(name, flag, 0666);
1025 if (openfd != INVALID_HANDLE && fstat(openfd, &buf) > 0)
1026 if (S_ISDIR(buf.st_mode))
1027 fatal("file `%s' is a directory", name);
1028 return iop_alloc(openfd, name, iop);
1031 #ifndef PIPES_SIMULATED /* real pipes */
1033 /* wait_any --- wait for a child process, close associated pipe */
1036 wait_any(interesting)
1037 int interesting; /* pid of interest, if any */
1039 RETSIGTYPE (*hstat)(), (*istat)(), (*qstat)();
1042 struct redirect *redp;
1045 hstat = signal(SIGHUP, SIG_IGN);
1046 istat = signal(SIGINT, SIG_IGN);
1047 qstat = signal(SIGQUIT, SIG_IGN);
1049 #ifdef HAVE_SYS_WAIT_H /* Posix compatible sys/wait.h */
1050 pid = wait(&status);
1052 pid = wait((union wait *)&status);
1054 if (interesting && pid == interesting) {
1056 } else if (pid != -1) {
1057 for (redp = red_head; redp != NULL; redp = redp->next)
1058 if (pid == redp->pid) {
1060 redp->status = status;
1064 if (pid == -1 && errno == ECHILD)
1067 signal(SIGHUP, hstat);
1068 signal(SIGINT, istat);
1069 signal(SIGQUIT, qstat);
1073 /* gawk_popen --- open an IOBUF on a child process */
1078 struct redirect *rp;
1084 * used to wait for any children to synchronize input and output,
1085 * but this could cause gawk to hang when it is started in a pipeline
1086 * and thus has a child process feeding it input (shell dependant)
1088 /*(void) wait_any(0);*/ /* wait for outstanding processes */
1091 fatal("cannot open pipe \"%s\" (%s)", cmd, strerror(errno));
1092 if ((pid = fork()) == 0) {
1094 fatal("close of stdout in child failed (%s)",
1097 fatal("dup of pipe failed (%s)", strerror(errno));
1098 if (close(p[0]) == -1 || close(p[1]) == -1)
1099 fatal("close of pipe failed (%s)", strerror(errno));
1100 execl("/bin/sh", "sh", "-c", cmd, NULL);
1104 fatal("cannot fork for \"%s\" (%s)", cmd, strerror(errno));
1106 if (close(p[1]) == -1)
1107 fatal("close of pipe failed (%s)", strerror(errno));
1108 rp->iop = iop_alloc(p[0], cmd, NULL);
1109 if (rp->iop == NULL)
1114 /* gawk_pclose --- close an open child pipe */
1118 struct redirect *rp;
1120 (void) iop_close(rp->iop);
1123 /* process previously found, return stored status */
1125 return (rp->status >> 8) & 0xFF;
1126 rp->status = wait_any(rp->pid);
1128 return (rp->status >> 8) & 0xFF;
1131 #else /* PIPES_SIMULATED */
1134 * use temporary file rather than pipe
1135 * except if popen() provides real pipes too
1138 #if defined(VMS) || defined(OS2) || defined (MSDOS) || defined(WIN32)
1140 /* gawk_popen --- open an IOBUF on a child process */
1145 struct redirect *rp;
1149 if ((current = popen(cmd, "r")) == NULL)
1151 rp->iop = iop_alloc(fileno(current), cmd, NULL);
1152 if (rp->iop == NULL) {
1153 (void) pclose(current);
1160 /* gawk_pclose --- close an open child pipe */
1164 struct redirect *rp;
1166 int rval, aval, fd = rp->iop->fd;
1168 rp->iop->fd = dup(fd); /* kludge to allow close() + pclose() */
1169 rval = iop_close(rp->iop);
1171 aval = pclose(rp->ifp);
1173 return (rval < 0 ? rval : aval);
1175 #else /* not (VMS || OS2 || MSDOS) */
1177 static struct pipeinfo {
1182 /* gawk_popen --- open an IOBUF on a child process */
1187 struct redirect *rp;
1189 extern char *strdup P((const char *));
1192 static char cmdbuf[256];
1194 /* get a name to use */
1195 if ((name = tempnam(".", "pip")) == NULL)
1197 sprintf(cmdbuf, "%s > %s", cmd, name);
1199 if ((current = open(name, O_RDONLY)) == INVALID_HANDLE)
1201 pipes[current].name = name;
1202 pipes[current].command = strdup(cmd);
1203 rp->iop = iop_alloc(current, name, NULL);
1204 if (rp->iop == NULL)
1205 (void) close(current);
1209 /* gawk_pclose --- close an open child pipe */
1213 struct redirect *rp;
1215 int cur = rp->iop->fd;
1218 rval = iop_close(rp->iop);
1221 /* check for an open file */
1222 if (pipes[cur].name == NULL)
1224 unlink(pipes[cur].name);
1225 free(pipes[cur].name);
1226 pipes[cur].name = NULL;
1227 free(pipes[cur].command);
1230 #endif /* not (VMS || OS2 || MSDOS) */
1232 #endif /* PIPES_SIMULATED */
1234 /* do_getline --- read in a line, into var and with redirection, as needed */
1240 struct redirect *rp = NULL;
1246 while (cnt == EOF) {
1247 if (tree->rnode == NULL) { /* no redirection */
1248 iop = nextfile(FALSE);
1249 if (iop == NULL) /* end of input */
1250 return tmp_number((AWKNUM) 0.0);
1252 int redir_error = 0;
1254 rp = redirect(tree->rnode, &redir_error);
1255 if (rp == NULL && redir_error) { /* failed redirect */
1256 if (! do_traditional) {
1257 s = strerror(redir_error);
1259 unref(ERRNO_node->var_value);
1260 ERRNO_node->var_value =
1261 make_string(s, strlen(s));
1263 return tmp_number((AWKNUM) -1.0);
1266 if (iop == NULL) /* end of input */
1267 return tmp_number((AWKNUM) 0.0);
1270 cnt = (*(iop->getrec))(&s, iop, RS->stptr[0], RS_regexp, &errcode);
1272 if (! do_traditional) {
1273 s = strerror(errcode);
1275 unref(ERRNO_node->var_value);
1276 ERRNO_node->var_value = make_string(s, strlen(s));
1278 return tmp_number((AWKNUM) -1.0);
1283 * Don't do iop_close() here if we are
1284 * reading from a pipe; otherwise
1285 * gawk_pclose will not be called.
1287 if ((rp->flag & RED_PIPE) == 0) {
1288 (void) iop_close(iop);
1291 rp->flag |= RED_EOF; /* sticky EOF */
1292 return tmp_number((AWKNUM) 0.0);
1294 continue; /* try another file */
1300 if (tree->lnode == NULL) /* no optional var. */
1301 set_record(s, cnt, TRUE);
1302 else { /* assignment to variable */
1303 Func_ptr after_assign = NULL;
1306 lhs = get_lhs(tree->lnode, &after_assign);
1308 *lhs = make_string(s, cnt);
1309 (*lhs)->flags |= MAYBE_NUM;
1310 /* we may have to regenerate $0 here! */
1311 if (after_assign != NULL)
1315 return tmp_number((AWKNUM) 1.0);
1318 /* pathopen --- pathopen with default file extension handling */
1324 int fd = do_pathopen(file);
1326 #ifdef DEFAULT_FILETYPE
1327 if (! do_traditional && fd <= INVALID_HANDLE) {
1331 int vms_save = vaxc$errno;
1334 /* append ".awk" and try again */
1335 emalloc(file_awk, char *, strlen(file) +
1336 sizeof(DEFAULT_FILETYPE) + 1, "pathopen");
1337 sprintf(file_awk, "%s%s", file, DEFAULT_FILETYPE);
1338 fd = do_pathopen(file_awk);
1340 if (fd <= INVALID_HANDLE) {
1343 vaxc$errno = vms_save;
1347 #endif /*DEFAULT_FILETYPE*/
1352 /* do_pathopen --- search $AWKPATH for source file */
1358 static const char *savepath = NULL;
1359 static int first = TRUE;
1360 const char *awkpath;
1361 char *cp, trypath[BUFSIZ];
1364 if (STREQ(file, "-"))
1368 return (devopen(file, "r"));
1372 if ((awkpath = getenv("AWKPATH")) != NULL && *awkpath)
1373 savepath = awkpath; /* used for restarting */
1379 /* some kind of path name, no search */
1381 return (devopen(file, "r"));
1385 /* this should take into account limits on size of trypath */
1386 for (cp = trypath; *awkpath && *awkpath != envsep; )
1389 if (cp != trypath) { /* nun-null element in path */
1390 /* add directory punctuation only if needed */
1391 if (! isdirpunct(*(cp-1)))
1393 /* append filename */
1396 strcpy(trypath, file);
1397 if ((fd = devopen(trypath, "r")) > INVALID_HANDLE)
1400 /* no luck, keep going */
1401 if(*awkpath == envsep && awkpath[1] != '\0')
1402 awkpath++; /* skip colon */
1403 } while (*awkpath != '\0');
1405 * You might have one of the awk paths defined, WITHOUT the current
1406 * working directory in it. Therefore try to open the file in the
1407 * current directory.
1409 return (devopen(file, "r"));
1424 /* iop_alloc --- allocate an IOBUF structure for an open fd */
1427 iop_alloc(fd, name, iop)
1434 if (fd == INVALID_HANDLE)
1437 emalloc(iop, IOBUF *, sizeof(IOBUF), "iop_alloc");
1440 iop->flag |= IOP_IS_TTY;
1441 iop->size = optimal_bufsize(fd, & sbuf);
1442 if (do_lint && S_ISREG(sbuf.st_mode) && sbuf.st_size == 0)
1443 warning("data file `%s' is empty", name);
1447 iop->off = iop->buf = NULL;
1450 iop->getrec = get_a_record;
1452 /* Use mmap only for regular files with positive sizes.
1453 The size must fit into size_t, so that mmap works correctly.
1454 Also, it must fit into int, so that iop->cnt won't overflow. */
1455 if (S_ISREG(sbuf.st_mode) && sbuf.st_size > 0
1456 && sbuf.st_size == (size_t) sbuf.st_size
1457 && sbuf.st_size == (int) sbuf.st_size) {
1460 iop->buf = iop->off = mmap((caddr_t) 0, sbuf.st_size,
1461 PROT_READ|PROT_WRITE, MAP_PRIVATE,
1463 /* cast is for buggy compilers (e.g. DEC OSF/1) */
1464 if (iop->buf == (caddr_t)MAP_FAILED) {
1465 iop->buf = iop->off = NULL;
1469 iop->flag |= IOP_MMAPPED;
1470 iop->size = sbuf.st_size;
1472 iop->end = iop->buf + iop->size;
1473 iop->cnt = sbuf.st_size;
1474 iop->getrec = mmap_get_record;
1476 iop->fd = INVALID_HANDLE;
1478 #if defined(HAVE_MADVISE) && defined(MADV_SEQUENTIAL)
1479 madvise(iop->buf, iop->size, MADV_SEQUENTIAL);
1482 * The following is a really gross hack.
1483 * We want to ensure that we have a copy of the input
1484 * data that won't go away, on the off chance that someone
1485 * will truncate the data file we've just mmap'ed.
1486 * So, we go through and touch each page, forcing the
1487 * system to give us a private copy. A page size of 512
1488 * guarantees this will work, even on the least common
1489 * denominator system (like, oh say, a VAX).
1491 for (cp = iop->buf; cp < iop->end; cp += 512)
1495 #endif /* HAVE_MMAP */
1499 /* These macros used by both record reading routines */
1500 #define set_RT_to_null() \
1501 (void)(! do_traditional && (unref(RT_node->var_value), \
1502 RT_node->var_value = Nnull_string))
1504 #define set_RT(str, len) \
1505 (void)(! do_traditional && (unref(RT_node->var_value), \
1506 RT_node->var_value = make_string(str, len)))
1510 * Get the next record. Uses a "split buffer" where the latter part is
1511 * the normal read buffer and the head part is an "overflow" area that is used
1512 * when a record spans the end of the normal buffer, in which case the first
1513 * part of the record is copied into the overflow area just before the
1514 * normal buffer. Thus, the eventual full record can be returned as a
1515 * contiguous area of memory with a minimum of copying. The overflow area
1516 * is expanded as needed, so that records are unlimited in length.
1517 * We also mark both the end of the buffer and the end of the read() with
1518 * a sentinel character (the current record separator) so that the inside
1519 * loop can run as a single test.
1521 * Note that since we know or can compute the end of the read and the end
1522 * of the buffer, the sentinel character does not get in the way of regexp
1523 * based searching, since we simply search up to that character, but not
1528 get_a_record(out, iop, grRS, RSre, errcode)
1529 char **out; /* pointer to pointer to data */
1530 IOBUF *iop; /* input IOP */
1531 register int grRS; /* first char in RS->stptr */
1532 Regexp *RSre; /* regexp for RS */
1533 int *errcode; /* pointer to error variable */
1535 register char *bp = iop->off;
1537 char *start = iop->off; /* beginning of record */
1539 static Regexp *RS_null_re = NULL;
1540 Regexp *rsre = NULL;
1541 int continuing = FALSE, continued = FALSE; /* used for re matching */
1544 /* first time through */
1545 if (RS_null_re == NULL) {
1546 RS_null_re = make_regexp("\n\n+", 3, TRUE, TRUE);
1547 if (RS_null_re == NULL)
1548 fatal("internal error: file `%s', line %d\n",
1549 __FILE__, __LINE__);
1552 if (iop->cnt == EOF) { /* previous read hit EOF */
1558 if (RS_is_null) /* special case: RS == "" */
1563 onecase = (IGNORECASE && isalpha(rs));
1567 /* set up sentinel */
1569 bufend = iop->buf + iop->size + iop->secsiz;
1570 *bufend = rs; /* add sentinel to buffer */
1574 for (;;) { /* break on end of record, read error or EOF */
1575 /* buffer mgmt, chunk #1 */
1577 * Following code is entered on the first call of this routine
1578 * for a new iop, or when we scan to the end of the buffer.
1579 * In the latter case, we copy the current partial record to
1580 * the space preceding the normal read buffer. If necessary,
1581 * we expand this space. This is done so that we can return
1582 * the record as a contiguous area of memory.
1584 if ((iop->flag & IOP_IS_INTERNAL) == 0 && bp >= bufend) {
1585 char *oldbuf = NULL;
1586 char *oldsplit = iop->buf + iop->secsiz;
1587 long len; /* record length so far */
1590 if (len > iop->secsiz) {
1591 /* expand secondary buffer */
1592 if (iop->secsiz == -2)
1594 while (len > iop->secsiz)
1597 emalloc(iop->buf, char *,
1598 iop->size+iop->secsiz+2, "get_a_record");
1599 bufend = iop->buf + iop->size + iop->secsiz;
1603 char *newsplit = iop->buf + iop->secsiz;
1605 if (start < oldsplit) {
1606 memcpy(newsplit - len, start,
1608 memcpy(newsplit - (bp - oldsplit),
1609 oldsplit, bp - oldsplit);
1611 memcpy(newsplit - len, start, len);
1613 bp = iop->end = iop->off = iop->buf + iop->secsiz;
1615 if (oldbuf != NULL) {
1620 /* buffer mgmt, chunk #2 */
1622 * Following code is entered whenever we have no more data to
1623 * scan. In most cases this will read into the beginning of
1624 * the main buffer, but in some cases (terminal, pipe etc.)
1625 * we may be doing smallish reads into more advanced positions.
1627 if (bp >= iop->end) {
1628 if ((iop->flag & IOP_IS_INTERNAL) != 0) {
1632 iop->cnt = read(iop->fd, iop->end, bufend - iop->end);
1633 if (iop->cnt == -1) {
1634 if (! do_traditional && errcode != NULL) {
1639 fatal("error reading input file `%s': %s",
1640 iop->name, strerror(errno));
1641 } else if (iop->cnt == 0) {
1643 * hit EOF before matching RS, so end
1644 * the record and set RT to ""
1647 /* see comments below about this test */
1653 if (iop->cnt != EOF) {
1654 iop->end += iop->cnt;
1655 *iop->end = rs; /* reset the sentinel */
1658 /* buffers are now setup and filled with data */
1659 /* search for RS, #1, regexp based, or RS = "" */
1661 * Attempt to simplify the code a bit. The case where
1662 * RS = "" can also be described by a regexp, RS = "\n\n+".
1663 * The buffer managment and searching code can thus now
1664 * use a common case (the one for regexps) both when RS is
1665 * a regexp, and when RS = "". This particularly benefits
1666 * us for keeping track of how many newlines were matched
1667 * in order to set RT.
1669 if (! do_traditional && RSre != NULL) /* regexp */
1671 else if (RS_is_null) /* RS = "" */
1677 * Look for regexp match of RS. Non-match conditions are:
1678 * 1. No match at all
1679 * 2. Match of a null string
1680 * 3. Match ends at exact end of buffer
1681 * Number 3 is subtle; we have to add more to the buffer
1682 * in case the match would have extended further into the
1683 * file, since regexp match by definition always matches the
1684 * longest possible match.
1686 * It is even more subtle than you might think. Suppose
1687 * the re matches at exactly the end of file. We don't know
1688 * that until we try to add more to the buffer. Thus, we
1689 * set a flag to indicate, that if eof really does happen,
1690 * don't break early.
1695 /* cases 1 and 2 are simple, just keep going */
1696 if (research(rsre, start, 0, iop->end - start, TRUE) == -1
1697 || RESTART(rsre, start) == REEND(rsre, start)) {
1699 * Leading newlines at the beginning of the file
1700 * should be ignored. Whew!
1702 if (RS_is_null && *start == '\n') {
1704 * have to catch the case of a
1705 * single newline at the front of
1706 * the record, which the regex
1709 while (*start == '\n' && start < iop->end)
1716 /* case 3, regex match at exact end */
1717 if (start + REEND(rsre, start) >= iop->end) {
1718 if (iop->cnt != EOF) {
1720 continuing = continued = TRUE;
1726 * Leading newlines at the beginning of the file
1727 * should be ignored. Whew!
1729 * Is this code ever executed?
1731 if (RS_is_null && RESTART(rsre, start) == 0) {
1732 start += REEND(rsre, start);
1735 bp = start + RESTART(rsre, start);
1736 set_RT(bp, REEND(rsre, start) - RESTART(rsre, start));
1738 iop->off = start + REEND(rsre, start);
1741 /* search for RS, #2, RS = <single char> */
1743 while (casetable[(int) *bp++] != rs)
1756 if ((iop->flag & IOP_IS_INTERNAL) != 0)
1757 iop->cnt = bp - start;
1760 && (((iop->flag & IOP_IS_INTERNAL) != 0)
1761 || (start == bp && ! continued))) {
1767 if (do_traditional || rsre == NULL) {
1770 bstart = iop->off = bp;
1772 if (onecase ? casetable[(int) *bp] != rs : *bp != rs) {
1777 } else if (RS_is_null && iop->cnt == EOF) {
1779 * special case, delete trailing newlines,
1780 * should never be more than one.
1782 while (bp[-1] == '\n')
1804 bufsize = atoi(argv[1]);
1807 iop = iop_alloc(0, "stdin", NULL);
1808 while ((cnt = get_a_record(&out, iop, rs[0], NULL, NULL)) > 0) {
1809 fwrite(out, 1, cnt, stdout);
1810 fwrite(rs, 1, 1, stdout);
1817 /* mmap_get_record --- pull a record out of a memory-mapped file */
1820 mmap_get_record(out, iop, grRS, RSre, errcode)
1821 char **out; /* pointer to pointer to data */
1822 IOBUF *iop; /* input IOP */
1823 register int grRS; /* first char in RS->stptr */
1824 Regexp *RSre; /* regexp for RS */
1825 int *errcode; /* pointer to error variable */
1827 register char *bp = iop->off;
1828 char *start = iop->off; /* beginning of record */
1830 static Regexp *RS_null_re = NULL;
1831 Regexp *rsre = NULL;
1833 register char *end = iop->end;
1836 /* first time through */
1837 if (RS_null_re == NULL) {
1838 RS_null_re = make_regexp("\n\n+", 3, TRUE, TRUE);
1839 if (RS_null_re == NULL)
1840 fatal("internal error: file `%s', line %d\n",
1841 __FILE__, __LINE__);
1844 if (iop->off >= iop->end) { /* previous record was last */
1847 iop->cnt = EOF; /* tested by higher level code */
1851 if (RS_is_null) /* special case: RS == "" */
1856 onecase = (IGNORECASE && isalpha(rs));
1860 /* if RS = "", skip leading newlines at the front of the file */
1861 if (RS_is_null && iop->off == iop->buf) {
1862 for (bp = iop->off; *bp == '\n'; bp++)
1866 iop->off = start = bp;
1870 * Regexp based searching. Either RS = "" or RS = <regex>
1871 * See comments in get_a_record.
1873 if (! do_traditional && RSre != NULL) /* regexp */
1875 else if (RS_is_null) /* RS = "" */
1881 * Look for regexp match of RS. Non-match conditions are:
1882 * 1. No match at all
1883 * 2. Match of a null string
1884 * 3. Match ends at exact end of buffer
1886 * #1 means that the record ends the file
1887 * and there is no text that actually matched RS.
1889 * #2: is probably like #1.
1891 * #3 is simple; since we have the whole file mapped, it's
1892 * the last record in the file.
1895 if (research(rsre, start, 0, iop->end - start, TRUE) == -1
1896 || RESTART(rsre, start) == REEND(rsre, start)) {
1897 /* no matching text, we have the record */
1899 iop->off = iop->end; /* all done with the record */
1901 /* special case, don't allow trailing newlines */
1902 if (RS_is_null && *(iop->end - 1) == '\n')
1903 return iop->end - start - 1;
1905 return iop->end - start;
1910 bp = start + RESTART(rsre, start);
1911 set_RT(bp, REEND(rsre, start) - RESTART(rsre, start));
1913 iop->off = start + REEND(rsre, start);
1918 * RS = "?", i.e., one character based searching.
1920 * Alas, we can't just plug the sentinel character in at
1921 * the end of the mmapp'ed file ( *(iop->end) = rs; ). This
1922 * works if we're lucky enough to have a file that does not
1923 * take up all of its last disk block. But if we end up with
1924 * file whose size is an even multiple of the disk block size,
1925 * assigning past the end of it delivers a SIGBUS. So, we have to
1926 * add the extra test in the while loop at the front that looks
1927 * for going past the end of the mapped object. Sigh.
1929 /* search for RS, #2, RS = <single char> */
1931 while (bp < end && casetable[*bp++] != rs)
1934 while (bp < end && *bp++ != rs)
1937 cnt = (bp - start) - 1;
1938 if (bp >= iop->end) {
1939 /* at end, may have actually seen rs, or may not */
1941 set_RT(bp - 1, 1); /* real RS seen */
1953 #endif /* HAVE_MMAP */
1955 /* set_RS --- update things as appropriate when RS is set */
1960 static NODE *save_rs = NULL;
1962 if (save_rs && cmp_nodes(RS_node->var_value, save_rs) == 0)
1965 save_rs = dupnode(RS_node->var_value);
1967 RS = force_string(RS_node->var_value);
1968 if (RS_regexp != NULL) {
1974 else if (RS->stlen > 1) {
1975 static int warned = FALSE;
1977 RS_regexp = make_regexp(RS->stptr, RS->stlen, IGNORECASE, TRUE);
1979 if (do_lint && ! warned) {
1980 warning("multicharacter value of `RS' is not portable");
1985 set_FS_if_not_FIELDWIDTHS();