1 /* $RCSfile: eval.c,v $$Revision: 4.1 $$Date: 92/08/07 18:20:29 $
3 * Copyright (c) 1991, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
9 * Revision 4.1 92/08/07 18:20:29 lwall
11 * Revision 4.0.1.4 92/06/08 13:20:20 lwall
12 * patch20: added explicit time_t support
13 * patch20: fixed confusion between a *var's real name and its effective name
14 * patch20: added Atari ST portability
15 * patch20: new warning for use of x with non-numeric right operand
16 * patch20: modulus with highest bit in left operand set didn't always work
17 * patch20: dbmclose(%array) didn't work
18 * patch20: added ... as variant on ..
19 * patch20: O_PIPE conflicted with Atari
21 * Revision 4.0.1.3 91/11/05 17:15:21 lwall
22 * patch11: prepared for ctype implementations that don't define isascii()
23 * patch11: various portability fixes
24 * patch11: added sort {} LIST
25 * patch11: added eval {}
26 * patch11: sysread() in socket was substituting recv()
27 * patch11: a last statement outside any block caused occasional core dumps
28 * patch11: missing arguments caused core dump in -D8 code
29 * patch11: eval 'stuff' now optimized to eval {stuff}
31 * Revision 4.0.1.2 91/06/07 11:07:23 lwall
32 * patch4: new copyright notice
33 * patch4: length($`), length($&), length($') now optimized to avoid string copy
34 * patch4: assignment wasn't correctly de-tainting the assigned variable.
35 * patch4: default top-of-form format is now FILEHANDLE_TOP
36 * patch4: added $^P variable to control calling of perldb routines
37 * patch4: taintchecks could improperly modify parent in vfork()
38 * patch4: many, many itty-bitty portability fixes
40 * Revision 4.0.1.1 91/04/11 17:43:48 lwall
41 * patch1: fixed failed fork to return undef as documented
42 * patch1: reduced maximum branch distance in eval.c
44 * Revision 4.0 91/03/20 01:16:48 lwall
52 extern int (*ppaddr[])();
55 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
63 /* I_FCNTL *MUST* not be defined for MS-DOS and OS/2
64 but fcntl.h is required for O_BINARY */
74 double sin(), cos(), atan2(), pow();
95 int arglast[8]; /* highest sp for arg--valid only for non-O_LIST args */
96 unsigned long tmpulong;
109 VOIDRET (*ihand)(); /* place to save signal during system() */
110 VOIDRET (*qhand)(); /* place to save signal during system() */
111 bool assigning = FALSE;
112 int mymarkbase = savestack->ary_fill;
116 optype = arg->arg_type;
117 maxarg = arg->arg_len;
119 str = arg->arg_ptr.arg_str;
120 if (sp + maxarg > stack->ary_max)
121 astore(stack, sp + maxarg, Nullstr);
122 st = stack->ary_array;
127 deb("%s (%lx) %d args:\n",opname[optype],arg,maxarg);
129 debname[dlevel] = opname[optype][0];
130 debdelim[dlevel] = ':';
131 if (++dlevel >= dlmax)
138 markbase = mymarkbase;
139 saveint(&stack_mark);
142 for (anum = 1; anum <= maxarg; anum++) {
143 argflags = arg[anum].arg_flags;
144 argtype = arg[anum].arg_type;
145 argptr = arg[anum].arg_ptr;
149 if (!ppaddr[optype] || optype == O_SUBR || optype == O_DBSUBR) {
150 st[++sp] = &str_undef;
160 deb("%d.EXPR =>\n",anum);
163 sp = eval(argptr.arg_arg,
164 (argflags & AF_ARYOK) ? G_ARRAY : G_SCALAR, sp);
165 if (sp + (maxarg - anum) > stack->ary_max)
166 astore(stack, sp + (maxarg - anum), Nullstr);
167 st = stack->ary_array; /* possibly reallocated */
173 deb("%d.CMD (%lx) =>\n",anum,argptr.arg_cmd);
176 sp = cmd_exec(argptr.arg_cmd, gimme, sp);
177 if (sp + (maxarg - anum) > stack->ary_max)
178 astore(stack, sp + (maxarg - anum), Nullstr);
179 st = stack->ary_array; /* possibly reallocated */
184 case O_ITEM2: argtype = 2; break;
185 case O_ITEM3: argtype = 3; break;
186 default: argtype = anum; break;
188 str = afetch(stab_array(argptr.arg_stab),
189 arg[argtype].arg_len - arybase, TRUE);
192 (void)sprintf(buf,"LARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
193 arg[argtype].arg_len);
200 case O_ITEM2: argtype = 2; break;
201 case O_ITEM3: argtype = 3; break;
202 default: argtype = anum; break;
204 st[++sp] = afetch(stab_array(argptr.arg_stab),
205 arg[argtype].arg_len - arybase, FALSE);
208 (void)sprintf(buf,"ARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
209 arg[argtype].arg_len);
215 stab = argptr.arg_stab;
216 st[++sp] = (STR*)stab;
217 if (!stab_xarray(stab))
219 if (!stab_xhash(stab))
222 stab_io(stab) = stio_new();
225 (void)sprintf(buf,"STAR *%s -> *%s",
226 stab_name(argptr.arg_stab), stab_ename(argptr.arg_stab));
232 str = st[++sp] = (STR*)argptr.arg_stab;
235 (void)sprintf(buf,"LSTAR *%s -> *%s",
236 stab_name(argptr.arg_stab), stab_ename(argptr.arg_stab));
242 st[++sp] = STAB_STR(argptr.arg_stab);
245 (void)sprintf(buf,"STAB $%s",stab_name(argptr.arg_stab));
251 str_numset(str, (double)STAB_LEN(argptr.arg_stab));
255 (void)sprintf(buf,"LENSTAB $%s",stab_name(argptr.arg_stab));
264 deb("%d.LEXPR =>\n",anum);
267 if (argflags & AF_ARYOK) {
268 sp = eval(argptr.arg_arg, G_ARRAY, sp);
269 if (sp + (maxarg - anum) > stack->ary_max)
270 astore(stack, sp + (maxarg - anum), Nullstr);
271 st = stack->ary_array; /* possibly reallocated */
274 sp = eval(argptr.arg_arg, G_SCALAR, sp);
275 st = stack->ary_array; /* possibly reallocated */
283 (void)sprintf(buf,"LVAL $%s",stab_name(argptr.arg_stab));
288 str = STAB_STR(argptr.arg_stab);
290 fatal("panic: A_LVAL");
293 if (argflags & AF_PRE) {
294 if (argflags & AF_UP)
300 str = arg->arg_ptr.arg_str;
302 else if (argflags & AF_POST) {
303 st[sp] = str_mortal(str);
304 if (argflags & AF_UP)
309 str = arg->arg_ptr.arg_str;
316 stab = argptr.arg_stab;
317 str = stab_array(argptr.arg_stab)->ary_magic;
318 if (optype != O_SASSIGN || argflags & (AF_PRE|AF_POST))
319 str_numset(str,(double)(stab_array(stab)->ary_fill+arybase));
324 fatal("panic: A_LEXPR");
327 stab = argptr.arg_stab;
328 st[++sp] = stab_array(stab)->ary_magic;
329 str_numset(st[sp],(double)(stab_array(stab)->ary_fill+arybase));
335 st[++sp] = argptr.arg_str;
341 (void) interp(str,argptr.arg_str,sp);
342 st = stack->ary_array;
349 tmps = str_get(interp(str,argptr.arg_str,sp));
350 st = stack->ary_array;
354 fp = mypopen(tmps,"r");
357 if (gimme == G_SCALAR) {
358 while (str_gets(str,fp,str->str_cur) != Nullch)
364 if (++sp > stack->ary_max) {
365 astore(stack, sp, Nullstr);
366 st = stack->ary_array;
368 str = st[sp] = Str_new(56,80);
369 if (str_gets(str,fp,0) == Nullch) {
373 if (str->str_len - str->str_cur > 20) {
374 str->str_len = str->str_cur+1;
375 Renew(str->str_ptr, str->str_len, char);
380 statusvalue = mypclose(fp);
385 if (gimme == G_SCALAR)
393 if (curcsv->wantarray == G_ARRAY)
403 last_in_stab = stabent(str_get(STAB_STR(argptr.arg_stab)),TRUE);
408 argflags |= AF_POST; /* enable newline chopping */
409 last_in_stab = argptr.arg_stab;
424 last_in_stab = argptr.arg_stab;
428 if (anum > 1) /* assign to scalar */
429 gimme = G_SCALAR; /* force context to scalar */
430 if (gimme == G_ARRAY)
434 if (stab_io(last_in_stab)) {
435 fp = stab_io(last_in_stab)->ifp;
437 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
438 if (stab_io(last_in_stab)->flags & IOF_START) {
439 stab_io(last_in_stab)->flags &= ~IOF_START;
440 stab_io(last_in_stab)->lines = 0;
441 if (alen(stab_array(last_in_stab)) < 0) {
442 tmpstr = str_make("-",1); /* assume stdin */
443 (void)apush(stab_array(last_in_stab), tmpstr);
446 fp = nextargv(last_in_stab);
447 if (!fp) { /* Note: fp != stab_io(last_in_stab)->ifp */
448 (void)do_close(last_in_stab,FALSE); /* now it does*/
449 stab_io(last_in_stab)->flags |= IOF_START;
452 else if (argtype == A_GLOB) {
453 (void) interp(str,stab_val(last_in_stab),sp);
454 st = stack->ary_array;
455 tmpstr = Str_new(55,0);
457 str_set(tmpstr, "perlglob ");
458 str_scat(tmpstr,str);
459 str_cat(tmpstr," |");
462 str_nset(tmpstr,cshname,cshlen);
463 str_cat(tmpstr," -cf 'set nonomatch; glob ");
464 str_scat(tmpstr,str);
465 str_cat(tmpstr,"'|");
467 str_set(tmpstr, "echo ");
468 str_scat(tmpstr,str);
470 "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
473 (void)do_open(last_in_stab,tmpstr->str_ptr,
475 fp = stab_io(last_in_stab)->ifp;
481 warn("Read on closed filehandle <%s>",stab_ename(last_in_stab));
482 tmplen = str->str_len; /* remember if already alloced */
484 Str_Grow(str,80); /* try short-buffering it */
488 else if (!str_gets(str,fp, optype == O_RCAT ? str->str_cur : 0)) {
490 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
491 fp = nextargv(last_in_stab);
494 (void)do_close(last_in_stab,FALSE);
495 stab_io(last_in_stab)->flags |= IOF_START;
497 else if (argflags & AF_POST) {
498 (void)do_close(last_in_stab,FALSE);
503 if (gimme == G_ARRAY) {
511 stab_io(last_in_stab)->lines++;
514 str->str_tainted = 1; /* Anything from the outside world...*/
516 if (argflags & AF_POST) {
517 if (str->str_cur > 0)
519 if (str->str_ptr[str->str_cur] == rschar)
520 str->str_ptr[str->str_cur] = '\0';
523 for (tmps = str->str_ptr; *tmps; tmps++)
524 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
525 index("$&*(){}[]'\";\\|?<>~`",*tmps))
527 if (*tmps && stat(str->str_ptr,&statbuf) < 0)
528 goto keepgoing; /* unmatched wildcard? */
530 if (gimme == G_ARRAY) {
531 if (str->str_len - str->str_cur > 20) {
532 str->str_len = str->str_cur+1;
533 Renew(str->str_ptr, str->str_len, char);
536 if (++sp > stack->ary_max) {
537 astore(stack, sp, Nullstr);
538 st = stack->ary_array;
540 str = Str_new(58,80);
543 else if (!tmplen && str->str_len - str->str_cur > 80) {
544 /* try to reclaim a bit of scalar space on 1st alloc */
545 if (str->str_cur < 60)
548 str->str_len = str->str_cur+40; /* allow some slop */
549 Renew(str->str_ptr, str->str_len, char);
561 if (strEQ(tmps, "NULL"))
562 deb("%d.%s\n",anum,tmps);
564 deb("%d.%s = '%s'\n",anum,tmps,str_peek(st[sp]));
571 if (ppaddr[optype]) {
574 /* pretend like we've been maintaining stack_* all along */
575 stack_ary = stack->ary_array;
576 stack_sp = stack_ary + sp;
577 if (mark[optype] && stack_mark != arglast[0])
578 warn("Inconsistent stack mark %d != %d", stack_mark, arglast[0]);
579 stack_max = stack_ary + stack->ary_max;
581 status = (*ppaddr[optype])(str, arg, gimme);
583 if (savestack->ary_fill > mymarkbase) {
584 warn("Inconsistent stack base");
585 restorelist(mymarkbase);
587 sp = stack_sp - stack_ary;
589 warn("TOO MANY POPS");
597 if (optype < O_CHOWN)
604 if (gimme == G_ARRAY)
612 if (gimme == G_ARRAY)
615 STR_SSET(str,st[arglast[anum]-arglast[0]]);
619 if (gimme == G_ARRAY)
622 STR_SSET(str,st[arglast[anum]-arglast[0]]);
631 if (gimme == G_ARRAY && arg[1].arg_flags & AF_ARYOK) {
632 sp = do_repeatary(arglast);
636 anum = (int)str_gnum(st[2]);
638 tmpstr = Str_new(50, 0);
640 str_nset(tmpstr,tmps,str->str_cur);
641 tmps = str_get(tmpstr); /* force to be string */
642 STR_GROW(str, (anum * str->str_cur) + 1);
643 repeatcpy(str->str_ptr, tmps, tmpstr->str_cur, anum);
644 str->str_cur *= anum;
645 str->str_ptr[str->str_cur] = '\0';
650 if (dowarn && st[2]->str_pok && !looks_like_number(st[2]))
651 warn("Right operand of x is not numeric");
652 str_sset(str,&str_no);
657 sp = do_match(str,arg,
659 if (gimme == G_ARRAY)
664 sp = do_match(str,arg,
666 str_sset(str, str_true(str) ? &str_no : &str_yes);
670 sp = do_subst(str,arg,arglast[0]);
673 sp = do_subst(str,arg,arglast[0]);
674 str = arg->arg_ptr.arg_str;
675 str_set(str, str_true(str) ? No : Yes);
678 if (arg[1].arg_flags & AF_ARYOK) {
679 if (arg->arg_len == 1) {
680 arg->arg_type = O_LOCAL;
684 arg->arg_type = O_AASSIGN;
689 arg->arg_type = O_SASSIGN;
694 arglast[2] = arglast[1]; /* push a null array */
704 if (tainted && !st[2]->str_tainted)
707 STR_SSET(str, st[2]);
712 str = arg->arg_ptr.arg_str;
713 for (sp = arglast[0] + 1; sp <= arglast[1]; sp++)
718 if (arg[1].arg_type & A_DONT) {
719 sp = do_defined(str,arg,
723 else if (str->str_pok || str->str_nok)
727 if (arg[1].arg_type & A_DONT) {
728 sp = do_undef(str,arg,
732 else if (str != stab_val(defstab)) {
734 if (str->str_state == SS_INCR)
736 Safefree(str->str_ptr);
737 str->str_ptr = Nullch;
740 str->str_pok = str->str_nok = 0;
745 sp = do_study(str,arg,
749 value = str_gnum(st[1]);
750 value = pow(value,str_gnum(st[2]));
753 value = str_gnum(st[1]);
754 value *= str_gnum(st[2]);
757 if ((value = str_gnum(st[2])) == 0.0)
758 fatal("Illegal division by zero");
760 /* insure that 20./5. == 4. */
765 if ((double)(int)x == x &&
766 (double)(int)value == value &&
767 (k = (int)x/(int)value)*(int)value == (int)x) {
774 value = str_gnum(st[1]) / value;
778 tmpulong = (unsigned long) str_gnum(st[2]);
780 fatal("Illegal modulus zero");
782 value = str_gnum(st[1]);
784 value = (double)(((unsigned long)value) % tmpulong);
786 tmplong = (long)value;
787 value = (double)(tmpulong - ((-tmplong - 1) % tmpulong)) - 1;
792 value = str_gnum(st[1]);
793 value += str_gnum(st[2]);
796 value = str_gnum(st[1]);
797 value -= str_gnum(st[2]);
800 value = str_gnum(st[1]);
801 anum = (int)str_gnum(st[2]);
803 value = (double)(U_L(value) << anum);
807 value = str_gnum(st[1]);
808 anum = (int)str_gnum(st[2]);
810 value = (double)(U_L(value) >> anum);
814 value = str_gnum(st[1]);
815 value = (value < str_gnum(st[2])) ? 1.0 : 0.0;
818 value = str_gnum(st[1]);
819 value = (value > str_gnum(st[2])) ? 1.0 : 0.0;
822 value = str_gnum(st[1]);
823 value = (value <= str_gnum(st[2])) ? 1.0 : 0.0;
826 value = str_gnum(st[1]);
827 value = (value >= str_gnum(st[2])) ? 1.0 : 0.0;
831 if ((!st[1]->str_nok && !looks_like_number(st[1])) ||
832 (!st[2]->str_nok && !looks_like_number(st[2])) )
833 warn("Possible use of == on string value");
835 value = str_gnum(st[1]);
836 value = (value == str_gnum(st[2])) ? 1.0 : 0.0;
839 value = str_gnum(st[1]);
840 value = (value != str_gnum(st[2])) ? 1.0 : 0.0;
843 value = str_gnum(st[1]);
844 value -= str_gnum(st[2]);
847 else if (value < 0.0)
851 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
852 value = str_gnum(st[1]);
854 value = (double)(U_L(value) & U_L(str_gnum(st[2])));
859 do_vop(optype,str,st[1],st[2]);
862 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
863 value = str_gnum(st[1]);
865 value = (double)(U_L(value) ^ U_L(str_gnum(st[2])));
870 do_vop(optype,str,st[1],st[2]);
873 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
874 value = str_gnum(st[1]);
876 value = (double)(U_L(value) | U_L(str_gnum(st[2])));
881 do_vop(optype,str,st[1],st[2]);
883 /* use register in evaluating str_true() */
885 if (str_true(st[1])) {
888 argflags = arg[anum].arg_flags;
889 if (gimme == G_ARRAY)
890 argflags |= AF_ARYOK;
891 argtype = arg[anum].arg_type & A_MASK;
892 argptr = arg[anum].arg_ptr;
900 str_sset(str, st[1]);
908 if (str_true(st[1])) {
910 str_sset(str, st[1]);
920 argflags = arg[anum].arg_flags;
921 if (gimme == G_ARRAY)
922 argflags |= AF_ARYOK;
923 argtype = arg[anum].arg_type & A_MASK;
924 argptr = arg[anum].arg_ptr;
931 anum = (str_true(st[1]) ? 2 : 3);
932 optype = (anum == 2 ? O_ITEM2 : O_ITEM3);
933 argflags = arg[anum].arg_flags;
934 if (gimme == G_ARRAY)
935 argflags |= AF_ARYOK;
936 argtype = arg[anum].arg_type & A_MASK;
937 argptr = arg[anum].arg_ptr;
943 if (gimme == G_ARRAY)
948 value = -str_gnum(st[1]);
952 { char xxx = str_true(st[1]); value = (double) !xxx; }
954 value = (double) !str_true(st[1]);
958 if (!sawvec || st[1]->str_nok) {
960 value = (double) ~U_L(str_gnum(st[1]));
967 for (anum = str->str_cur; anum; anum--, tmps++)
972 stab_efullname(str,defoutstab);
974 if ((arg[1].arg_type & A_MASK) == A_WORD)
975 defoutstab = arg[1].arg_ptr.arg_stab;
977 defoutstab = stabent(str_get(st[1]),TRUE);
978 if (!stab_io(defoutstab))
979 stab_io(defoutstab) = stio_new();
980 curoutstab = defoutstab;
987 else if ((arg[1].arg_type & A_MASK) == A_WORD) {
988 if (!(stab = arg[1].arg_ptr.arg_stab))
992 stab = stabent(str_get(st[1]),TRUE);
993 if (!stab_io(stab)) {
999 fp = stab_io(stab)->ofp;
1000 if (stab_io(stab)->fmt_stab)
1001 form = stab_form(stab_io(stab)->fmt_stab);
1003 form = stab_form(stab);
1007 warn("No format for filehandle");
1009 if (stab_io(stab)->ifp)
1010 warn("Filehandle only opened for input");
1012 warn("Write on closed filehandle");
1019 format(&outrec,form,sp);
1020 do_write(&outrec,stab,sp);
1021 if (stab_io(stab)->flags & IOF_FLUSH)
1028 anum = arg[1].arg_type & A_MASK;
1029 if (anum == A_WORD || anum == A_STAB)
1030 stab = arg[1].arg_ptr.arg_stab;
1032 stab = stabent(str_get(st[1]),TRUE);
1033 if (st[3]->str_nok || st[3]->str_pok)
1034 anum = (int)str_gnum(st[3]);
1037 value = (double)hdbmopen(stab_hash(stab),str_get(st[2]),anum);
1040 fatal("No dbm or ndbm on this machine");
1044 anum = arg[1].arg_type & A_MASK;
1045 if (anum == A_WORD || anum == A_STAB)
1046 stab = arg[1].arg_ptr.arg_stab;
1048 stab = stabent(str_get(st[1]),TRUE);
1049 hdbmclose(stab_hash(stab));
1052 fatal("No dbm or ndbm on this machine");
1055 if ((arg[1].arg_type & A_MASK) == A_WORD)
1056 stab = arg[1].arg_ptr.arg_stab;
1058 stab = stabent(str_get(st[1]),TRUE);
1059 tmps = str_get(st[2]);
1060 if (do_open(stab,tmps,st[2]->str_cur)) {
1061 value = (double)forkprocess;
1062 stab_io(stab)->lines = 0;
1065 else if (forkprocess == 0) /* we are a new child */
1071 value = (double) do_trans(str,arg);
1072 str = arg->arg_ptr.arg_str;
1075 str_set(arg->arg_ptr.arg_str, do_trans(str,arg) == 0 ? Yes : No);
1076 str = arg->arg_ptr.arg_str;
1081 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1082 stab = arg[1].arg_ptr.arg_stab;
1084 stab = stabent(str_get(st[1]),TRUE);
1085 str_set(str, do_close(stab,TRUE) ? Yes : No );
1089 sp = do_each(str,stab_hash(arg[1].arg_ptr.arg_stab),
1094 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
1098 str->str_nok = str->str_pok = 0;
1099 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
1100 str->str_state = SS_ARY;
1103 ary = stab_array(arg[1].arg_ptr.arg_stab);
1104 maxarg = ary->ary_fill + 1;
1105 if (gimme == G_ARRAY) { /* array wanted */
1108 if (maxarg > 0 && sp + maxarg > stack->ary_max) {
1109 astore(stack,sp + maxarg, Nullstr);
1110 st = stack->ary_array;
1113 Copy(ary->ary_array, &st[1], maxarg, STR*);
1118 value = (double)maxarg;
1122 anum = ((int)str_gnum(st[2])) - arybase;
1123 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,FALSE);
1126 tmpstab = arg[1].arg_ptr.arg_stab;
1127 tmps = str_get(st[2]);
1128 str = hdelete(stab_hash(tmpstab),tmps,st[2]->str_cur);
1129 if (tmpstab == envstab)
1130 my_setenv(tmps,Nullch);
1135 str->str_nok = str->str_pok = 0;
1136 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
1137 str->str_state = SS_HASH;
1140 if (gimme == G_ARRAY) { /* array wanted */
1141 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
1146 tmpstab = arg[1].arg_ptr.arg_stab;
1147 if (!stab_hash(tmpstab)->tbl_fill)
1149 sprintf(buf,"%d/%d",stab_hash(tmpstab)->tbl_fill,
1150 stab_hash(tmpstab)->tbl_max+1);
1155 tmpstab = arg[1].arg_ptr.arg_stab;
1156 tmps = str_get(st[2]);
1157 str = hfetch(stab_hash(tmpstab),tmps,st[2]->str_cur,FALSE);
1160 anum = ((int)str_gnum(st[2])) - arybase;
1161 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,TRUE);
1162 if (!str || str == &str_undef)
1163 fatal("Assignment to non-creatable value, subscript %d",anum);
1166 tmpstab = arg[1].arg_ptr.arg_stab;
1167 tmps = str_get(st[2]);
1168 anum = st[2]->str_cur;
1169 str = hfetch(stab_hash(tmpstab),tmps,anum,TRUE);
1170 if (!str || str == &str_undef)
1171 fatal("Assignment to non-creatable value, subscript \"%s\"",tmps);
1172 if (tmpstab == envstab) /* heavy wizardry going on here */
1173 str_magic(str, tmpstab, 'E', tmps, anum); /* str is now magic */
1174 /* he threw the brick up into the air */
1175 else if (tmpstab == sigstab)
1176 str_magic(str, tmpstab, 'S', tmps, anum);
1178 else if (stab_hash(tmpstab)->tbl_dbm)
1179 str_magic(str, tmpstab, 'D', tmps, anum);
1181 else if (tmpstab == DBline)
1182 str_magic(str, tmpstab, 'L', tmps, anum);
1187 goto do_slice_already;
1191 goto do_slice_already;
1195 goto do_slice_already;
1199 goto do_slice_already;
1204 sp = do_slice(arg[1].arg_ptr.arg_stab,str,anum,argtype,
1208 sp = do_splice(stab_array(arg[1].arg_ptr.arg_stab),gimme,arglast);
1211 if (arglast[2] - arglast[1] != 1)
1212 str = do_push(stab_array(arg[1].arg_ptr.arg_stab),arglast);
1214 str = Str_new(51,0); /* must copy the STR */
1215 str_sset(str,st[2]);
1216 (void)apush(stab_array(arg[1].arg_ptr.arg_stab),str);
1220 str = apop(ary = stab_array(arg[1].arg_ptr.arg_stab));
1221 goto staticalization;
1223 str = ashift(ary = stab_array(arg[1].arg_ptr.arg_stab));
1227 if (ary->ary_flags & ARF_REAL)
1228 (void)str_2mortal(str);
1231 sp = do_unpack(str,gimme,arglast);
1234 value = str_gnum(st[3]);
1235 sp = do_split(str, arg[2].arg_ptr.arg_spat, (int)value,
1240 value = (double)str_len(stab_val(defstab));
1242 value = (double)str_len(st[1]);
1245 do_sprintf(str, sp-arglast[0], st+1);
1248 anum = ((int)str_gnum(st[2])) - arybase; /* anum=where to start*/
1249 tmps = str_get(st[1]); /* force conversion to string */
1251 if (argtype = (str == st[1]))
1252 str = arg->arg_ptr.arg_str;
1254 anum += st[1]->str_cur + arybase;
1255 if (anum < 0 || anum > st[1]->str_cur)
1258 optype = maxarg < 3 ? st[1]->str_cur : (int)str_gnum(st[3]);
1262 anum = st[1]->str_cur - anum; /* anum=how many bytes left*/
1265 str_nset(str, tmps, anum);
1266 if (argtype) { /* it's an lvalue! */
1267 Lstring *lstr = (Lstring*)str;
1269 str->str_magic = st[1];
1270 st[1]->str_rare = 's';
1271 lstr->lstr_offset = tmps - str_get(st[1]);
1272 lstr->lstr_len = anum;
1278 (void)do_pack(str,arglast);
1281 sp = do_grep(arg,str,gimme,arglast);
1284 do_join(str,arglast);
1287 tmps = str_get(st[1]);
1288 value = (double) (str_cmp(st[1],st[2]) < 0);
1291 tmps = str_get(st[1]);
1292 value = (double) (str_cmp(st[1],st[2]) > 0);
1295 tmps = str_get(st[1]);
1296 value = (double) (str_cmp(st[1],st[2]) <= 0);
1299 tmps = str_get(st[1]);
1300 value = (double) (str_cmp(st[1],st[2]) >= 0);
1303 tmps = str_get(st[1]);
1304 value = (double) str_eq(st[1],st[2]);
1307 tmps = str_get(st[1]);
1308 value = (double) !str_eq(st[1],st[2]);
1311 tmps = str_get(st[1]);
1312 value = (double) str_cmp(st[1],st[2]);
1315 sp = do_subr(arg,gimme,arglast);
1316 st = stack->ary_array + arglast[0]; /* maybe realloced */
1319 sp = do_subr(arg,gimme,arglast);
1320 st = stack->ary_array + arglast[0]; /* maybe realloced */
1323 sp = do_caller(arg,maxarg,gimme,arglast);
1324 st = stack->ary_array + arglast[0]; /* maybe realloced */
1327 sp = do_sort(str,arg,
1331 if (gimme == G_ARRAY)
1332 sp = do_reverse(arglast);
1334 sp = do_sreverse(str, arglast);
1337 if (arglast[2] - arglast[1] != 1) {
1338 do_join(str,arglast);
1339 tmps = str_get(str);
1343 tmps = str_get(st[2]);
1345 if (!tmps || !*tmps)
1346 tmps = "Warning: something's wrong";
1350 if (arglast[2] - arglast[1] != 1) {
1351 do_join(str,arglast);
1352 tmps = str_get(str);
1356 tmps = str_get(st[2]);
1358 if (!tmps || !*tmps)
1364 if ((arg[1].arg_type & A_MASK) == A_WORD)
1365 stab = arg[1].arg_ptr.arg_stab;
1367 stab = stabent(str_get(st[1]),TRUE);
1370 if (!stab_io(stab)) {
1372 warn("Filehandle never opened");
1375 if (!(fp = stab_io(stab)->ofp)) {
1377 if (stab_io(stab)->ifp)
1378 warn("Filehandle opened only for input");
1380 warn("Print on closed filehandle");
1385 if (optype == O_PRTF || arglast[2] - arglast[1] != 1)
1386 value = (double)do_aprint(arg,fp,arglast);
1388 value = (double)do_print(st[2],fp);
1389 if (orslen && optype == O_PRINT)
1390 if (fwrite(ors, 1, orslen, fp) == 0)
1393 if (stab_io(stab)->flags & IOF_FLUSH)
1394 if (fflush(fp) == EOF)
1402 tmps = str_get(st[1]);
1403 if (!tmps || !*tmps) {
1404 tmpstr = hfetch(stab_hash(envstab),"HOME",4,FALSE);
1405 tmps = str_get(tmpstr);
1407 if (!tmps || !*tmps) {
1408 tmpstr = hfetch(stab_hash(envstab),"LOGDIR",6,FALSE);
1409 tmps = str_get(tmpstr);
1412 TAINT_PROPER("chdir");
1414 value = (double)(chdir(tmps) >= 0);
1420 anum = (int)str_gnum(st[1]);
1427 tmps = str_get(st[1]);
1428 str_reset(tmps,curcmd->c_stash);
1432 if (gimme == G_ARRAY)
1435 str = st[sp - arglast[0]]; /* unwanted list, return last item */
1441 stab = last_in_stab;
1442 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1443 stab = arg[1].arg_ptr.arg_stab;
1445 stab = stabent(str_get(st[1]),TRUE);
1446 str_set(str, do_eof(stab) ? Yes : No);
1452 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1453 stab = arg[1].arg_ptr.arg_stab;
1455 stab = stabent(str_get(st[1]),TRUE);
1458 if (!stab || do_eof(stab)) /* make sure we have fp with something */
1465 *str->str_ptr = getc(stab_io(stab)->ifp); /* should never be EOF */
1471 stab = last_in_stab;
1472 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1473 stab = arg[1].arg_ptr.arg_stab;
1475 stab = stabent(str_get(st[1]),TRUE);
1477 value = (double)do_tell(stab);
1479 (void)do_tell(stab);
1485 if ((arg[1].arg_type & A_MASK) == A_WORD)
1486 stab = arg[1].arg_ptr.arg_stab;
1488 stab = stabent(str_get(st[1]),TRUE);
1489 tmps = str_get(st[2]);
1490 anum = (int)str_gnum(st[3]);
1492 maxarg = sp - arglast[0];
1494 warn("Too many args on read");
1496 maxarg = (int)str_gnum(st[4]);
1499 if (!stab_io(stab) || !stab_io(stab)->ifp)
1502 if (optype == O_RECV) {
1503 argtype = sizeof buf;
1504 STR_GROW(st[2], anum+1), (tmps = str_get(st[2])); /* sneaky */
1505 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, maxarg,
1508 st[2]->str_cur = anum;
1509 st[2]->str_ptr[anum] = '\0';
1510 str_nset(str,buf,argtype);
1513 str_sset(str,&str_undef);
1517 if (optype == O_RECV)
1520 STR_GROW(st[2], anum+maxarg+1), (tmps = str_get(st[2])); /* sneaky */
1521 if (optype == O_SYSREAD) {
1522 anum = read(fileno(stab_io(stab)->ifp), tmps+maxarg, anum);
1526 if (stab_io(stab)->type == 's') {
1527 argtype = sizeof buf;
1528 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps+maxarg, anum, 0,
1533 anum = fread(tmps+maxarg, 1, anum, stab_io(stab)->ifp);
1536 st[2]->str_cur = anum+maxarg;
1537 st[2]->str_ptr[anum+maxarg] = '\0';
1538 value = (double)anum;
1542 if ((arg[1].arg_type & A_MASK) == A_WORD)
1543 stab = arg[1].arg_ptr.arg_stab;
1545 stab = stabent(str_get(st[1]),TRUE);
1546 tmps = str_get(st[2]);
1547 anum = (int)str_gnum(st[3]);
1549 stio = stab_io(stab);
1550 maxarg = sp - arglast[0];
1551 if (!stio || !stio->ifp) {
1554 if (optype == O_SYSWRITE)
1555 warn("Syswrite on closed filehandle");
1557 warn("Send on closed socket");
1560 else if (optype == O_SYSWRITE) {
1562 warn("Too many args on syswrite");
1564 optype = (int)str_gnum(st[4]);
1567 anum = write(fileno(stab_io(stab)->ifp), tmps+optype, anum);
1570 else if (maxarg >= 4) {
1572 warn("Too many args on send");
1573 tmps2 = str_get(st[4]);
1574 anum = sendto(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur,
1575 anum, tmps2, st[4]->str_cur);
1578 anum = send(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur, anum);
1585 value = (double)anum;
1588 if ((arg[1].arg_type & A_MASK) == A_WORD)
1589 stab = arg[1].arg_ptr.arg_stab;
1591 stab = stabent(str_get(st[1]),TRUE);
1592 value = str_gnum(st[2]);
1593 str_set(str, do_seek(stab,
1594 (long)value, (int)str_gnum(st[3]) ) ? Yes : No);
1598 tmps = "_SUB_"; /* just fake up a "last _SUB_" */
1600 if (curcsv && curcsv->wantarray == G_ARRAY) {
1601 lastretstr = Nullstr;
1602 lastspbase = arglast[1];
1603 lastsize = arglast[2] - arglast[1];
1606 lastretstr = str_mortal(st[arglast[2] - arglast[0]]);
1613 tmps = str_get(arg[1].arg_ptr.arg_str);
1615 while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
1616 strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
1619 deb("(Skipping label #%d %s)\n",loop_ptr,
1620 loop_stack[loop_ptr].loop_label);
1627 deb("(Found label #%d %s)\n",loop_ptr,
1628 loop_stack[loop_ptr].loop_label);
1633 if (tmps && strEQ(tmps, "_SUB_"))
1634 fatal("Can't return outside a subroutine");
1635 fatal("Bad label: %s", maxarg > 0 ? tmps : "<null>");
1637 if (!lastretstr && optype == O_LAST && lastsize) {
1639 st += lastspbase + 1;
1640 optype = loop_stack[loop_ptr].loop_sp - lastspbase; /* negative */
1642 for (anum = lastsize; anum > 0; anum--,st++)
1643 st[optype] = str_mortal(st[0]);
1645 longjmp(loop_stack[loop_ptr].loop_env, O_LAST);
1647 longjmp(loop_stack[loop_ptr].loop_env, optype);
1649 case O_GOTO:/* shudder */
1650 goto_targ = str_get(arg[1].arg_ptr.arg_str);
1652 goto_targ = Nullch; /* just restart from top */
1653 if (optype == O_DUMP) {
1657 longjmp(top_env, 1);
1659 tmps = str_get(st[1]);
1663 anum = (int) str_gnum(st[3]) - arybase;
1666 else if (anum > st[1]->str_cur)
1667 anum = st[1]->str_cur;
1670 if (!(tmps2 = fbminstr((unsigned char*)tmps + anum,
1671 (unsigned char*)tmps + st[1]->str_cur, st[2])))
1673 if (tmps2 = fbminstr(Null(unsigned char*),Null(unsigned char*),Nullstr))
1675 value = (double)(-1 + arybase);
1677 value = (double)(tmps2 - tmps + arybase);
1680 tmps = str_get(st[1]);
1681 tmps2 = str_get(st[2]);
1683 anum = st[1]->str_cur;
1685 anum = (int) str_gnum(st[3]) - arybase + st[2]->str_cur;
1688 else if (anum > st[1]->str_cur)
1689 anum = st[1]->str_cur;
1692 if (!(tmps2 = rninstr(tmps, tmps + anum,
1693 tmps2, tmps2 + st[2]->str_cur)))
1695 if (tmps2 = rninstr(Nullch,Nullch,Nullch,Nullch))
1697 value = (double)(-1 + arybase);
1699 value = (double)(tmps2 - tmps + arybase);
1703 value = (double) time(Null(long*));
1707 sp = do_tms(str,gimme,arglast);
1713 when = (time_t)str_gnum(st[1]);
1714 sp = do_time(str,localtime(&when),
1721 when = (time_t)str_gnum(st[1]);
1722 sp = do_time(str,gmtime(&when),
1726 sp = do_truncate(str,arg,
1731 sp = do_stat(str,arg,
1736 tmps = str_get(st[1]);
1738 str_set(str,fcrypt(tmps,str_get(st[2])));
1740 str_set(str,crypt(tmps,str_get(st[2])));
1744 "The crypt() function is unimplemented due to excessive paranoia.");
1748 value = str_gnum(st[1]);
1749 value = atan2(value,str_gnum(st[2]));
1753 value = str_gnum(stab_val(defstab));
1755 value = str_gnum(st[1]);
1760 value = str_gnum(stab_val(defstab));
1762 value = str_gnum(st[1]);
1769 value = str_gnum(st[1]);
1773 value = rand() * value / 2147483648.0;
1776 value = rand() * value / 65536.0;
1779 value = rand() * value / 32768.0;
1781 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1792 anum = (int)str_gnum(st[1]);
1797 value = str_gnum(stab_val(defstab));
1799 value = str_gnum(st[1]);
1804 value = str_gnum(stab_val(defstab));
1806 value = str_gnum(st[1]);
1808 fatal("Can't take log of %g\n", value);
1813 value = str_gnum(stab_val(defstab));
1815 value = str_gnum(st[1]);
1817 fatal("Can't take sqrt of %g\n", value);
1818 value = sqrt(value);
1822 value = str_gnum(stab_val(defstab));
1824 value = str_gnum(st[1]);
1826 (void)modf(value,&value);
1828 (void)modf(-value,&value);
1834 tmps = str_get(stab_val(defstab));
1836 tmps = str_get(st[1]);
1838 value = (double) (*tmps & 255);
1841 value = (double) (anum & 255);
1847 tmps = str_get(stab_val(defstab));
1849 tmps = str_get(st[1]);
1852 anum = alarm((unsigned int)atoi(tmps));
1855 value = (double)anum;
1858 fatal("Unsupported function alarm");
1865 tmps = str_get(st[1]);
1867 if (!tmps || !*tmps)
1868 sleep((32767<<16)+32767);
1870 sleep((unsigned int)atoi(tmps));
1872 value = (double)when;
1874 value = ((double)when) - value;
1878 sp = do_range(gimme,arglast);
1881 if (gimme == G_ARRAY) { /* it's a range */
1882 /* can we optimize to constant array? */
1883 if ((arg[1].arg_type & A_MASK) == A_SINGLE &&
1884 (arg[2].arg_type & A_MASK) == A_SINGLE) {
1885 st[2] = arg[2].arg_ptr.arg_str;
1886 sp = do_range(gimme,arglast);
1887 st = stack->ary_array;
1888 maxarg = sp - arglast[0];
1889 str_free(arg[1].arg_ptr.arg_str);
1890 arg[1].arg_ptr.arg_str = Nullstr;
1891 str_free(arg[2].arg_ptr.arg_str);
1892 arg[2].arg_ptr.arg_str = Nullstr;
1893 arg->arg_type = O_ARRAY;
1894 arg[1].arg_type = A_STAB|A_DONT;
1896 stab = arg[1].arg_ptr.arg_stab = aadd(genstab());
1897 ary = stab_array(stab);
1898 afill(ary,maxarg - 1);
1901 while (maxarg-- > 0)
1902 ary->ary_array[maxarg] = str_smake(st[maxarg]);
1906 arg->arg_type = optype = O_RANGE;
1907 maxarg = arg->arg_len = 2;
1909 arg[anum].arg_flags &= ~AF_ARYOK;
1910 argflags = arg[anum].arg_flags;
1911 argtype = arg[anum].arg_type & A_MASK;
1912 arg[anum].arg_type = argtype;
1913 argptr = arg[anum].arg_ptr;
1919 arg->arg_type = O_FLIP;
1922 if ((arg[1].arg_type & A_MASK) == A_SINGLE ?
1923 last_in_stab && (int)str_gnum(st[1]) == stab_io(last_in_stab)->lines
1926 arg[2].arg_type &= ~A_DONT;
1927 arg[1].arg_type |= A_DONT;
1928 arg->arg_type = optype = O_FLOP;
1929 if (arg->arg_flags & AF_COMMON) {
1930 str_numset(str,0.0);
1932 argflags = arg[2].arg_flags;
1933 argtype = arg[2].arg_type & A_MASK;
1934 argptr = arg[2].arg_ptr;
1940 str_numset(str,1.0);
1948 if ((arg[2].arg_type & A_MASK) == A_SINGLE ?
1949 last_in_stab && (int)str_gnum(st[2]) == stab_io(last_in_stab)->lines
1952 arg->arg_type = O_FLIP;
1953 arg[1].arg_type &= ~A_DONT;
1954 arg[2].arg_type |= A_DONT;
1965 if (tmpstab = stabent("$",allstabs))
1966 str_numset(STAB_STR(tmpstab),(double)getpid());
1967 hclear(pidstatus, FALSE); /* no kids, so don't wait for 'em */
1969 value = (double)anum;
1972 fatal("Unsupported function fork");
1978 anum = wait(&argflags);
1980 pidgone(anum,argflags);
1981 value = (double)anum;
1983 statusvalue = (unsigned short)argflags;
1986 fatal("Unsupported function wait");
1992 anum = (int)str_gnum(st[1]);
1993 optype = (int)str_gnum(st[2]);
1994 anum = wait4pid(anum, &argflags,optype);
1995 value = (double)anum;
1997 statusvalue = (unsigned short)argflags;
2000 fatal("Unsupported function wait");
2006 if (arglast[2] - arglast[1] == 1) {
2008 tainted |= st[2]->str_tainted;
2009 TAINT_PROPER("system");
2012 while ((anum = vfork()) == -1) {
2013 if (errno != EAGAIN) {
2021 ihand = signal(SIGINT, SIG_IGN);
2022 qhand = signal(SIGQUIT, SIG_IGN);
2023 argtype = wait4pid(anum, &argflags, 0);
2027 (void)signal(SIGINT, ihand);
2028 (void)signal(SIGQUIT, qhand);
2029 statusvalue = (unsigned short)argflags;
2033 value = (double)((unsigned int)argflags & 0xffff);
2035 do_execfree(); /* free any memory child malloced on vfork */
2038 if ((arg[1].arg_type & A_MASK) == A_STAB)
2039 value = (double)do_aexec(st[1],arglast);
2040 else if (arglast[2] - arglast[1] != 1)
2041 value = (double)do_aexec(Nullstr,arglast);
2043 value = (double)do_exec(str_get(str_mortal(st[2])));
2047 if ((arg[1].arg_type & A_MASK) == A_STAB)
2048 value = (double)do_aspawn(st[1],arglast);
2049 else if (arglast[2] - arglast[1] != 1)
2050 value = (double)do_aspawn(Nullstr,arglast);
2052 value = (double)do_spawn(str_get(str_mortal(st[2])));
2057 if ((arg[1].arg_type & A_MASK) == A_STAB)
2058 value = (double)do_aexec(st[1],arglast);
2059 else if (arglast[2] - arglast[1] != 1)
2060 value = (double)do_aexec(Nullstr,arglast);
2064 tainted |= st[2]->str_tainted;
2065 TAINT_PROPER("exec");
2067 value = (double)do_exec(str_get(str_mortal(st[2])));
2072 tmps = str_get(stab_val(defstab));
2074 tmps = str_get(st[1]);
2075 value = (double)scanhex(tmps, 99, &argtype);
2080 tmps = str_get(stab_val(defstab));
2082 tmps = str_get(st[1]);
2083 while (*tmps && (isSPACE(*tmps) || *tmps == '0'))
2086 value = (double)scanhex(++tmps, 99, &argtype);
2088 value = (double)scanoct(tmps, 99, &argtype);
2091 /* These common exits are hidden here in the middle of the switches for the
2092 benefit of those machines with limited branch addressing. Sigh. */
2099 anum = sp - arglast[0];
2102 deb("%s RETURNS ()\n",opname[optype]);
2105 deb("%s RETURNS (\"%s\")\n",opname[optype],
2106 st[1] ? str_get(st[1]) : "");
2109 tmps = st[1] ? str_get(st[1]) : "";
2110 deb("%s RETURNS %d ARGS (\"%s\",%s\"%s\")\n",opname[optype],
2111 anum,tmps,anum==2?"":"...,",
2112 st[anum] ? str_get(st[anum]) : "");
2118 stack_ary = stack->ary_array;
2119 stack_max = stack_ary + stack->ary_max;
2120 stack_sp = stack_ary + sp;
2140 str_numset(str,value);
2147 deb("%s RETURNS \"%f\"\n",opname[optype],value);
2150 stack_ary = stack->ary_array;
2151 stack_max = stack_ary + stack->ary_max;
2152 stack_sp = stack_ary + arglast[0] + 1;
2153 return arglast[0] + 1;
2154 #ifdef SMALLSWITCHES
2161 value = (double)apply(optype,arglast);
2164 fatal("Unsupported function chown");
2169 value = (double)apply(optype,arglast);
2172 fatal("Unsupported function kill");
2178 value = (double)apply(optype,arglast);
2187 anum = umask((int)str_gnum(st[1]));
2188 value = (double)anum;
2190 TAINT_PROPER("umask");
2194 fatal("Unsupported function umask");
2197 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2201 if ((anum = do_ipcget(optype, arglast)) == -1)
2203 value = (double)anum;
2208 anum = do_ipcctl(optype, arglast);
2212 value = (double)anum;
2215 str_set(str,"0 but true");
2219 value = (double)(do_msgsnd(arglast) >= 0);
2222 value = (double)(do_msgrcv(arglast) >= 0);
2225 value = (double)(do_semop(arglast) >= 0);
2229 value = (double)(do_shmio(optype, arglast) >= 0);
2231 #else /* not SYSVIPC */
2243 fatal("System V IPC is not implemented on this machine");
2244 #endif /* not SYSVIPC */
2246 tmps = str_get(st[1]);
2247 tmps2 = str_get(st[2]);
2249 TAINT_PROPER("rename");
2252 value = (double)(rename(tmps,tmps2) >= 0);
2254 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
2257 if (euid || stat(tmps2,&statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
2258 (void)UNLINK(tmps2);
2259 if (!(anum = link(tmps,tmps2)))
2260 anum = UNLINK(tmps);
2262 value = (double)(anum >= 0);
2267 tmps = str_get(st[1]);
2268 tmps2 = str_get(st[2]);
2270 TAINT_PROPER("link");
2272 value = (double)(link(tmps,tmps2) >= 0);
2275 fatal("Unsupported function link");
2279 tmps = str_get(st[1]);
2280 anum = (int)str_gnum(st[2]);
2282 TAINT_PROPER("mkdir");
2285 value = (double)(mkdir(tmps,anum) >= 0);
2288 (void)strcpy(buf,"mkdir ");
2290 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
2292 for (tmps2 = buf+6; *tmps; ) {
2296 (void)strcpy(tmps2," 2>&1");
2297 rsfp = mypopen(buf,"r");
2300 tmps2 = fgets(buf,sizeof buf,rsfp);
2301 (void)mypclose(rsfp);
2302 if (tmps2 != Nullch) {
2303 for (errno = 1; errno < sys_nerr; errno++) {
2304 if (instr(buf,sys_errlist[errno])) /* you don't see this */
2309 #define EACCES EPERM
2311 if (instr(buf,"cannot make"))
2313 else if (instr(buf,"existing file"))
2315 else if (instr(buf,"ile exists"))
2317 else if (instr(buf,"non-exist"))
2319 else if (instr(buf,"does not exist"))
2321 else if (instr(buf,"not empty"))
2323 else if (instr(buf,"cannot access"))
2329 else { /* some mkdirs return no failure indication */
2330 tmps = str_get(st[1]);
2331 anum = (stat(tmps,&statbuf) >= 0);
2332 if (optype == O_RMDIR)
2337 errno = EACCES; /* a guess */
2338 value = (double)anum;
2347 tmps = str_get(stab_val(defstab));
2349 tmps = str_get(st[1]);
2351 TAINT_PROPER("rmdir");
2354 value = (double)(rmdir(tmps) >= 0);
2357 (void)strcpy(buf,"rmdir ");
2358 goto one_liner; /* see above in HAS_MKDIR */
2362 value = (double)getppid();
2365 fatal("Unsupported function getppid");
2373 anum = (int)str_gnum(st[1]);
2374 #ifdef _POSIX_SOURCE
2376 fatal("POSIX getpgrp can't take an argument");
2377 value = (double)getpgrp();
2379 value = (double)getpgrp(anum);
2383 fatal("The getpgrp() function is unimplemented on this machine");
2388 argtype = (int)str_gnum(st[1]);
2389 anum = (int)str_gnum(st[2]);
2391 TAINT_PROPER("setpgrp");
2393 value = (double)(setpgrp(argtype,anum) >= 0);
2396 fatal("The setpgrp() function is unimplemented on this machine");
2400 #ifdef HAS_GETPRIORITY
2401 argtype = (int)str_gnum(st[1]);
2402 anum = (int)str_gnum(st[2]);
2403 value = (double)getpriority(argtype,anum);
2406 fatal("The getpriority() function is unimplemented on this machine");
2410 #ifdef HAS_SETPRIORITY
2411 argtype = (int)str_gnum(st[1]);
2412 anum = (int)str_gnum(st[2]);
2413 optype = (int)str_gnum(st[3]);
2415 TAINT_PROPER("setpriority");
2417 value = (double)(setpriority(argtype,anum,optype) >= 0);
2420 fatal("The setpriority() function is unimplemented on this machine");
2426 tmps = str_get(stab_val(defstab));
2428 tmps = str_get(st[1]);
2430 TAINT_PROPER("chroot");
2432 value = (double)(chroot(tmps) >= 0);
2435 fatal("Unsupported function chroot");
2441 stab = last_in_stab;
2442 else if ((arg[1].arg_type & A_MASK) == A_WORD)
2443 stab = arg[1].arg_ptr.arg_stab;
2445 stab = stabent(str_get(st[1]),TRUE);
2446 argtype = U_I(str_gnum(st[2]));
2448 TAINT_PROPER("ioctl");
2450 anum = do_ctl(optype,stab,argtype,st[3]);
2454 value = (double)anum;
2457 str_set(str,"0 but true");
2463 stab = last_in_stab;
2464 else if ((arg[1].arg_type & A_MASK) == A_WORD)
2465 stab = arg[1].arg_ptr.arg_stab;
2467 stab = stabent(str_get(st[1]),TRUE);
2468 if (stab && stab_io(stab))
2469 fp = stab_io(stab)->ifp;
2473 argtype = (int)str_gnum(st[2]);
2474 value = (double)(flock(fileno(fp),argtype) >= 0);
2480 fatal("The flock() function is unimplemented on this machine");
2484 ary = stab_array(arg[1].arg_ptr.arg_stab);
2485 if (arglast[2] - arglast[1] != 1)
2486 do_unshift(ary,arglast);
2488 STR *tmpstr = Str_new(52,0); /* must copy the STR */
2489 str_sset(tmpstr,st[2]);
2491 (void)astore(ary,0,tmpstr);
2493 value = (double)(ary->ary_fill + 1);
2497 sp = do_try(arg[1].arg_ptr.arg_cmd,
2502 sp = do_eval(st[1], O_EVAL, curcmd->c_stash, TRUE,
2505 str_free(arg[1].arg_ptr.arg_str);
2506 arg[1].arg_ptr.arg_cmd = eval_root;
2507 arg[1].arg_type = (A_CMD|A_DONT);
2508 arg[0].arg_type = O_TRY;
2516 tmpstr = stab_val(defstab);
2519 (arg[1].arg_type & A_MASK) != A_NULL ? st[1] : stab_val(defstab);
2521 tainted |= tmpstr->str_tainted;
2522 TAINT_PROPER("eval");
2524 sp = do_eval(tmpstr, optype, curcmd->c_stash, FALSE,
2552 if (mystat(arg,st[1]) < 0)
2554 if (cando(anum,argtype,&statcache))
2559 if (mystat(arg,st[1]) < 0)
2564 if (mystat(arg,st[1]) < 0)
2566 if (statcache.st_uid == (optype == O_FTEOWNED ? euid : uid) )
2570 if (mystat(arg,st[1]) < 0)
2572 if (!statcache.st_size)
2576 if (mystat(arg,st[1]) < 0)
2578 value = (double)statcache.st_size;
2582 if (mystat(arg,st[1]) < 0)
2584 value = (double)(basetime - statcache.st_mtime) / 86400.0;
2587 if (mystat(arg,st[1]) < 0)
2589 value = (double)(basetime - statcache.st_atime) / 86400.0;
2592 if (mystat(arg,st[1]) < 0)
2594 value = (double)(basetime - statcache.st_ctime) / 86400.0;
2598 if (mystat(arg,st[1]) < 0)
2600 if (S_ISSOCK(statcache.st_mode))
2604 if (mystat(arg,st[1]) < 0)
2606 if (S_ISCHR(statcache.st_mode))
2610 if (mystat(arg,st[1]) < 0)
2612 if (S_ISBLK(statcache.st_mode))
2616 if (mystat(arg,st[1]) < 0)
2618 if (S_ISREG(statcache.st_mode))
2622 if (mystat(arg,st[1]) < 0)
2624 if (S_ISDIR(statcache.st_mode))
2628 if (mystat(arg,st[1]) < 0)
2630 if (S_ISFIFO(statcache.st_mode))
2634 if (mylstat(arg,st[1]) < 0)
2636 if (S_ISLNK(statcache.st_mode))
2641 tmps = str_get(st[1]);
2642 tmps2 = str_get(st[2]);
2644 TAINT_PROPER("symlink");
2646 value = (double)(symlink(tmps,tmps2) >= 0);
2649 fatal("Unsupported function symlink");
2654 tmps = str_get(stab_val(defstab));
2656 tmps = str_get(st[1]);
2657 anum = readlink(tmps,buf,sizeof buf);
2660 str_nset(str,buf,anum);
2663 goto say_undef; /* just pretend it's a normal file */
2686 if (mystat(arg,st[1]) < 0)
2688 if (statcache.st_mode & anum)
2692 if (arg[1].arg_type & A_DONT) {
2693 stab = arg[1].arg_ptr.arg_stab;
2697 stab = stabent(tmps = str_get(st[1]),FALSE);
2698 if (stab && stab_io(stab) && stab_io(stab)->ifp)
2699 anum = fileno(stab_io(stab)->ifp);
2700 else if (isDIGIT(*tmps))
2709 str = do_fttext(arg,st[1]);
2713 if ((arg[1].arg_type & A_MASK) == A_WORD)
2714 stab = arg[1].arg_ptr.arg_stab;
2716 stab = stabent(str_get(st[1]),TRUE);
2718 value = (double)do_socket(stab,arglast);
2720 (void)do_socket(stab,arglast);
2724 if ((arg[1].arg_type & A_MASK) == A_WORD)
2725 stab = arg[1].arg_ptr.arg_stab;
2727 stab = stabent(str_get(st[1]),TRUE);
2729 value = (double)do_bind(stab,arglast);
2731 (void)do_bind(stab,arglast);
2735 if ((arg[1].arg_type & A_MASK) == A_WORD)
2736 stab = arg[1].arg_ptr.arg_stab;
2738 stab = stabent(str_get(st[1]),TRUE);
2740 value = (double)do_connect(stab,arglast);
2742 (void)do_connect(stab,arglast);
2746 if ((arg[1].arg_type & A_MASK) == A_WORD)
2747 stab = arg[1].arg_ptr.arg_stab;
2749 stab = stabent(str_get(st[1]),TRUE);
2751 value = (double)do_listen(stab,arglast);
2753 (void)do_listen(stab,arglast);
2757 if ((arg[1].arg_type & A_MASK) == A_WORD)
2758 stab = arg[1].arg_ptr.arg_stab;
2760 stab = stabent(str_get(st[1]),TRUE);
2761 if ((arg[2].arg_type & A_MASK) == A_WORD)
2762 stab2 = arg[2].arg_ptr.arg_stab;
2764 stab2 = stabent(str_get(st[2]),TRUE);
2765 do_accept(str,stab,stab2);
2773 sp = do_ghent(optype,
2781 sp = do_gnent(optype,
2789 sp = do_gpent(optype,
2797 sp = do_gsent(optype,
2801 value = (double) sethostent((int)str_gnum(st[1]));
2804 value = (double) setnetent((int)str_gnum(st[1]));
2807 value = (double) setprotoent((int)str_gnum(st[1]));
2810 value = (double) setservent((int)str_gnum(st[1]));
2813 value = (double) endhostent();
2816 value = (double) endnetent();
2819 value = (double) endprotoent();
2822 value = (double) endservent();
2825 if ((arg[1].arg_type & A_MASK) == A_WORD)
2826 stab = arg[1].arg_ptr.arg_stab;
2828 stab = stabent(str_get(st[1]),TRUE);
2829 if ((arg[2].arg_type & A_MASK) == A_WORD)
2830 stab2 = arg[2].arg_ptr.arg_stab;
2832 stab2 = stabent(str_get(st[2]),TRUE);
2834 value = (double)do_spair(stab,stab2,arglast);
2836 (void)do_spair(stab,stab2,arglast);
2840 if ((arg[1].arg_type & A_MASK) == A_WORD)
2841 stab = arg[1].arg_ptr.arg_stab;
2843 stab = stabent(str_get(st[1]),TRUE);
2845 value = (double)do_shutdown(stab,arglast);
2847 (void)do_shutdown(stab,arglast);
2852 if ((arg[1].arg_type & A_MASK) == A_WORD)
2853 stab = arg[1].arg_ptr.arg_stab;
2855 stab = stabent(str_get(st[1]),TRUE);
2856 sp = do_sopt(optype,stab,arglast);
2860 if ((arg[1].arg_type & A_MASK) == A_WORD)
2861 stab = arg[1].arg_ptr.arg_stab;
2863 stab = stabent(str_get(st[1]),TRUE);
2866 sp = do_getsockname(optype,stab,arglast);
2869 #else /* HAS_SOCKET not defined */
2902 fatal("Unsupported socket function");
2903 #endif /* HAS_SOCKET */
2906 sp = do_select(gimme,arglast);
2909 fatal("select not implemented");
2914 if ((arg[1].arg_type & A_MASK) == A_WORD)
2915 stab = arg[1].arg_ptr.arg_stab;
2917 stab = stabent(str_get(st[1]),TRUE);
2918 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2925 if ((arg[1].arg_type & A_MASK) == A_WORD)
2926 stab = arg[1].arg_ptr.arg_stab;
2928 stab = stabent(str_get(st[1]),TRUE);
2929 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2937 fp->_flag |= _IOBIN;
2941 str_set(str, (setmode(fileno(fp), O_BINARY) != -1) ? Yes : No);
2949 sp = do_vec(str == st[1], arg->arg_ptr.arg_str, arglast);
2955 sp = do_gpwent(optype,
2959 value = (double) setpwent();
2962 value = (double) endpwent();
2967 fatal("Unsupported password function");
2974 sp = do_ggrent(optype,
2978 value = (double) setgrent();
2981 value = (double) endgrent();
2986 fatal("Unsupported group function");
2991 if (!(tmps = getlogin()))
2995 fatal("Unsupported function getlogin");
3006 if ((arg[1].arg_type & A_MASK) == A_WORD)
3007 stab = arg[1].arg_ptr.arg_stab;
3009 stab = stabent(str_get(st[1]),TRUE);
3012 sp = do_dirop(optype,stab,gimme,arglast);
3015 value = (double)do_syscall(arglast);
3019 if ((arg[1].arg_type & A_MASK) == A_WORD)
3020 stab = arg[1].arg_ptr.arg_stab;
3022 stab = stabent(str_get(st[1]),TRUE);
3023 if ((arg[2].arg_type & A_MASK) == A_WORD)
3024 stab2 = arg[2].arg_ptr.arg_stab;
3026 stab2 = stabent(str_get(st[2]),TRUE);
3027 do_pipe(str,stab,stab2);
3030 fatal("Unsupported function pipe");
3041 deb("%s RETURNS \"%s\"\n",opname[optype],str_get(str));
3044 stack_ary = stack->ary_array;
3045 stack_max = stack_ary + stack->ary_max;
3046 stack_sp = stack_ary + arglast[0] + 1;
3047 return arglast[0] + 1;