-// Warn if we can't allocate size bytes of memory.
-void *malloc_or_warn(size_t size)
-{
- void *ptr = malloc(size);
- if (ptr == NULL && size != 0)
- bb_error_msg(bb_msg_memory_exhausted);
- return ptr;
-}
-
-// Die if we can't allocate size bytes of memory.
-void *xmalloc(size_t size)
-{
- void *ptr = malloc(size);
- if (ptr == NULL && size != 0)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- return ptr;
-}
-
-// Die if we can't resize previously allocated memory. (This returns a pointer
-// to the new memory, which may or may not be the same as the old memory.
-// It'll copy the contents to a new chunk and free the old one if necessary.)
-void *xrealloc(void *ptr, size_t size)
-{
- ptr = realloc(ptr, size);
- if (ptr == NULL && size != 0)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- return ptr;
-}
-#endif /* DMALLOC */
-
-// Die if we can't allocate and zero size bytes of memory.
-void *xzalloc(size_t size)
-{
- void *ptr = xmalloc(size);
- memset(ptr, 0, size);
- return ptr;
-}
-
-// Die if we can't copy a string to freshly allocated memory.
-char * xstrdup(const char *s)
-{
- char *t;
-
- if (s == NULL)
- return NULL;
-
- t = strdup(s);
-
- if (t == NULL)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
-
- return t;
-}
-
-// Die if we can't allocate n+1 bytes (space for the null terminator) and copy
-// the (possibly truncated to length n) string into it.
-char * xstrndup(const char *s, int n)
-{
- int m;
- char *t;
-
- if (ENABLE_DEBUG && s == NULL)
- bb_error_msg_and_die("xstrndup bug");
-
- /* We can just xmalloc(n+1) and strncpy into it, */
- /* but think about xstrndup("abc", 10000) wastage! */
- m = n;
- t = (char*) s;
- while (m) {
- if (!*t) break;
- m--;
- t++;
- }
- n -= m;
- t = xmalloc(n + 1);
- t[n] = '\0';
-
- return memcpy(t, s, n);
-}
-
-// Die if we can't open a file and return a FILE * to it.
-// Notice we haven't got xfread(), This is for use with fscanf() and friends.
-FILE *xfopen(const char *path, const char *mode)
-{
- FILE *fp = fopen(path, mode);
- if (fp == NULL)
- bb_perror_msg_and_die("can't open '%s'", path);
- return fp;
-}
-
-// Die if we can't open a file and return a fd.
-int xopen3(const char *pathname, int flags, int mode)
-{
- int ret;
-
- ret = open(pathname, flags, mode);
- if (ret < 0) {
- bb_perror_msg_and_die("can't open '%s'", pathname);
- }
- return ret;
-}
-
-// Die if we can't open an existing file and return a fd.
-int xopen(const char *pathname, int flags)
-{
- return xopen3(pathname, flags, 0666);
-}
-
-// Warn if we can't open a file and return a fd.
-int open3_or_warn(const char *pathname, int flags, int mode)
-{
- int ret;
-
- ret = open(pathname, flags, mode);
- if (ret < 0) {
- bb_perror_msg("can't open '%s'", pathname);
- }
- return ret;
-}
-
-// Warn if we can't open a file and return a fd.
-int open_or_warn(const char *pathname, int flags)
-{
- return open3_or_warn(pathname, flags, 0666);
-}