1 /* $Header: str.c,v 3.0.1.11 90/11/13 15:27:14 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.11 90/11/13 15:27:14 lwall
10 * patch41: fixed a couple of malloc/free problems
12 * Revision 3.0.1.10 90/11/10 02:06:29 lwall
13 * patch38: temp string values are now copied less often
14 * patch38: array slurps are now faster and take less memory
15 * patch38: fixed a memory leakage on local(*foo)
17 * Revision 3.0.1.9 90/10/16 10:41:21 lwall
18 * patch29: the undefined value could get defined by devious means
19 * patch29: undefined values compared inconsistently
20 * patch29: taintperl now checks for world writable PATH components
22 * Revision 3.0.1.8 90/08/09 05:22:18 lwall
23 * patch19: the number to string converter wasn't allocating enough space
24 * patch19: tainting didn't work on setgid scripts
26 * Revision 3.0.1.7 90/03/27 16:24:11 lwall
27 * patch16: strings with prefix chopped off sometimes freed wrong
28 * patch16: taint check blows up on undefined array element
30 * Revision 3.0.1.6 90/03/12 17:02:14 lwall
31 * patch13: substr as lvalue didn't invalidate old numeric value
33 * Revision 3.0.1.5 90/02/28 18:30:38 lwall
34 * patch9: you may now undef $/ to have no input record separator
35 * patch9: nested evals clobbered their longjmp environment
36 * patch9: sometimes perl thought ordinary data was a symbol table entry
37 * patch9: insufficient space allocated for numeric string on sun4
38 * patch9: underscore in an array name in a double-quoted string not recognized
39 * patch9: "@foo{}" not recognized unless %foo defined
40 * patch9: "$foo[$[]" gives error
42 * Revision 3.0.1.4 89/12/21 20:21:35 lwall
43 * patch7: errno may now be a macro with an lvalue
44 * patch7: made nested or recursive foreach work right
46 * Revision 3.0.1.3 89/11/17 15:38:23 lwall
47 * patch5: some machines typedef unchar too
48 * patch5: substitution on leading components occasionally caused <> corruption
50 * Revision 3.0.1.2 89/11/11 04:56:22 lwall
51 * patch2: uchar gives Crays fits
53 * Revision 3.0.1.1 89/10/26 23:23:41 lwall
54 * patch1: string ordering tests were wrong
55 * patch1: $/ now works even when STDSTDIO undefined
57 * Revision 3.0 89/10/18 15:23:38 lwall
66 extern char **environ;
74 tainted |= str->str_tainted;
76 return str->str_pok ? str->str_ptr : str_2ptr(str);
80 /* dlb ... guess we have a "crippled cc".
81 * dlb the following functions are usually macros.
88 if (*Str->str_ptr > '0' ||
90 (Str->str_cur && *Str->str_ptr != '0'))
95 return (Str->str_u.str_nval != 0.0);
105 tainted |= Str->str_tainted;
108 return Str->str_u.str_nval;
109 return str_2num(Str);
111 #endif /* str_gnum */
112 /* dlb ... end of crutch */
120 unsigned long newlen;
123 register char *s = str->str_ptr;
126 if (newlen >= 0x10000) {
127 fprintf(stderr, "Allocation too large: %lx\n", newlen);
131 if (str->str_state == SS_INCR) { /* data before str_ptr? */
132 str->str_len += str->str_u.str_useful;
133 str->str_ptr -= str->str_u.str_useful;
134 str->str_u.str_useful = 0L;
135 bcopy(s, str->str_ptr, str->str_cur+1);
137 str->str_state = SS_NORM; /* normal again */
138 if (newlen > str->str_len)
139 newlen += 10 * (newlen - str->str_cur); /* avoid copy each time */
141 if (newlen > str->str_len) { /* need more room? */
143 Renew(s,newlen,char);
145 New(703,s,newlen,char);
147 str->str_len = newlen;
157 str->str_pok = 0; /* invalidate pointer */
158 if (str->str_state == SS_INCR)
161 str->str_u.str_nval = num;
162 str->str_state = SS_NORM;
163 str->str_nok = 1; /* validate number */
165 str->str_tainted = tainted;
181 olderrno = errno; /* some Xenix systems wipe out errno here */
182 #if defined(scs) && defined(ns32000)
183 gcvt(str->str_u.str_nval,20,s);
186 if (str->str_u.str_nval == 0.0)
190 (void)sprintf(s,"%.20g",str->str_u.str_nval);
200 if (str == &str_undef)
203 warn("Use of uninitialized variable");
208 str->str_cur = s - str->str_ptr;
212 fprintf(stderr,"0x%lx ptr(%s)\n",str,str->str_ptr);
223 if (str->str_state == SS_INCR)
224 Str_Grow(str,0); /* just force copy down */
225 str->str_state = SS_NORM;
226 if (str->str_len && str->str_pok)
227 str->str_u.str_nval = atof(str->str_ptr);
229 if (str == &str_undef)
232 warn("Use of uninitialized variable");
233 str->str_u.str_nval = 0.0;
238 fprintf(stderr,"0x%lx num(%g)\n",str,str->str_u.str_nval);
240 return str->str_u.str_nval;
243 /* Note: str_sset() should not be called with a source string that needs
244 * be reused, since it may destroy the source string if it is marked
254 tainted |= sstr->str_tainted;
256 if (sstr == dstr || dstr == &str_undef)
259 dstr->str_pok = dstr->str_nok = 0;
260 else if (sstr->str_pok) {
263 * Check to see if we can just swipe the string. If so, it's a
264 * possible small lose on short strings, but a big win on long ones.
265 * It might even be a win on short strings if dstr->str_ptr
266 * has to be allocated and sstr->str_ptr has to be freed.
269 if (sstr->str_pok & SP_TEMP) { /* slated for free anyway? */
271 if (dstr->str_state == SS_INCR)
272 dstr->str_ptr -= dstr->str_u.str_useful;
273 Safefree(dstr->str_ptr);
275 dstr->str_ptr = sstr->str_ptr;
276 dstr->str_len = sstr->str_len;
277 dstr->str_cur = sstr->str_cur;
278 dstr->str_state = sstr->str_state;
279 dstr->str_pok = sstr->str_pok & ~SP_TEMP;
281 dstr->str_tainted = sstr->str_tainted;
283 sstr->str_ptr = Nullch;
285 sstr->str_pok = 0; /* wipe out any weird flags */
286 sstr->str_state = 0; /* so sstr frees uneventfully */
288 else /* have to copy actual string */
289 str_nset(dstr,sstr->str_ptr,sstr->str_cur);
290 if (dstr->str_nok = sstr->str_nok)
291 dstr->str_u.str_nval = sstr->str_u.str_nval;
294 dstr->str_u = sstr->str_u;
296 dstr->str_u.str_nval = sstr->str_u.str_nval;
298 if (dstr->str_cur == sizeof(STBP)) {
299 char *tmps = dstr->str_ptr;
301 if (*tmps == 'S' && bcmp(tmps,"StB",4) == 0) {
302 if (!dstr->str_magic) {
303 dstr->str_magic = str_smake(sstr->str_magic);
304 dstr->str_magic->str_rare = 'X';
310 else if (sstr->str_nok)
311 str_numset(dstr,sstr->str_u.str_nval);
313 if (dstr->str_state == SS_INCR)
314 Str_Grow(dstr,0); /* just force copy down */
317 dstr->str_u = sstr->str_u;
319 dstr->str_u.str_nval = sstr->str_u.str_nval;
321 dstr->str_pok = dstr->str_nok = 0;
325 str_nset(str,ptr,len)
330 if (str == &str_undef)
332 STR_GROW(str, len + 1);
334 (void)bcopy(ptr,str->str_ptr,len);
336 *(str->str_ptr+str->str_cur) = '\0';
337 str->str_nok = 0; /* invalidate number */
338 str->str_pok = 1; /* validate pointer */
340 str->str_tainted = tainted;
350 if (str == &str_undef)
355 STR_GROW(str, len + 1);
356 (void)bcopy(ptr,str->str_ptr,len+1);
358 str->str_nok = 0; /* invalidate number */
359 str->str_pok = 1; /* validate pointer */
361 str->str_tainted = tainted;
365 str_chop(str,ptr) /* like set but assuming ptr is in str */
369 register STRLEN delta;
372 fatal("str_chop: internal inconsistency");
373 delta = ptr - str->str_ptr;
374 str->str_len -= delta;
375 str->str_cur -= delta;
376 str->str_ptr += delta;
377 if (str->str_state == SS_INCR)
378 str->str_u.str_useful += delta;
380 str->str_u.str_useful = delta;
381 str->str_state = SS_INCR;
383 str->str_nok = 0; /* invalidate number */
384 str->str_pok = 1; /* validate pointer (and unstudy str) */
387 str_ncat(str,ptr,len)
392 if (str == &str_undef)
396 STR_GROW(str, str->str_cur + len + 1);
397 (void)bcopy(ptr,str->str_ptr+str->str_cur,len);
399 *(str->str_ptr+str->str_cur) = '\0';
400 str->str_nok = 0; /* invalidate number */
401 str->str_pok = 1; /* validate pointer */
403 str->str_tainted |= tainted;
412 tainted |= sstr->str_tainted;
416 if (!(sstr->str_pok))
417 (void)str_2ptr(sstr);
419 str_ncat(dstr,sstr->str_ptr,sstr->str_cur);
428 if (str == &str_undef)
435 STR_GROW(str, str->str_cur + len + 1);
436 (void)bcopy(ptr,str->str_ptr+str->str_cur,len+1);
438 str->str_nok = 0; /* invalidate number */
439 str->str_pok = 1; /* validate pointer */
441 str->str_tainted |= tainted;
446 str_append_till(str,from,fromend,delim,keeplist)
449 register char *fromend;
456 if (str == &str_undef)
460 len = fromend - from;
461 STR_GROW(str, str->str_cur + len + 1);
462 str->str_nok = 0; /* invalidate number */
463 str->str_pok = 1; /* validate pointer */
464 to = str->str_ptr+str->str_cur;
465 for (; from < fromend; from++,to++) {
466 if (*from == '\\' && from+1 < fromend && delim != '\\') {
468 if (from[1] == delim || from[1] == '\\')
473 else if (from[1] && index(keeplist,from[1]))
478 else if (*from == delim)
483 str->str_cur = to - str->str_ptr;
500 freestrroot = str->str_magic;
501 str->str_magic = Nullstr;
502 str->str_state = SS_NORM;
505 Newz(700+x,str,1,STR);
508 STR_GROW(str, len + 1);
513 str_magic(str, stab, how, name, namlen)
520 if (str == &str_undef || str->str_magic)
522 str->str_magic = Str_new(75,namlen);
523 str = str->str_magic;
524 str->str_u.str_stab = stab;
527 str_nset(str,name,namlen);
531 str_insert(bigstr,offset,len,little,littlelen)
540 register char *midend;
541 register char *bigend;
544 if (bigstr == &str_undef)
547 bigstr->str_pok = SP_VALID; /* disable possible screamer */
550 if (i > 0) { /* string might grow */
551 STR_GROW(bigstr, bigstr->str_cur + i + 1);
552 big = bigstr->str_ptr;
553 mid = big + offset + len;
554 midend = bigend = big + bigstr->str_cur;
557 while (midend > mid) /* shove everything down */
558 *--bigend = *--midend;
559 (void)bcopy(little,big+offset,littlelen);
560 bigstr->str_cur += i;
564 (void)bcopy(little,bigstr->str_ptr+offset,len);
568 big = bigstr->str_ptr;
571 bigend = big + bigstr->str_cur;
574 fatal("panic: str_insert");
576 if (mid - big > bigend - midend) { /* faster to shorten from end */
578 (void)bcopy(little, mid, littlelen);
583 (void)bcopy(midend, mid, i);
587 bigstr->str_cur = mid - big;
589 else if (i = mid - big) { /* faster from front */
592 str_chop(bigstr,midend-i);
597 (void)bcopy(little, mid, littlelen);
599 else if (littlelen) {
601 str_chop(bigstr,midend);
602 (void)bcopy(little,midend,littlelen);
605 str_chop(bigstr,midend);
610 /* make str point to what nstr did */
613 str_replace(str,nstr)
617 if (str == &str_undef)
619 if (str->str_state == SS_INCR)
620 Str_Grow(str,0); /* just force copy down */
621 if (nstr->str_state == SS_INCR)
624 Safefree(str->str_ptr);
625 str->str_ptr = nstr->str_ptr;
626 str->str_len = nstr->str_len;
627 str->str_cur = nstr->str_cur;
628 str->str_pok = nstr->str_pok;
629 str->str_nok = nstr->str_nok;
631 str->str_u = nstr->str_u;
633 str->str_u.str_nval = nstr->str_u.str_nval;
636 str->str_tainted = nstr->str_tainted;
639 str_free(nstr->str_magic);
647 if (!str || str == &str_undef)
649 if (str->str_state) {
650 if (str->str_state == SS_FREE) /* already freed */
652 if (str->str_state == SS_INCR && !(str->str_pok & 2)) {
653 str->str_ptr -= str->str_u.str_useful;
654 str->str_len += str->str_u.str_useful;
658 str_free(str->str_magic);
661 Safefree(str->str_ptr);
662 if ((str->str_pok & SP_INTRP) && str->str_u.str_args)
663 arg_free(str->str_u.str_args);
667 if (str->str_len > 127) { /* next user not likely to want more */
668 Safefree(str->str_ptr); /* so give it back to malloc */
669 str->str_ptr = Nullch;
673 str->str_ptr[0] = '\0';
675 if ((str->str_pok & SP_INTRP) && str->str_u.str_args)
676 arg_free(str->str_u.str_args);
680 str->str_state = SS_FREE;
682 str->str_tainted = 0;
684 str->str_magic = freestrroot;
686 #endif /* LEAKTEST */
707 if (!str1 || str1 == &str_undef)
708 return (str2 == Nullstr || str2 == &str_undef || !str2->str_cur);
709 if (!str2 || str2 == &str_undef)
710 return !str1->str_cur;
713 (void)str_2ptr(str1);
715 (void)str_2ptr(str2);
717 if (str1->str_cur != str2->str_cur)
720 return !bcmp(str1->str_ptr, str2->str_ptr, str1->str_cur);
729 if (!str1 || str1 == &str_undef)
730 return (str2 == Nullstr || str2 == &str_undef || !str2->str_cur)?0:-1;
731 if (!str2 || str2 == &str_undef)
732 return str1->str_cur != 0;
735 (void)str_2ptr(str1);
737 (void)str_2ptr(str2);
739 if (str1->str_cur < str2->str_cur) {
740 if (retval = memcmp(str1->str_ptr, str2->str_ptr, str1->str_cur))
745 else if (retval = memcmp(str1->str_ptr, str2->str_ptr, str2->str_cur))
747 else if (str1->str_cur == str2->str_cur)
754 str_gets(str,fp,append)
759 register char *bp; /* we're going to steal some values */
760 register int cnt; /* from the stdio struct and put EVERYTHING */
761 register STDCHAR *ptr; /* in the innermost loop into registers */
762 register int newline = record_separator;/* (assuming >= 6 registers) */
766 register int get_paragraph;
767 register char *oldbp;
770 if (str == &str_undef)
772 if (get_paragraph = !rslen) { /* yes, that's an assignment */
774 oldbp = Nullch; /* remember last \n position (none) */
776 #ifdef STDSTDIO /* Here is some breathtakingly efficient cheating */
777 cnt = fp->_cnt; /* get count into register */
778 str->str_nok = 0; /* invalidate number */
779 str->str_pok = 1; /* validate pointer */
780 if (str->str_len <= cnt + 1) { /* make sure we have the room */
781 if (cnt > 80 && str->str_len > 0) {
782 shortbuffered = cnt - str->str_len + 1;
783 cnt = str->str_len - 1;
787 STR_GROW(str, append+cnt+2);/* (remembering cnt can be -1) */
792 bp = str->str_ptr + append; /* move these two too to registers */
796 while (--cnt >= 0) { /* this */ /* eat */
797 if ((*bp++ = *ptr++) == newline) /* really */ /* dust */
798 goto thats_all_folks; /* screams */ /* sed :-) */
801 if (shortbuffered) { /* oh well, must extend */
804 if (get_paragraph && oldbp)
805 obpx = oldbp - str->str_ptr;
806 bpx = bp - str->str_ptr; /* prepare for possible relocation */
807 STR_GROW(str, str->str_len + append + cnt + 2);
808 bp = str->str_ptr + bpx; /* reconstitute our pointer */
809 if (get_paragraph && oldbp)
810 oldbp = str->str_ptr + obpx;
814 fp->_cnt = cnt; /* deregisterize cnt and ptr */
816 i = _filbuf(fp); /* get more characters */
818 ptr = fp->_ptr; /* reregisterize cnt and ptr */
820 bpx = bp - str->str_ptr; /* prepare for possible relocation */
821 if (get_paragraph && oldbp)
822 obpx = oldbp - str->str_ptr;
824 STR_GROW(str, bpx + cnt + 2);
825 bp = str->str_ptr + bpx; /* reconstitute our pointer */
826 if (get_paragraph && oldbp)
827 oldbp = str->str_ptr + obpx;
829 if (i == newline) { /* all done for now? */
831 goto thats_all_folks;
833 else if (i == EOF) /* all done for ever? */
834 goto thats_really_all_folks;
835 *bp++ = i; /* now go back to screaming loop */
839 if (get_paragraph && bp - 1 != oldbp) {
840 oldbp = bp; /* remember where this newline was */
841 goto screamer; /* and go back to the fray */
843 thats_really_all_folks:
845 cnt += shortbuffered;
846 fp->_cnt = cnt; /* put these back or we're in trouble */
849 str->str_cur = bp - str->str_ptr; /* set length */
851 #else /* !STDSTDIO */ /* The big, slow, and stupid way */
854 static char buf[8192];
855 char * bpe = buf + sizeof(buf) - 3;
860 while ((i = getc(fp)) != EOF && (*bp++ = i) != newline && bp < bpe);
861 if (i == newline && get_paragraph &&
862 (i = getc(fp)) != EOF && (*bp++ = i) != newline && bp < bpe)
870 if (i != newline && i != EOF) {
876 #endif /* STDSTDIO */
878 return str->str_cur - append ? str->str_ptr : Nullch;
887 CMD *oldcurcmd = curcmd;
888 int oldperldb = perldb;
892 str_sset(linestr,str);
894 oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
895 bufend = bufptr + linestr->str_cur;
896 if (++loop_ptr >= loop_max) {
898 Renew(loop_stack, loop_max, struct loop);
900 loop_stack[loop_ptr].loop_label = "_EVAL_";
901 loop_stack[loop_ptr].loop_sp = 0;
904 deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
907 if (setjmp(loop_stack[loop_ptr].loop_env)) {
911 fatal("%s\n",stab_val(stabent("@",TRUE))->str_ptr);
915 char *tmps = loop_stack[loop_ptr].loop_label;
916 deb("(Popping label #%d %s)\n",loop_ptr,
923 curcmd->c_line = oldcurcmd->c_line;
928 if (retval || error_count)
929 fatal("Invalid component in string or format");
932 if (cmd->c_type != C_EXPR || cmd->c_next || arg->arg_type != O_LIST)
933 fatal("panic: error in parselist %d %x %d", cmd->c_type,
934 cmd->c_next, arg ? arg->arg_type : -1);
943 register char *s = str_get(src);
944 register char *send = s + src->str_cur;
949 register int brackets;
954 toparse = Str_new(76,0);
958 str_nset(toparse,"",0);
961 if (*s == '\\' && s[1] && index("$@[{\\]}",s[1])) {
962 str_ncat(str, t, s - t);
964 if (*nointrp && s+1 < send)
965 if (*s != '@' && (*s != '$' || index(nointrp,s[1])))
967 str_ncat(str, "$b", 2);
972 else if ((*s == '@' || (*s == '$' && !index(nointrp,s[1]))) &&
976 if (*s == '$' && s[1] == '#' && (isalpha(s[2]) || s[2] == '_'))
978 s = scanreg(s,send,tokenbuf);
980 (!(stab = stabent(tokenbuf,FALSE)) ||
981 (*s == '{' ? !stab_xhash(stab) : !stab_xarray(stab)) )) {
984 continue; /* grandfather @ from old scripts */
986 str_ncat(str,"$a",2);
987 str_ncat(toparse,",",1);
988 if (t[1] != '{' && (*s == '[' || *s == '{' /* }} */ ) &&
989 (stab = stabent(tokenbuf,FALSE)) &&
990 ((*s == '[') ? (stab_xarray(stab) != 0) : (stab_xhash(stab) != 0)) ) {
1012 s = cpytill(tokenbuf,s+1,send,*s,&len);
1014 fatal("Unterminated string");
1019 } while (brackets > 0 && s < send);
1021 fatal("Unmatched brackets in string");
1022 if (*nointrp) { /* we're in a regular expression */
1024 if (*d == '{' && s[-1] == '}') { /* maybe {n,m} */
1026 if (isdigit(*d)) { /* matches /^{\d,?\d*}$/ */
1032 s = checkpoint; /* Is {n,m}! Backoff! */
1035 else if (*d == '[' && s[-1] == ']') { /* char class? */
1036 int weight = 2; /* let's weigh the evidence */
1038 unsigned char un_char = 0, last_un_char;
1040 Zero(seen,256,char);
1044 else if (d[1] == '$')
1046 if (isdigit(d[1])) {
1048 if (isdigit(d[2]) && !d[3])
1054 for (d++; d < s; d++) {
1055 last_un_char = un_char;
1056 un_char = (unsigned char)*d;
1060 weight -= seen[un_char] * 10;
1061 if (isalpha(d[1]) || isdigit(d[1]) ||
1063 d = scanreg(d,s,tokenbuf);
1064 if (stabent(tokenbuf,FALSE))
1069 else if (*d == '$' && d[1] &&
1070 index("[#!%*<>()-=",d[1])) {
1071 if (!d[2] || /*{*/ index("])} =",d[2]))
1080 if (index("wds",d[1]))
1082 else if (seen['\''] || seen['"'])
1084 else if (index("rnftb",d[1]))
1086 else if (isdigit(d[1])) {
1088 while (d[1] && isdigit(d[1]))
1096 if (last_un_char < (unsigned char) d[1]
1098 if (index("aA01! ",last_un_char))
1100 if (index("zZ79~",d[1]))
1106 if (isalpha(*d) && d[1] && isalpha(d[1])) {
1108 if (yylex() != WORD)
1112 if (un_char == last_un_char + 1)
1114 weight -= seen[un_char];
1121 fprintf(stderr,"[%s] weight %d\n",
1122 checkpoint+1,weight);
1125 if (weight >= 0) /* probably a character class */
1131 str_ncat(toparse, "join($\",", 8);
1132 if (t[1] == '{' && s[-1] == '}') {
1133 str_ncat(toparse, t, 1);
1134 str_ncat(toparse, t+2, s - t - 3);
1137 str_ncat(toparse, t, s - t);
1139 str_ncat(toparse, ")", 1);
1145 str_ncat(str,t,s-t);
1146 if (toparse->str_ptr && *toparse->str_ptr == ',') {
1147 *toparse->str_ptr = '(';
1148 str_ncat(toparse,",$$);",5);
1149 str->str_u.str_args = parselist(toparse);
1150 str->str_u.str_args->arg_len--; /* ignore $$ reference */
1153 str->str_u.str_args = Nullarg;
1155 str->str_pok |= SP_INTRP;
1157 str_replace(src,str);
1168 register char *send;
1169 register STR **elem;
1171 if (str == &str_undef)
1173 if (!(src->str_pok & SP_INTRP)) {
1174 int oldsave = savestack->ary_fill;
1176 (void)savehptr(&curstash);
1177 curstash = curcmd->c_stash; /* so stabent knows right package */
1179 restorelist(oldsave);
1181 s = src->str_ptr; /* assumed valid since str_pok set */
1183 send = s + src->str_cur;
1185 if (src->str_u.str_args) {
1186 (void)eval(src->str_u.str_args,G_ARRAY,sp);
1187 /* Assuming we have correct # of args */
1188 elem = stack->ary_array + sp;
1193 if (*s == '$' && s+1 < send) {
1194 str_ncat(str,t,s-t);
1197 str_scat(str,*++elem);
1200 str_ncat(str,++s,1);
1208 str_ncat(str,t,s-t);
1218 if (!str || str == &str_undef)
1221 str->str_u.str_nval += 1.0;
1225 if (!str->str_pok || !*str->str_ptr) {
1226 str->str_u.str_nval = 1.0;
1232 while (isalpha(*d)) d++;
1233 while (isdigit(*d)) d++;
1235 str_numset(str,atof(str->str_ptr) + 1.0); /* punt */
1239 while (d >= str->str_ptr) {
1249 *(d--) -= 'z' - 'a' + 1;
1252 /* oh,oh, the number grew */
1253 STR_GROW(str, str->str_cur + 2);
1255 for (d = str->str_ptr + str->str_cur; d > str->str_ptr; d--)
1267 if (!str || str == &str_undef)
1270 str->str_u.str_nval -= 1.0;
1274 if (!str->str_pok) {
1275 str->str_u.str_nval = -1.0;
1279 str_numset(str,atof(str->str_ptr) - 1.0);
1282 /* Make a string that will exist for the duration of the expression
1283 * evaluation. Actually, it may have to last longer than that, but
1284 * hopefully cmd_exec won't free it until it has been assigned to a
1285 * permanent location. */
1287 static long tmps_size = -1;
1293 register STR *str = Str_new(78,0);
1295 str_sset(str,oldstr);
1296 if (++tmps_max > tmps_size) {
1297 tmps_size = tmps_max;
1298 if (!(tmps_size & 127)) {
1300 Renew(tmps_list, tmps_size + 128, STR*);
1302 New(702,tmps_list, 128, STR*);
1305 tmps_list[tmps_max] = str;
1307 str->str_pok |= SP_TEMP;
1311 /* same thing without the copying */
1317 if (str == &str_undef)
1319 if (++tmps_max > tmps_size) {
1320 tmps_size = tmps_max;
1321 if (!(tmps_size & 127)) {
1323 Renew(tmps_list, tmps_size + 128, STR*);
1325 New(704,tmps_list, 128, STR*);
1328 tmps_list[tmps_max] = str;
1330 str->str_pok |= SP_TEMP;
1339 register STR *str = Str_new(79,0);
1343 str_nset(str,s,len);
1351 register STR *str = Str_new(80,0);
1357 /* make an exact duplicate of old */
1363 register STR *new = Str_new(81,0);
1367 if (old->str_state == SS_FREE) {
1368 warn("semi-panic: attempt to dup freed string");
1371 if (old->str_state == SS_INCR && !(old->str_pok & 2))
1374 Safefree(new->str_ptr);
1375 Copy(old,new,1,STR);
1377 new->str_ptr = nsavestr(old->str_ptr,old->str_len);
1385 register HENT *entry;
1386 register STAB *stab;
1389 register SPAT *spat;
1392 if (!*s) { /* reset ?? searches */
1393 for (spat = stash->tbl_spatroot;
1395 spat = spat->spat_next) {
1396 spat->spat_flags &= ~SPAT_USED;
1401 /* reset variables */
1403 if (!stash->tbl_array)
1411 for ( ; i <= max; i++) {
1412 for (entry = stash->tbl_array[i];
1414 entry = entry->hent_next) {
1415 stab = (STAB*)entry->hent_val;
1416 str = stab_val(stab);
1420 str->str_tainted = tainted;
1422 if (str->str_ptr != Nullch)
1423 str->str_ptr[0] = '\0';
1424 if (stab_xarray(stab)) {
1425 aclear(stab_xarray(stab));
1427 if (stab_xhash(stab)) {
1428 hclear(stab_xhash(stab), FALSE);
1429 if (stab == envstab)
1430 environ[0] = Nullch;
1443 fprintf(stderr,"%s %d %d %d\n",s,tainted,uid, euid);
1445 if (tainted && (!euid || euid != uid || egid != gid)) {
1455 register STR *envstr;
1457 envstr = hfetch(stab_hash(envstab),"PATH",4,FALSE);
1458 if (envstr == &str_undef || envstr->str_tainted) {
1460 if (envstr->str_tainted == 2)
1461 taintproper("Insecure directory in PATH");
1463 taintproper("Insecure PATH");
1465 envstr = hfetch(stab_hash(envstab),"IFS",3,FALSE);
1466 if (envstr != &str_undef && envstr->str_tainted) {
1468 taintproper("Insecure IFS");