1993-11-03 23:41:59 +00:00
|
|
|
/*
|
1994-06-15 22:41:19 +00:00
|
|
|
* $Id: etc.c,v 1.7 1994/02/13 20:41:05 jkh Exp $
|
1993-11-03 23:41:59 +00:00
|
|
|
*/
|
|
|
|
|
1994-06-15 22:41:19 +00:00
|
|
|
#include <err.h>
|
1993-11-03 23:41:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/*
|
1994-06-15 22:41:19 +00:00
|
|
|
* Like malloc but get fatal error if memory is exhausted.
|
1993-11-03 23:41:59 +00:00
|
|
|
*/
|
1994-06-15 22:41:19 +00:00
|
|
|
void *
|
|
|
|
xmalloc(size)
|
|
|
|
size_t size;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1994-06-15 22:41:19 +00:00
|
|
|
register void *result = (void *)malloc(size);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
errx(1, "virtual memory exhausted");
|
|
|
|
|
|
|
|
return result;
|
1993-11-03 23:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-06-15 22:41:19 +00:00
|
|
|
* Like realloc but get fatal error if memory is exhausted.
|
1993-11-03 23:41:59 +00:00
|
|
|
*/
|
1994-06-15 22:41:19 +00:00
|
|
|
void *
|
|
|
|
xrealloc(ptr, size)
|
|
|
|
void *ptr;
|
|
|
|
size_t size;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1994-06-15 22:41:19 +00:00
|
|
|
register void *result;
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-06-15 22:41:19 +00:00
|
|
|
if (ptr == NULL)
|
|
|
|
result = (void *)malloc(size);
|
|
|
|
else
|
|
|
|
result = (void *)realloc(ptr, size);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-06-15 22:41:19 +00:00
|
|
|
if (!result)
|
|
|
|
errx(1, "virtual memory exhausted");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
1993-11-03 23:41:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a newly-allocated string whose contents concatenate
|
|
|
|
* the strings S1, S2, S3.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
concat(s1, s2, s3)
|
1993-12-11 12:02:10 +00:00
|
|
|
const char *s1, *s2, *s3;
|
1993-11-03 23:41:59 +00:00
|
|
|
{
|
1994-06-15 22:41:19 +00:00
|
|
|
register int len1 = strlen(s1),
|
|
|
|
len2 = strlen(s2),
|
|
|
|
len3 = strlen(s3);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-06-15 22:41:19 +00:00
|
|
|
register char *result = (char *)xmalloc(len1 + len2 + len3 + 1);
|
1993-11-03 23:41:59 +00:00
|
|
|
|
1994-06-15 22:41:19 +00:00
|
|
|
strcpy(result, s1);
|
|
|
|
strcpy(result + len1, s2);
|
|
|
|
strcpy(result + len1 + len2, s3);
|
1993-11-03 23:41:59 +00:00
|
|
|
result[len1 + len2 + len3] = 0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|