1 /* $Header: eval.c,v 3.0.1.2 89/11/17 15:19:34 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 3.0.1.2 89/11/17 15:19:34 lwall
10 * patch5: simplified a too-complex expression for some machine or other
12 * Revision 3.0.1.1 89/11/11 04:31:51 lwall
13 * patch2: mkdir and rmdir needed to quote argument when passed to shell
14 * patch2: mkdir and rmdir now return better error codes
15 * patch2: fileno, seekdir, rewinddir and closedir now disallow defaults
17 * Revision 3.0 89/10/18 15:17:04 lwall
35 static void (*ihand)();
36 static void (*qhand)();
38 static int (*ihand)();
39 static int (*qhand)();
46 static struct lstring *lstr;
47 static char old_record_separator;
49 double sin(), cos(), atan2(), pow();
54 extern char *sys_errlist[];
73 int arglast[8]; /* highest sp for arg--valid only for non-O_LIST args */
74 unsigned long tmplong;
81 bool assigning = FALSE;
82 double exp(), log(), sqrt(), modf();
83 char *crypt(), *getenv();
84 extern void grow_dlevel();
88 optype = arg->arg_type;
89 maxarg = arg->arg_len;
91 str = arg->arg_ptr.arg_str;
92 if (sp + maxarg > stack->ary_max)
93 astore(stack, sp + maxarg, Nullstr);
94 st = stack->ary_array;
99 deb("%s (%lx) %d args:\n",opname[optype],arg,maxarg);
101 debname[dlevel] = opname[optype][0];
102 debdelim[dlevel] = ':';
103 if (++dlevel >= dlmax)
108 #include "evalargs.xc"
116 if (gimme == G_ARRAY)
122 if (gimme == G_ARRAY)
125 STR_SSET(str,st[arglast[anum]-arglast[0]]);
129 if (gimme == G_ARRAY)
132 STR_SSET(str,st[arglast[anum]-arglast[0]]);
142 anum = (int)str_gnum(st[2]);
144 tmpstr = Str_new(50,0);
145 str_sset(tmpstr,str);
147 str_scat(str,tmpstr);
150 str_sset(str,&str_no);
154 sp = do_match(str,arg,
156 if (gimme == G_ARRAY)
161 sp = do_match(str,arg,
163 if (gimme == G_ARRAY)
165 str_sset(str, str_true(str) ? &str_no : &str_yes);
169 sp = do_subst(str,arg,arglast[0]);
172 sp = do_subst(str,arg,arglast[0]);
173 str = arg->arg_ptr.arg_str;
174 str_set(str, str_true(str) ? No : Yes);
177 if (arg[1].arg_flags & AF_ARYOK) {
178 if (arg->arg_len == 1) {
179 arg->arg_type = O_LOCAL;
183 arg->arg_type = O_AASSIGN;
188 arg->arg_type = O_SASSIGN;
193 arglast[2] = arglast[1]; /* push a null array */
202 STR_SSET(str, st[2]);
207 str = arg->arg_ptr.arg_str;
208 for (sp = arglast[0] + 1; sp <= arglast[1]; sp++)
213 if (arg[1].arg_type & A_DONT) {
214 sp = do_defined(str,arg,
218 else if (str->str_pok || str->str_nok)
222 if (arg[1].arg_type & A_DONT) {
223 sp = do_undef(str,arg,
227 else if (str != stab_val(defstab)) {
228 str->str_pok = str->str_nok = 0;
233 sp = do_study(str,arg,
237 value = str_gnum(st[1]);
238 value = pow(value,str_gnum(st[2]));
241 value = str_gnum(st[1]);
242 value *= str_gnum(st[2]);
245 if ((value = str_gnum(st[2])) == 0.0)
246 fatal("Illegal division by zero");
247 value = str_gnum(st[1]) / value;
250 tmplong = (long) str_gnum(st[2]);
252 fatal("Illegal modulus zero");
253 when = (long)str_gnum(st[1]);
256 value = (double)(when % tmplong);
258 value = (double)(tmplong - (-when % tmplong));
262 value = str_gnum(st[1]);
263 value += str_gnum(st[2]);
266 value = str_gnum(st[1]);
267 value -= str_gnum(st[2]);
270 value = str_gnum(st[1]);
271 anum = (int)str_gnum(st[2]);
273 value = (double)(((long)value) << anum);
277 value = str_gnum(st[1]);
278 anum = (int)str_gnum(st[2]);
280 value = (double)(((long)value) >> anum);
284 value = str_gnum(st[1]);
285 value = (value < str_gnum(st[2])) ? 1.0 : 0.0;
288 value = str_gnum(st[1]);
289 value = (value > str_gnum(st[2])) ? 1.0 : 0.0;
292 value = str_gnum(st[1]);
293 value = (value <= str_gnum(st[2])) ? 1.0 : 0.0;
296 value = str_gnum(st[1]);
297 value = (value >= str_gnum(st[2])) ? 1.0 : 0.0;
301 if ((!st[1]->str_nok && !looks_like_number(st[1])) ||
302 (!st[2]->str_nok && !looks_like_number(st[2])) )
303 warn("Possible use of == on string value");
305 value = str_gnum(st[1]);
306 value = (value == str_gnum(st[2])) ? 1.0 : 0.0;
309 value = str_gnum(st[1]);
310 value = (value != str_gnum(st[2])) ? 1.0 : 0.0;
313 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
314 value = str_gnum(st[1]);
316 value = (double)(((long)value) & (long)str_gnum(st[2]));
321 do_vop(optype,str,st[1],st[2]);
324 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
325 value = str_gnum(st[1]);
327 value = (double)(((long)value) ^ (long)str_gnum(st[2]));
332 do_vop(optype,str,st[1],st[2]);
335 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
336 value = str_gnum(st[1]);
338 value = (double)(((long)value) | (long)str_gnum(st[2]));
343 do_vop(optype,str,st[1],st[2]);
345 /* use register in evaluating str_true() */
347 if (str_true(st[1])) {
350 argflags = arg[anum].arg_flags;
351 if (gimme == G_ARRAY)
352 argflags |= AF_ARYOK;
353 argtype = arg[anum].arg_type & A_MASK;
354 argptr = arg[anum].arg_ptr;
362 str_sset(str, st[1]);
370 if (str_true(st[1])) {
372 str_sset(str, st[1]);
382 argflags = arg[anum].arg_flags;
383 if (gimme == G_ARRAY)
384 argflags |= AF_ARYOK;
385 argtype = arg[anum].arg_type & A_MASK;
386 argptr = arg[anum].arg_ptr;
393 anum = (str_true(st[1]) ? 2 : 3);
394 optype = (anum == 2 ? O_ITEM2 : O_ITEM3);
395 argflags = arg[anum].arg_flags;
396 if (gimme == G_ARRAY)
397 argflags |= AF_ARYOK;
398 argtype = arg[anum].arg_type & A_MASK;
399 argptr = arg[anum].arg_ptr;
405 if (gimme == G_ARRAY)
410 value = -str_gnum(st[1]);
413 value = (double) !str_true(st[1]);
417 value = (double) ~(long)str_gnum(st[1]);
421 tmps = stab_name(defoutstab);
423 if ((arg[1].arg_type & A_MASK) == A_WORD)
424 defoutstab = arg[1].arg_ptr.arg_stab;
426 defoutstab = stabent(str_get(st[1]),TRUE);
427 if (!stab_io(defoutstab))
428 stab_io(defoutstab) = stio_new();
429 curoutstab = defoutstab;
437 else if ((arg[1].arg_type & A_MASK) == A_WORD) {
438 if (!(stab = arg[1].arg_ptr.arg_stab))
442 stab = stabent(str_get(st[1]),TRUE);
443 if (!stab_io(stab)) {
449 fp = stab_io(stab)->ofp;
451 if (stab_io(stab)->fmt_stab)
452 form = stab_form(stab_io(stab)->fmt_stab);
454 form = stab_form(stab);
458 warn("No format for filehandle");
460 if (stab_io(stab)->ifp)
461 warn("Filehandle only opened for input");
463 warn("Write on closed filehandle");
470 format(&outrec,form,sp);
471 do_write(&outrec,stab_io(stab),sp);
472 if (stab_io(stab)->flags & IOF_FLUSH)
479 if ((arg[1].arg_type & A_MASK) == A_WORD)
480 stab = arg[1].arg_ptr.arg_stab;
482 stab = stabent(str_get(st[1]),TRUE);
483 anum = (int)str_gnum(st[3]);
484 value = (double)hdbmopen(stab_hash(stab),str_get(st[2]),anum);
487 fatal("No dbm or ndbm on this machine");
491 if ((arg[1].arg_type & A_MASK) == A_WORD)
492 stab = arg[1].arg_ptr.arg_stab;
494 stab = stabent(str_get(st[1]),TRUE);
495 hdbmclose(stab_hash(stab));
498 fatal("No dbm or ndbm on this machine");
501 if ((arg[1].arg_type & A_MASK) == A_WORD)
502 stab = arg[1].arg_ptr.arg_stab;
504 stab = stabent(str_get(st[1]),TRUE);
505 if (do_open(stab,str_get(st[2]))) {
506 value = (double)forkprocess;
507 stab_io(stab)->lines = 0;
514 value = (double) do_trans(str,arg);
515 str = arg->arg_ptr.arg_str;
518 str_set(arg->arg_ptr.arg_str, do_trans(str,arg) == 0 ? Yes : No);
519 str = arg->arg_ptr.arg_str;
524 else if ((arg[1].arg_type & A_MASK) == A_WORD)
525 stab = arg[1].arg_ptr.arg_stab;
527 stab = stabent(str_get(st[1]),TRUE);
528 str_set(str, do_close(stab,TRUE) ? Yes : No );
532 sp = do_each(str,stab_hash(arg[1].arg_ptr.arg_stab),
537 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
541 str->str_nok = str->str_pok = 0;
542 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
543 str->str_state = SS_ARY;
546 ary = stab_array(arg[1].arg_ptr.arg_stab);
547 maxarg = ary->ary_fill + 1;
548 if (gimme == G_ARRAY) { /* array wanted */
551 if (maxarg > 0 && sp + maxarg > stack->ary_max) {
552 astore(stack,sp + maxarg, Nullstr);
553 st = stack->ary_array;
555 Copy(ary->ary_array, &st[sp+1], maxarg, STR*);
560 str = afetch(ary,maxarg - 1,FALSE);
563 anum = ((int)str_gnum(st[2])) - arybase;
564 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,FALSE);
569 tmpstab = arg[1].arg_ptr.arg_stab;
570 tmps = str_get(st[2]);
571 str = hdelete(stab_hash(tmpstab),tmps,st[2]->str_cur);
572 if (tmpstab == envstab)
578 str->str_nok = str->str_pok = 0;
579 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
580 str->str_state = SS_HASH;
583 if (gimme == G_ARRAY) { /* array wanted */
584 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
589 tmpstab = arg[1].arg_ptr.arg_stab;
590 sprintf(buf,"%d/%d",stab_hash(tmpstab)->tbl_fill,
591 stab_hash(tmpstab)->tbl_max+1);
596 tmpstab = arg[1].arg_ptr.arg_stab;
597 tmps = str_get(st[2]);
598 str = hfetch(stab_hash(tmpstab),tmps,st[2]->str_cur,FALSE);
603 anum = ((int)str_gnum(st[2])) - arybase;
604 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,TRUE);
606 fatal("Assignment to non-creatable value, subscript %d",anum);
609 tmpstab = arg[1].arg_ptr.arg_stab;
610 tmps = str_get(st[2]);
611 anum = st[2]->str_cur;
612 str = hfetch(stab_hash(tmpstab),tmps,anum,TRUE);
614 fatal("Assignment to non-creatable value, subscript \"%s\"",tmps);
615 if (tmpstab == envstab) /* heavy wizardry going on here */
616 str_magic(str, tmpstab, 'E', tmps, anum); /* str is now magic */
617 /* he threw the brick up into the air */
618 else if (tmpstab == sigstab)
619 str_magic(str, tmpstab, 'S', tmps, anum);
621 else if (stab_hash(tmpstab)->tbl_dbm)
622 str_magic(str, tmpstab, 'D', tmps, anum);
628 goto do_slice_already;
632 goto do_slice_already;
636 goto do_slice_already;
641 sp = do_slice(arg[1].arg_ptr.arg_stab,anum,argtype,
645 if (arglast[2] - arglast[1] != 1)
646 str = do_push(stab_array(arg[1].arg_ptr.arg_stab),arglast);
648 str = Str_new(51,0); /* must copy the STR */
650 (void)apush(stab_array(arg[1].arg_ptr.arg_stab),str);
654 str = apop(ary = stab_array(arg[1].arg_ptr.arg_stab));
655 goto staticalization;
657 str = ashift(ary = stab_array(arg[1].arg_ptr.arg_stab));
661 if (ary->ary_flags & ARF_REAL)
662 (void)str_2static(str);
665 sp = do_unpack(str,gimme,arglast);
668 value = str_gnum(st[3]);
669 sp = do_split(str, arg[2].arg_ptr.arg_spat, (int)value,
674 value = (double)str_len(stab_val(defstab));
676 value = (double)str_len(st[1]);
679 do_sprintf(str, sp-arglast[0], st+1);
682 anum = ((int)str_gnum(st[2])) - arybase; /* anum=where to start*/
683 tmps = str_get(st[1]); /* force conversion to string */
684 if (argtype = (str == st[1]))
685 str = arg->arg_ptr.arg_str;
687 anum += st[1]->str_cur + arybase;
688 if (anum < 0 || anum > st[1]->str_cur)
691 optype = (int)str_gnum(st[3]);
695 anum = st[1]->str_cur - anum; /* anum=how many bytes left*/
698 str_nset(str, tmps, anum);
699 if (argtype) { /* it's an lvalue! */
700 lstr = (struct lstring*)str;
701 str->str_magic = st[1];
702 st[1]->str_rare = 's';
703 lstr->lstr_offset = tmps - str_get(st[1]);
704 lstr->lstr_len = anum;
709 (void)do_pack(str,arglast);
712 sp = do_grep(arg,str,gimme,arglast);
715 do_join(str,arglast);
718 tmps = str_get(st[1]);
719 value = (double) (str_cmp(st[1],st[2]) < 0);
722 tmps = str_get(st[1]);
723 value = (double) (str_cmp(st[1],st[2]) > 0);
726 tmps = str_get(st[1]);
727 value = (double) (str_cmp(st[1],st[2]) <= 0);
730 tmps = str_get(st[1]);
731 value = (double) (str_cmp(st[1],st[2]) >= 0);
734 tmps = str_get(st[1]);
735 value = (double) str_eq(st[1],st[2]);
738 tmps = str_get(st[1]);
739 value = (double) !str_eq(st[1],st[2]);
742 sp = do_subr(arg,gimme,arglast);
743 st = stack->ary_array + arglast[0]; /* maybe realloced */
746 sp = do_dbsubr(arg,gimme,arglast);
747 st = stack->ary_array + arglast[0]; /* maybe realloced */
750 if ((arg[1].arg_type & A_MASK) == A_WORD)
751 stab = arg[1].arg_ptr.arg_stab;
753 stab = stabent(str_get(st[1]),TRUE);
756 sp = do_sort(str,stab,
764 if (arglast[2] - arglast[1] != 1) {
765 do_join(str,arglast);
766 tmps = str_get(st[1]);
770 tmps = str_get(st[2]);
773 tmps = "Warning: something's wrong";
777 if (arglast[2] - arglast[1] != 1) {
778 do_join(str,arglast);
779 tmps = str_get(st[1]);
783 tmps = str_get(st[2]);
791 if ((arg[1].arg_type & A_MASK) == A_WORD)
792 stab = arg[1].arg_ptr.arg_stab;
794 stab = stabent(str_get(st[1]),TRUE);
797 if (!stab_io(stab)) {
799 warn("Filehandle never opened");
802 if (!(fp = stab_io(stab)->ofp)) {
804 if (stab_io(stab)->ifp)
805 warn("Filehandle opened only for input");
807 warn("Print on closed filehandle");
812 if (optype == O_PRTF || arglast[2] - arglast[1] != 1)
813 value = (double)do_aprint(arg,fp,arglast);
815 value = (double)do_print(st[2],fp);
816 if (orslen && optype == O_PRINT)
817 if (fwrite(ors, 1, orslen, fp) == 0)
820 if (stab_io(stab)->flags & IOF_FLUSH)
821 if (fflush(fp) == EOF)
827 tmps = str_get(stab_val(defstab));
829 tmps = str_get(st[1]);
830 if (!tmps || !*tmps) {
831 tmpstr = hfetch(stab_hash(envstab),"HOME",4,FALSE);
833 tmps = str_get(tmpstr);
835 if (!tmps || !*tmps) {
836 tmpstr = hfetch(stab_hash(envstab),"LOGDIR",6,FALSE);
838 tmps = str_get(tmpstr);
841 taintproper("Insecure dependency in chdir");
843 value = (double)(chdir(tmps) >= 0);
849 anum = (int)str_gnum(st[1]);
856 tmps = str_get(st[1]);
857 str_reset(tmps,arg[2].arg_ptr.arg_hash);
861 if (gimme == G_ARRAY)
864 str = st[sp - arglast[0]]; /* unwanted list, return last item */
871 else if ((arg[1].arg_type & A_MASK) == A_WORD)
872 stab = arg[1].arg_ptr.arg_stab;
874 stab = stabent(str_get(st[1]),TRUE);
875 str_set(str, do_eof(stab) ? Yes : No);
881 else if ((arg[1].arg_type & A_MASK) == A_WORD)
882 stab = arg[1].arg_ptr.arg_stab;
884 stab = stabent(str_get(st[1]),TRUE);
885 if (do_eof(stab)) /* make sure we have fp with something */
892 *str->str_ptr = getc(stab_io(stab)->ifp); /* should never be EOF */
899 else if ((arg[1].arg_type & A_MASK) == A_WORD)
900 stab = arg[1].arg_ptr.arg_stab;
902 stab = stabent(str_get(st[1]),TRUE);
904 value = (double)do_tell(stab);
911 if ((arg[1].arg_type & A_MASK) == A_WORD)
912 stab = arg[1].arg_ptr.arg_stab;
914 stab = stabent(str_get(st[1]),TRUE);
915 tmps = str_get(st[2]);
916 anum = (int)str_gnum(st[3]);
917 STR_GROW(st[2], anum+1), (tmps = str_get(st[2])); /* sneaky */
919 if (!stab_io(stab) || !stab_io(stab)->ifp)
922 else if (optype == O_RECV) {
923 argtype = sizeof buf;
924 optype = (int)str_gnum(st[4]);
925 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, optype,
928 st[2]->str_cur = anum;
929 st[2]->str_ptr[anum] = '\0';
930 str_nset(str,buf,argtype);
933 str_sset(str,&str_undef);
936 else if (stab_io(stab)->type == 's') {
937 argtype = sizeof buf;
938 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, 0,
942 else if (optype == O_RECV)
946 anum = fread(tmps, 1, anum, stab_io(stab)->ifp);
949 st[2]->str_cur = anum;
950 st[2]->str_ptr[anum] = '\0';
951 value = (double)anum;
955 if ((arg[1].arg_type & A_MASK) == A_WORD)
956 stab = arg[1].arg_ptr.arg_stab;
958 stab = stabent(str_get(st[1]),TRUE);
959 tmps = str_get(st[2]);
960 anum = (int)str_gnum(st[3]);
961 optype = sp - arglast[0];
964 warn("Too many args on send");
966 tmps2 = str_get(st[4]);
967 anum = sendto(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur,
968 anum, tmps2, st[4]->str_cur);
971 anum = send(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur, anum);
974 value = (double)anum;
980 if ((arg[1].arg_type & A_MASK) == A_WORD)
981 stab = arg[1].arg_ptr.arg_stab;
983 stab = stabent(str_get(st[1]),TRUE);
984 value = str_gnum(st[2]);
985 str_set(str, do_seek(stab,
986 (long)value, (int)str_gnum(st[3]) ) ? Yes : No);
990 tmps = "SUB"; /* just fake up a "last SUB" */
992 if (gimme == G_ARRAY) {
993 lastretstr = Nullstr;
994 lastspbase = arglast[1];
995 lastsize = arglast[2] - arglast[1];
998 lastretstr = str_static(st[arglast[2] - arglast[0]]);
1004 tmps = str_get(arg[1].arg_ptr.arg_str);
1006 while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
1007 strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
1010 deb("(Skipping label #%d %s)\n",loop_ptr,
1011 loop_stack[loop_ptr].loop_label);
1018 deb("(Found label #%d %s)\n",loop_ptr,
1019 loop_stack[loop_ptr].loop_label);
1024 fatal("Bad label: %s", maxarg > 0 ? tmps : "<null>");
1025 if (!lastretstr && optype == O_LAST && lastsize) {
1027 st += lastspbase + 1;
1028 optype = loop_stack[loop_ptr].loop_sp - lastspbase; /* negative */
1030 for (anum = lastsize; anum > 0; anum--,st++)
1031 st[optype] = str_static(st[0]);
1033 longjmp(loop_stack[loop_ptr].loop_env, O_LAST);
1035 longjmp(loop_stack[loop_ptr].loop_env, optype);
1037 case O_GOTO:/* shudder */
1038 goto_targ = str_get(arg[1].arg_ptr.arg_str);
1040 goto_targ = Nullch; /* just restart from top */
1041 if (optype == O_DUMP) {
1045 longjmp(top_env, 1);
1047 tmps = str_get(st[1]);
1049 if (!(tmps2 = fbminstr((unsigned char*)tmps,
1050 (unsigned char*)tmps + st[1]->str_cur, st[2])))
1052 if (tmps2 = fbminstr(Null(unsigned char*),Null(unsigned char*),Nullstr))
1054 value = (double)(-1 + arybase);
1056 value = (double)(tmps2 - tmps + arybase);
1059 tmps = str_get(st[1]);
1060 tmps2 = str_get(st[2]);
1062 if (!(tmps2 = rninstr(tmps, tmps + st[1]->str_cur,
1063 tmps2, tmps2 + st[2]->str_cur)))
1065 if (tmps2 = rninstr(Nullch,Nullch,Nullch,Nullch))
1067 value = (double)(-1 + arybase);
1069 value = (double)(tmps2 - tmps + arybase);
1073 value = (double) time(Null(long*));
1077 sp = do_tms(str,gimme,arglast);
1083 when = (long)str_gnum(st[1]);
1084 sp = do_time(str,localtime(&when),
1091 when = (long)str_gnum(st[1]);
1092 sp = do_time(str,gmtime(&when),
1097 sp = do_stat(str,arg,
1102 tmps = str_get(st[1]);
1104 str_set(str,fcrypt(tmps,str_get(st[2])));
1106 str_set(str,crypt(tmps,str_get(st[2])));
1110 "The crypt() function is unimplemented due to excessive paranoia.");
1114 value = str_gnum(st[1]);
1115 value = atan2(value,str_gnum(st[2]));
1119 value = str_gnum(stab_val(defstab));
1121 value = str_gnum(st[1]);
1126 value = str_gnum(stab_val(defstab));
1128 value = str_gnum(st[1]);
1135 value = str_gnum(st[1]);
1139 value = rand() * value / 2147483648.0;
1142 value = rand() * value / 65536.0;
1145 value = rand() * value / 32768.0;
1147 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1158 anum = (int)str_gnum(st[1]);
1163 value = str_gnum(stab_val(defstab));
1165 value = str_gnum(st[1]);
1170 value = str_gnum(stab_val(defstab));
1172 value = str_gnum(st[1]);
1177 value = str_gnum(stab_val(defstab));
1179 value = str_gnum(st[1]);
1180 value = sqrt(value);
1184 value = str_gnum(stab_val(defstab));
1186 value = str_gnum(st[1]);
1188 (void)modf(value,&value);
1190 (void)modf(-value,&value);
1196 tmps = str_get(stab_val(defstab));
1198 tmps = str_get(st[1]);
1200 value = (double) *tmps;
1203 value = (double) anum;
1210 tmps = str_get(st[1]);
1212 if (!tmps || !*tmps)
1213 sleep((32767<<16)+32767);
1215 sleep((unsigned int)atoi(tmps));
1217 value = (double)when;
1219 value = ((double)when) - value;
1223 sp = do_range(gimme,arglast);
1226 if (gimme == G_ARRAY) { /* it's a range */
1227 /* can we optimize to constant array? */
1228 if ((arg[1].arg_type & A_MASK) == A_SINGLE &&
1229 (arg[2].arg_type & A_MASK) == A_SINGLE) {
1230 st[2] = arg[2].arg_ptr.arg_str;
1231 sp = do_range(gimme,arglast);
1232 st = stack->ary_array;
1233 maxarg = sp - arglast[0];
1234 str_free(arg[1].arg_ptr.arg_str);
1235 str_free(arg[2].arg_ptr.arg_str);
1236 arg->arg_type = O_ARRAY;
1237 arg[1].arg_type = A_STAB|A_DONT;
1239 stab = arg[1].arg_ptr.arg_stab = aadd(genstab());
1240 ary = stab_array(stab);
1241 afill(ary,maxarg - 1);
1243 while (maxarg-- > 0)
1244 ary->ary_array[maxarg] = str_smake(st[maxarg]);
1247 arg->arg_type = optype = O_RANGE;
1248 maxarg = arg->arg_len = 2;
1250 arg[anum].arg_flags &= ~AF_ARYOK;
1251 argflags = arg[anum].arg_flags;
1252 argtype = arg[anum].arg_type & A_MASK;
1253 arg[anum].arg_type = argtype;
1254 argptr = arg[anum].arg_ptr;
1260 arg->arg_type = O_FLIP;
1263 if ((arg[1].arg_type & A_MASK) == A_SINGLE ?
1264 last_in_stab && (int)str_gnum(st[1]) == stab_io(last_in_stab)->lines
1267 str_numset(str,0.0);
1269 arg->arg_type = optype = O_FLOP;
1270 arg[2].arg_type &= ~A_DONT;
1271 arg[1].arg_type |= A_DONT;
1272 argflags = arg[2].arg_flags;
1273 argtype = arg[2].arg_type & A_MASK;
1274 argptr = arg[2].arg_ptr;
1283 if ((arg[2].arg_type & A_MASK) == A_SINGLE ?
1284 last_in_stab && (int)str_gnum(st[2]) == stab_io(last_in_stab)->lines
1287 arg->arg_type = O_FLIP;
1288 arg[1].arg_type &= ~A_DONT;
1289 arg[2].arg_type |= A_DONT;
1295 if (!anum && (tmpstab = stabent("$",allstabs)))
1296 str_numset(STAB_STR(tmpstab),(double)getpid());
1297 value = (double)anum;
1301 ihand = signal(SIGINT, SIG_IGN);
1302 qhand = signal(SIGQUIT, SIG_IGN);
1303 anum = wait(&argflags);
1305 pidgone(anum,argflags);
1306 value = (double)anum;
1310 (void)signal(SIGINT, ihand);
1311 (void)signal(SIGQUIT, qhand);
1312 statusvalue = (unsigned short)argflags;
1316 if (arglast[2] - arglast[1] == 1) {
1318 tainted |= st[2]->str_tainted;
1319 taintproper("Insecure dependency in system");
1322 while ((anum = vfork()) == -1) {
1323 if (errno != EAGAIN) {
1331 ihand = signal(SIGINT, SIG_IGN);
1332 qhand = signal(SIGQUIT, SIG_IGN);
1333 while ((argtype = wait(&argflags)) != anum && argtype >= 0)
1334 pidgone(argtype,argflags);
1338 (void)signal(SIGINT, ihand);
1339 (void)signal(SIGQUIT, qhand);
1340 statusvalue = (unsigned short)argflags;
1344 value = (double)((unsigned int)argflags & 0xffff);
1348 if ((arg[1].arg_type & A_MASK) == A_STAB)
1349 value = (double)do_aexec(st[1],arglast);
1350 else if (arglast[2] - arglast[1] != 1)
1351 value = (double)do_aexec(Nullstr,arglast);
1353 value = (double)do_exec(str_get(str_static(st[2])));
1357 if ((arg[1].arg_type & A_MASK) == A_STAB)
1358 value = (double)do_aexec(st[1],arglast);
1359 else if (arglast[2] - arglast[1] != 1)
1360 value = (double)do_aexec(Nullstr,arglast);
1362 value = (double)do_exec(str_get(str_static(st[2])));
1375 tmps = str_get(stab_val(defstab));
1377 tmps = str_get(st[1]);
1386 case '0': case '1': case '2': case '3': case '4':
1387 case '5': case '6': case '7':
1389 anum += *tmps++ & 15;
1391 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1392 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1396 anum += (*tmps++ & 7) + 9;
1405 value = (double)anum;
1412 value = (double)apply(optype,arglast);
1420 anum = umask((int)str_gnum(st[1]));
1421 value = (double)anum;
1423 taintproper("Insecure dependency in umask");
1427 tmps = str_get(st[1]);
1428 tmps2 = str_get(st[2]);
1430 taintproper("Insecure dependency in rename");
1433 value = (double)(rename(tmps,tmps2) >= 0);
1435 if (euid || stat(tmps2,&statbuf) < 0 ||
1436 (statbuf.st_mode & S_IFMT) != S_IFDIR )
1437 (void)UNLINK(tmps2); /* avoid unlinking a directory */
1438 if (!(anum = link(tmps,tmps2)))
1439 anum = UNLINK(tmps);
1440 value = (double)(anum >= 0);
1444 tmps = str_get(st[1]);
1445 tmps2 = str_get(st[2]);
1447 taintproper("Insecure dependency in link");
1449 value = (double)(link(tmps,tmps2) >= 0);
1452 tmps = str_get(st[1]);
1453 anum = (int)str_gnum(st[2]);
1455 taintproper("Insecure dependency in mkdir");
1458 value = (double)(mkdir(tmps,anum) >= 0);
1461 (void)strcpy(buf,"mkdir ");
1463 #if !defined(MKDIR) || !defined(RMDIR)
1465 for (tmps2 = buf+6; *tmps; ) {
1469 (void)strcpy(tmps2," 2>&1");
1470 rsfp = mypopen(buf,"r");
1473 tmps2 = fgets(buf,sizeof buf,rsfp);
1474 (void)mypclose(rsfp);
1475 if (tmps2 != Nullch) {
1476 for (errno = 1; errno < sys_nerr; errno++) {
1477 if (instr(buf,sys_errlist[errno])) /* you don't see this */
1482 #define EACCES EPERM
1484 if (instr(buf,"cannot make"))
1486 else if (instr(buf,"non-exist"))
1488 else if (instr(buf,"not empty"))
1490 else if (instr(buf,"cannot access"))
1496 else { /* some mkdirs return no failure indication */
1497 tmps = str_get(st[1]);
1498 anum = (stat(tmps,&statbuf) >= 0);
1499 if (optype == O_RMDIR)
1504 errno = EACCES; /* a guess */
1505 value = (double)anum;
1514 tmps = str_get(stab_val(defstab));
1516 tmps = str_get(st[1]);
1518 taintproper("Insecure dependency in rmdir");
1521 value = (double)(rmdir(tmps) >= 0);
1524 (void)strcpy(buf,"rmdir ");
1525 goto one_liner; /* see above in MKDIR */
1528 value = (double)getppid();
1535 anum = (int)str_gnum(st[1]);
1536 value = (double)getpgrp(anum);
1539 fatal("The getpgrp() function is unimplemented on this machine");
1544 argtype = (int)str_gnum(st[1]);
1545 anum = (int)str_gnum(st[2]);
1547 taintproper("Insecure dependency in setpgrp");
1549 value = (double)(setpgrp(argtype,anum) >= 0);
1552 fatal("The setpgrp() function is unimplemented on this machine");
1557 argtype = (int)str_gnum(st[1]);
1558 anum = (int)str_gnum(st[2]);
1559 value = (double)getpriority(argtype,anum);
1562 fatal("The getpriority() function is unimplemented on this machine");
1567 argtype = (int)str_gnum(st[1]);
1568 anum = (int)str_gnum(st[2]);
1569 optype = (int)str_gnum(st[3]);
1571 taintproper("Insecure dependency in setpriority");
1573 value = (double)(setpriority(argtype,anum,optype) >= 0);
1576 fatal("The setpriority() function is unimplemented on this machine");
1581 tmps = str_get(stab_val(defstab));
1583 tmps = str_get(st[1]);
1585 taintproper("Insecure dependency in chroot");
1587 value = (double)(chroot(tmps) >= 0);
1592 stab = last_in_stab;
1593 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1594 stab = arg[1].arg_ptr.arg_stab;
1596 stab = stabent(str_get(st[1]),TRUE);
1597 argtype = (int)str_gnum(st[2]);
1599 taintproper("Insecure dependency in ioctl");
1601 anum = do_ctl(optype,stab,argtype,st[3]);
1606 str_set(str,"0 but true");
1612 stab = last_in_stab;
1613 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1614 stab = arg[1].arg_ptr.arg_stab;
1616 stab = stabent(str_get(st[1]),TRUE);
1617 if (stab && stab_io(stab))
1618 fp = stab_io(stab)->ifp;
1622 argtype = (int)str_gnum(st[2]);
1623 value = (double)(flock(fileno(fp),argtype) >= 0);
1629 fatal("The flock() function is unimplemented on this machine");
1633 ary = stab_array(arg[1].arg_ptr.arg_stab);
1634 if (arglast[2] - arglast[1] != 1)
1635 do_unshift(ary,arglast);
1637 str = Str_new(52,0); /* must copy the STR */
1638 str_sset(str,st[2]);
1640 (void)astore(ary,0,str);
1642 value = (double)(ary->ary_fill + 1);
1647 tmpstr = stab_val(defstab);
1650 (arg[1].arg_type & A_MASK) != A_NULL ? st[1] : stab_val(defstab);
1652 tainted |= tmpstr->str_tainted;
1653 taintproper("Insecure dependency in eval");
1655 sp = do_eval(tmpstr, optype, arg[2].arg_ptr.arg_hash,
1683 if (mystat(arg,st[1]) < 0)
1685 if (cando(anum,argtype,&statcache))
1690 if (mystat(arg,st[1]) < 0)
1695 if (mystat(arg,st[1]) < 0)
1697 if (statcache.st_uid == (optype == O_FTEOWNED ? euid : uid) )
1701 if (mystat(arg,st[1]) < 0)
1703 if (!statcache.st_size)
1707 if (mystat(arg,st[1]) < 0)
1709 if (statcache.st_size)
1716 goto check_file_type;
1722 goto check_file_type;
1725 goto check_file_type;
1728 goto check_file_type;
1732 if (mystat(arg,st[1]) < 0)
1734 if ((statcache.st_mode & S_IFMT) == anum )
1740 goto check_file_type;
1746 if (lstat(str_get(st[1]),&statcache) < 0)
1748 if ((statcache.st_mode & S_IFMT) == S_IFLNK )
1754 tmps = str_get(st[1]);
1755 tmps2 = str_get(st[2]);
1757 taintproper("Insecure dependency in symlink");
1759 value = (double)(symlink(tmps,tmps2) >= 0);
1762 fatal("Unsupported function symlink()");
1767 tmps = str_get(stab_val(defstab));
1769 tmps = str_get(st[1]);
1770 anum = readlink(tmps,buf,sizeof buf);
1773 str_nset(str,buf,anum);
1776 fatal("Unsupported function readlink()");
1787 if (mystat(arg,st[1]) < 0)
1789 if (statcache.st_mode & anum)
1793 if (arg[1].arg_type & A_DONT) {
1794 stab = arg[1].arg_ptr.arg_stab;
1798 stab = stabent(tmps = str_get(st[1]),FALSE);
1799 if (stab && stab_io(stab) && stab_io(stab)->ifp)
1800 anum = fileno(stab_io(stab)->ifp);
1801 else if (isdigit(*tmps))
1810 str = do_fttext(arg,st[1]);
1814 if ((arg[1].arg_type & A_MASK) == A_WORD)
1815 stab = arg[1].arg_ptr.arg_stab;
1817 stab = stabent(str_get(st[1]),TRUE);
1819 value = (double)do_socket(stab,arglast);
1821 (void)do_socket(stab,arglast);
1825 if ((arg[1].arg_type & A_MASK) == A_WORD)
1826 stab = arg[1].arg_ptr.arg_stab;
1828 stab = stabent(str_get(st[1]),TRUE);
1830 value = (double)do_bind(stab,arglast);
1832 (void)do_bind(stab,arglast);
1836 if ((arg[1].arg_type & A_MASK) == A_WORD)
1837 stab = arg[1].arg_ptr.arg_stab;
1839 stab = stabent(str_get(st[1]),TRUE);
1841 value = (double)do_connect(stab,arglast);
1843 (void)do_connect(stab,arglast);
1847 if ((arg[1].arg_type & A_MASK) == A_WORD)
1848 stab = arg[1].arg_ptr.arg_stab;
1850 stab = stabent(str_get(st[1]),TRUE);
1852 value = (double)do_listen(stab,arglast);
1854 (void)do_listen(stab,arglast);
1858 if ((arg[1].arg_type & A_MASK) == A_WORD)
1859 stab = arg[1].arg_ptr.arg_stab;
1861 stab = stabent(str_get(st[1]),TRUE);
1862 if ((arg[2].arg_type & A_MASK) == A_WORD)
1863 stab2 = arg[2].arg_ptr.arg_stab;
1865 stab2 = stabent(str_get(st[2]),TRUE);
1866 do_accept(str,stab,stab2);
1874 sp = do_ghent(optype,
1882 sp = do_gnent(optype,
1890 sp = do_gpent(optype,
1898 sp = do_gsent(optype,
1902 value = (double) sethostent((int)str_gnum(st[1]));
1905 value = (double) setnetent((int)str_gnum(st[1]));
1908 value = (double) setprotoent((int)str_gnum(st[1]));
1911 value = (double) setservent((int)str_gnum(st[1]));
1914 value = (double) endhostent();
1917 value = (double) endnetent();
1920 value = (double) endprotoent();
1923 value = (double) endservent();
1926 sp = do_select(gimme,arglast);
1929 if ((arg[1].arg_type & A_MASK) == A_WORD)
1930 stab = arg[1].arg_ptr.arg_stab;
1932 stab = stabent(str_get(st[1]),TRUE);
1933 if ((arg[2].arg_type & A_MASK) == A_WORD)
1934 stab2 = arg[2].arg_ptr.arg_stab;
1936 stab2 = stabent(str_get(st[2]),TRUE);
1938 value = (double)do_spair(stab,stab2,arglast);
1940 (void)do_spair(stab,stab2,arglast);
1944 if ((arg[1].arg_type & A_MASK) == A_WORD)
1945 stab = arg[1].arg_ptr.arg_stab;
1947 stab = stabent(str_get(st[1]),TRUE);
1949 value = (double)do_shutdown(stab,arglast);
1951 (void)do_shutdown(stab,arglast);
1956 if ((arg[1].arg_type & A_MASK) == A_WORD)
1957 stab = arg[1].arg_ptr.arg_stab;
1959 stab = stabent(str_get(st[1]),TRUE);
1960 sp = do_sopt(optype,stab,arglast);
1964 if ((arg[1].arg_type & A_MASK) == A_WORD)
1965 stab = arg[1].arg_ptr.arg_stab;
1967 stab = stabent(str_get(st[1]),TRUE);
1968 sp = do_getsockname(optype,stab,arglast);
1971 #else /* SOCKET not defined */
2005 fatal("Unsupported socket function");
2010 if ((arg[1].arg_type & A_MASK) == A_WORD)
2011 stab = arg[1].arg_ptr.arg_stab;
2013 stab = stabent(str_get(st[1]),TRUE);
2014 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2019 sp = do_vec(str == st[1], arg->arg_ptr.arg_str, arglast);
2024 sp = do_gpwent(optype,
2028 value = (double) setpwent();
2031 value = (double) endpwent();
2036 sp = do_ggrent(optype,
2040 value = (double) setgrent();
2043 value = (double) endgrent();
2046 if (!(tmps = getlogin()))
2058 if ((arg[1].arg_type & A_MASK) == A_WORD)
2059 stab = arg[1].arg_ptr.arg_stab;
2061 stab = stabent(str_get(st[1]),TRUE);
2062 sp = do_dirop(optype,stab,gimme,arglast);
2065 value = (double)do_syscall(arglast);
2075 deb("%s RETURNS \"%s\"\n",opname[optype],str_get(str));
2078 return arglast[0] + 1;
2085 deb("%s RETURNS ARRAY OF %d ARGS\n",opname[optype],sp - arglast[0]);
2107 str_numset(str,value);
2114 deb("%s RETURNS \"%f\"\n",opname[optype],value);
2117 return arglast[0] + 1;