bc3fd1c5f52231d40c5e571ef98d668c437fdb12
[platform/upstream/kbd.git] / src / xmalloc.c
1 /* Error-free versions of some libc routines */
2
3 #include "config.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sysexits.h>
9 #include "kbd.h"
10 #include "nls.h"
11 #include "xmalloc.h"
12
13 extern char *progname;
14
15 static void __attribute__((noreturn))
16 nomem(void)
17 {
18         fprintf(stderr, _("%s: out of memory\n"), progname);
19         exit(EX_OSERR);
20 }
21
22 void *
23 xmalloc(size_t sz)
24 {
25         void *p = malloc(sz);
26         if (p == NULL)
27                 nomem();
28         return p;
29 }
30
31 void *
32 xrealloc(void *pp, size_t sz)
33 {
34         void *p = realloc(pp, sz);
35         if (p == NULL)
36                 nomem();
37         return p;
38 }
39
40 char *
41 xstrdup(char *p)
42 {
43         char *q = strdup(p);
44         if (q == NULL)
45                 nomem();
46         return q;
47 }
48
49 char *
50 xstrndup(char *p, size_t n)
51 {
52         char *q = strndup(p, n);
53         if (q == NULL)
54                 nomem();
55         return q;
56 }
57
58 void *
59 xfree(void *p)
60 {
61         if (p != NULL)
62                 free(p);
63         return NULL;
64 }