-#ifdef L_xmalloc
-// 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;
-}
-#endif
-
-#ifdef L_xrealloc
-// 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
-#endif /* DMALLOC */
-
-
-#ifdef L_xzalloc
-// 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;
-}
-#endif
-
-#ifdef L_xstrdup
-// 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;
-}
-#endif
-
-#ifdef L_xstrndup
-// 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)
-{
- char *t;
-
- if (ENABLE_DEBUG && s == NULL)
- bb_error_msg_and_die("xstrndup bug");
-
- t = xmalloc(++n);
-
- return safe_strncpy(t,s,n);
-}
-#endif
-
-#ifdef L_xfopen
-// 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;
- if ((fp = fopen(path, mode)) == NULL)
- bb_perror_msg_and_die("%s", path);
- return fp;
-}
-#endif
-
-#ifdef L_xopen
-// Die if we can't open an existing file and return an fd.
-int xopen(const char *pathname, int flags)
-{
- if (ENABLE_DEBUG && (flags & O_CREAT))
- bb_error_msg_and_die("xopen() with O_CREAT");
-
- return xopen3(pathname, flags, 0777);
-}
-#endif
-
-#ifdef L_xopen3
-// Die if we can't open a new file and return an 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("%s", pathname);
- }
- return ret;
-}
-#endif
-
-#ifdef L_xread
-// Die with an error message if we can't read the entire buffer.
-void xread(int fd, void *buf, size_t count)
-{
- while (count) {
- ssize_t size;
-
- if ((size = safe_read(fd, buf, count)) < 1)
- bb_error_msg_and_die("Short read");
- count -= size;
- buf = ((char *) buf) + size;
- }
-}
-#endif
-
-#ifdef L_xwrite
-// Die with an error message if we can't write the entire buffer.
-void xwrite(int fd, void *buf, size_t count)
-{
- while (count) {
- ssize_t size;
-
- if ((size = safe_write(fd, buf, count)) < 1)
- bb_error_msg_and_die("Short write");
- count -= size;
- buf = ((char *) buf) + size;
- }
-}
-#endif
-
-#ifdef L_xlseek
-// Die with an error message if we can't lseek to the right spot.
-void xlseek(int fd, off_t offset, int whence)
-{
- if (offset != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek");
-}
-#endif
-
-#ifdef L_xread_char
-// Die with an error message if we can't read one character.
-unsigned char xread_char(int fd)
-{
- char tmp;