1 /* $Id: xstrdup.c,v 1.4 2002/03/02 20:35:52 sverrehu Exp $ */
2 /*------------------------------------------------------------------------
3  |  FILE            xstrdup.c
4  |  MODULE OF       xalloc - memory allocation with error checking
5  |
6  |  DESCRIPTION     strdup-matching routine. Aborts program or calls a
7  |                  user supplied function if no more memory left.
8  |
9  |  WRITTEN BY      Sverre H. Huseby <shh@thathost.com>
10  +----------------------------------------------------------------------*/
11 
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "xalloc.h"
16 
17 extern void _xaOutOfMem(void);
18 
19 /*-----------------------------------------------------------------------+
20 |  PUBLIC FUNCTIONS                                                      |
21 +-----------------------------------------------------------------------*/
22 
23 /*------------------------------------------------------------------------
24  |  NAME          xstrdup
25  |
26  |  FUNCTION      Do as strdup(3), but with error handling.
27  |
28  |  SYNOPSIS      #include "xalloc.h"
29  |                char *xstrdup(size_t size);
30  |
31  |  INPUT         size    number of bytes to allocate.
32  |
33  |  RETURNS       Pointer to buffer allocated. Never returns in case
34  |                of error.
35  */
36 char *
xstrdup(const char * s)37 xstrdup(const char *s)
38 {
39     char *ret;
40 
41     if ((ret = strdup(s)) == NULL)
42 	_xaOutOfMem();
43     return ret;
44 }
45