2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley Software License Agreement
12 * specifies the terms and conditions for redistribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
18 #include "sh.tconst.h"
25 tchar
**blkcat(tchar
**, tchar
**);
26 tchar
**blkend(tchar
**);
43 return ((char *)cp
< end
);
57 n
= p
= (tchar
*)xalloc((unsigned)(p
- s
)*sizeof (tchar
));
62 p
= (tchar
*) xalloc((strlen_(s
) + 1)*sizeof (tchar
));
72 static tchar
*av
[2] = {0, 0};
77 error("Out of memory");
80 printf("i=%d: Out of memory\n", i
);
81 chdir("/usr/bill/cshcore");
84 return (0); /* fool lint */
118 blkcpy(tchar
**oav
, tchar
**bv
)
122 while (*av
++ = *bv
++)
128 blkcat(tchar
**up
, tchar
**vp
)
131 (void) blkcpy(blkend(up
), vp
);
149 (tchar
**)xcalloc((unsigned)(blklen(v
) + 1),
151 tchar
**onewv
= newv
;
154 *newv
++ = savestr(*v
++);
159 strspl(tchar
*cp
, tchar
*dp
)
169 ep
= (tchar
*) xalloc((unsigned)(((p
- cp
) +
170 (q
- dp
) - 1))*sizeof (tchar
));
171 for (p
= ep
, q
= cp
; *p
++ = *q
++; )
173 for (p
--, q
= dp
; *p
++ = *q
++; )
176 int len1
= strlen_(cp
);
177 int len2
= strlen_(dp
);
179 ep
= (tchar
*)xalloc((unsigned)(len1
+ len2
+ 1)*sizeof (tchar
));
187 blkspl(tchar
**up
, tchar
**vp
)
190 (tchar
**)xcalloc((unsigned)(blklen(up
) + blklen(vp
) + 1),
193 (void) blkcpy(wp
, up
);
194 return (blkcat(wp
, vp
));
216 * To avoid NIS+ functions to get hold of 0/1/2,
217 * use descriptor 0, and dup it to 1 and 2.
219 open("/dev/null", O_RDONLY
);
225 * Move descriptor i to j.
226 * If j is -1 then we just want to get i to a safe place,
227 * i.e. to a unit > 2. This also happens in dcopy.
255 if (i
== j
|| i
< 0 || j
< 0 && i
> 2)
265 return (renum(i
, j
));
275 if (j
== -1 && k
> 2) {
281 (void) close(k
); /* no need ofr unsetfd() */
289 copy(tchar
*to
, tchar
*from
, int size
)
300 * Left shift a command argument list, discarding
301 * the first c arguments. Used in "shift" commands
302 * as well as by commands like "repeat".
305 lshift(tchar
**v
, int c
)
309 while (*u
&& --c
>= 0)
323 while (*cp
&& digit(*cp
))
332 (tchar
**)xcalloc((unsigned)(blklen(v
) + 1),
335 return (blkcpy(nv
, v
));
352 while (*dp
++ &= TRIM
)
362 bferr("Undefined variable");
366 prefix(tchar
*sub
, tchar
*str
)
374 if (*sub
++ != *str
++)
403 blkcpy_(char **oav
, char **bv
)
407 while (*av
++ = *bv
++)
413 blkcat_(char **up
, char **vp
)
416 (void) blkcpy_(blkend_(up
), vp
);
421 blkspl_(char **up
, char **vp
)
424 (char **)xcalloc((unsigned)(blklen_(up
) + blklen_(vp
) + 1),
427 (void) blkcpy_(wp
, up
);
428 return (blkcat_(wp
, vp
));
432 * If stack address was passed to free(), we have no good way to see if
433 * they are really in the stack. Therefore, we record the bottom of heap,
434 * and filter out the address not within heap's top(end) and bottom
438 static char *xalloc_bottom
;
445 if ((rptr
= malloc(size
)) == NULL
)
446 return (nomem(size
));
448 if (bp
> xalloc_bottom
)
454 xrealloc(void *ptr
, size_t size
)
456 char *rptr
= ptr
, *bp
;
459 return (xalloc(size
));
461 /* data area, but not in heap area. don't touch it */
467 (void) memcpy(rptr
, ptr
, size
);
470 if (rptr
< xalloc_bottom
) {
471 /* address in the heap */
477 if ((rptr
= realloc(ptr
, size
)) == NULL
)
478 return (nomem(size
));
480 if (bp
> xalloc_bottom
)
485 * can be a memory block returned indirectly from
486 * library functions. update bottom, and check it again.
488 xalloc_bottom
= sbrk(0);
489 if (rptr
<= xalloc_bottom
)
504 if (rptr
< xalloc_bottom
) {
508 xalloc_bottom
= sbrk(0);
509 if (rptr
<= xalloc_bottom
) {
515 xcalloc(size_t i
, size_t j
)
521 (void) memset(cp
, '\0', i
);