Take UTF-8 spaces into account for empty strings
[smenu.git] / xmalloc.c
blob52850a8dca887456631b40d86bef7a4590f7d5b5
1 /* **************************** */
2 /* Memory management functions. */
3 /* **************************** */
5 /* ***************************************************************** */
6 /* Created by Kevin Locke (from numerous canonical examples). */
7 /* */
8 /* Adapted for use by smenu. */
9 /* */
10 /* I hereby place this file in the public domain. It may be freely */
11 /* reproduced, distributed, used, modified, built upon, or otherwise */
12 /* employed by anyone for any purpose without restriction. */
13 /* ***************************************************************** */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
20 #include "config.h"
21 #include "xmalloc.h"
23 /* The following rpl_* function are necessary for AIX which doesn't */
24 /* provide 'GNU compatible' allocation functions. */
25 /* Every call to malloc()/realloc() is then replaced by a call to */
26 /* rpl_malloc()/rpl_realloc() as defined in the GNU generated config.h. */
27 /* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
29 #ifdef malloc
31 #undef malloc
32 extern void *malloc(size_t);
34 void *
35 rpl_malloc(size_t size)
37 if (!size)
38 size++;
39 return malloc(size);
42 #undef realloc
43 extern void *
44 realloc(void *, size_t);
46 void *
47 rpl_realloc(void *ptr, size_t size)
49 if (!size)
50 size++;
51 return (ptr ? realloc(ptr, size) : malloc(size));
54 #endif
56 /* ================================================================== */
57 /* Customized malloc. */
58 /* Displays an error message and exits gracefully if an error occurs. */
59 /* ================================================================== */
60 void *
61 xmalloc(size_t size)
63 void *allocated;
64 size_t real_size;
66 real_size = (size > 0) ? size : 1;
67 allocated = malloc(real_size);
68 if (allocated == NULL)
70 fprintf(stderr,
71 "Error: Insufficient memory (attempt to malloc %zu bytes)\n",
72 size);
74 exit(EXIT_FAILURE);
77 return allocated;
80 /* ================================================================== */
81 /* Customized calloc. */
82 /* Displays an error message and exits gracefully if an error occurs. */
83 /* ================================================================== */
84 void *
85 xcalloc(size_t n, size_t size)
87 void *allocated;
89 n = (n > 0) ? n : 1;
90 size = (size > 0) ? size : 1;
91 allocated = calloc(n, size);
92 if (allocated == NULL)
94 fprintf(stderr,
95 "Error: Insufficient memory (attempt to calloc %zu bytes)\n",
96 size);
98 exit(EXIT_FAILURE);
101 return allocated;
104 /* ================================================================== */
105 /* Customized realloc. */
106 /* Displays an error message and exits gracefully if an error occurs. */
107 /* ================================================================== */
108 void *
109 xrealloc(void *p, size_t size)
111 void *allocated;
113 allocated = realloc(p, size);
114 if (allocated == NULL && size > 0)
116 fprintf(stderr,
117 "Error: Insufficient memory (attempt to realloc %zu bytes)\n",
118 size);
120 exit(EXIT_FAILURE);
123 return allocated;
126 /* ================================================================== */
127 /* strdup implementation using xmalloc. */
128 /* Displays an error message and exits gracefully if an error occurs. */
129 /* ================================================================== */
130 char *
131 xstrdup(const char *str)
133 char *p;
135 p = malloc(strlen(str) + 1);
137 if (p == NULL)
139 fprintf(stderr, "Error: Insufficient memory for xstrdup.\n");
140 exit(EXIT_FAILURE);
143 strcpy(p, str);
145 return p;
148 /* ================================================================== */
149 /* strndup implementation using xmalloc. */
150 /* This version guarantees that there is a final '\0'. */
151 /* Displays an error message and exits gracefully if an error occurs. */
152 /* ================================================================== */
153 char *
154 xstrndup(const char *str, size_t len)
156 char *p;
158 p = memchr(str, '\0', len);
160 if (p != NULL)
161 len = p - str;
163 p = malloc(len + 1);
165 if (p == NULL)
167 fprintf(stderr, "Error: Insufficient memory for xstrndup.\n");
168 exit(EXIT_FAILURE);
171 memcpy(p, str, len);
172 p[len] = '\0';
174 return p;