1 /* $Header: eval.c,v 4.0 91/03/20 01:16:48 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 4.0 91/03/20 01:16:48 lwall
17 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
32 static void (*ihand)();
33 static void (*qhand)();
35 static int (*ihand)();
36 static int (*qhand)();
43 static struct lstring *lstr;
44 static int old_rschar;
47 double sin(), cos(), atan2(), pow();
68 int arglast[8]; /* highest sp for arg--valid only for non-O_LIST args */
69 unsigned long tmplong;
76 bool assigning = FALSE;
77 double exp(), log(), sqrt(), modf();
78 char *crypt(), *getenv();
79 extern void grow_dlevel();
83 optype = arg->arg_type;
84 maxarg = arg->arg_len;
86 str = arg->arg_ptr.arg_str;
87 if (sp + maxarg > stack->ary_max)
88 astore(stack, sp + maxarg, Nullstr);
89 st = stack->ary_array;
94 deb("%s (%lx) %d args:\n",opname[optype],arg,maxarg);
96 debname[dlevel] = opname[optype][0];
97 debdelim[dlevel] = ':';
98 if (++dlevel >= dlmax)
103 for (anum = 1; anum <= maxarg; anum++) {
104 argflags = arg[anum].arg_flags;
105 argtype = arg[anum].arg_type;
106 argptr = arg[anum].arg_ptr;
110 st[++sp] = &str_undef;
119 deb("%d.EXPR =>\n",anum);
122 sp = eval(argptr.arg_arg,
123 (argflags & AF_ARYOK) ? G_ARRAY : G_SCALAR, sp);
124 if (sp + (maxarg - anum) > stack->ary_max)
125 astore(stack, sp + (maxarg - anum), Nullstr);
126 st = stack->ary_array; /* possibly reallocated */
132 deb("%d.CMD (%lx) =>\n",anum,argptr.arg_cmd);
135 sp = cmd_exec(argptr.arg_cmd, gimme, sp);
136 if (sp + (maxarg - anum) > stack->ary_max)
137 astore(stack, sp + (maxarg - anum), Nullstr);
138 st = stack->ary_array; /* possibly reallocated */
143 case O_ITEM2: argtype = 2; break;
144 case O_ITEM3: argtype = 3; break;
145 default: argtype = anum; break;
147 str = afetch(stab_array(argptr.arg_stab),
148 arg[argtype].arg_len - arybase, TRUE);
151 (void)sprintf(buf,"LARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
152 arg[argtype].arg_len);
159 case O_ITEM2: argtype = 2; break;
160 case O_ITEM3: argtype = 3; break;
161 default: argtype = anum; break;
163 st[++sp] = afetch(stab_array(argptr.arg_stab),
164 arg[argtype].arg_len - arybase, FALSE);
167 (void)sprintf(buf,"ARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
168 arg[argtype].arg_len);
174 stab = argptr.arg_stab;
175 st[++sp] = (STR*)stab;
176 if (!stab_xarray(stab))
178 if (!stab_xhash(stab))
181 stab_io(stab) = stio_new();
184 (void)sprintf(buf,"STAR *%s",stab_name(argptr.arg_stab));
190 str = st[++sp] = (STR*)argptr.arg_stab;
193 (void)sprintf(buf,"LSTAR *%s",stab_name(argptr.arg_stab));
199 st[++sp] = STAB_STR(argptr.arg_stab);
202 (void)sprintf(buf,"STAB $%s",stab_name(argptr.arg_stab));
211 deb("%d.LEXPR =>\n",anum);
214 if (argflags & AF_ARYOK) {
215 sp = eval(argptr.arg_arg, G_ARRAY, sp);
216 if (sp + (maxarg - anum) > stack->ary_max)
217 astore(stack, sp + (maxarg - anum), Nullstr);
218 st = stack->ary_array; /* possibly reallocated */
221 sp = eval(argptr.arg_arg, G_SCALAR, sp);
222 st = stack->ary_array; /* possibly reallocated */
230 (void)sprintf(buf,"LVAL $%s",stab_name(argptr.arg_stab));
235 str = STAB_STR(argptr.arg_stab);
237 fatal("panic: A_LVAL");
240 if (argflags & AF_PRE) {
241 if (argflags & AF_UP)
247 str = arg->arg_ptr.arg_str;
249 else if (argflags & AF_POST) {
250 st[sp] = str_mortal(str);
251 if (argflags & AF_UP)
256 str = arg->arg_ptr.arg_str;
263 stab = argptr.arg_stab;
264 str = stab_array(argptr.arg_stab)->ary_magic;
265 if (optype != O_SASSIGN || argflags & (AF_PRE|AF_POST))
266 str_numset(str,(double)(stab_array(stab)->ary_fill+arybase));
271 fatal("panic: A_LEXPR");
274 stab = argptr.arg_stab;
275 st[++sp] = stab_array(stab)->ary_magic;
276 str_numset(st[sp],(double)(stab_array(stab)->ary_fill+arybase));
282 st[++sp] = argptr.arg_str;
288 (void) interp(str,argptr.arg_str,sp);
289 st = stack->ary_array;
296 tmps = str_get(interp(str,argptr.arg_str,sp));
297 st = stack->ary_array;
299 taintproper("Insecure dependency in ``");
301 fp = mypopen(tmps,"r");
304 if (gimme == G_SCALAR) {
305 while (str_gets(str,fp,str->str_cur) != Nullch)
310 if (++sp > stack->ary_max) {
311 astore(stack, sp, Nullstr);
312 st = stack->ary_array;
314 str = st[sp] = Str_new(56,80);
315 if (str_gets(str,fp,0) == Nullch) {
319 if (str->str_len - str->str_cur > 20) {
320 str->str_len = str->str_cur+1;
321 Renew(str->str_ptr, str->str_len, char);
326 statusvalue = mypclose(fp);
331 if (gimme == G_SCALAR)
339 if (curcsv->wantarray == G_ARRAY)
349 last_in_stab = stabent(str_get(STAB_STR(argptr.arg_stab)),TRUE);
354 argflags |= AF_POST; /* enable newline chopping */
355 last_in_stab = argptr.arg_stab;
370 last_in_stab = argptr.arg_stab;
374 if (anum > 1) /* assign to scalar */
375 gimme = G_SCALAR; /* force context to scalar */
376 if (gimme == G_ARRAY)
380 if (stab_io(last_in_stab)) {
381 fp = stab_io(last_in_stab)->ifp;
383 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
384 if (stab_io(last_in_stab)->flags & IOF_START) {
385 stab_io(last_in_stab)->flags &= ~IOF_START;
386 stab_io(last_in_stab)->lines = 0;
387 if (alen(stab_array(last_in_stab)) < 0) {
388 tmpstr = str_make("-",1); /* assume stdin */
389 (void)apush(stab_array(last_in_stab), tmpstr);
392 fp = nextargv(last_in_stab);
393 if (!fp) { /* Note: fp != stab_io(last_in_stab)->ifp */
394 (void)do_close(last_in_stab,FALSE); /* now it does*/
395 stab_io(last_in_stab)->flags |= IOF_START;
398 else if (argtype == A_GLOB) {
399 (void) interp(str,stab_val(last_in_stab),sp);
400 st = stack->ary_array;
401 tmpstr = Str_new(55,0);
403 str_set(tmpstr, "perlglob ");
404 str_scat(tmpstr,str);
405 str_cat(tmpstr," |");
408 str_nset(tmpstr,cshname,cshlen);
409 str_cat(tmpstr," -cf 'set nonomatch; glob ");
410 str_scat(tmpstr,str);
411 str_cat(tmpstr,"'|");
413 str_set(tmpstr, "echo ");
414 str_scat(tmpstr,str);
416 "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
419 (void)do_open(last_in_stab,tmpstr->str_ptr,
421 fp = stab_io(last_in_stab)->ifp;
427 warn("Read on closed filehandle <%s>",stab_name(last_in_stab));
428 when = str->str_len; /* remember if already alloced */
430 Str_Grow(str,80); /* try short-buffering it */
434 else if (!str_gets(str,fp, optype == O_RCAT ? str->str_cur : 0)) {
436 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
437 fp = nextargv(last_in_stab);
440 (void)do_close(last_in_stab,FALSE);
441 stab_io(last_in_stab)->flags |= IOF_START;
443 else if (argflags & AF_POST) {
444 (void)do_close(last_in_stab,FALSE);
449 if (gimme == G_ARRAY) {
457 stab_io(last_in_stab)->lines++;
460 str->str_tainted = 1; /* Anything from the outside world...*/
462 if (argflags & AF_POST) {
463 if (str->str_cur > 0)
465 if (str->str_ptr[str->str_cur] == rschar)
466 str->str_ptr[str->str_cur] = '\0';
469 for (tmps = str->str_ptr; *tmps; tmps++)
470 if (!isalpha(*tmps) && !isdigit(*tmps) &&
471 index("$&*(){}[]'\";\\|?<>~`",*tmps))
473 if (*tmps && stat(str->str_ptr,&statbuf) < 0)
474 goto keepgoing; /* unmatched wildcard? */
476 if (gimme == G_ARRAY) {
477 if (str->str_len - str->str_cur > 20) {
478 str->str_len = str->str_cur+1;
479 Renew(str->str_ptr, str->str_len, char);
482 if (++sp > stack->ary_max) {
483 astore(stack, sp, Nullstr);
484 st = stack->ary_array;
486 str = Str_new(58,80);
489 else if (!when && str->str_len - str->str_cur > 80) {
490 /* try to reclaim a bit of scalar space on 1st alloc */
491 if (str->str_cur < 60)
494 str->str_len = str->str_cur+40; /* allow some slop */
495 Renew(str->str_ptr, str->str_len, char);
507 deb("%d.%s = '%s'\n",anum,tmps,str_peek(st[sp]));
515 if (optype < O_CHOWN)
522 if (gimme == G_ARRAY)
530 if (gimme == G_ARRAY)
533 STR_SSET(str,st[arglast[anum]-arglast[0]]);
537 if (gimme == G_ARRAY)
540 STR_SSET(str,st[arglast[anum]-arglast[0]]);
549 if (gimme == G_ARRAY && arg[1].arg_flags & AF_ARYOK) {
550 sp = do_repeatary(arglast);
553 STR_SSET(str,st[arglast[1] - arglast[0]]);
554 anum = (int)str_gnum(st[arglast[2] - arglast[0]]);
556 tmpstr = Str_new(50, 0);
558 str_nset(tmpstr,tmps,str->str_cur);
559 tmps = str_get(tmpstr); /* force to be string */
560 STR_GROW(str, (anum * str->str_cur) + 1);
561 repeatcpy(str->str_ptr, tmps, tmpstr->str_cur, anum);
562 str->str_cur *= anum;
563 str->str_ptr[str->str_cur] = '\0';
568 str_sset(str,&str_no);
572 sp = do_match(str,arg,
574 if (gimme == G_ARRAY)
579 sp = do_match(str,arg,
581 str_sset(str, str_true(str) ? &str_no : &str_yes);
585 sp = do_subst(str,arg,arglast[0]);
588 sp = do_subst(str,arg,arglast[0]);
589 str = arg->arg_ptr.arg_str;
590 str_set(str, str_true(str) ? No : Yes);
593 if (arg[1].arg_flags & AF_ARYOK) {
594 if (arg->arg_len == 1) {
595 arg->arg_type = O_LOCAL;
599 arg->arg_type = O_AASSIGN;
604 arg->arg_type = O_SASSIGN;
609 arglast[2] = arglast[1]; /* push a null array */
618 STR_SSET(str, st[2]);
623 str = arg->arg_ptr.arg_str;
624 for (sp = arglast[0] + 1; sp <= arglast[1]; sp++)
629 if (arg[1].arg_type & A_DONT) {
630 sp = do_defined(str,arg,
634 else if (str->str_pok || str->str_nok)
638 if (arg[1].arg_type & A_DONT) {
639 sp = do_undef(str,arg,
643 else if (str != stab_val(defstab)) {
645 if (str->str_state == SS_INCR)
647 Safefree(str->str_ptr);
648 str->str_ptr = Nullch;
651 str->str_pok = str->str_nok = 0;
656 sp = do_study(str,arg,
660 value = str_gnum(st[1]);
661 value = pow(value,str_gnum(st[2]));
664 value = str_gnum(st[1]);
665 value *= str_gnum(st[2]);
668 if ((value = str_gnum(st[2])) == 0.0)
669 fatal("Illegal division by zero");
671 /* insure that 20./5. == 4. */
676 if ((double)(int)x == x &&
677 (double)(int)value == value &&
678 (k = (int)x/(int)value)*(int)value == (int)x) {
685 value = str_gnum(st[1]) / value;
689 tmplong = (long) str_gnum(st[2]);
691 fatal("Illegal modulus zero");
692 when = (long)str_gnum(st[1]);
695 value = (double)(when % tmplong);
697 value = (double)(tmplong - ((-when - 1) % tmplong)) - 1;
701 value = str_gnum(st[1]);
702 value += str_gnum(st[2]);
705 value = str_gnum(st[1]);
706 value -= str_gnum(st[2]);
709 value = str_gnum(st[1]);
710 anum = (int)str_gnum(st[2]);
712 value = (double)(U_L(value) << anum);
716 value = str_gnum(st[1]);
717 anum = (int)str_gnum(st[2]);
719 value = (double)(U_L(value) >> anum);
723 value = str_gnum(st[1]);
724 value = (value < str_gnum(st[2])) ? 1.0 : 0.0;
727 value = str_gnum(st[1]);
728 value = (value > str_gnum(st[2])) ? 1.0 : 0.0;
731 value = str_gnum(st[1]);
732 value = (value <= str_gnum(st[2])) ? 1.0 : 0.0;
735 value = str_gnum(st[1]);
736 value = (value >= str_gnum(st[2])) ? 1.0 : 0.0;
740 if ((!st[1]->str_nok && !looks_like_number(st[1])) ||
741 (!st[2]->str_nok && !looks_like_number(st[2])) )
742 warn("Possible use of == on string value");
744 value = str_gnum(st[1]);
745 value = (value == str_gnum(st[2])) ? 1.0 : 0.0;
748 value = str_gnum(st[1]);
749 value = (value != str_gnum(st[2])) ? 1.0 : 0.0;
752 value = str_gnum(st[1]);
753 value -= str_gnum(st[2]);
756 else if (value < 0.0)
760 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
761 value = str_gnum(st[1]);
763 value = (double)(U_L(value) & U_L(str_gnum(st[2])));
768 do_vop(optype,str,st[1],st[2]);
771 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
772 value = str_gnum(st[1]);
774 value = (double)(U_L(value) ^ U_L(str_gnum(st[2])));
779 do_vop(optype,str,st[1],st[2]);
782 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
783 value = str_gnum(st[1]);
785 value = (double)(U_L(value) | U_L(str_gnum(st[2])));
790 do_vop(optype,str,st[1],st[2]);
792 /* use register in evaluating str_true() */
794 if (str_true(st[1])) {
797 argflags = arg[anum].arg_flags;
798 if (gimme == G_ARRAY)
799 argflags |= AF_ARYOK;
800 argtype = arg[anum].arg_type & A_MASK;
801 argptr = arg[anum].arg_ptr;
809 str_sset(str, st[1]);
817 if (str_true(st[1])) {
819 str_sset(str, st[1]);
829 argflags = arg[anum].arg_flags;
830 if (gimme == G_ARRAY)
831 argflags |= AF_ARYOK;
832 argtype = arg[anum].arg_type & A_MASK;
833 argptr = arg[anum].arg_ptr;
840 anum = (str_true(st[1]) ? 2 : 3);
841 optype = (anum == 2 ? O_ITEM2 : O_ITEM3);
842 argflags = arg[anum].arg_flags;
843 if (gimme == G_ARRAY)
844 argflags |= AF_ARYOK;
845 argtype = arg[anum].arg_type & A_MASK;
846 argptr = arg[anum].arg_ptr;
852 if (gimme == G_ARRAY)
857 value = -str_gnum(st[1]);
860 value = (double) !str_true(st[1]);
863 if (!sawvec || st[1]->str_nok) {
865 value = (double) ~U_L(str_gnum(st[1]));
872 for (anum = str->str_cur; anum; anum--, tmps++)
877 stab_fullname(str,defoutstab);
879 if ((arg[1].arg_type & A_MASK) == A_WORD)
880 defoutstab = arg[1].arg_ptr.arg_stab;
882 defoutstab = stabent(str_get(st[1]),TRUE);
883 if (!stab_io(defoutstab))
884 stab_io(defoutstab) = stio_new();
885 curoutstab = defoutstab;
892 else if ((arg[1].arg_type & A_MASK) == A_WORD) {
893 if (!(stab = arg[1].arg_ptr.arg_stab))
897 stab = stabent(str_get(st[1]),TRUE);
898 if (!stab_io(stab)) {
904 fp = stab_io(stab)->ofp;
906 if (stab_io(stab)->fmt_stab)
907 form = stab_form(stab_io(stab)->fmt_stab);
909 form = stab_form(stab);
913 warn("No format for filehandle");
915 if (stab_io(stab)->ifp)
916 warn("Filehandle only opened for input");
918 warn("Write on closed filehandle");
925 format(&outrec,form,sp);
926 do_write(&outrec,stab_io(stab),sp);
927 if (stab_io(stab)->flags & IOF_FLUSH)
934 anum = arg[1].arg_type & A_MASK;
935 if (anum == A_WORD || anum == A_STAB)
936 stab = arg[1].arg_ptr.arg_stab;
938 stab = stabent(str_get(st[1]),TRUE);
939 if (st[3]->str_nok || st[3]->str_pok)
940 anum = (int)str_gnum(st[3]);
943 value = (double)hdbmopen(stab_hash(stab),str_get(st[2]),anum);
946 fatal("No dbm or ndbm on this machine");
950 if ((arg[1].arg_type & A_MASK) == A_WORD)
951 stab = arg[1].arg_ptr.arg_stab;
953 stab = stabent(str_get(st[1]),TRUE);
954 hdbmclose(stab_hash(stab));
957 fatal("No dbm or ndbm on this machine");
960 if ((arg[1].arg_type & A_MASK) == A_WORD)
961 stab = arg[1].arg_ptr.arg_stab;
963 stab = stabent(str_get(st[1]),TRUE);
964 tmps = str_get(st[2]);
965 if (do_open(stab,tmps,st[2]->str_cur)) {
966 value = (double)forkprocess;
967 stab_io(stab)->lines = 0;
970 else if (forkprocess == 0) /* we are a new child */
976 value = (double) do_trans(str,arg);
977 str = arg->arg_ptr.arg_str;
980 str_set(arg->arg_ptr.arg_str, do_trans(str,arg) == 0 ? Yes : No);
981 str = arg->arg_ptr.arg_str;
986 else if ((arg[1].arg_type & A_MASK) == A_WORD)
987 stab = arg[1].arg_ptr.arg_stab;
989 stab = stabent(str_get(st[1]),TRUE);
990 str_set(str, do_close(stab,TRUE) ? Yes : No );
994 sp = do_each(str,stab_hash(arg[1].arg_ptr.arg_stab),
999 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
1003 str->str_nok = str->str_pok = 0;
1004 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
1005 str->str_state = SS_ARY;
1008 ary = stab_array(arg[1].arg_ptr.arg_stab);
1009 maxarg = ary->ary_fill + 1;
1010 if (gimme == G_ARRAY) { /* array wanted */
1013 if (maxarg > 0 && sp + maxarg > stack->ary_max) {
1014 astore(stack,sp + maxarg, Nullstr);
1015 st = stack->ary_array;
1018 Copy(ary->ary_array, &st[1], maxarg, STR*);
1023 value = (double)maxarg;
1027 anum = ((int)str_gnum(st[2])) - arybase;
1028 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,FALSE);
1031 tmpstab = arg[1].arg_ptr.arg_stab;
1032 tmps = str_get(st[2]);
1033 str = hdelete(stab_hash(tmpstab),tmps,st[2]->str_cur);
1034 if (tmpstab == envstab)
1035 setenv(tmps,Nullch);
1040 str->str_nok = str->str_pok = 0;
1041 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
1042 str->str_state = SS_HASH;
1045 if (gimme == G_ARRAY) { /* array wanted */
1046 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
1051 tmpstab = arg[1].arg_ptr.arg_stab;
1052 if (!stab_hash(tmpstab)->tbl_fill)
1054 sprintf(buf,"%d/%d",stab_hash(tmpstab)->tbl_fill,
1055 stab_hash(tmpstab)->tbl_max+1);
1060 tmpstab = arg[1].arg_ptr.arg_stab;
1061 tmps = str_get(st[2]);
1062 str = hfetch(stab_hash(tmpstab),tmps,st[2]->str_cur,FALSE);
1065 anum = ((int)str_gnum(st[2])) - arybase;
1066 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,TRUE);
1067 if (!str || str == &str_undef)
1068 fatal("Assignment to non-creatable value, subscript %d",anum);
1071 tmpstab = arg[1].arg_ptr.arg_stab;
1072 tmps = str_get(st[2]);
1073 anum = st[2]->str_cur;
1074 str = hfetch(stab_hash(tmpstab),tmps,anum,TRUE);
1075 if (!str || str == &str_undef)
1076 fatal("Assignment to non-creatable value, subscript \"%s\"",tmps);
1077 if (tmpstab == envstab) /* heavy wizardry going on here */
1078 str_magic(str, tmpstab, 'E', tmps, anum); /* str is now magic */
1079 /* he threw the brick up into the air */
1080 else if (tmpstab == sigstab)
1081 str_magic(str, tmpstab, 'S', tmps, anum);
1083 else if (stab_hash(tmpstab)->tbl_dbm)
1084 str_magic(str, tmpstab, 'D', tmps, anum);
1086 else if (perldb && tmpstab == DBline)
1087 str_magic(str, tmpstab, 'L', tmps, anum);
1092 goto do_slice_already;
1096 goto do_slice_already;
1100 goto do_slice_already;
1104 goto do_slice_already;
1109 sp = do_slice(arg[1].arg_ptr.arg_stab,str,anum,argtype,
1113 sp = do_splice(stab_array(arg[1].arg_ptr.arg_stab),gimme,arglast);
1116 if (arglast[2] - arglast[1] != 1)
1117 str = do_push(stab_array(arg[1].arg_ptr.arg_stab),arglast);
1119 str = Str_new(51,0); /* must copy the STR */
1120 str_sset(str,st[2]);
1121 (void)apush(stab_array(arg[1].arg_ptr.arg_stab),str);
1125 str = apop(ary = stab_array(arg[1].arg_ptr.arg_stab));
1126 goto staticalization;
1128 str = ashift(ary = stab_array(arg[1].arg_ptr.arg_stab));
1132 if (ary->ary_flags & ARF_REAL)
1133 (void)str_2mortal(str);
1136 sp = do_unpack(str,gimme,arglast);
1139 value = str_gnum(st[3]);
1140 sp = do_split(str, arg[2].arg_ptr.arg_spat, (int)value,
1145 value = (double)str_len(stab_val(defstab));
1147 value = (double)str_len(st[1]);
1150 do_sprintf(str, sp-arglast[0], st+1);
1153 anum = ((int)str_gnum(st[2])) - arybase; /* anum=where to start*/
1154 tmps = str_get(st[1]); /* force conversion to string */
1155 if (argtype = (str == st[1]))
1156 str = arg->arg_ptr.arg_str;
1158 anum += st[1]->str_cur + arybase;
1159 if (anum < 0 || anum > st[1]->str_cur)
1162 optype = maxarg < 3 ? st[1]->str_cur : (int)str_gnum(st[3]);
1166 anum = st[1]->str_cur - anum; /* anum=how many bytes left*/
1169 str_nset(str, tmps, anum);
1170 if (argtype) { /* it's an lvalue! */
1171 lstr = (struct lstring*)str;
1172 str->str_magic = st[1];
1173 st[1]->str_rare = 's';
1174 lstr->lstr_offset = tmps - str_get(st[1]);
1175 lstr->lstr_len = anum;
1180 (void)do_pack(str,arglast);
1183 sp = do_grep(arg,str,gimme,arglast);
1186 do_join(str,arglast);
1189 tmps = str_get(st[1]);
1190 value = (double) (str_cmp(st[1],st[2]) < 0);
1193 tmps = str_get(st[1]);
1194 value = (double) (str_cmp(st[1],st[2]) > 0);
1197 tmps = str_get(st[1]);
1198 value = (double) (str_cmp(st[1],st[2]) <= 0);
1201 tmps = str_get(st[1]);
1202 value = (double) (str_cmp(st[1],st[2]) >= 0);
1205 tmps = str_get(st[1]);
1206 value = (double) str_eq(st[1],st[2]);
1209 tmps = str_get(st[1]);
1210 value = (double) !str_eq(st[1],st[2]);
1213 tmps = str_get(st[1]);
1214 value = (double) str_cmp(st[1],st[2]);
1217 sp = do_subr(arg,gimme,arglast);
1218 st = stack->ary_array + arglast[0]; /* maybe realloced */
1221 sp = do_subr(arg,gimme,arglast);
1222 st = stack->ary_array + arglast[0]; /* maybe realloced */
1225 sp = do_caller(arg,maxarg,gimme,arglast);
1226 st = stack->ary_array + arglast[0]; /* maybe realloced */
1229 if ((arg[1].arg_type & A_MASK) == A_WORD)
1230 stab = arg[1].arg_ptr.arg_stab;
1232 stab = stabent(str_get(st[1]),TRUE);
1233 sp = do_sort(str,stab,
1237 if (gimme == G_ARRAY)
1238 sp = do_reverse(arglast);
1240 sp = do_sreverse(str, arglast);
1243 if (arglast[2] - arglast[1] != 1) {
1244 do_join(str,arglast);
1245 tmps = str_get(str);
1249 tmps = str_get(st[2]);
1251 if (!tmps || !*tmps)
1252 tmps = "Warning: something's wrong";
1256 if (arglast[2] - arglast[1] != 1) {
1257 do_join(str,arglast);
1258 tmps = str_get(str);
1262 tmps = str_get(st[2]);
1264 if (!tmps || !*tmps)
1270 if ((arg[1].arg_type & A_MASK) == A_WORD)
1271 stab = arg[1].arg_ptr.arg_stab;
1273 stab = stabent(str_get(st[1]),TRUE);
1276 if (!stab_io(stab)) {
1278 warn("Filehandle never opened");
1281 if (!(fp = stab_io(stab)->ofp)) {
1283 if (stab_io(stab)->ifp)
1284 warn("Filehandle opened only for input");
1286 warn("Print on closed filehandle");
1291 if (optype == O_PRTF || arglast[2] - arglast[1] != 1)
1292 value = (double)do_aprint(arg,fp,arglast);
1294 value = (double)do_print(st[2],fp);
1295 if (orslen && optype == O_PRINT)
1296 if (fwrite(ors, 1, orslen, fp) == 0)
1299 if (stab_io(stab)->flags & IOF_FLUSH)
1300 if (fflush(fp) == EOF)
1308 tmps = str_get(st[1]);
1309 if (!tmps || !*tmps) {
1310 tmpstr = hfetch(stab_hash(envstab),"HOME",4,FALSE);
1311 tmps = str_get(tmpstr);
1313 if (!tmps || !*tmps) {
1314 tmpstr = hfetch(stab_hash(envstab),"LOGDIR",6,FALSE);
1315 tmps = str_get(tmpstr);
1318 taintproper("Insecure dependency in chdir");
1320 value = (double)(chdir(tmps) >= 0);
1326 anum = (int)str_gnum(st[1]);
1333 tmps = str_get(st[1]);
1334 str_reset(tmps,curcmd->c_stash);
1338 if (gimme == G_ARRAY)
1341 str = st[sp - arglast[0]]; /* unwanted list, return last item */
1347 stab = last_in_stab;
1348 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1349 stab = arg[1].arg_ptr.arg_stab;
1351 stab = stabent(str_get(st[1]),TRUE);
1352 str_set(str, do_eof(stab) ? Yes : No);
1358 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1359 stab = arg[1].arg_ptr.arg_stab;
1361 stab = stabent(str_get(st[1]),TRUE);
1364 if (!stab || do_eof(stab)) /* make sure we have fp with something */
1371 *str->str_ptr = getc(stab_io(stab)->ifp); /* should never be EOF */
1377 stab = last_in_stab;
1378 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1379 stab = arg[1].arg_ptr.arg_stab;
1381 stab = stabent(str_get(st[1]),TRUE);
1383 value = (double)do_tell(stab);
1385 (void)do_tell(stab);
1391 if ((arg[1].arg_type & A_MASK) == A_WORD)
1392 stab = arg[1].arg_ptr.arg_stab;
1394 stab = stabent(str_get(st[1]),TRUE);
1395 tmps = str_get(st[2]);
1396 anum = (int)str_gnum(st[3]);
1398 maxarg = sp - arglast[0];
1400 warn("Too many args on read");
1402 maxarg = (int)str_gnum(st[4]);
1405 if (!stab_io(stab) || !stab_io(stab)->ifp)
1408 if (optype == O_RECV) {
1409 argtype = sizeof buf;
1410 STR_GROW(st[2], anum+1), (tmps = str_get(st[2])); /* sneaky */
1411 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, maxarg,
1414 st[2]->str_cur = anum;
1415 st[2]->str_ptr[anum] = '\0';
1416 str_nset(str,buf,argtype);
1419 str_sset(str,&str_undef);
1423 if (optype == O_RECV)
1426 STR_GROW(st[2], anum+maxarg+1), (tmps = str_get(st[2])); /* sneaky */
1428 if (stab_io(stab)->type == 's') {
1429 argtype = sizeof buf;
1430 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps+maxarg, anum, 0,
1435 if (optype == O_SYSREAD) {
1436 anum = read(fileno(stab_io(stab)->ifp), tmps+maxarg, anum);
1439 anum = fread(tmps+maxarg, 1, anum, stab_io(stab)->ifp);
1442 st[2]->str_cur = anum+maxarg;
1443 st[2]->str_ptr[anum+maxarg] = '\0';
1444 value = (double)anum;
1448 if ((arg[1].arg_type & A_MASK) == A_WORD)
1449 stab = arg[1].arg_ptr.arg_stab;
1451 stab = stabent(str_get(st[1]),TRUE);
1452 tmps = str_get(st[2]);
1453 anum = (int)str_gnum(st[3]);
1455 stio = stab_io(stab);
1456 maxarg = sp - arglast[0];
1457 if (!stio || !stio->ifp) {
1460 if (optype == O_SYSWRITE)
1461 warn("Syswrite on closed filehandle");
1463 warn("Send on closed socket");
1466 else if (optype == O_SYSWRITE) {
1468 warn("Too many args on syswrite");
1470 optype = (int)str_gnum(st[4]);
1473 anum = write(fileno(stab_io(stab)->ifp), tmps+optype, anum);
1476 else if (maxarg >= 4) {
1478 warn("Too many args on send");
1479 tmps2 = str_get(st[4]);
1480 anum = sendto(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur,
1481 anum, tmps2, st[4]->str_cur);
1484 anum = send(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur, anum);
1491 value = (double)anum;
1494 if ((arg[1].arg_type & A_MASK) == A_WORD)
1495 stab = arg[1].arg_ptr.arg_stab;
1497 stab = stabent(str_get(st[1]),TRUE);
1498 value = str_gnum(st[2]);
1499 str_set(str, do_seek(stab,
1500 (long)value, (int)str_gnum(st[3]) ) ? Yes : No);
1504 tmps = "_SUB_"; /* just fake up a "last _SUB_" */
1506 if (curcsv && curcsv->wantarray == G_ARRAY) {
1507 lastretstr = Nullstr;
1508 lastspbase = arglast[1];
1509 lastsize = arglast[2] - arglast[1];
1512 lastretstr = str_mortal(st[arglast[2] - arglast[0]]);
1518 tmps = str_get(arg[1].arg_ptr.arg_str);
1520 while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
1521 strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
1524 deb("(Skipping label #%d %s)\n",loop_ptr,
1525 loop_stack[loop_ptr].loop_label);
1532 deb("(Found label #%d %s)\n",loop_ptr,
1533 loop_stack[loop_ptr].loop_label);
1538 if (tmps && strEQ(tmps, "_SUB_"))
1539 fatal("Can't return outside a subroutine");
1540 fatal("Bad label: %s", maxarg > 0 ? tmps : "<null>");
1542 if (!lastretstr && optype == O_LAST && lastsize) {
1544 st += lastspbase + 1;
1545 optype = loop_stack[loop_ptr].loop_sp - lastspbase; /* negative */
1547 for (anum = lastsize; anum > 0; anum--,st++)
1548 st[optype] = str_mortal(st[0]);
1550 longjmp(loop_stack[loop_ptr].loop_env, O_LAST);
1552 longjmp(loop_stack[loop_ptr].loop_env, optype);
1554 case O_GOTO:/* shudder */
1555 goto_targ = str_get(arg[1].arg_ptr.arg_str);
1557 goto_targ = Nullch; /* just restart from top */
1558 if (optype == O_DUMP) {
1562 longjmp(top_env, 1);
1564 tmps = str_get(st[1]);
1568 anum = (int) str_gnum(st[3]) - arybase;
1571 else if (anum > st[1]->str_cur)
1572 anum = st[1]->str_cur;
1575 if (!(tmps2 = fbminstr((unsigned char*)tmps + anum,
1576 (unsigned char*)tmps + st[1]->str_cur, st[2])))
1578 if (tmps2 = fbminstr(Null(unsigned char*),Null(unsigned char*),Nullstr))
1580 value = (double)(-1 + arybase);
1582 value = (double)(tmps2 - tmps + arybase);
1585 tmps = str_get(st[1]);
1586 tmps2 = str_get(st[2]);
1588 anum = st[1]->str_cur;
1590 anum = (int) str_gnum(st[3]) - arybase + st[2]->str_cur;
1593 else if (anum > st[1]->str_cur)
1594 anum = st[1]->str_cur;
1597 if (!(tmps2 = rninstr(tmps, tmps + anum,
1598 tmps2, tmps2 + st[2]->str_cur)))
1600 if (tmps2 = rninstr(Nullch,Nullch,Nullch,Nullch))
1602 value = (double)(-1 + arybase);
1604 value = (double)(tmps2 - tmps + arybase);
1608 value = (double) time(Null(long*));
1612 sp = do_tms(str,gimme,arglast);
1618 when = (long)str_gnum(st[1]);
1619 sp = do_time(str,localtime(&when),
1626 when = (long)str_gnum(st[1]);
1627 sp = do_time(str,gmtime(&when),
1631 sp = do_truncate(str,arg,
1636 sp = do_stat(str,arg,
1641 tmps = str_get(st[1]);
1643 str_set(str,fcrypt(tmps,str_get(st[2])));
1645 str_set(str,crypt(tmps,str_get(st[2])));
1649 "The crypt() function is unimplemented due to excessive paranoia.");
1653 value = str_gnum(st[1]);
1654 value = atan2(value,str_gnum(st[2]));
1658 value = str_gnum(stab_val(defstab));
1660 value = str_gnum(st[1]);
1665 value = str_gnum(stab_val(defstab));
1667 value = str_gnum(st[1]);
1674 value = str_gnum(st[1]);
1678 value = rand() * value / 2147483648.0;
1681 value = rand() * value / 65536.0;
1684 value = rand() * value / 32768.0;
1686 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1697 anum = (int)str_gnum(st[1]);
1702 value = str_gnum(stab_val(defstab));
1704 value = str_gnum(st[1]);
1709 value = str_gnum(stab_val(defstab));
1711 value = str_gnum(st[1]);
1713 fatal("Can't take log of %g\n", value);
1718 value = str_gnum(stab_val(defstab));
1720 value = str_gnum(st[1]);
1722 fatal("Can't take sqrt of %g\n", value);
1723 value = sqrt(value);
1727 value = str_gnum(stab_val(defstab));
1729 value = str_gnum(st[1]);
1731 (void)modf(value,&value);
1733 (void)modf(-value,&value);
1739 tmps = str_get(stab_val(defstab));
1741 tmps = str_get(st[1]);
1743 value = (double) (*tmps & 255);
1746 value = (double) (anum & 255);
1752 tmps = str_get(stab_val(defstab));
1754 tmps = str_get(st[1]);
1757 anum = alarm((unsigned int)atoi(tmps));
1760 value = (double)anum;
1763 fatal("Unsupported function alarm");
1770 tmps = str_get(st[1]);
1772 if (!tmps || !*tmps)
1773 sleep((32767<<16)+32767);
1775 sleep((unsigned int)atoi(tmps));
1777 value = (double)when;
1779 value = ((double)when) - value;
1783 sp = do_range(gimme,arglast);
1786 if (gimme == G_ARRAY) { /* it's a range */
1787 /* can we optimize to constant array? */
1788 if ((arg[1].arg_type & A_MASK) == A_SINGLE &&
1789 (arg[2].arg_type & A_MASK) == A_SINGLE) {
1790 st[2] = arg[2].arg_ptr.arg_str;
1791 sp = do_range(gimme,arglast);
1792 st = stack->ary_array;
1793 maxarg = sp - arglast[0];
1794 str_free(arg[1].arg_ptr.arg_str);
1795 arg[1].arg_ptr.arg_str = Nullstr;
1796 str_free(arg[2].arg_ptr.arg_str);
1797 arg[2].arg_ptr.arg_str = Nullstr;
1798 arg->arg_type = O_ARRAY;
1799 arg[1].arg_type = A_STAB|A_DONT;
1801 stab = arg[1].arg_ptr.arg_stab = aadd(genstab());
1802 ary = stab_array(stab);
1803 afill(ary,maxarg - 1);
1806 while (maxarg-- > 0)
1807 ary->ary_array[maxarg] = str_smake(st[maxarg]);
1811 arg->arg_type = optype = O_RANGE;
1812 maxarg = arg->arg_len = 2;
1814 arg[anum].arg_flags &= ~AF_ARYOK;
1815 argflags = arg[anum].arg_flags;
1816 argtype = arg[anum].arg_type & A_MASK;
1817 arg[anum].arg_type = argtype;
1818 argptr = arg[anum].arg_ptr;
1824 arg->arg_type = O_FLIP;
1827 if ((arg[1].arg_type & A_MASK) == A_SINGLE ?
1828 last_in_stab && (int)str_gnum(st[1]) == stab_io(last_in_stab)->lines
1831 str_numset(str,0.0);
1833 arg->arg_type = optype = O_FLOP;
1834 arg[2].arg_type &= ~A_DONT;
1835 arg[1].arg_type |= A_DONT;
1836 argflags = arg[2].arg_flags;
1837 argtype = arg[2].arg_type & A_MASK;
1838 argptr = arg[2].arg_ptr;
1847 if ((arg[2].arg_type & A_MASK) == A_SINGLE ?
1848 last_in_stab && (int)str_gnum(st[2]) == stab_io(last_in_stab)->lines
1851 arg->arg_type = O_FLIP;
1852 arg[1].arg_type &= ~A_DONT;
1853 arg[2].arg_type |= A_DONT;
1861 if (tmpstab = stabent("$",allstabs))
1862 str_numset(STAB_STR(tmpstab),(double)getpid());
1863 hclear(pidstatus); /* no kids, so don't wait for 'em */
1865 value = (double)anum;
1868 fatal("Unsupported function fork");
1874 anum = wait(&argflags);
1876 pidgone(anum,argflags);
1877 value = (double)anum;
1879 statusvalue = (unsigned short)argflags;
1882 fatal("Unsupported function wait");
1888 anum = (int)str_gnum(st[1]);
1889 optype = (int)str_gnum(st[2]);
1890 anum = wait4pid(anum, &argflags,optype);
1891 value = (double)anum;
1893 statusvalue = (unsigned short)argflags;
1896 fatal("Unsupported function wait");
1902 if (arglast[2] - arglast[1] == 1) {
1904 tainted |= st[2]->str_tainted;
1905 taintproper("Insecure dependency in system");
1908 while ((anum = vfork()) == -1) {
1909 if (errno != EAGAIN) {
1917 ihand = signal(SIGINT, SIG_IGN);
1918 qhand = signal(SIGQUIT, SIG_IGN);
1919 argtype = wait4pid(anum, &argflags, 0);
1923 (void)signal(SIGINT, ihand);
1924 (void)signal(SIGQUIT, qhand);
1925 statusvalue = (unsigned short)argflags;
1929 value = (double)((unsigned int)argflags & 0xffff);
1931 do_execfree(); /* free any memory child malloced on vfork */
1934 if ((arg[1].arg_type & A_MASK) == A_STAB)
1935 value = (double)do_aexec(st[1],arglast);
1936 else if (arglast[2] - arglast[1] != 1)
1937 value = (double)do_aexec(Nullstr,arglast);
1939 value = (double)do_exec(str_get(str_mortal(st[2])));
1943 if ((arg[1].arg_type & A_MASK) == A_STAB)
1944 value = (double)do_aspawn(st[1],arglast);
1945 else if (arglast[2] - arglast[1] != 1)
1946 value = (double)do_aspawn(Nullstr,arglast);
1948 value = (double)do_spawn(str_get(str_mortal(st[2])));
1953 if ((arg[1].arg_type & A_MASK) == A_STAB)
1954 value = (double)do_aexec(st[1],arglast);
1955 else if (arglast[2] - arglast[1] != 1)
1956 value = (double)do_aexec(Nullstr,arglast);
1958 value = (double)do_exec(str_get(str_mortal(st[2])));
1963 tmps = str_get(stab_val(defstab));
1965 tmps = str_get(st[1]);
1966 value = (double)scanhex(tmps, 99, &argtype);
1971 tmps = str_get(stab_val(defstab));
1973 tmps = str_get(st[1]);
1974 while (*tmps && isascii(*tmps) && (isspace(*tmps) || *tmps == '0'))
1977 value = (double)scanhex(++tmps, 99, &argtype);
1979 value = (double)scanoct(tmps, 99, &argtype);
1981 #ifdef SMALLSWITCHES
1988 value = (double)apply(optype,arglast);
1991 fatal("Unsupported function chown");
1996 value = (double)apply(optype,arglast);
1999 fatal("Unsupported function kill");
2005 value = (double)apply(optype,arglast);
2014 anum = umask((int)str_gnum(st[1]));
2015 value = (double)anum;
2017 taintproper("Insecure dependency in umask");
2021 fatal("Unsupported function umask");
2024 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2028 if ((anum = do_ipcget(optype, arglast)) == -1)
2030 value = (double)anum;
2035 anum = do_ipcctl(optype, arglast);
2039 value = (double)anum;
2042 str_set(str,"0 but true");
2046 value = (double)(do_msgsnd(arglast) >= 0);
2049 value = (double)(do_msgrcv(arglast) >= 0);
2052 value = (double)(do_semop(arglast) >= 0);
2056 value = (double)(do_shmio(optype, arglast) >= 0);
2058 #else /* not SYSVIPC */
2070 fatal("System V IPC is not implemented on this machine");
2071 #endif /* not SYSVIPC */
2073 tmps = str_get(st[1]);
2074 tmps2 = str_get(st[2]);
2076 taintproper("Insecure dependency in rename");
2079 value = (double)(rename(tmps,tmps2) >= 0);
2081 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
2084 if (euid || stat(tmps2,&statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
2085 (void)UNLINK(tmps2);
2086 if (!(anum = link(tmps,tmps2)))
2087 anum = UNLINK(tmps);
2089 value = (double)(anum >= 0);
2094 tmps = str_get(st[1]);
2095 tmps2 = str_get(st[2]);
2097 taintproper("Insecure dependency in link");
2099 value = (double)(link(tmps,tmps2) >= 0);
2102 fatal("Unsupported function link");
2106 tmps = str_get(st[1]);
2107 anum = (int)str_gnum(st[2]);
2109 taintproper("Insecure dependency in mkdir");
2112 value = (double)(mkdir(tmps,anum) >= 0);
2115 (void)strcpy(buf,"mkdir ");
2117 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
2119 for (tmps2 = buf+6; *tmps; ) {
2123 (void)strcpy(tmps2," 2>&1");
2124 rsfp = mypopen(buf,"r");
2127 tmps2 = fgets(buf,sizeof buf,rsfp);
2128 (void)mypclose(rsfp);
2129 if (tmps2 != Nullch) {
2130 for (errno = 1; errno < sys_nerr; errno++) {
2131 if (instr(buf,sys_errlist[errno])) /* you don't see this */
2136 #define EACCES EPERM
2138 if (instr(buf,"cannot make"))
2140 else if (instr(buf,"existing file"))
2142 else if (instr(buf,"ile exists"))
2144 else if (instr(buf,"non-exist"))
2146 else if (instr(buf,"does not exist"))
2148 else if (instr(buf,"not empty"))
2150 else if (instr(buf,"cannot access"))
2156 else { /* some mkdirs return no failure indication */
2157 tmps = str_get(st[1]);
2158 anum = (stat(tmps,&statbuf) >= 0);
2159 if (optype == O_RMDIR)
2164 errno = EACCES; /* a guess */
2165 value = (double)anum;
2174 tmps = str_get(stab_val(defstab));
2176 tmps = str_get(st[1]);
2178 taintproper("Insecure dependency in rmdir");
2181 value = (double)(rmdir(tmps) >= 0);
2184 (void)strcpy(buf,"rmdir ");
2185 goto one_liner; /* see above in HAS_MKDIR */
2189 value = (double)getppid();
2192 fatal("Unsupported function getppid");
2200 anum = (int)str_gnum(st[1]);
2201 value = (double)getpgrp(anum);
2204 fatal("The getpgrp() function is unimplemented on this machine");
2209 argtype = (int)str_gnum(st[1]);
2210 anum = (int)str_gnum(st[2]);
2212 taintproper("Insecure dependency in setpgrp");
2214 value = (double)(setpgrp(argtype,anum) >= 0);
2217 fatal("The setpgrp() function is unimplemented on this machine");
2221 #ifdef HAS_GETPRIORITY
2222 argtype = (int)str_gnum(st[1]);
2223 anum = (int)str_gnum(st[2]);
2224 value = (double)getpriority(argtype,anum);
2227 fatal("The getpriority() function is unimplemented on this machine");
2231 #ifdef HAS_SETPRIORITY
2232 argtype = (int)str_gnum(st[1]);
2233 anum = (int)str_gnum(st[2]);
2234 optype = (int)str_gnum(st[3]);
2236 taintproper("Insecure dependency in setpriority");
2238 value = (double)(setpriority(argtype,anum,optype) >= 0);
2241 fatal("The setpriority() function is unimplemented on this machine");
2247 tmps = str_get(stab_val(defstab));
2249 tmps = str_get(st[1]);
2251 taintproper("Insecure dependency in chroot");
2253 value = (double)(chroot(tmps) >= 0);
2256 fatal("Unsupported function chroot");
2262 stab = last_in_stab;
2263 else if ((arg[1].arg_type & A_MASK) == A_WORD)
2264 stab = arg[1].arg_ptr.arg_stab;
2266 stab = stabent(str_get(st[1]),TRUE);
2267 argtype = U_I(str_gnum(st[2]));
2269 taintproper("Insecure dependency in ioctl");
2271 anum = do_ctl(optype,stab,argtype,st[3]);
2275 value = (double)anum;
2278 str_set(str,"0 but true");
2284 stab = last_in_stab;
2285 else if ((arg[1].arg_type & A_MASK) == A_WORD)
2286 stab = arg[1].arg_ptr.arg_stab;
2288 stab = stabent(str_get(st[1]),TRUE);
2289 if (stab && stab_io(stab))
2290 fp = stab_io(stab)->ifp;
2294 argtype = (int)str_gnum(st[2]);
2295 value = (double)(flock(fileno(fp),argtype) >= 0);
2301 fatal("The flock() function is unimplemented on this machine");
2305 ary = stab_array(arg[1].arg_ptr.arg_stab);
2306 if (arglast[2] - arglast[1] != 1)
2307 do_unshift(ary,arglast);
2309 STR *tmpstr = Str_new(52,0); /* must copy the STR */
2310 str_sset(tmpstr,st[2]);
2312 (void)astore(ary,0,tmpstr);
2314 value = (double)(ary->ary_fill + 1);
2321 tmpstr = stab_val(defstab);
2324 (arg[1].arg_type & A_MASK) != A_NULL ? st[1] : stab_val(defstab);
2326 tainted |= tmpstr->str_tainted;
2327 taintproper("Insecure dependency in eval");
2329 sp = do_eval(tmpstr, optype, curcmd->c_stash,
2357 if (mystat(arg,st[1]) < 0)
2359 if (cando(anum,argtype,&statcache))
2364 if (mystat(arg,st[1]) < 0)
2369 if (mystat(arg,st[1]) < 0)
2371 if (statcache.st_uid == (optype == O_FTEOWNED ? euid : uid) )
2375 if (mystat(arg,st[1]) < 0)
2377 if (!statcache.st_size)
2381 if (mystat(arg,st[1]) < 0)
2383 value = (double)statcache.st_size;
2387 if (mystat(arg,st[1]) < 0)
2389 value = (double)(basetime - statcache.st_mtime) / 86400.0;
2392 if (mystat(arg,st[1]) < 0)
2394 value = (double)(basetime - statcache.st_atime) / 86400.0;
2397 if (mystat(arg,st[1]) < 0)
2399 value = (double)(basetime - statcache.st_ctime) / 86400.0;
2403 if (mystat(arg,st[1]) < 0)
2405 if (S_ISSOCK(statcache.st_mode))
2409 if (mystat(arg,st[1]) < 0)
2411 if (S_ISCHR(statcache.st_mode))
2415 if (mystat(arg,st[1]) < 0)
2417 if (S_ISBLK(statcache.st_mode))
2421 if (mystat(arg,st[1]) < 0)
2423 if (S_ISREG(statcache.st_mode))
2427 if (mystat(arg,st[1]) < 0)
2429 if (S_ISDIR(statcache.st_mode))
2433 if (mystat(arg,st[1]) < 0)
2435 if (S_ISFIFO(statcache.st_mode))
2439 if (mylstat(arg,st[1]) < 0)
2441 if (S_ISLNK(statcache.st_mode))
2446 tmps = str_get(st[1]);
2447 tmps2 = str_get(st[2]);
2449 taintproper("Insecure dependency in symlink");
2451 value = (double)(symlink(tmps,tmps2) >= 0);
2454 fatal("Unsupported function symlink");
2459 tmps = str_get(stab_val(defstab));
2461 tmps = str_get(st[1]);
2462 anum = readlink(tmps,buf,sizeof buf);
2465 str_nset(str,buf,anum);
2468 goto say_undef; /* just pretend it's a normal file */
2491 if (mystat(arg,st[1]) < 0)
2493 if (statcache.st_mode & anum)
2497 if (arg[1].arg_type & A_DONT) {
2498 stab = arg[1].arg_ptr.arg_stab;
2502 stab = stabent(tmps = str_get(st[1]),FALSE);
2503 if (stab && stab_io(stab) && stab_io(stab)->ifp)
2504 anum = fileno(stab_io(stab)->ifp);
2505 else if (isdigit(*tmps))
2514 str = do_fttext(arg,st[1]);
2518 if ((arg[1].arg_type & A_MASK) == A_WORD)
2519 stab = arg[1].arg_ptr.arg_stab;
2521 stab = stabent(str_get(st[1]),TRUE);
2523 value = (double)do_socket(stab,arglast);
2525 (void)do_socket(stab,arglast);
2529 if ((arg[1].arg_type & A_MASK) == A_WORD)
2530 stab = arg[1].arg_ptr.arg_stab;
2532 stab = stabent(str_get(st[1]),TRUE);
2534 value = (double)do_bind(stab,arglast);
2536 (void)do_bind(stab,arglast);
2540 if ((arg[1].arg_type & A_MASK) == A_WORD)
2541 stab = arg[1].arg_ptr.arg_stab;
2543 stab = stabent(str_get(st[1]),TRUE);
2545 value = (double)do_connect(stab,arglast);
2547 (void)do_connect(stab,arglast);
2551 if ((arg[1].arg_type & A_MASK) == A_WORD)
2552 stab = arg[1].arg_ptr.arg_stab;
2554 stab = stabent(str_get(st[1]),TRUE);
2556 value = (double)do_listen(stab,arglast);
2558 (void)do_listen(stab,arglast);
2562 if ((arg[1].arg_type & A_MASK) == A_WORD)
2563 stab = arg[1].arg_ptr.arg_stab;
2565 stab = stabent(str_get(st[1]),TRUE);
2566 if ((arg[2].arg_type & A_MASK) == A_WORD)
2567 stab2 = arg[2].arg_ptr.arg_stab;
2569 stab2 = stabent(str_get(st[2]),TRUE);
2570 do_accept(str,stab,stab2);
2578 sp = do_ghent(optype,
2586 sp = do_gnent(optype,
2594 sp = do_gpent(optype,
2602 sp = do_gsent(optype,
2606 value = (double) sethostent((int)str_gnum(st[1]));
2609 value = (double) setnetent((int)str_gnum(st[1]));
2612 value = (double) setprotoent((int)str_gnum(st[1]));
2615 value = (double) setservent((int)str_gnum(st[1]));
2618 value = (double) endhostent();
2621 value = (double) endnetent();
2624 value = (double) endprotoent();
2627 value = (double) endservent();
2630 if ((arg[1].arg_type & A_MASK) == A_WORD)
2631 stab = arg[1].arg_ptr.arg_stab;
2633 stab = stabent(str_get(st[1]),TRUE);
2634 if ((arg[2].arg_type & A_MASK) == A_WORD)
2635 stab2 = arg[2].arg_ptr.arg_stab;
2637 stab2 = stabent(str_get(st[2]),TRUE);
2639 value = (double)do_spair(stab,stab2,arglast);
2641 (void)do_spair(stab,stab2,arglast);
2645 if ((arg[1].arg_type & A_MASK) == A_WORD)
2646 stab = arg[1].arg_ptr.arg_stab;
2648 stab = stabent(str_get(st[1]),TRUE);
2650 value = (double)do_shutdown(stab,arglast);
2652 (void)do_shutdown(stab,arglast);
2657 if ((arg[1].arg_type & A_MASK) == A_WORD)
2658 stab = arg[1].arg_ptr.arg_stab;
2660 stab = stabent(str_get(st[1]),TRUE);
2661 sp = do_sopt(optype,stab,arglast);
2665 if ((arg[1].arg_type & A_MASK) == A_WORD)
2666 stab = arg[1].arg_ptr.arg_stab;
2668 stab = stabent(str_get(st[1]),TRUE);
2671 sp = do_getsockname(optype,stab,arglast);
2674 #else /* HAS_SOCKET not defined */
2707 fatal("Unsupported socket function");
2708 #endif /* HAS_SOCKET */
2711 sp = do_select(gimme,arglast);
2714 fatal("select not implemented");
2719 if ((arg[1].arg_type & A_MASK) == A_WORD)
2720 stab = arg[1].arg_ptr.arg_stab;
2722 stab = stabent(str_get(st[1]),TRUE);
2723 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2730 if ((arg[1].arg_type & A_MASK) == A_WORD)
2731 stab = arg[1].arg_ptr.arg_stab;
2733 stab = stabent(str_get(st[1]),TRUE);
2734 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2737 str_set(str, (setmode(fileno(fp), O_BINARY) != -1) ? Yes : No);
2744 sp = do_vec(str == st[1], arg->arg_ptr.arg_str, arglast);
2750 sp = do_gpwent(optype,
2754 value = (double) setpwent();
2757 value = (double) endpwent();
2762 fatal("Unsupported password function");
2769 sp = do_ggrent(optype,
2773 value = (double) setgrent();
2776 value = (double) endgrent();
2781 fatal("Unsupported group function");
2786 if (!(tmps = getlogin()))
2790 fatal("Unsupported function getlogin");
2801 if ((arg[1].arg_type & A_MASK) == A_WORD)
2802 stab = arg[1].arg_ptr.arg_stab;
2804 stab = stabent(str_get(st[1]),TRUE);
2807 sp = do_dirop(optype,stab,gimme,arglast);
2810 value = (double)do_syscall(arglast);
2814 if ((arg[1].arg_type & A_MASK) == A_WORD)
2815 stab = arg[1].arg_ptr.arg_stab;
2817 stab = stabent(str_get(st[1]),TRUE);
2818 if ((arg[2].arg_type & A_MASK) == A_WORD)
2819 stab2 = arg[2].arg_ptr.arg_stab;
2821 stab2 = stabent(str_get(st[2]),TRUE);
2822 do_pipe(str,stab,stab2);
2825 fatal("Unsupported function pipe");
2836 deb("%s RETURNS \"%s\"\n",opname[optype],str_get(str));
2839 return arglast[0] + 1;
2846 anum = sp - arglast[0];
2849 deb("%s RETURNS ()\n",opname[optype]);
2852 deb("%s RETURNS (\"%s\")\n",opname[optype],str_get(st[1]));
2855 tmps = str_get(st[1]);
2856 deb("%s RETURNS %d ARGS (\"%s\",%s\"%s\")\n",opname[optype],
2857 anum,tmps,anum==2?"":"...,",str_get(st[anum]));
2882 str_numset(str,value);
2889 deb("%s RETURNS \"%f\"\n",opname[optype],value);
2892 return arglast[0] + 1;