1 /* $Header: eval.c,v 3.0.1.6 90/03/27 15:53:51 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.6 90/03/27 15:53:51 lwall
10 * patch16: MSDOS support
11 * patch16: support for machines that can't cast negative floats to unsigned ints
12 * patch16: ioctl didn't return values correctly
14 * Revision 3.0.1.5 90/03/12 16:37:40 lwall
15 * patch13: undef $/ didn't work as advertised
16 * patch13: added list slice operator (LIST)[LIST]
17 * patch13: added splice operator: @oldelems = splice(@array,$offset,$len,LIST)
19 * Revision 3.0.1.4 90/02/28 17:36:59 lwall
20 * patch9: added pipe function
21 * patch9: a return in scalar context wouldn't return array
22 * patch9: !~ now always returns scalar even in array context
23 * patch9: some machines can't cast float to long with high bit set
24 * patch9: piped opens returned undef in child
25 * patch9: @array in scalar context now returns length of array
26 * patch9: chdir; coredumped
27 * patch9: wait no longer ignores signals
28 * patch9: mkdir now handles odd versions of /bin/mkdir
29 * patch9: -l FILEHANDLE now disallowed
31 * Revision 3.0.1.3 89/12/21 20:03:05 lwall
32 * patch7: errno may now be a macro with an lvalue
33 * patch7: ANSI strerror() is now supported
34 * patch7: send() didn't allow a TO argument
35 * patch7: ord() now always returns positive even on signed char machines
37 * Revision 3.0.1.2 89/11/17 15:19:34 lwall
38 * patch5: constant numeric subscripts get lost inside ?:
40 * Revision 3.0.1.1 89/11/11 04:31:51 lwall
41 * patch2: mkdir and rmdir needed to quote argument when passed to shell
42 * patch2: mkdir and rmdir now return better error codes
43 * patch2: fileno, seekdir, rewinddir and closedir now disallow defaults
45 * Revision 3.0 89/10/18 15:17:04 lwall
63 static void (*ihand)();
64 static void (*qhand)();
66 static int (*ihand)();
67 static int (*qhand)();
74 static struct lstring *lstr;
75 static int old_record_separator;
78 double sin(), cos(), atan2(), pow();
99 int arglast[8]; /* highest sp for arg--valid only for non-O_LIST args */
100 unsigned long tmplong;
107 bool assigning = FALSE;
108 double exp(), log(), sqrt(), modf();
109 char *crypt(), *getenv();
110 extern void grow_dlevel();
114 optype = arg->arg_type;
115 maxarg = arg->arg_len;
117 str = arg->arg_ptr.arg_str;
118 if (sp + maxarg > stack->ary_max)
119 astore(stack, sp + maxarg, Nullstr);
120 st = stack->ary_array;
125 deb("%s (%lx) %d args:\n",opname[optype],arg,maxarg);
127 debname[dlevel] = opname[optype][0];
128 debdelim[dlevel] = ':';
129 if (++dlevel >= dlmax)
134 #include "evalargs.xc"
142 if (gimme == G_ARRAY)
148 if (gimme == G_ARRAY)
151 STR_SSET(str,st[arglast[anum]-arglast[0]]);
155 if (gimme == G_ARRAY)
158 STR_SSET(str,st[arglast[anum]-arglast[0]]);
168 anum = (int)str_gnum(st[2]);
170 tmpstr = Str_new(50, 0);
171 str_sset(tmpstr,str);
172 tmps = str_get(tmpstr); /* force to be string */
173 STR_GROW(str, (anum * str->str_cur) + 1);
174 repeatcpy(str->str_ptr, tmps, tmpstr->str_cur, anum);
175 str->str_cur *= anum;
176 str->str_ptr[str->str_cur] = '\0';
179 str_sset(str,&str_no);
183 sp = do_match(str,arg,
185 if (gimme == G_ARRAY)
190 sp = do_match(str,arg,
192 str_sset(str, str_true(str) ? &str_no : &str_yes);
196 sp = do_subst(str,arg,arglast[0]);
199 sp = do_subst(str,arg,arglast[0]);
200 str = arg->arg_ptr.arg_str;
201 str_set(str, str_true(str) ? No : Yes);
204 if (arg[1].arg_flags & AF_ARYOK) {
205 if (arg->arg_len == 1) {
206 arg->arg_type = O_LOCAL;
210 arg->arg_type = O_AASSIGN;
215 arg->arg_type = O_SASSIGN;
220 arglast[2] = arglast[1]; /* push a null array */
229 STR_SSET(str, st[2]);
234 str = arg->arg_ptr.arg_str;
235 for (sp = arglast[0] + 1; sp <= arglast[1]; sp++)
240 if (arg[1].arg_type & A_DONT) {
241 sp = do_defined(str,arg,
245 else if (str->str_pok || str->str_nok)
249 if (arg[1].arg_type & A_DONT) {
250 sp = do_undef(str,arg,
254 else if (str != stab_val(defstab)) {
255 str->str_pok = str->str_nok = 0;
260 sp = do_study(str,arg,
264 value = str_gnum(st[1]);
265 value = pow(value,str_gnum(st[2]));
268 value = str_gnum(st[1]);
269 value *= str_gnum(st[2]);
272 if ((value = str_gnum(st[2])) == 0.0)
273 fatal("Illegal division by zero");
274 value = str_gnum(st[1]) / value;
277 tmplong = (long) str_gnum(st[2]);
279 fatal("Illegal modulus zero");
280 when = (long)str_gnum(st[1]);
283 value = (double)(when % tmplong);
285 value = (double)(tmplong - (-when % tmplong));
289 value = str_gnum(st[1]);
290 value += str_gnum(st[2]);
293 value = str_gnum(st[1]);
294 value -= str_gnum(st[2]);
297 value = str_gnum(st[1]);
298 anum = (int)str_gnum(st[2]);
300 value = (double)(U_L(value) << anum);
304 value = str_gnum(st[1]);
305 anum = (int)str_gnum(st[2]);
307 value = (double)(U_L(value) >> anum);
311 value = str_gnum(st[1]);
312 value = (value < str_gnum(st[2])) ? 1.0 : 0.0;
315 value = str_gnum(st[1]);
316 value = (value > str_gnum(st[2])) ? 1.0 : 0.0;
319 value = str_gnum(st[1]);
320 value = (value <= str_gnum(st[2])) ? 1.0 : 0.0;
323 value = str_gnum(st[1]);
324 value = (value >= str_gnum(st[2])) ? 1.0 : 0.0;
328 if ((!st[1]->str_nok && !looks_like_number(st[1])) ||
329 (!st[2]->str_nok && !looks_like_number(st[2])) )
330 warn("Possible use of == on string value");
332 value = str_gnum(st[1]);
333 value = (value == str_gnum(st[2])) ? 1.0 : 0.0;
336 value = str_gnum(st[1]);
337 value = (value != str_gnum(st[2])) ? 1.0 : 0.0;
340 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
341 value = str_gnum(st[1]);
343 value = (double)(U_L(value) & U_L(str_gnum(st[2])));
348 do_vop(optype,str,st[1],st[2]);
351 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
352 value = str_gnum(st[1]);
354 value = (double)(U_L(value) ^ U_L(str_gnum(st[2])));
359 do_vop(optype,str,st[1],st[2]);
362 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
363 value = str_gnum(st[1]);
365 value = (double)(U_L(value) | U_L(str_gnum(st[2])));
370 do_vop(optype,str,st[1],st[2]);
372 /* use register in evaluating str_true() */
374 if (str_true(st[1])) {
377 argflags = arg[anum].arg_flags;
378 if (gimme == G_ARRAY)
379 argflags |= AF_ARYOK;
380 argtype = arg[anum].arg_type & A_MASK;
381 argptr = arg[anum].arg_ptr;
389 str_sset(str, st[1]);
397 if (str_true(st[1])) {
399 str_sset(str, st[1]);
409 argflags = arg[anum].arg_flags;
410 if (gimme == G_ARRAY)
411 argflags |= AF_ARYOK;
412 argtype = arg[anum].arg_type & A_MASK;
413 argptr = arg[anum].arg_ptr;
420 anum = (str_true(st[1]) ? 2 : 3);
421 optype = (anum == 2 ? O_ITEM2 : O_ITEM3);
422 argflags = arg[anum].arg_flags;
423 if (gimme == G_ARRAY)
424 argflags |= AF_ARYOK;
425 argtype = arg[anum].arg_type & A_MASK;
426 argptr = arg[anum].arg_ptr;
432 if (gimme == G_ARRAY)
437 value = -str_gnum(st[1]);
440 value = (double) !str_true(st[1]);
444 value = (double) ~U_L(str_gnum(st[1]));
448 tmps = stab_name(defoutstab);
450 if ((arg[1].arg_type & A_MASK) == A_WORD)
451 defoutstab = arg[1].arg_ptr.arg_stab;
453 defoutstab = stabent(str_get(st[1]),TRUE);
454 if (!stab_io(defoutstab))
455 stab_io(defoutstab) = stio_new();
456 curoutstab = defoutstab;
464 else if ((arg[1].arg_type & A_MASK) == A_WORD) {
465 if (!(stab = arg[1].arg_ptr.arg_stab))
469 stab = stabent(str_get(st[1]),TRUE);
470 if (!stab_io(stab)) {
476 fp = stab_io(stab)->ofp;
478 if (stab_io(stab)->fmt_stab)
479 form = stab_form(stab_io(stab)->fmt_stab);
481 form = stab_form(stab);
485 warn("No format for filehandle");
487 if (stab_io(stab)->ifp)
488 warn("Filehandle only opened for input");
490 warn("Write on closed filehandle");
497 format(&outrec,form,sp);
498 do_write(&outrec,stab_io(stab),sp);
499 if (stab_io(stab)->flags & IOF_FLUSH)
506 if ((arg[1].arg_type & A_MASK) == A_WORD)
507 stab = arg[1].arg_ptr.arg_stab;
509 stab = stabent(str_get(st[1]),TRUE);
510 anum = (int)str_gnum(st[3]);
511 value = (double)hdbmopen(stab_hash(stab),str_get(st[2]),anum);
514 fatal("No dbm or ndbm on this machine");
518 if ((arg[1].arg_type & A_MASK) == A_WORD)
519 stab = arg[1].arg_ptr.arg_stab;
521 stab = stabent(str_get(st[1]),TRUE);
522 hdbmclose(stab_hash(stab));
525 fatal("No dbm or ndbm on this machine");
528 if ((arg[1].arg_type & A_MASK) == A_WORD)
529 stab = arg[1].arg_ptr.arg_stab;
531 stab = stabent(str_get(st[1]),TRUE);
532 tmps = str_get(st[2]);
533 if (do_open(stab,tmps,st[2]->str_cur)) {
534 value = (double)forkprocess;
535 stab_io(stab)->lines = 0;
538 else if (forkprocess == 0) /* we are a new child */
544 value = (double) do_trans(str,arg);
545 str = arg->arg_ptr.arg_str;
548 str_set(arg->arg_ptr.arg_str, do_trans(str,arg) == 0 ? Yes : No);
549 str = arg->arg_ptr.arg_str;
554 else if ((arg[1].arg_type & A_MASK) == A_WORD)
555 stab = arg[1].arg_ptr.arg_stab;
557 stab = stabent(str_get(st[1]),TRUE);
558 str_set(str, do_close(stab,TRUE) ? Yes : No );
562 sp = do_each(str,stab_hash(arg[1].arg_ptr.arg_stab),
567 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
571 str->str_nok = str->str_pok = 0;
572 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
573 str->str_state = SS_ARY;
576 ary = stab_array(arg[1].arg_ptr.arg_stab);
577 maxarg = ary->ary_fill + 1;
578 if (gimme == G_ARRAY) { /* array wanted */
581 if (maxarg > 0 && sp + maxarg > stack->ary_max) {
582 astore(stack,sp + maxarg, Nullstr);
583 st = stack->ary_array;
585 Copy(ary->ary_array, &st[sp+1], maxarg, STR*);
590 value = (double)maxarg;
594 anum = ((int)str_gnum(st[2])) - arybase;
595 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,FALSE);
600 tmpstab = arg[1].arg_ptr.arg_stab;
601 tmps = str_get(st[2]);
602 str = hdelete(stab_hash(tmpstab),tmps,st[2]->str_cur);
603 if (tmpstab == envstab)
609 str->str_nok = str->str_pok = 0;
610 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
611 str->str_state = SS_HASH;
614 if (gimme == G_ARRAY) { /* array wanted */
615 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
620 tmpstab = arg[1].arg_ptr.arg_stab;
621 sprintf(buf,"%d/%d",stab_hash(tmpstab)->tbl_fill,
622 stab_hash(tmpstab)->tbl_max+1);
627 tmpstab = arg[1].arg_ptr.arg_stab;
628 tmps = str_get(st[2]);
629 str = hfetch(stab_hash(tmpstab),tmps,st[2]->str_cur,FALSE);
634 anum = ((int)str_gnum(st[2])) - arybase;
635 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,TRUE);
637 fatal("Assignment to non-creatable value, subscript %d",anum);
640 tmpstab = arg[1].arg_ptr.arg_stab;
641 tmps = str_get(st[2]);
642 anum = st[2]->str_cur;
643 str = hfetch(stab_hash(tmpstab),tmps,anum,TRUE);
645 fatal("Assignment to non-creatable value, subscript \"%s\"",tmps);
646 if (tmpstab == envstab) /* heavy wizardry going on here */
647 str_magic(str, tmpstab, 'E', tmps, anum); /* str is now magic */
648 /* he threw the brick up into the air */
649 else if (tmpstab == sigstab)
650 str_magic(str, tmpstab, 'S', tmps, anum);
652 else if (stab_hash(tmpstab)->tbl_dbm)
653 str_magic(str, tmpstab, 'D', tmps, anum);
659 goto do_slice_already;
663 goto do_slice_already;
667 goto do_slice_already;
671 goto do_slice_already;
676 sp = do_slice(arg[1].arg_ptr.arg_stab,str,anum,argtype,
680 sp = do_splice(stab_array(arg[1].arg_ptr.arg_stab),str,gimme,arglast);
683 if (arglast[2] - arglast[1] != 1)
684 str = do_push(stab_array(arg[1].arg_ptr.arg_stab),arglast);
686 str = Str_new(51,0); /* must copy the STR */
688 (void)apush(stab_array(arg[1].arg_ptr.arg_stab),str);
692 str = apop(ary = stab_array(arg[1].arg_ptr.arg_stab));
693 goto staticalization;
695 str = ashift(ary = stab_array(arg[1].arg_ptr.arg_stab));
699 if (ary->ary_flags & ARF_REAL)
700 (void)str_2static(str);
703 sp = do_unpack(str,gimme,arglast);
706 value = str_gnum(st[3]);
707 sp = do_split(str, arg[2].arg_ptr.arg_spat, (int)value,
712 value = (double)str_len(stab_val(defstab));
714 value = (double)str_len(st[1]);
717 do_sprintf(str, sp-arglast[0], st+1);
720 anum = ((int)str_gnum(st[2])) - arybase; /* anum=where to start*/
721 tmps = str_get(st[1]); /* force conversion to string */
722 if (argtype = (str == st[1]))
723 str = arg->arg_ptr.arg_str;
725 anum += st[1]->str_cur + arybase;
726 if (anum < 0 || anum > st[1]->str_cur)
729 optype = (int)str_gnum(st[3]);
733 anum = st[1]->str_cur - anum; /* anum=how many bytes left*/
736 str_nset(str, tmps, anum);
737 if (argtype) { /* it's an lvalue! */
738 lstr = (struct lstring*)str;
739 str->str_magic = st[1];
740 st[1]->str_rare = 's';
741 lstr->lstr_offset = tmps - str_get(st[1]);
742 lstr->lstr_len = anum;
747 (void)do_pack(str,arglast);
750 sp = do_grep(arg,str,gimme,arglast);
753 do_join(str,arglast);
756 tmps = str_get(st[1]);
757 value = (double) (str_cmp(st[1],st[2]) < 0);
760 tmps = str_get(st[1]);
761 value = (double) (str_cmp(st[1],st[2]) > 0);
764 tmps = str_get(st[1]);
765 value = (double) (str_cmp(st[1],st[2]) <= 0);
768 tmps = str_get(st[1]);
769 value = (double) (str_cmp(st[1],st[2]) >= 0);
772 tmps = str_get(st[1]);
773 value = (double) str_eq(st[1],st[2]);
776 tmps = str_get(st[1]);
777 value = (double) !str_eq(st[1],st[2]);
780 sp = do_subr(arg,gimme,arglast);
781 st = stack->ary_array + arglast[0]; /* maybe realloced */
784 sp = do_dbsubr(arg,gimme,arglast);
785 st = stack->ary_array + arglast[0]; /* maybe realloced */
788 if ((arg[1].arg_type & A_MASK) == A_WORD)
789 stab = arg[1].arg_ptr.arg_stab;
791 stab = stabent(str_get(st[1]),TRUE);
794 sp = do_sort(str,stab,
802 if (arglast[2] - arglast[1] != 1) {
803 do_join(str,arglast);
804 tmps = str_get(st[1]);
808 tmps = str_get(st[2]);
811 tmps = "Warning: something's wrong";
815 if (arglast[2] - arglast[1] != 1) {
816 do_join(str,arglast);
817 tmps = str_get(st[1]);
821 tmps = str_get(st[2]);
829 if ((arg[1].arg_type & A_MASK) == A_WORD)
830 stab = arg[1].arg_ptr.arg_stab;
832 stab = stabent(str_get(st[1]),TRUE);
835 if (!stab_io(stab)) {
837 warn("Filehandle never opened");
840 if (!(fp = stab_io(stab)->ofp)) {
842 if (stab_io(stab)->ifp)
843 warn("Filehandle opened only for input");
845 warn("Print on closed filehandle");
850 if (optype == O_PRTF || arglast[2] - arglast[1] != 1)
851 value = (double)do_aprint(arg,fp,arglast);
853 value = (double)do_print(st[2],fp);
854 if (orslen && optype == O_PRINT)
855 if (fwrite(ors, 1, orslen, fp) == 0)
858 if (stab_io(stab)->flags & IOF_FLUSH)
859 if (fflush(fp) == EOF)
867 tmps = str_get(st[1]);
868 if (!tmps || !*tmps) {
869 tmpstr = hfetch(stab_hash(envstab),"HOME",4,FALSE);
871 tmps = str_get(tmpstr);
873 if (!tmps || !*tmps) {
874 tmpstr = hfetch(stab_hash(envstab),"LOGDIR",6,FALSE);
876 tmps = str_get(tmpstr);
879 taintproper("Insecure dependency in chdir");
881 value = (double)(chdir(tmps) >= 0);
887 anum = (int)str_gnum(st[1]);
894 tmps = str_get(st[1]);
895 str_reset(tmps,arg[2].arg_ptr.arg_hash);
899 if (gimme == G_ARRAY)
902 str = st[sp - arglast[0]]; /* unwanted list, return last item */
909 else if ((arg[1].arg_type & A_MASK) == A_WORD)
910 stab = arg[1].arg_ptr.arg_stab;
912 stab = stabent(str_get(st[1]),TRUE);
913 str_set(str, do_eof(stab) ? Yes : No);
919 else if ((arg[1].arg_type & A_MASK) == A_WORD)
920 stab = arg[1].arg_ptr.arg_stab;
922 stab = stabent(str_get(st[1]),TRUE);
923 if (do_eof(stab)) /* make sure we have fp with something */
930 *str->str_ptr = getc(stab_io(stab)->ifp); /* should never be EOF */
937 else if ((arg[1].arg_type & A_MASK) == A_WORD)
938 stab = arg[1].arg_ptr.arg_stab;
940 stab = stabent(str_get(st[1]),TRUE);
942 value = (double)do_tell(stab);
949 if ((arg[1].arg_type & A_MASK) == A_WORD)
950 stab = arg[1].arg_ptr.arg_stab;
952 stab = stabent(str_get(st[1]),TRUE);
953 tmps = str_get(st[2]);
954 anum = (int)str_gnum(st[3]);
955 STR_GROW(st[2], anum+1), (tmps = str_get(st[2])); /* sneaky */
957 if (!stab_io(stab) || !stab_io(stab)->ifp)
960 else if (optype == O_RECV) {
961 argtype = sizeof buf;
962 optype = (int)str_gnum(st[4]);
963 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, optype,
966 st[2]->str_cur = anum;
967 st[2]->str_ptr[anum] = '\0';
968 str_nset(str,buf,argtype);
971 str_sset(str,&str_undef);
974 else if (stab_io(stab)->type == 's') {
975 argtype = sizeof buf;
976 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, 0,
980 else if (optype == O_RECV)
984 anum = fread(tmps, 1, anum, stab_io(stab)->ifp);
987 st[2]->str_cur = anum;
988 st[2]->str_ptr[anum] = '\0';
989 value = (double)anum;
993 if ((arg[1].arg_type & A_MASK) == A_WORD)
994 stab = arg[1].arg_ptr.arg_stab;
996 stab = stabent(str_get(st[1]),TRUE);
997 tmps = str_get(st[2]);
998 anum = (int)str_gnum(st[3]);
999 optype = sp - arglast[0];
1002 warn("Too many args on send");
1003 stio = stab_io(stab);
1004 if (!stio || !stio->ifp) {
1007 warn("Send on closed socket");
1009 else if (optype >= 4) {
1010 tmps2 = str_get(st[4]);
1011 anum = sendto(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur,
1012 anum, tmps2, st[4]->str_cur);
1015 anum = send(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur, anum);
1018 value = (double)anum;
1024 if ((arg[1].arg_type & A_MASK) == A_WORD)
1025 stab = arg[1].arg_ptr.arg_stab;
1027 stab = stabent(str_get(st[1]),TRUE);
1028 value = str_gnum(st[2]);
1029 str_set(str, do_seek(stab,
1030 (long)value, (int)str_gnum(st[3]) ) ? Yes : No);
1034 tmps = "_SUB_"; /* just fake up a "last _SUB_" */
1036 if (wantarray == G_ARRAY) {
1037 lastretstr = Nullstr;
1038 lastspbase = arglast[1];
1039 lastsize = arglast[2] - arglast[1];
1042 lastretstr = str_static(st[arglast[2] - arglast[0]]);
1048 tmps = str_get(arg[1].arg_ptr.arg_str);
1050 while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
1051 strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
1054 deb("(Skipping label #%d %s)\n",loop_ptr,
1055 loop_stack[loop_ptr].loop_label);
1062 deb("(Found label #%d %s)\n",loop_ptr,
1063 loop_stack[loop_ptr].loop_label);
1068 fatal("Bad label: %s", maxarg > 0 ? tmps : "<null>");
1069 if (!lastretstr && optype == O_LAST && lastsize) {
1071 st += lastspbase + 1;
1072 optype = loop_stack[loop_ptr].loop_sp - lastspbase; /* negative */
1074 for (anum = lastsize; anum > 0; anum--,st++)
1075 st[optype] = str_static(st[0]);
1077 longjmp(loop_stack[loop_ptr].loop_env, O_LAST);
1079 longjmp(loop_stack[loop_ptr].loop_env, optype);
1081 case O_GOTO:/* shudder */
1082 goto_targ = str_get(arg[1].arg_ptr.arg_str);
1084 goto_targ = Nullch; /* just restart from top */
1085 if (optype == O_DUMP) {
1089 longjmp(top_env, 1);
1091 tmps = str_get(st[1]);
1093 if (!(tmps2 = fbminstr((unsigned char*)tmps,
1094 (unsigned char*)tmps + st[1]->str_cur, st[2])))
1096 if (tmps2 = fbminstr(Null(unsigned char*),Null(unsigned char*),Nullstr))
1098 value = (double)(-1 + arybase);
1100 value = (double)(tmps2 - tmps + arybase);
1103 tmps = str_get(st[1]);
1104 tmps2 = str_get(st[2]);
1106 if (!(tmps2 = rninstr(tmps, tmps + st[1]->str_cur,
1107 tmps2, tmps2 + st[2]->str_cur)))
1109 if (tmps2 = rninstr(Nullch,Nullch,Nullch,Nullch))
1111 value = (double)(-1 + arybase);
1113 value = (double)(tmps2 - tmps + arybase);
1117 value = (double) time(Null(long*));
1121 sp = do_tms(str,gimme,arglast);
1127 when = (long)str_gnum(st[1]);
1128 sp = do_time(str,localtime(&when),
1135 when = (long)str_gnum(st[1]);
1136 sp = do_time(str,gmtime(&when),
1141 sp = do_stat(str,arg,
1146 tmps = str_get(st[1]);
1148 str_set(str,fcrypt(tmps,str_get(st[2])));
1150 str_set(str,crypt(tmps,str_get(st[2])));
1154 "The crypt() function is unimplemented due to excessive paranoia.");
1158 value = str_gnum(st[1]);
1159 value = atan2(value,str_gnum(st[2]));
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]);
1179 value = str_gnum(st[1]);
1183 value = rand() * value / 2147483648.0;
1186 value = rand() * value / 65536.0;
1189 value = rand() * value / 32768.0;
1191 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1202 anum = (int)str_gnum(st[1]);
1207 value = str_gnum(stab_val(defstab));
1209 value = str_gnum(st[1]);
1214 value = str_gnum(stab_val(defstab));
1216 value = str_gnum(st[1]);
1221 value = str_gnum(stab_val(defstab));
1223 value = str_gnum(st[1]);
1224 value = sqrt(value);
1228 value = str_gnum(stab_val(defstab));
1230 value = str_gnum(st[1]);
1232 (void)modf(value,&value);
1234 (void)modf(-value,&value);
1240 tmps = str_get(stab_val(defstab));
1242 tmps = str_get(st[1]);
1244 value = (double) (*tmps & 255);
1247 value = (double) (anum & 255);
1254 tmps = str_get(st[1]);
1256 if (!tmps || !*tmps)
1257 sleep((32767<<16)+32767);
1259 sleep((unsigned int)atoi(tmps));
1261 value = (double)when;
1263 value = ((double)when) - value;
1267 sp = do_range(gimme,arglast);
1270 if (gimme == G_ARRAY) { /* it's a range */
1271 /* can we optimize to constant array? */
1272 if ((arg[1].arg_type & A_MASK) == A_SINGLE &&
1273 (arg[2].arg_type & A_MASK) == A_SINGLE) {
1274 st[2] = arg[2].arg_ptr.arg_str;
1275 sp = do_range(gimme,arglast);
1276 st = stack->ary_array;
1277 maxarg = sp - arglast[0];
1278 str_free(arg[1].arg_ptr.arg_str);
1279 str_free(arg[2].arg_ptr.arg_str);
1280 arg->arg_type = O_ARRAY;
1281 arg[1].arg_type = A_STAB|A_DONT;
1283 stab = arg[1].arg_ptr.arg_stab = aadd(genstab());
1284 ary = stab_array(stab);
1285 afill(ary,maxarg - 1);
1287 while (maxarg-- > 0)
1288 ary->ary_array[maxarg] = str_smake(st[maxarg]);
1291 arg->arg_type = optype = O_RANGE;
1292 maxarg = arg->arg_len = 2;
1294 arg[anum].arg_flags &= ~AF_ARYOK;
1295 argflags = arg[anum].arg_flags;
1296 argtype = arg[anum].arg_type & A_MASK;
1297 arg[anum].arg_type = argtype;
1298 argptr = arg[anum].arg_ptr;
1304 arg->arg_type = O_FLIP;
1307 if ((arg[1].arg_type & A_MASK) == A_SINGLE ?
1308 last_in_stab && (int)str_gnum(st[1]) == stab_io(last_in_stab)->lines
1311 str_numset(str,0.0);
1313 arg->arg_type = optype = O_FLOP;
1314 arg[2].arg_type &= ~A_DONT;
1315 arg[1].arg_type |= A_DONT;
1316 argflags = arg[2].arg_flags;
1317 argtype = arg[2].arg_type & A_MASK;
1318 argptr = arg[2].arg_ptr;
1327 if ((arg[2].arg_type & A_MASK) == A_SINGLE ?
1328 last_in_stab && (int)str_gnum(st[2]) == stab_io(last_in_stab)->lines
1331 arg->arg_type = O_FLIP;
1332 arg[1].arg_type &= ~A_DONT;
1333 arg[2].arg_type |= A_DONT;
1340 if (!anum && (tmpstab = stabent("$",allstabs)))
1341 str_numset(STAB_STR(tmpstab),(double)getpid());
1342 value = (double)anum;
1345 fatal("Unsupported function fork");
1351 anum = wait(&argflags);
1353 pidgone(anum,argflags);
1354 value = (double)anum;
1356 statusvalue = (unsigned short)argflags;
1359 fatal("Unsupported function wait");
1365 if (arglast[2] - arglast[1] == 1) {
1367 tainted |= st[2]->str_tainted;
1368 taintproper("Insecure dependency in system");
1371 while ((anum = vfork()) == -1) {
1372 if (errno != EAGAIN) {
1380 ihand = signal(SIGINT, SIG_IGN);
1381 qhand = signal(SIGQUIT, SIG_IGN);
1382 while ((argtype = wait(&argflags)) != anum && argtype >= 0)
1383 pidgone(argtype,argflags);
1387 (void)signal(SIGINT, ihand);
1388 (void)signal(SIGQUIT, qhand);
1389 statusvalue = (unsigned short)argflags;
1393 value = (double)((unsigned int)argflags & 0xffff);
1397 if ((arg[1].arg_type & A_MASK) == A_STAB)
1398 value = (double)do_aexec(st[1],arglast);
1399 else if (arglast[2] - arglast[1] != 1)
1400 value = (double)do_aexec(Nullstr,arglast);
1402 value = (double)do_exec(str_get(str_static(st[2])));
1406 if ((arg[1].arg_type & A_MASK) == A_STAB)
1407 value = (double)do_aspawn(st[1],arglast);
1408 else if (arglast[2] - arglast[1] != 1)
1409 value = (double)do_aspawn(Nullstr,arglast);
1411 value = (double)do_spawn(str_get(str_static(st[2])));
1416 if ((arg[1].arg_type & A_MASK) == A_STAB)
1417 value = (double)do_aexec(st[1],arglast);
1418 else if (arglast[2] - arglast[1] != 1)
1419 value = (double)do_aexec(Nullstr,arglast);
1421 value = (double)do_exec(str_get(str_static(st[2])));
1434 tmps = str_get(stab_val(defstab));
1436 tmps = str_get(st[1]);
1445 case '0': case '1': case '2': case '3': case '4':
1446 case '5': case '6': case '7':
1448 anum += *tmps++ & 15;
1450 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1451 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1455 anum += (*tmps++ & 7) + 9;
1464 value = (double)anum;
1468 value = (double)apply(optype,arglast);
1471 fatal("Unsupported function chown");
1476 value = (double)apply(optype,arglast);
1479 fatal("Unsupported function kill");
1485 value = (double)apply(optype,arglast);
1494 anum = umask((int)str_gnum(st[1]));
1495 value = (double)anum;
1497 taintproper("Insecure dependency in umask");
1501 fatal("Unsupported function umask");
1505 tmps = str_get(st[1]);
1506 tmps2 = str_get(st[2]);
1508 taintproper("Insecure dependency in rename");
1511 value = (double)(rename(tmps,tmps2) >= 0);
1513 if (euid || stat(tmps2,&statbuf) < 0 ||
1514 (statbuf.st_mode & S_IFMT) != S_IFDIR )
1515 (void)UNLINK(tmps2); /* avoid unlinking a directory */
1516 if (!(anum = link(tmps,tmps2)))
1517 anum = UNLINK(tmps);
1518 value = (double)(anum >= 0);
1523 tmps = str_get(st[1]);
1524 tmps2 = str_get(st[2]);
1526 taintproper("Insecure dependency in link");
1528 value = (double)(link(tmps,tmps2) >= 0);
1531 fatal("Unsupported function link");
1535 tmps = str_get(st[1]);
1536 anum = (int)str_gnum(st[2]);
1538 taintproper("Insecure dependency in mkdir");
1541 value = (double)(mkdir(tmps,anum) >= 0);
1544 (void)strcpy(buf,"mkdir ");
1546 #if !defined(MKDIR) || !defined(RMDIR)
1548 for (tmps2 = buf+6; *tmps; ) {
1552 (void)strcpy(tmps2," 2>&1");
1553 rsfp = mypopen(buf,"r");
1556 tmps2 = fgets(buf,sizeof buf,rsfp);
1557 (void)mypclose(rsfp);
1558 if (tmps2 != Nullch) {
1559 for (errno = 1; errno < sys_nerr; errno++) {
1560 if (instr(buf,sys_errlist[errno])) /* you don't see this */
1565 #define EACCES EPERM
1567 if (instr(buf,"cannot make"))
1569 else if (instr(buf,"non-exist"))
1571 else if (instr(buf,"does not exist"))
1573 else if (instr(buf,"not empty"))
1575 else if (instr(buf,"cannot access"))
1581 else { /* some mkdirs return no failure indication */
1582 tmps = str_get(st[1]);
1583 anum = (stat(tmps,&statbuf) >= 0);
1584 if (optype == O_RMDIR)
1589 errno = EACCES; /* a guess */
1590 value = (double)anum;
1599 tmps = str_get(stab_val(defstab));
1601 tmps = str_get(st[1]);
1603 taintproper("Insecure dependency in rmdir");
1606 value = (double)(rmdir(tmps) >= 0);
1609 (void)strcpy(buf,"rmdir ");
1610 goto one_liner; /* see above in MKDIR */
1614 value = (double)getppid();
1617 fatal("Unsupported function getppid");
1625 anum = (int)str_gnum(st[1]);
1626 value = (double)getpgrp(anum);
1629 fatal("The getpgrp() function is unimplemented on this machine");
1634 argtype = (int)str_gnum(st[1]);
1635 anum = (int)str_gnum(st[2]);
1637 taintproper("Insecure dependency in setpgrp");
1639 value = (double)(setpgrp(argtype,anum) >= 0);
1642 fatal("The setpgrp() function is unimplemented on this machine");
1647 argtype = (int)str_gnum(st[1]);
1648 anum = (int)str_gnum(st[2]);
1649 value = (double)getpriority(argtype,anum);
1652 fatal("The getpriority() function is unimplemented on this machine");
1657 argtype = (int)str_gnum(st[1]);
1658 anum = (int)str_gnum(st[2]);
1659 optype = (int)str_gnum(st[3]);
1661 taintproper("Insecure dependency in setpriority");
1663 value = (double)(setpriority(argtype,anum,optype) >= 0);
1666 fatal("The setpriority() function is unimplemented on this machine");
1672 tmps = str_get(stab_val(defstab));
1674 tmps = str_get(st[1]);
1676 taintproper("Insecure dependency in chroot");
1678 value = (double)(chroot(tmps) >= 0);
1681 fatal("Unsupported function chroot");
1687 stab = last_in_stab;
1688 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1689 stab = arg[1].arg_ptr.arg_stab;
1691 stab = stabent(str_get(st[1]),TRUE);
1692 argtype = U_I(str_gnum(st[2]));
1694 taintproper("Insecure dependency in ioctl");
1696 anum = do_ctl(optype,stab,argtype,st[3]);
1700 value = (double)anum;
1703 str_set(str,"0 but true");
1709 stab = last_in_stab;
1710 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1711 stab = arg[1].arg_ptr.arg_stab;
1713 stab = stabent(str_get(st[1]),TRUE);
1714 if (stab && stab_io(stab))
1715 fp = stab_io(stab)->ifp;
1719 argtype = (int)str_gnum(st[2]);
1720 value = (double)(flock(fileno(fp),argtype) >= 0);
1726 fatal("The flock() function is unimplemented on this machine");
1730 ary = stab_array(arg[1].arg_ptr.arg_stab);
1731 if (arglast[2] - arglast[1] != 1)
1732 do_unshift(ary,arglast);
1734 str = Str_new(52,0); /* must copy the STR */
1735 str_sset(str,st[2]);
1737 (void)astore(ary,0,str);
1739 value = (double)(ary->ary_fill + 1);
1744 tmpstr = stab_val(defstab);
1747 (arg[1].arg_type & A_MASK) != A_NULL ? st[1] : stab_val(defstab);
1749 tainted |= tmpstr->str_tainted;
1750 taintproper("Insecure dependency in eval");
1752 sp = do_eval(tmpstr, optype, arg[2].arg_ptr.arg_hash,
1780 if (mystat(arg,st[1]) < 0)
1782 if (cando(anum,argtype,&statcache))
1787 if (mystat(arg,st[1]) < 0)
1792 if (mystat(arg,st[1]) < 0)
1794 if (statcache.st_uid == (optype == O_FTEOWNED ? euid : uid) )
1798 if (mystat(arg,st[1]) < 0)
1800 if (!statcache.st_size)
1804 if (mystat(arg,st[1]) < 0)
1806 if (statcache.st_size)
1813 goto check_file_type;
1819 goto check_file_type;
1823 goto check_file_type;
1829 goto check_file_type;
1833 if (mystat(arg,st[1]) < 0)
1835 if ((statcache.st_mode & S_IFMT) == anum )
1841 goto check_file_type;
1846 if (arg[1].arg_type & A_DONT)
1847 fatal("You must supply explicit filename with -l");
1849 if (lstat(str_get(st[1]),&statcache) < 0)
1851 if ((statcache.st_mode & S_IFMT) == S_IFLNK )
1857 tmps = str_get(st[1]);
1858 tmps2 = str_get(st[2]);
1860 taintproper("Insecure dependency in symlink");
1862 value = (double)(symlink(tmps,tmps2) >= 0);
1865 fatal("Unsupported function symlink");
1870 tmps = str_get(stab_val(defstab));
1872 tmps = str_get(st[1]);
1873 anum = readlink(tmps,buf,sizeof buf);
1876 str_nset(str,buf,anum);
1879 fatal("Unsupported function readlink");
1902 if (mystat(arg,st[1]) < 0)
1904 if (statcache.st_mode & anum)
1908 if (arg[1].arg_type & A_DONT) {
1909 stab = arg[1].arg_ptr.arg_stab;
1913 stab = stabent(tmps = str_get(st[1]),FALSE);
1914 if (stab && stab_io(stab) && stab_io(stab)->ifp)
1915 anum = fileno(stab_io(stab)->ifp);
1916 else if (isdigit(*tmps))
1925 str = do_fttext(arg,st[1]);
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);
1934 value = (double)do_socket(stab,arglast);
1936 (void)do_socket(stab,arglast);
1940 if ((arg[1].arg_type & A_MASK) == A_WORD)
1941 stab = arg[1].arg_ptr.arg_stab;
1943 stab = stabent(str_get(st[1]),TRUE);
1945 value = (double)do_bind(stab,arglast);
1947 (void)do_bind(stab,arglast);
1951 if ((arg[1].arg_type & A_MASK) == A_WORD)
1952 stab = arg[1].arg_ptr.arg_stab;
1954 stab = stabent(str_get(st[1]),TRUE);
1956 value = (double)do_connect(stab,arglast);
1958 (void)do_connect(stab,arglast);
1962 if ((arg[1].arg_type & A_MASK) == A_WORD)
1963 stab = arg[1].arg_ptr.arg_stab;
1965 stab = stabent(str_get(st[1]),TRUE);
1967 value = (double)do_listen(stab,arglast);
1969 (void)do_listen(stab,arglast);
1973 if ((arg[1].arg_type & A_MASK) == A_WORD)
1974 stab = arg[1].arg_ptr.arg_stab;
1976 stab = stabent(str_get(st[1]),TRUE);
1977 if ((arg[2].arg_type & A_MASK) == A_WORD)
1978 stab2 = arg[2].arg_ptr.arg_stab;
1980 stab2 = stabent(str_get(st[2]),TRUE);
1981 do_accept(str,stab,stab2);
1989 sp = do_ghent(optype,
1997 sp = do_gnent(optype,
2005 sp = do_gpent(optype,
2013 sp = do_gsent(optype,
2017 value = (double) sethostent((int)str_gnum(st[1]));
2020 value = (double) setnetent((int)str_gnum(st[1]));
2023 value = (double) setprotoent((int)str_gnum(st[1]));
2026 value = (double) setservent((int)str_gnum(st[1]));
2029 value = (double) endhostent();
2032 value = (double) endnetent();
2035 value = (double) endprotoent();
2038 value = (double) endservent();
2041 sp = do_select(gimme,arglast);
2044 if ((arg[1].arg_type & A_MASK) == A_WORD)
2045 stab = arg[1].arg_ptr.arg_stab;
2047 stab = stabent(str_get(st[1]),TRUE);
2048 if ((arg[2].arg_type & A_MASK) == A_WORD)
2049 stab2 = arg[2].arg_ptr.arg_stab;
2051 stab2 = stabent(str_get(st[2]),TRUE);
2053 value = (double)do_spair(stab,stab2,arglast);
2055 (void)do_spair(stab,stab2,arglast);
2059 if ((arg[1].arg_type & A_MASK) == A_WORD)
2060 stab = arg[1].arg_ptr.arg_stab;
2062 stab = stabent(str_get(st[1]),TRUE);
2064 value = (double)do_shutdown(stab,arglast);
2066 (void)do_shutdown(stab,arglast);
2071 if ((arg[1].arg_type & A_MASK) == A_WORD)
2072 stab = arg[1].arg_ptr.arg_stab;
2074 stab = stabent(str_get(st[1]),TRUE);
2075 sp = do_sopt(optype,stab,arglast);
2079 if ((arg[1].arg_type & A_MASK) == A_WORD)
2080 stab = arg[1].arg_ptr.arg_stab;
2082 stab = stabent(str_get(st[1]),TRUE);
2083 sp = do_getsockname(optype,stab,arglast);
2086 #else /* SOCKET not defined */
2120 fatal("Unsupported socket function");
2125 if ((arg[1].arg_type & A_MASK) == A_WORD)
2126 stab = arg[1].arg_ptr.arg_stab;
2128 stab = stabent(str_get(st[1]),TRUE);
2129 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2136 if ((arg[1].arg_type & A_MASK) == A_WORD)
2137 stab = arg[1].arg_ptr.arg_stab;
2139 stab = stabent(str_get(st[1]),TRUE);
2140 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2143 str_set(str, (setmode(fileno(fp), O_BINARY) != -1) ? Yes : No);
2150 sp = do_vec(str == st[1], arg->arg_ptr.arg_str, arglast);
2156 sp = do_gpwent(optype,
2160 value = (double) setpwent();
2163 value = (double) endpwent();
2168 fatal("Unsupported password function");
2175 sp = do_ggrent(optype,
2179 value = (double) setgrent();
2182 value = (double) endgrent();
2187 fatal("Unsupported group function");
2192 if (!(tmps = getlogin()))
2196 fatal("Unsupported function getlogin");
2207 if ((arg[1].arg_type & A_MASK) == A_WORD)
2208 stab = arg[1].arg_ptr.arg_stab;
2210 stab = stabent(str_get(st[1]),TRUE);
2211 sp = do_dirop(optype,stab,gimme,arglast);
2214 value = (double)do_syscall(arglast);
2218 if ((arg[1].arg_type & A_MASK) == A_WORD)
2219 stab = arg[1].arg_ptr.arg_stab;
2221 stab = stabent(str_get(st[1]),TRUE);
2222 if ((arg[2].arg_type & A_MASK) == A_WORD)
2223 stab2 = arg[2].arg_ptr.arg_stab;
2225 stab2 = stabent(str_get(st[2]),TRUE);
2226 do_pipe(str,stab,stab2);
2229 fatal("Unsupported function pipe");
2240 deb("%s RETURNS \"%s\"\n",opname[optype],str_get(str));
2243 return arglast[0] + 1;
2250 anum = sp - arglast[0];
2253 deb("%s RETURNS ()\n",opname[optype]);
2256 deb("%s RETURNS (\"%s\")\n",opname[optype],str_get(st[1]));
2259 deb("%s RETURNS %d ARGS (\"%s\",%s\"%s\"\n",opname[optype],anum,
2260 str_get(st[1]),anum==2?"":"...,",str_get(st[anum]));
2285 str_numset(str,value);
2292 deb("%s RETURNS \"%f\"\n",opname[optype],value);
2295 return arglast[0] + 1;