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");
83 printf("i=%d: Out of memory\n", i
);
84 chdir("/usr/bill/cshcore");
87 return (0); /* fool lint */
121 blkcpy(tchar
**oav
, tchar
**bv
)
125 while (*av
++ = *bv
++)
131 blkcat(tchar
**up
, tchar
**vp
)
134 (void) blkcpy(blkend(up
), vp
);
152 (tchar
**)xcalloc((unsigned)(blklen(v
) + 1),
154 tchar
**onewv
= newv
;
157 *newv
++ = savestr(*v
++);
162 strspl(tchar
*cp
, tchar
*dp
)
172 ep
= (tchar
*) xalloc((unsigned)(((p
- cp
) +
173 (q
- dp
) - 1))*sizeof (tchar
));
174 for (p
= ep
, q
= cp
; *p
++ = *q
++; )
176 for (p
--, q
= dp
; *p
++ = *q
++; )
179 int len1
= strlen_(cp
);
180 int len2
= strlen_(dp
);
182 ep
= (tchar
*)xalloc((unsigned)(len1
+ len2
+ 1)*sizeof (tchar
));
190 blkspl(tchar
**up
, tchar
**vp
)
193 (tchar
**)xcalloc((unsigned)(blklen(up
) + blklen(vp
) + 1),
196 (void) blkcpy(wp
, up
);
197 return (blkcat(wp
, vp
));
219 * To avoid NIS+ functions to get hold of 0/1/2,
220 * use descriptor 0, and dup it to 1 and 2.
222 open("/dev/null", 0);
228 * Move descriptor i to j.
229 * If j is -1 then we just want to get i to a safe place,
230 * i.e. to a unit > 2. This also happens in dcopy.
258 if (i
== j
|| i
< 0 || j
< 0 && i
> 2)
268 return (renum(i
, j
));
278 if (j
== -1 && k
> 2) {
284 (void) close(k
); /* no need ofr unsetfd() */
292 copy(tchar
*to
, tchar
*from
, int size
)
303 * Left shift a command argument list, discarding
304 * the first c arguments. Used in "shift" commands
305 * as well as by commands like "repeat".
308 lshift(tchar
**v
, int c
)
312 while (*u
&& --c
>= 0)
326 while (*cp
&& digit(*cp
))
335 (tchar
**)xcalloc((unsigned)(blklen(v
) + 1),
338 return (blkcpy(nv
, v
));
355 while (*dp
++ &= TRIM
)
365 bferr("Undefined variable");
369 prefix(tchar
*sub
, tchar
*str
)
377 if (*sub
++ != *str
++)
406 blkcpy_(char **oav
, char **bv
)
410 while (*av
++ = *bv
++)
416 blkcat_(char **up
, char **vp
)
419 (void) blkcpy_(blkend_(up
), vp
);
424 blkspl_(char **up
, char **vp
)
427 (char **)xcalloc((unsigned)(blklen_(up
) + blklen_(vp
) + 1),
430 (void) blkcpy_(wp
, up
);
431 return (blkcat_(wp
, vp
));
435 * If stack address was passed to free(), we have no good way to see if
436 * they are really in the stack. Therefore, we record the bottom of heap,
437 * and filter out the address not within heap's top(end) and bottom
441 static char *xalloc_bottom
;
448 if ((rptr
= malloc(size
)) == NULL
)
449 return (nomem(size
));
451 if (bp
> xalloc_bottom
)
457 xrealloc(void *ptr
, size_t size
)
459 char *rptr
= ptr
, *bp
;
462 return (xalloc(size
));
464 /* data area, but not in heap area. don't touch it */
470 (void) memcpy(rptr
, ptr
, size
);
473 if (rptr
< xalloc_bottom
) {
474 /* address in the heap */
480 if ((rptr
= realloc(ptr
, size
)) == NULL
)
481 return (nomem(size
));
483 if (bp
> xalloc_bottom
)
488 if (rptr
> (char *)&rptr
) {
489 /* in the stack frame */
494 * can be a memory block returned indirectly from
495 * library functions. update bottom, and check it again.
497 xalloc_bottom
= sbrk(0);
498 if (rptr
<= xalloc_bottom
)
513 if (rptr
< xalloc_bottom
) {
518 if (rptr
> (char *)&rptr
) {
519 /* in the stack frame */
523 xalloc_bottom
= sbrk(0);
524 if (rptr
<= xalloc_bottom
) {
530 xcalloc(size_t i
, size_t j
)
536 (void) memset(cp
, '\0', i
);