/* $RCSfile: util.c,v $$Revision: 4.0.1.1 $$Date: 91/06/07 12:20:35 $
* Copyright (c) 1991, Larry Wall
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
* Revision 4.0.1.1 91/06/07 12:20:35 lwall
* patch4: new copyright notice
* Revision 4.0 91/03/20 01:58:25 lwall
#define MEM_SIZE unsigned int
static char nomem
[] = "Out of memory!\n";
/* paranoid version of malloc */
ptr
= malloc(size
?size
:1); /* malloc(0) is NASTY on our system */
fprintf(stderr
,"0x%x: (%05d) malloc %d bytes\n",ptr
,an
++,size
);
fputs(nomem
,stdout
) FLUSH
;
/* paranoid version of realloc */
ptr
= realloc(where
,size
?size
:1); /* realloc(0) is NASTY on our system */
fprintf(stderr
,"0x%x: (%05d) rfree\n",where
,an
++);
fprintf(stderr
,"0x%x: (%05d) realloc %d bytes\n",ptr
,an
++,size
);
fputs(nomem
,stdout
) FLUSH
;
/* safe version of free */
fprintf(stderr
,"0x%x: (%05d) free\n",where
,an
++);
/* safe version of string copy */
register char *dest
= to
;
for (len
--; len
&& (*dest
++ = *from
++); len
--) ;
/* copy a string up to some (non-backslashed) delimiter, if any */
register char *to
, *from
;
for (; *from
; from
++,to
++) {
else if (from
[1] == '\\')
register char *to
, *from
;
for (; *from
; from
++,to
++) {
/* return ptr to little string in big string, NULL if not found */
register char *t
, *s
, *x
;
for (x
=t
,s
=little
; *s
; x
++,s
++) {
/* copy a string to a safe spot */
register char *newaddr
= safemalloc((MEM_SIZE
)(strlen(str
)+1));
(void)strcpy(newaddr
,str
);
/* grow a static string to at least a certain length */
growstr(strptr
,curlen
,newlen
)
if (newlen
> *curlen
) { /* need more room? */
*strptr
= saferealloc(*strptr
,(MEM_SIZE
)newlen
);
*strptr
= safemalloc((MEM_SIZE
)newlen
);
fprintf(stderr
,pat
,a1
,a2
,a3
,a4
);
fprintf(stderr
,pat
,a1
,a2
,a3
,a4
);
static bool firstsetenv
= TRUE
;
register int i
=envix(nam
); /* where does it go? */
if (!environ
[i
]) { /* does not exist yet */
if (firstsetenv
) { /* need we copy environment? */
char **tmpenv
= (char**) /* point our wand at memory */
safemalloc((i
+2) * sizeof(char*));
char **tmpenv
= Null(char **);
for (j
=0; j
<i
; j
++) /* copy environment */
environ
= tmpenv
; /* tell exec where it is now */
environ
= (char**) saferealloc((char*) environ
,
/* just expand it a bit */
environ
[i
+1] = Nullch
; /* make sure it's null terminated */
environ
[i
] = safemalloc(strlen(nam
) + strlen(val
) + 2);
/* this may or may not be in */
/* the old environ structure */
sprintf(environ
[i
],"%s=%s",nam
,val
);/* all that work just for this */
register int i
, len
= strlen(nam
);
for (i
= 0; environ
[i
]; i
++) {
if (strnEQ(environ
[i
],nam
,len
) && environ
[i
][len
] == '=')
break; /* strnEQ must come first to avoid */