1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 #include <sys/types.h>
21 void *xmalloc(size_t size)
23 void *ptr = malloc(size);
24 if (ptr == NULL && size != 0)
25 bb_error_msg_and_die(bb_msg_memory_exhausted);
31 void *xrealloc(void *ptr, size_t size)
33 ptr = realloc(ptr, size);
34 if (ptr == NULL && size != 0)
35 bb_error_msg_and_die(bb_msg_memory_exhausted);
41 void *xcalloc(size_t nmemb, size_t size)
43 void *ptr = calloc(nmemb, size);
44 if (ptr == NULL && nmemb != 0 && size != 0)
45 bb_error_msg_and_die(bb_msg_memory_exhausted);
52 char * bb_xstrdup (const char *s)
62 bb_error_msg_and_die(bb_msg_memory_exhausted);
69 char * bb_xstrndup (const char *s, int n)
74 bb_error_msg_and_die("bb_xstrndup bug");
78 return safe_strncpy(t,s,n);
83 FILE *bb_xfopen(const char *path, const char *mode)
86 if ((fp = fopen(path, mode)) == NULL)
87 bb_perror_msg_and_die("%s", path);
93 int bb_xopen(const char *pathname, int flags)
95 return bb_xopen3(pathname, flags, 0777);
100 int bb_xopen3(const char *pathname, int flags, int mode)
104 ret = open(pathname, flags, mode);
106 bb_perror_msg_and_die("%s", pathname);
113 ssize_t bb_xread(int fd, void *buf, size_t count)
117 size = read(fd, buf, count);
119 bb_perror_msg_and_die(bb_msg_read_error);
126 void bb_xread_all(int fd, void *buf, size_t count)
131 if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */
132 bb_error_msg_and_die("Short read");
135 buf = ((char *) buf) + size;
142 unsigned char bb_xread_char(int fd)
146 bb_xread_all(fd, &tmp, 1);
153 void bb_xferror(FILE *fp, const char *fn)
156 bb_error_msg_and_die("%s", fn);
161 #ifdef L_xferror_stdout
162 void bb_xferror_stdout(void)
164 bb_xferror(stdout, bb_msg_standard_output);
168 #ifdef L_xfflush_stdout
169 void bb_xfflush_stdout(void)
171 if (fflush(stdout)) {
172 bb_perror_msg_and_die(bb_msg_standard_output);
177 // GCC forces inlining of strlen everywhere, which is generally a byte
178 // larger than calling a function, and it's called a lot so it adds up.
180 size_t bb_strlen(const char *string)
182 return(__builtin_strlen(string));
189 c-file-style: "linux"