1 /* $Header: eval.c,v 3.0.1.4 90/02/28 17:36:59 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.4 90/02/28 17:36:59 lwall
10 * patch9: added pipe function
11 * patch9: a return in scalar context wouldn't return array
12 * patch9: !~ now always returns scalar even in array context
13 * patch9: some machines can't cast float to long with high bit set
14 * patch9: piped opens returned undef in child
15 * patch9: @array in scalar context now returns length of array
16 * patch9: chdir; coredumped
17 * patch9: wait no longer ignores signals
18 * patch9: mkdir now handles odd versions of /bin/mkdir
19 * patch9: -l FILEHANDLE now disallowed
21 * Revision 3.0.1.3 89/12/21 20:03:05 lwall
22 * patch7: errno may now be a macro with an lvalue
23 * patch7: ANSI strerror() is now supported
24 * patch7: send() didn't allow a TO argument
25 * patch7: ord() now always returns positive even on signed char machines
27 * Revision 3.0.1.2 89/11/17 15:19:34 lwall
28 * patch5: constant numeric subscripts get lost inside ?:
30 * Revision 3.0.1.1 89/11/11 04:31:51 lwall
31 * patch2: mkdir and rmdir needed to quote argument when passed to shell
32 * patch2: mkdir and rmdir now return better error codes
33 * patch2: fileno, seekdir, rewinddir and closedir now disallow defaults
35 * Revision 3.0 89/10/18 15:17:04 lwall
50 static void (*ihand)();
51 static void (*qhand)();
53 static int (*ihand)();
54 static int (*qhand)();
61 static struct lstring *lstr;
62 static char old_record_separator;
65 double sin(), cos(), atan2(), pow();
86 int arglast[8]; /* highest sp for arg--valid only for non-O_LIST args */
87 unsigned long tmplong;
94 bool assigning = FALSE;
95 double exp(), log(), sqrt(), modf();
96 char *crypt(), *getenv();
97 extern void grow_dlevel();
101 optype = arg->arg_type;
102 maxarg = arg->arg_len;
104 str = arg->arg_ptr.arg_str;
105 if (sp + maxarg > stack->ary_max)
106 astore(stack, sp + maxarg, Nullstr);
107 st = stack->ary_array;
112 deb("%s (%lx) %d args:\n",opname[optype],arg,maxarg);
114 debname[dlevel] = opname[optype][0];
115 debdelim[dlevel] = ':';
116 if (++dlevel >= dlmax)
121 #include "evalargs.xc"
129 if (gimme == G_ARRAY)
135 if (gimme == G_ARRAY)
138 STR_SSET(str,st[arglast[anum]-arglast[0]]);
142 if (gimme == G_ARRAY)
145 STR_SSET(str,st[arglast[anum]-arglast[0]]);
155 anum = (int)str_gnum(st[2]);
157 tmpstr = Str_new(50, 0);
158 str_sset(tmpstr,str);
159 tmps = str_get(tmpstr); /* force to be string */
160 STR_GROW(str, (anum * str->str_cur) + 1);
161 repeatcpy(str->str_ptr, tmps, tmpstr->str_cur, anum);
162 str->str_cur *= anum; str->str_ptr[str->str_cur] = '\0';
165 str_sset(str,&str_no);
169 sp = do_match(str,arg,
171 if (gimme == G_ARRAY)
176 sp = do_match(str,arg,
178 str_sset(str, str_true(str) ? &str_no : &str_yes);
182 sp = do_subst(str,arg,arglast[0]);
185 sp = do_subst(str,arg,arglast[0]);
186 str = arg->arg_ptr.arg_str;
187 str_set(str, str_true(str) ? No : Yes);
190 if (arg[1].arg_flags & AF_ARYOK) {
191 if (arg->arg_len == 1) {
192 arg->arg_type = O_LOCAL;
196 arg->arg_type = O_AASSIGN;
201 arg->arg_type = O_SASSIGN;
206 arglast[2] = arglast[1]; /* push a null array */
215 STR_SSET(str, st[2]);
220 str = arg->arg_ptr.arg_str;
221 for (sp = arglast[0] + 1; sp <= arglast[1]; sp++)
226 if (arg[1].arg_type & A_DONT) {
227 sp = do_defined(str,arg,
231 else if (str->str_pok || str->str_nok)
235 if (arg[1].arg_type & A_DONT) {
236 sp = do_undef(str,arg,
240 else if (str != stab_val(defstab)) {
241 str->str_pok = str->str_nok = 0;
246 sp = do_study(str,arg,
250 value = str_gnum(st[1]);
251 value = pow(value,str_gnum(st[2]));
254 value = str_gnum(st[1]);
255 value *= str_gnum(st[2]);
258 if ((value = str_gnum(st[2])) == 0.0)
259 fatal("Illegal division by zero");
260 value = str_gnum(st[1]) / value;
263 tmplong = (long) str_gnum(st[2]);
265 fatal("Illegal modulus zero");
266 when = (long)str_gnum(st[1]);
269 value = (double)(when % tmplong);
271 value = (double)(tmplong - (-when % tmplong));
275 value = str_gnum(st[1]);
276 value += str_gnum(st[2]);
279 value = str_gnum(st[1]);
280 value -= str_gnum(st[2]);
283 value = str_gnum(st[1]);
284 anum = (int)str_gnum(st[2]);
286 value = (double)(((unsigned long)value) << anum);
290 value = str_gnum(st[1]);
291 anum = (int)str_gnum(st[2]);
293 value = (double)(((unsigned long)value) >> anum);
297 value = str_gnum(st[1]);
298 value = (value < str_gnum(st[2])) ? 1.0 : 0.0;
301 value = str_gnum(st[1]);
302 value = (value > str_gnum(st[2])) ? 1.0 : 0.0;
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;
314 if ((!st[1]->str_nok && !looks_like_number(st[1])) ||
315 (!st[2]->str_nok && !looks_like_number(st[2])) )
316 warn("Possible use of == on string value");
318 value = str_gnum(st[1]);
319 value = (value == str_gnum(st[2])) ? 1.0 : 0.0;
322 value = str_gnum(st[1]);
323 value = (value != str_gnum(st[2])) ? 1.0 : 0.0;
326 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
327 value = str_gnum(st[1]);
329 value = (double)(((unsigned long)value) &
330 (unsigned long)str_gnum(st[2]));
335 do_vop(optype,str,st[1],st[2]);
338 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
339 value = str_gnum(st[1]);
341 value = (double)(((unsigned long)value) ^
342 (unsigned long)str_gnum(st[2]));
347 do_vop(optype,str,st[1],st[2]);
350 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
351 value = str_gnum(st[1]);
353 value = (double)(((unsigned long)value) |
354 (unsigned long)str_gnum(st[2]));
359 do_vop(optype,str,st[1],st[2]);
361 /* use register in evaluating str_true() */
363 if (str_true(st[1])) {
366 argflags = arg[anum].arg_flags;
367 if (gimme == G_ARRAY)
368 argflags |= AF_ARYOK;
369 argtype = arg[anum].arg_type & A_MASK;
370 argptr = arg[anum].arg_ptr;
378 str_sset(str, st[1]);
386 if (str_true(st[1])) {
388 str_sset(str, st[1]);
398 argflags = arg[anum].arg_flags;
399 if (gimme == G_ARRAY)
400 argflags |= AF_ARYOK;
401 argtype = arg[anum].arg_type & A_MASK;
402 argptr = arg[anum].arg_ptr;
409 anum = (str_true(st[1]) ? 2 : 3);
410 optype = (anum == 2 ? O_ITEM2 : O_ITEM3);
411 argflags = arg[anum].arg_flags;
412 if (gimme == G_ARRAY)
413 argflags |= AF_ARYOK;
414 argtype = arg[anum].arg_type & A_MASK;
415 argptr = arg[anum].arg_ptr;
421 if (gimme == G_ARRAY)
426 value = -str_gnum(st[1]);
429 value = (double) !str_true(st[1]);
433 value = (double) ~(unsigned long)str_gnum(st[1]);
437 tmps = stab_name(defoutstab);
439 if ((arg[1].arg_type & A_MASK) == A_WORD)
440 defoutstab = arg[1].arg_ptr.arg_stab;
442 defoutstab = stabent(str_get(st[1]),TRUE);
443 if (!stab_io(defoutstab))
444 stab_io(defoutstab) = stio_new();
445 curoutstab = defoutstab;
453 else if ((arg[1].arg_type & A_MASK) == A_WORD) {
454 if (!(stab = arg[1].arg_ptr.arg_stab))
458 stab = stabent(str_get(st[1]),TRUE);
459 if (!stab_io(stab)) {
465 fp = stab_io(stab)->ofp;
467 if (stab_io(stab)->fmt_stab)
468 form = stab_form(stab_io(stab)->fmt_stab);
470 form = stab_form(stab);
474 warn("No format for filehandle");
476 if (stab_io(stab)->ifp)
477 warn("Filehandle only opened for input");
479 warn("Write on closed filehandle");
486 format(&outrec,form,sp);
487 do_write(&outrec,stab_io(stab),sp);
488 if (stab_io(stab)->flags & IOF_FLUSH)
495 if ((arg[1].arg_type & A_MASK) == A_WORD)
496 stab = arg[1].arg_ptr.arg_stab;
498 stab = stabent(str_get(st[1]),TRUE);
499 anum = (int)str_gnum(st[3]);
500 value = (double)hdbmopen(stab_hash(stab),str_get(st[2]),anum);
503 fatal("No dbm or ndbm on this machine");
507 if ((arg[1].arg_type & A_MASK) == A_WORD)
508 stab = arg[1].arg_ptr.arg_stab;
510 stab = stabent(str_get(st[1]),TRUE);
511 hdbmclose(stab_hash(stab));
514 fatal("No dbm or ndbm on this machine");
517 if ((arg[1].arg_type & A_MASK) == A_WORD)
518 stab = arg[1].arg_ptr.arg_stab;
520 stab = stabent(str_get(st[1]),TRUE);
521 tmps = str_get(st[2]);
522 if (do_open(stab,tmps,st[2]->str_cur)) {
523 value = (double)forkprocess;
524 stab_io(stab)->lines = 0;
527 else if (forkprocess == 0) /* we are a new child */
533 value = (double) do_trans(str,arg);
534 str = arg->arg_ptr.arg_str;
537 str_set(arg->arg_ptr.arg_str, do_trans(str,arg) == 0 ? Yes : No);
538 str = arg->arg_ptr.arg_str;
543 else if ((arg[1].arg_type & A_MASK) == A_WORD)
544 stab = arg[1].arg_ptr.arg_stab;
546 stab = stabent(str_get(st[1]),TRUE);
547 str_set(str, do_close(stab,TRUE) ? Yes : No );
551 sp = do_each(str,stab_hash(arg[1].arg_ptr.arg_stab),
556 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
560 str->str_nok = str->str_pok = 0;
561 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
562 str->str_state = SS_ARY;
565 ary = stab_array(arg[1].arg_ptr.arg_stab);
566 maxarg = ary->ary_fill + 1;
567 if (gimme == G_ARRAY) { /* array wanted */
570 if (maxarg > 0 && sp + maxarg > stack->ary_max) {
571 astore(stack,sp + maxarg, Nullstr);
572 st = stack->ary_array;
574 Copy(ary->ary_array, &st[sp+1], maxarg, STR*);
579 value = (double)maxarg;
583 anum = ((int)str_gnum(st[2])) - arybase;
584 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,FALSE);
589 tmpstab = arg[1].arg_ptr.arg_stab;
590 tmps = str_get(st[2]);
591 str = hdelete(stab_hash(tmpstab),tmps,st[2]->str_cur);
592 if (tmpstab == envstab)
598 str->str_nok = str->str_pok = 0;
599 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
600 str->str_state = SS_HASH;
603 if (gimme == G_ARRAY) { /* array wanted */
604 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
609 tmpstab = arg[1].arg_ptr.arg_stab;
610 sprintf(buf,"%d/%d",stab_hash(tmpstab)->tbl_fill,
611 stab_hash(tmpstab)->tbl_max+1);
616 tmpstab = arg[1].arg_ptr.arg_stab;
617 tmps = str_get(st[2]);
618 str = hfetch(stab_hash(tmpstab),tmps,st[2]->str_cur,FALSE);
623 anum = ((int)str_gnum(st[2])) - arybase;
624 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,TRUE);
626 fatal("Assignment to non-creatable value, subscript %d",anum);
629 tmpstab = arg[1].arg_ptr.arg_stab;
630 tmps = str_get(st[2]);
631 anum = st[2]->str_cur;
632 str = hfetch(stab_hash(tmpstab),tmps,anum,TRUE);
634 fatal("Assignment to non-creatable value, subscript \"%s\"",tmps);
635 if (tmpstab == envstab) /* heavy wizardry going on here */
636 str_magic(str, tmpstab, 'E', tmps, anum); /* str is now magic */
637 /* he threw the brick up into the air */
638 else if (tmpstab == sigstab)
639 str_magic(str, tmpstab, 'S', tmps, anum);
641 else if (stab_hash(tmpstab)->tbl_dbm)
642 str_magic(str, tmpstab, 'D', tmps, anum);
648 goto do_slice_already;
652 goto do_slice_already;
656 goto do_slice_already;
661 sp = do_slice(arg[1].arg_ptr.arg_stab,anum,argtype,
665 if (arglast[2] - arglast[1] != 1)
666 str = do_push(stab_array(arg[1].arg_ptr.arg_stab),arglast);
668 str = Str_new(51,0); /* must copy the STR */
670 (void)apush(stab_array(arg[1].arg_ptr.arg_stab),str);
674 str = apop(ary = stab_array(arg[1].arg_ptr.arg_stab));
675 goto staticalization;
677 str = ashift(ary = stab_array(arg[1].arg_ptr.arg_stab));
681 if (ary->ary_flags & ARF_REAL)
682 (void)str_2static(str);
685 sp = do_unpack(str,gimme,arglast);
688 value = str_gnum(st[3]);
689 sp = do_split(str, arg[2].arg_ptr.arg_spat, (int)value,
694 value = (double)str_len(stab_val(defstab));
696 value = (double)str_len(st[1]);
699 do_sprintf(str, sp-arglast[0], st+1);
702 anum = ((int)str_gnum(st[2])) - arybase; /* anum=where to start*/
703 tmps = str_get(st[1]); /* force conversion to string */
704 if (argtype = (str == st[1]))
705 str = arg->arg_ptr.arg_str;
707 anum += st[1]->str_cur + arybase;
708 if (anum < 0 || anum > st[1]->str_cur)
711 optype = (int)str_gnum(st[3]);
715 anum = st[1]->str_cur - anum; /* anum=how many bytes left*/
718 str_nset(str, tmps, anum);
719 if (argtype) { /* it's an lvalue! */
720 lstr = (struct lstring*)str;
721 str->str_magic = st[1];
722 st[1]->str_rare = 's';
723 lstr->lstr_offset = tmps - str_get(st[1]);
724 lstr->lstr_len = anum;
729 (void)do_pack(str,arglast);
732 sp = do_grep(arg,str,gimme,arglast);
735 do_join(str,arglast);
738 tmps = str_get(st[1]);
739 value = (double) (str_cmp(st[1],st[2]) < 0);
742 tmps = str_get(st[1]);
743 value = (double) (str_cmp(st[1],st[2]) > 0);
746 tmps = str_get(st[1]);
747 value = (double) (str_cmp(st[1],st[2]) <= 0);
750 tmps = str_get(st[1]);
751 value = (double) (str_cmp(st[1],st[2]) >= 0);
754 tmps = str_get(st[1]);
755 value = (double) str_eq(st[1],st[2]);
758 tmps = str_get(st[1]);
759 value = (double) !str_eq(st[1],st[2]);
762 sp = do_subr(arg,gimme,arglast);
763 st = stack->ary_array + arglast[0]; /* maybe realloced */
766 sp = do_dbsubr(arg,gimme,arglast);
767 st = stack->ary_array + arglast[0]; /* maybe realloced */
770 if ((arg[1].arg_type & A_MASK) == A_WORD)
771 stab = arg[1].arg_ptr.arg_stab;
773 stab = stabent(str_get(st[1]),TRUE);
776 sp = do_sort(str,stab,
784 if (arglast[2] - arglast[1] != 1) {
785 do_join(str,arglast);
786 tmps = str_get(st[1]);
790 tmps = str_get(st[2]);
793 tmps = "Warning: something's wrong";
797 if (arglast[2] - arglast[1] != 1) {
798 do_join(str,arglast);
799 tmps = str_get(st[1]);
803 tmps = str_get(st[2]);
811 if ((arg[1].arg_type & A_MASK) == A_WORD)
812 stab = arg[1].arg_ptr.arg_stab;
814 stab = stabent(str_get(st[1]),TRUE);
817 if (!stab_io(stab)) {
819 warn("Filehandle never opened");
822 if (!(fp = stab_io(stab)->ofp)) {
824 if (stab_io(stab)->ifp)
825 warn("Filehandle opened only for input");
827 warn("Print on closed filehandle");
832 if (optype == O_PRTF || arglast[2] - arglast[1] != 1)
833 value = (double)do_aprint(arg,fp,arglast);
835 value = (double)do_print(st[2],fp);
836 if (orslen && optype == O_PRINT)
837 if (fwrite(ors, 1, orslen, fp) == 0)
840 if (stab_io(stab)->flags & IOF_FLUSH)
841 if (fflush(fp) == EOF)
849 tmps = str_get(st[1]);
850 if (!tmps || !*tmps) {
851 tmpstr = hfetch(stab_hash(envstab),"HOME",4,FALSE);
853 tmps = str_get(tmpstr);
855 if (!tmps || !*tmps) {
856 tmpstr = hfetch(stab_hash(envstab),"LOGDIR",6,FALSE);
858 tmps = str_get(tmpstr);
861 taintproper("Insecure dependency in chdir");
863 value = (double)(chdir(tmps) >= 0);
869 anum = (int)str_gnum(st[1]);
876 tmps = str_get(st[1]);
877 str_reset(tmps,arg[2].arg_ptr.arg_hash);
881 if (gimme == G_ARRAY)
884 str = st[sp - arglast[0]]; /* unwanted list, return last item */
891 else if ((arg[1].arg_type & A_MASK) == A_WORD)
892 stab = arg[1].arg_ptr.arg_stab;
894 stab = stabent(str_get(st[1]),TRUE);
895 str_set(str, do_eof(stab) ? Yes : No);
901 else if ((arg[1].arg_type & A_MASK) == A_WORD)
902 stab = arg[1].arg_ptr.arg_stab;
904 stab = stabent(str_get(st[1]),TRUE);
905 if (do_eof(stab)) /* make sure we have fp with something */
912 *str->str_ptr = getc(stab_io(stab)->ifp); /* should never be EOF */
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);
924 value = (double)do_tell(stab);
931 if ((arg[1].arg_type & A_MASK) == A_WORD)
932 stab = arg[1].arg_ptr.arg_stab;
934 stab = stabent(str_get(st[1]),TRUE);
935 tmps = str_get(st[2]);
936 anum = (int)str_gnum(st[3]);
937 STR_GROW(st[2], anum+1), (tmps = str_get(st[2])); /* sneaky */
939 if (!stab_io(stab) || !stab_io(stab)->ifp)
942 else if (optype == O_RECV) {
943 argtype = sizeof buf;
944 optype = (int)str_gnum(st[4]);
945 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, optype,
948 st[2]->str_cur = anum;
949 st[2]->str_ptr[anum] = '\0';
950 str_nset(str,buf,argtype);
953 str_sset(str,&str_undef);
956 else if (stab_io(stab)->type == 's') {
957 argtype = sizeof buf;
958 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, 0,
962 else if (optype == O_RECV)
966 anum = fread(tmps, 1, anum, stab_io(stab)->ifp);
969 st[2]->str_cur = anum;
970 st[2]->str_ptr[anum] = '\0';
971 value = (double)anum;
975 if ((arg[1].arg_type & A_MASK) == A_WORD)
976 stab = arg[1].arg_ptr.arg_stab;
978 stab = stabent(str_get(st[1]),TRUE);
979 tmps = str_get(st[2]);
980 anum = (int)str_gnum(st[3]);
981 optype = sp - arglast[0];
984 warn("Too many args on send");
985 stio = stab_io(stab);
986 if (!stio || !stio->ifp) {
989 warn("Send on closed socket");
991 else if (optype >= 4) {
992 tmps2 = str_get(st[4]);
993 anum = sendto(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur,
994 anum, tmps2, st[4]->str_cur);
997 anum = send(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur, anum);
1000 value = (double)anum;
1006 if ((arg[1].arg_type & A_MASK) == A_WORD)
1007 stab = arg[1].arg_ptr.arg_stab;
1009 stab = stabent(str_get(st[1]),TRUE);
1010 value = str_gnum(st[2]);
1011 str_set(str, do_seek(stab,
1012 (long)value, (int)str_gnum(st[3]) ) ? Yes : No);
1016 tmps = "_SUB_"; /* just fake up a "last _SUB_" */
1018 if (wantarray == G_ARRAY) {
1019 lastretstr = Nullstr;
1020 lastspbase = arglast[1];
1021 lastsize = arglast[2] - arglast[1];
1024 lastretstr = str_static(st[arglast[2] - arglast[0]]);
1030 tmps = str_get(arg[1].arg_ptr.arg_str);
1032 while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
1033 strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
1036 deb("(Skipping label #%d %s)\n",loop_ptr,
1037 loop_stack[loop_ptr].loop_label);
1044 deb("(Found label #%d %s)\n",loop_ptr,
1045 loop_stack[loop_ptr].loop_label);
1050 fatal("Bad label: %s", maxarg > 0 ? tmps : "<null>");
1051 if (!lastretstr && optype == O_LAST && lastsize) {
1053 st += lastspbase + 1;
1054 optype = loop_stack[loop_ptr].loop_sp - lastspbase; /* negative */
1056 for (anum = lastsize; anum > 0; anum--,st++)
1057 st[optype] = str_static(st[0]);
1059 longjmp(loop_stack[loop_ptr].loop_env, O_LAST);
1061 longjmp(loop_stack[loop_ptr].loop_env, optype);
1063 case O_GOTO:/* shudder */
1064 goto_targ = str_get(arg[1].arg_ptr.arg_str);
1066 goto_targ = Nullch; /* just restart from top */
1067 if (optype == O_DUMP) {
1071 longjmp(top_env, 1);
1073 tmps = str_get(st[1]);
1075 if (!(tmps2 = fbminstr((unsigned char*)tmps,
1076 (unsigned char*)tmps + st[1]->str_cur, st[2])))
1078 if (tmps2 = fbminstr(Null(unsigned char*),Null(unsigned char*),Nullstr))
1080 value = (double)(-1 + arybase);
1082 value = (double)(tmps2 - tmps + arybase);
1085 tmps = str_get(st[1]);
1086 tmps2 = str_get(st[2]);
1088 if (!(tmps2 = rninstr(tmps, tmps + st[1]->str_cur,
1089 tmps2, tmps2 + st[2]->str_cur)))
1091 if (tmps2 = rninstr(Nullch,Nullch,Nullch,Nullch))
1093 value = (double)(-1 + arybase);
1095 value = (double)(tmps2 - tmps + arybase);
1099 value = (double) time(Null(long*));
1103 sp = do_tms(str,gimme,arglast);
1109 when = (long)str_gnum(st[1]);
1110 sp = do_time(str,localtime(&when),
1117 when = (long)str_gnum(st[1]);
1118 sp = do_time(str,gmtime(&when),
1123 sp = do_stat(str,arg,
1128 tmps = str_get(st[1]);
1130 str_set(str,fcrypt(tmps,str_get(st[2])));
1132 str_set(str,crypt(tmps,str_get(st[2])));
1136 "The crypt() function is unimplemented due to excessive paranoia.");
1140 value = str_gnum(st[1]);
1141 value = atan2(value,str_gnum(st[2]));
1145 value = str_gnum(stab_val(defstab));
1147 value = str_gnum(st[1]);
1152 value = str_gnum(stab_val(defstab));
1154 value = str_gnum(st[1]);
1161 value = str_gnum(st[1]);
1165 value = rand() * value / 2147483648.0;
1168 value = rand() * value / 65536.0;
1171 value = rand() * value / 32768.0;
1173 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1184 anum = (int)str_gnum(st[1]);
1189 value = str_gnum(stab_val(defstab));
1191 value = str_gnum(st[1]);
1196 value = str_gnum(stab_val(defstab));
1198 value = str_gnum(st[1]);
1203 value = str_gnum(stab_val(defstab));
1205 value = str_gnum(st[1]);
1206 value = sqrt(value);
1210 value = str_gnum(stab_val(defstab));
1212 value = str_gnum(st[1]);
1214 (void)modf(value,&value);
1216 (void)modf(-value,&value);
1222 tmps = str_get(stab_val(defstab));
1224 tmps = str_get(st[1]);
1226 value = (double) (*tmps & 255);
1229 value = (double) (anum & 255);
1236 tmps = str_get(st[1]);
1238 if (!tmps || !*tmps)
1239 sleep((32767<<16)+32767);
1241 sleep((unsigned int)atoi(tmps));
1243 value = (double)when;
1245 value = ((double)when) - value;
1249 sp = do_range(gimme,arglast);
1252 if (gimme == G_ARRAY) { /* it's a range */
1253 /* can we optimize to constant array? */
1254 if ((arg[1].arg_type & A_MASK) == A_SINGLE &&
1255 (arg[2].arg_type & A_MASK) == A_SINGLE) {
1256 st[2] = arg[2].arg_ptr.arg_str;
1257 sp = do_range(gimme,arglast);
1258 st = stack->ary_array;
1259 maxarg = sp - arglast[0];
1260 str_free(arg[1].arg_ptr.arg_str);
1261 str_free(arg[2].arg_ptr.arg_str);
1262 arg->arg_type = O_ARRAY;
1263 arg[1].arg_type = A_STAB|A_DONT;
1265 stab = arg[1].arg_ptr.arg_stab = aadd(genstab());
1266 ary = stab_array(stab);
1267 afill(ary,maxarg - 1);
1269 while (maxarg-- > 0)
1270 ary->ary_array[maxarg] = str_smake(st[maxarg]);
1273 arg->arg_type = optype = O_RANGE;
1274 maxarg = arg->arg_len = 2;
1276 arg[anum].arg_flags &= ~AF_ARYOK;
1277 argflags = arg[anum].arg_flags;
1278 argtype = arg[anum].arg_type & A_MASK;
1279 arg[anum].arg_type = argtype;
1280 argptr = arg[anum].arg_ptr;
1286 arg->arg_type = O_FLIP;
1289 if ((arg[1].arg_type & A_MASK) == A_SINGLE ?
1290 last_in_stab && (int)str_gnum(st[1]) == stab_io(last_in_stab)->lines
1293 str_numset(str,0.0);
1295 arg->arg_type = optype = O_FLOP;
1296 arg[2].arg_type &= ~A_DONT;
1297 arg[1].arg_type |= A_DONT;
1298 argflags = arg[2].arg_flags;
1299 argtype = arg[2].arg_type & A_MASK;
1300 argptr = arg[2].arg_ptr;
1309 if ((arg[2].arg_type & A_MASK) == A_SINGLE ?
1310 last_in_stab && (int)str_gnum(st[2]) == stab_io(last_in_stab)->lines
1313 arg->arg_type = O_FLIP;
1314 arg[1].arg_type &= ~A_DONT;
1315 arg[2].arg_type |= A_DONT;
1321 if (!anum && (tmpstab = stabent("$",allstabs)))
1322 str_numset(STAB_STR(tmpstab),(double)getpid());
1323 value = (double)anum;
1327 /* ihand = signal(SIGINT, SIG_IGN); */
1328 /* qhand = signal(SIGQUIT, SIG_IGN); */
1329 anum = wait(&argflags);
1331 pidgone(anum,argflags);
1332 value = (double)anum;
1334 /* ihand = qhand = 0; */
1336 /* (void)signal(SIGINT, ihand); */
1337 /* (void)signal(SIGQUIT, qhand); */
1338 statusvalue = (unsigned short)argflags;
1342 if (arglast[2] - arglast[1] == 1) {
1344 tainted |= st[2]->str_tainted;
1345 taintproper("Insecure dependency in system");
1348 while ((anum = vfork()) == -1) {
1349 if (errno != EAGAIN) {
1357 ihand = signal(SIGINT, SIG_IGN);
1358 qhand = signal(SIGQUIT, SIG_IGN);
1359 while ((argtype = wait(&argflags)) != anum && argtype >= 0)
1360 pidgone(argtype,argflags);
1364 (void)signal(SIGINT, ihand);
1365 (void)signal(SIGQUIT, qhand);
1366 statusvalue = (unsigned short)argflags;
1370 value = (double)((unsigned int)argflags & 0xffff);
1374 if ((arg[1].arg_type & A_MASK) == A_STAB)
1375 value = (double)do_aexec(st[1],arglast);
1376 else if (arglast[2] - arglast[1] != 1)
1377 value = (double)do_aexec(Nullstr,arglast);
1379 value = (double)do_exec(str_get(str_static(st[2])));
1383 if ((arg[1].arg_type & A_MASK) == A_STAB)
1384 value = (double)do_aexec(st[1],arglast);
1385 else if (arglast[2] - arglast[1] != 1)
1386 value = (double)do_aexec(Nullstr,arglast);
1388 value = (double)do_exec(str_get(str_static(st[2])));
1401 tmps = str_get(stab_val(defstab));
1403 tmps = str_get(st[1]);
1412 case '0': case '1': case '2': case '3': case '4':
1413 case '5': case '6': case '7':
1415 anum += *tmps++ & 15;
1417 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1418 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1422 anum += (*tmps++ & 7) + 9;
1431 value = (double)anum;
1438 value = (double)apply(optype,arglast);
1446 anum = umask((int)str_gnum(st[1]));
1447 value = (double)anum;
1449 taintproper("Insecure dependency in umask");
1453 tmps = str_get(st[1]);
1454 tmps2 = str_get(st[2]);
1456 taintproper("Insecure dependency in rename");
1459 value = (double)(rename(tmps,tmps2) >= 0);
1461 if (euid || stat(tmps2,&statbuf) < 0 ||
1462 (statbuf.st_mode & S_IFMT) != S_IFDIR )
1463 (void)UNLINK(tmps2); /* avoid unlinking a directory */
1464 if (!(anum = link(tmps,tmps2)))
1465 anum = UNLINK(tmps);
1466 value = (double)(anum >= 0);
1470 tmps = str_get(st[1]);
1471 tmps2 = str_get(st[2]);
1473 taintproper("Insecure dependency in link");
1475 value = (double)(link(tmps,tmps2) >= 0);
1478 tmps = str_get(st[1]);
1479 anum = (int)str_gnum(st[2]);
1481 taintproper("Insecure dependency in mkdir");
1484 value = (double)(mkdir(tmps,anum) >= 0);
1487 (void)strcpy(buf,"mkdir ");
1489 #if !defined(MKDIR) || !defined(RMDIR)
1491 for (tmps2 = buf+6; *tmps; ) {
1495 (void)strcpy(tmps2," 2>&1");
1496 rsfp = mypopen(buf,"r");
1499 tmps2 = fgets(buf,sizeof buf,rsfp);
1500 (void)mypclose(rsfp);
1501 if (tmps2 != Nullch) {
1502 for (errno = 1; errno < sys_nerr; errno++) {
1503 if (instr(buf,sys_errlist[errno])) /* you don't see this */
1508 #define EACCES EPERM
1510 if (instr(buf,"cannot make"))
1512 else if (instr(buf,"non-exist"))
1514 else if (instr(buf,"does not exist"))
1516 else if (instr(buf,"not empty"))
1518 else if (instr(buf,"cannot access"))
1524 else { /* some mkdirs return no failure indication */
1525 tmps = str_get(st[1]);
1526 anum = (stat(tmps,&statbuf) >= 0);
1527 if (optype == O_RMDIR)
1532 errno = EACCES; /* a guess */
1533 value = (double)anum;
1542 tmps = str_get(stab_val(defstab));
1544 tmps = str_get(st[1]);
1546 taintproper("Insecure dependency in rmdir");
1549 value = (double)(rmdir(tmps) >= 0);
1552 (void)strcpy(buf,"rmdir ");
1553 goto one_liner; /* see above in MKDIR */
1556 value = (double)getppid();
1563 anum = (int)str_gnum(st[1]);
1564 value = (double)getpgrp(anum);
1567 fatal("The getpgrp() function is unimplemented on this machine");
1572 argtype = (int)str_gnum(st[1]);
1573 anum = (int)str_gnum(st[2]);
1575 taintproper("Insecure dependency in setpgrp");
1577 value = (double)(setpgrp(argtype,anum) >= 0);
1580 fatal("The setpgrp() function is unimplemented on this machine");
1585 argtype = (int)str_gnum(st[1]);
1586 anum = (int)str_gnum(st[2]);
1587 value = (double)getpriority(argtype,anum);
1590 fatal("The getpriority() function is unimplemented on this machine");
1595 argtype = (int)str_gnum(st[1]);
1596 anum = (int)str_gnum(st[2]);
1597 optype = (int)str_gnum(st[3]);
1599 taintproper("Insecure dependency in setpriority");
1601 value = (double)(setpriority(argtype,anum,optype) >= 0);
1604 fatal("The setpriority() function is unimplemented on this machine");
1609 tmps = str_get(stab_val(defstab));
1611 tmps = str_get(st[1]);
1613 taintproper("Insecure dependency in chroot");
1615 value = (double)(chroot(tmps) >= 0);
1620 stab = last_in_stab;
1621 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1622 stab = arg[1].arg_ptr.arg_stab;
1624 stab = stabent(str_get(st[1]),TRUE);
1625 argtype = (unsigned int)str_gnum(st[2]);
1627 taintproper("Insecure dependency in ioctl");
1629 anum = do_ctl(optype,stab,argtype,st[3]);
1634 str_set(str,"0 but true");
1640 stab = last_in_stab;
1641 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1642 stab = arg[1].arg_ptr.arg_stab;
1644 stab = stabent(str_get(st[1]),TRUE);
1645 if (stab && stab_io(stab))
1646 fp = stab_io(stab)->ifp;
1650 argtype = (int)str_gnum(st[2]);
1651 value = (double)(flock(fileno(fp),argtype) >= 0);
1657 fatal("The flock() function is unimplemented on this machine");
1661 ary = stab_array(arg[1].arg_ptr.arg_stab);
1662 if (arglast[2] - arglast[1] != 1)
1663 do_unshift(ary,arglast);
1665 str = Str_new(52,0); /* must copy the STR */
1666 str_sset(str,st[2]);
1668 (void)astore(ary,0,str);
1670 value = (double)(ary->ary_fill + 1);
1675 tmpstr = stab_val(defstab);
1678 (arg[1].arg_type & A_MASK) != A_NULL ? st[1] : stab_val(defstab);
1680 tainted |= tmpstr->str_tainted;
1681 taintproper("Insecure dependency in eval");
1683 sp = do_eval(tmpstr, optype, arg[2].arg_ptr.arg_hash,
1711 if (mystat(arg,st[1]) < 0)
1713 if (cando(anum,argtype,&statcache))
1718 if (mystat(arg,st[1]) < 0)
1723 if (mystat(arg,st[1]) < 0)
1725 if (statcache.st_uid == (optype == O_FTEOWNED ? euid : uid) )
1729 if (mystat(arg,st[1]) < 0)
1731 if (!statcache.st_size)
1735 if (mystat(arg,st[1]) < 0)
1737 if (statcache.st_size)
1744 goto check_file_type;
1750 goto check_file_type;
1753 goto check_file_type;
1756 goto check_file_type;
1760 if (mystat(arg,st[1]) < 0)
1762 if ((statcache.st_mode & S_IFMT) == anum )
1768 goto check_file_type;
1773 if (arg[1].arg_type & A_DONT)
1774 fatal("You must supply explicit filename with -l");
1776 if (lstat(str_get(st[1]),&statcache) < 0)
1778 if ((statcache.st_mode & S_IFMT) == S_IFLNK )
1784 tmps = str_get(st[1]);
1785 tmps2 = str_get(st[2]);
1787 taintproper("Insecure dependency in symlink");
1789 value = (double)(symlink(tmps,tmps2) >= 0);
1792 fatal("Unsupported function symlink()");
1797 tmps = str_get(stab_val(defstab));
1799 tmps = str_get(st[1]);
1800 anum = readlink(tmps,buf,sizeof buf);
1803 str_nset(str,buf,anum);
1806 fatal("Unsupported function readlink()");
1817 if (mystat(arg,st[1]) < 0)
1819 if (statcache.st_mode & anum)
1823 if (arg[1].arg_type & A_DONT) {
1824 stab = arg[1].arg_ptr.arg_stab;
1828 stab = stabent(tmps = str_get(st[1]),FALSE);
1829 if (stab && stab_io(stab) && stab_io(stab)->ifp)
1830 anum = fileno(stab_io(stab)->ifp);
1831 else if (isdigit(*tmps))
1840 str = do_fttext(arg,st[1]);
1844 if ((arg[1].arg_type & A_MASK) == A_WORD)
1845 stab = arg[1].arg_ptr.arg_stab;
1847 stab = stabent(str_get(st[1]),TRUE);
1849 value = (double)do_socket(stab,arglast);
1851 (void)do_socket(stab,arglast);
1855 if ((arg[1].arg_type & A_MASK) == A_WORD)
1856 stab = arg[1].arg_ptr.arg_stab;
1858 stab = stabent(str_get(st[1]),TRUE);
1860 value = (double)do_bind(stab,arglast);
1862 (void)do_bind(stab,arglast);
1866 if ((arg[1].arg_type & A_MASK) == A_WORD)
1867 stab = arg[1].arg_ptr.arg_stab;
1869 stab = stabent(str_get(st[1]),TRUE);
1871 value = (double)do_connect(stab,arglast);
1873 (void)do_connect(stab,arglast);
1877 if ((arg[1].arg_type & A_MASK) == A_WORD)
1878 stab = arg[1].arg_ptr.arg_stab;
1880 stab = stabent(str_get(st[1]),TRUE);
1882 value = (double)do_listen(stab,arglast);
1884 (void)do_listen(stab,arglast);
1888 if ((arg[1].arg_type & A_MASK) == A_WORD)
1889 stab = arg[1].arg_ptr.arg_stab;
1891 stab = stabent(str_get(st[1]),TRUE);
1892 if ((arg[2].arg_type & A_MASK) == A_WORD)
1893 stab2 = arg[2].arg_ptr.arg_stab;
1895 stab2 = stabent(str_get(st[2]),TRUE);
1896 do_accept(str,stab,stab2);
1904 sp = do_ghent(optype,
1912 sp = do_gnent(optype,
1920 sp = do_gpent(optype,
1928 sp = do_gsent(optype,
1932 value = (double) sethostent((int)str_gnum(st[1]));
1935 value = (double) setnetent((int)str_gnum(st[1]));
1938 value = (double) setprotoent((int)str_gnum(st[1]));
1941 value = (double) setservent((int)str_gnum(st[1]));
1944 value = (double) endhostent();
1947 value = (double) endnetent();
1950 value = (double) endprotoent();
1953 value = (double) endservent();
1956 sp = do_select(gimme,arglast);
1959 if ((arg[1].arg_type & A_MASK) == A_WORD)
1960 stab = arg[1].arg_ptr.arg_stab;
1962 stab = stabent(str_get(st[1]),TRUE);
1963 if ((arg[2].arg_type & A_MASK) == A_WORD)
1964 stab2 = arg[2].arg_ptr.arg_stab;
1966 stab2 = stabent(str_get(st[2]),TRUE);
1968 value = (double)do_spair(stab,stab2,arglast);
1970 (void)do_spair(stab,stab2,arglast);
1974 if ((arg[1].arg_type & A_MASK) == A_WORD)
1975 stab = arg[1].arg_ptr.arg_stab;
1977 stab = stabent(str_get(st[1]),TRUE);
1979 value = (double)do_shutdown(stab,arglast);
1981 (void)do_shutdown(stab,arglast);
1986 if ((arg[1].arg_type & A_MASK) == A_WORD)
1987 stab = arg[1].arg_ptr.arg_stab;
1989 stab = stabent(str_get(st[1]),TRUE);
1990 sp = do_sopt(optype,stab,arglast);
1994 if ((arg[1].arg_type & A_MASK) == A_WORD)
1995 stab = arg[1].arg_ptr.arg_stab;
1997 stab = stabent(str_get(st[1]),TRUE);
1998 sp = do_getsockname(optype,stab,arglast);
2001 #else /* SOCKET not defined */
2035 fatal("Unsupported socket function");
2040 if ((arg[1].arg_type & A_MASK) == A_WORD)
2041 stab = arg[1].arg_ptr.arg_stab;
2043 stab = stabent(str_get(st[1]),TRUE);
2044 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2049 sp = do_vec(str == st[1], arg->arg_ptr.arg_str, arglast);
2054 sp = do_gpwent(optype,
2058 value = (double) setpwent();
2061 value = (double) endpwent();
2066 sp = do_ggrent(optype,
2070 value = (double) setgrent();
2073 value = (double) endgrent();
2076 if (!(tmps = getlogin()))
2088 if ((arg[1].arg_type & A_MASK) == A_WORD)
2089 stab = arg[1].arg_ptr.arg_stab;
2091 stab = stabent(str_get(st[1]),TRUE);
2092 sp = do_dirop(optype,stab,gimme,arglast);
2095 value = (double)do_syscall(arglast);
2098 if ((arg[1].arg_type & A_MASK) == A_WORD)
2099 stab = arg[1].arg_ptr.arg_stab;
2101 stab = stabent(str_get(st[1]),TRUE);
2102 if ((arg[2].arg_type & A_MASK) == A_WORD)
2103 stab2 = arg[2].arg_ptr.arg_stab;
2105 stab2 = stabent(str_get(st[2]),TRUE);
2106 do_pipe(str,stab,stab2);
2117 deb("%s RETURNS \"%s\"\n",opname[optype],str_get(str));
2120 return arglast[0] + 1;
2127 anum = sp - arglast[0];
2130 deb("%s RETURNS ()\n",opname[optype]);
2133 deb("%s RETURNS (\"%s\")\n",opname[optype],str_get(st[1]));
2136 deb("%s RETURNS %d ARGS (\"%s\",%s\"%s\"\n",opname[optype],anum,
2137 str_get(st[1]),anum==2?"":"...,",str_get(st[anum]));
2162 str_numset(str,value);
2169 deb("%s RETURNS \"%f\"\n",opname[optype],value);
2172 return arglast[0] + 1;