use library version of rmut
[unix-history] / usr / src / old / awk / run.c
index 91f7d84..c563f6e 100644 (file)
@@ -1,15 +1,20 @@
-/*     run.c   4.1     82/05/07        */
+#ifndef lint
+static char sccsid[] = "@(#)run.c      4.8 %G%";
+#endif
 
 
+#include "sys/param.h"
 #include "awk.def"
 #include "awk.def"
-#include       "math.h"
-#define RECSIZE 512
+#include "math.h"
 #include "awk.h"
 #include "stdio.h"
 #include "awk.h"
 #include "stdio.h"
+#include "fcntl.h"
+#define RECSIZE BUFSIZ
 
 
-#define FILENUM        10
+#define FILENUM        NOFILE
 struct
 {
        FILE *fp;
 struct
 {
        FILE *fp;
+       int type;
        char *fname;
 } files[FILENUM];
 FILE *popen();
        char *fname;
 } files[FILENUM];
 FILE *popen();
@@ -20,13 +25,20 @@ int pairstack[PA2NUM], paircnt;
 node *winner = (node *)NULL;
 #define MAXTMP 20
 cell tmps[MAXTMP];
 node *winner = (node *)NULL;
 #define MAXTMP 20
 cell tmps[MAXTMP];
-static cell nullval ={0,0,0.0,NUM,0};
+static cell nullval ={EMPTY,EMPTY,0.0,NUM,0};
 obj    true    ={ OBOOL, BTRUE, 0 };
 obj    false   ={ OBOOL, BFALSE, 0 };
 
 run()
 {
 obj    true    ={ OBOOL, BTRUE, 0 };
 obj    false   ={ OBOOL, BFALSE, 0 };
 
 run()
 {
+       register int i;
+
        execute(winner);
        execute(winner);
+
+       /* Wait for children to complete if output to a pipe. */
+       for (i=0; i<FILENUM; i++)
+               if (files[i].fp && files[i].type == '|')
+                       pclose(files[i].fp);
 }
 
 obj execute(u) node *u;
 }
 
 obj execute(u) node *u;
@@ -118,7 +130,7 @@ obj arrayel(a,b) node *a; obj b;
        s = getsval(b.optr);
        x = (cell *) a;
        if (!(x->tval&ARR)) {
        s = getsval(b.optr);
        x = (cell *) a;
        if (!(x->tval&ARR)) {
-               xfree(x->sval);
+               strfree(x->sval);
                x->tval &= ~STR;
                x->tval |= ARR;
                x->sval = (char *) makesymtab();
                x->tval &= ~STR;
                x->tval |= ARR;
                x->sval = (char *) makesymtab();
@@ -214,7 +226,7 @@ obj relop(a,n) node **a;
 tempfree(a) obj a;
 {
        if (!istemp(a)) return;
 tempfree(a) obj a;
 {
        if (!istemp(a)) return;
-       xfree(a.optr->sval);
+       strfree(a.optr->sval);
        a.optr->tval = 0;
 }
 
        a.optr->tval = 0;
 }
 
@@ -362,7 +374,7 @@ char *format(s,a) char *s; node *a;
                        break;
                }
                if (flag == 0) {
                        break;
                }
                if (flag == 0) {
-                       sprintf(p, "%s", fmt);
+                       (void)sprintf(p, "%s", fmt);
                        p += strlen(p);
                        continue;
                }
                        p += strlen(p);
                        continue;
                }
@@ -372,10 +384,10 @@ char *format(s,a) char *s; node *a;
                a = a->nnext;
                if (flag != 4)  /* watch out for converting to numbers! */
                        xf = getfval(x.optr);
                a = a->nnext;
                if (flag != 4)  /* watch out for converting to numbers! */
                        xf = getfval(x.optr);
-               if (flag==1) sprintf(p, fmt, xf);
-               else if (flag==2) sprintf(p, fmt, (long)xf);
-               else if (flag==3) sprintf(p, fmt, (int)xf);
-               else if (flag==4) sprintf(p, fmt, x.optr->sval==NULL ? "" : getsval(x.optr));
+               if (flag==1) (void)sprintf(p, fmt, xf);
+               else if (flag==2) (void)sprintf(p, fmt, (long)xf);
+               else if (flag==3) (void)sprintf(p, fmt, (int)xf);
+               else if (flag==4) (void)sprintf(p, fmt, x.optr->sval==NULL ? "" : getsval(x.optr));
                tempfree(x);
                p += strlen(p);
                s++;
                tempfree(x);
                p += strlen(p);
                s++;
@@ -629,7 +641,7 @@ obj split(a,nnn) node **a;
                        while (*s!=' ' && *s!='\t' && *s!='\n' && *s!='\0');
                        temp = *s;
                        *s = '\0';
                        while (*s!=' ' && *s!='\t' && *s!='\n' && *s!='\0');
                        temp = *s;
                        *s = '\0';
-                       sprintf(num, "%d", n);
+                       (void)sprintf(num, "%d", n);
                        if (isnumber(t))
                                setsymtab(num, tostring(t), atof(t), STR|NUM, ap->sval);
                        else
                        if (isnumber(t))
                                setsymtab(num, tostring(t), atof(t), STR|NUM, ap->sval);
                        else
@@ -646,7 +658,7 @@ obj split(a,nnn) node **a;
                                s++;
                        temp = *s;
                        *s = '\0';
                                s++;
                        temp = *s;
                        *s = '\0';
-                       sprintf(num, "%d", n);
+                       (void)sprintf(num, "%d", n);
                        if (isnumber(t))
                                setsymtab(num, tostring(t), atof(t), STR|NUM, ap->sval);
                        else
                        if (isnumber(t))
                                setsymtab(num, tostring(t), atof(t), STR|NUM, ap->sval);
                        else
@@ -743,6 +755,7 @@ obj instat(a, n) node **a;
                        tempfree(x);
                }
        }
                        tempfree(x);
                }
        }
+       return (true);
 }
 
 obj jump(a,n) node **a;
 }
 
 obj jump(a,n) node **a;
@@ -862,7 +875,10 @@ redirprint(s, a, b) char *s; node *b;
                files[i].fp = fopen(x.optr->sval, "w");
        if (files[i].fp == NULL)
                error(FATAL, "can't open file %s", x.optr->sval);
                files[i].fp = fopen(x.optr->sval, "w");
        if (files[i].fp == NULL)
                error(FATAL, "can't open file %s", x.optr->sval);
+       if (fcntl(fileno(files[i].fp), F_SETFD, 1) < 0)
+               error(FATAL, "close on exec failure");
        files[i].fname = tostring(x.optr->sval);
        files[i].fname = tostring(x.optr->sval);
+       files[i].type = a;
 doit:
        fprintf(files[i].fp, "%s", s);
 #ifndef gcos
 doit:
        fprintf(files[i].fp, "%s", s);
 #ifndef gcos