4114090de4f090401f2a642e1dd6951e09a13b87
[oweals/busybox.git] / libbb / xfuncs.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17
18 /* Since gcc always inlines strlen(), this saves a byte or two, but we need
19  * the #undef here to avoid endless loop from #define strlen bb_strlen */
20 #ifdef L_strlen
21 #define BB_STRLEN_IMPLEMENTATION
22 #endif
23
24 #include "libbb.h"
25
26
27 #ifndef DMALLOC
28 #ifdef L_xmalloc
29 void *xmalloc(size_t size)
30 {
31         void *ptr = malloc(size);
32         if (ptr == NULL && size != 0)
33                 bb_error_msg_and_die(bb_msg_memory_exhausted);
34         return ptr;
35 }
36 #endif
37
38 #ifdef L_xrealloc
39 void *xrealloc(void *ptr, size_t size)
40 {
41         ptr = realloc(ptr, size);
42         if (ptr == NULL && size != 0)
43                 bb_error_msg_and_die(bb_msg_memory_exhausted);
44         return ptr;
45 }
46 #endif
47
48 #ifdef L_xcalloc
49 void *xcalloc(size_t nmemb, size_t size)
50 {
51         void *ptr = calloc(nmemb, size);
52         if (ptr == NULL && nmemb != 0 && size != 0)
53                 bb_error_msg_and_die(bb_msg_memory_exhausted);
54         return ptr;
55 }
56 #endif
57 #endif /* DMALLOC */
58
59 #ifdef L_xstrdup
60 extern char * bb_xstrdup (const char *s) {
61         char *t;
62
63         if (s == NULL)
64                 return NULL;
65
66         t = strdup (s);
67
68         if (t == NULL)
69                 bb_error_msg_and_die(bb_msg_memory_exhausted);
70
71         return t;
72 }
73 #endif
74
75 #ifdef L_xstrndup
76 extern char * bb_xstrndup (const char *s, int n) {
77         char *t;
78
79         if (s == NULL)
80                 bb_error_msg_and_die("bb_xstrndup bug");
81
82         t = xmalloc(++n);
83
84         return safe_strncpy(t,s,n);
85 }
86 #endif
87
88 #ifdef L_xfopen
89 FILE *bb_xfopen(const char *path, const char *mode)
90 {
91         FILE *fp;
92         if ((fp = fopen(path, mode)) == NULL)
93                 bb_perror_msg_and_die("%s", path);
94         return fp;
95 }
96 #endif
97
98 #ifdef L_xopen
99 int bb_xopen(const char *pathname, int flags)
100 {
101         int ret;
102
103         ret = open(pathname, flags, 0777);
104         if (ret == -1) {
105                 bb_perror_msg_and_die("%s", pathname);
106         }
107         return ret;
108 }
109 #endif
110
111 #ifdef L_xread
112 ssize_t bb_xread(int fd, void *buf, size_t count)
113 {
114         ssize_t size;
115
116         size = read(fd, buf, count);
117         if (size == -1) {
118                 bb_perror_msg_and_die(bb_msg_read_error);
119         }
120         return(size);
121 }
122 #endif
123
124 #ifdef L_xread_all
125 void bb_xread_all(int fd, void *buf, size_t count)
126 {
127         ssize_t size;
128
129         while (count) {
130                 if ((size = bb_xread(fd, buf, count)) == 0) {   /* EOF */
131                         bb_error_msg_and_die("Short read");
132                 }
133                 count -= size;
134                 buf = ((char *) buf) + size;
135         }
136         return;
137 }
138 #endif
139
140 #ifdef L_xread_char
141 unsigned char bb_xread_char(int fd)
142 {
143         char tmp;
144
145         bb_xread_all(fd, &tmp, 1);
146
147         return(tmp);
148 }
149 #endif
150
151 #ifdef L_xferror
152 void bb_xferror(FILE *fp, const char *fn)
153 {
154         if (ferror(fp)) {
155                 bb_error_msg_and_die("%s", fn);
156         }
157 }
158 #endif
159
160 #ifdef L_xferror_stdout
161 void bb_xferror_stdout(void)
162 {
163         bb_xferror(stdout, bb_msg_standard_output);
164 }
165 #endif
166
167 #ifdef L_xfflush_stdout
168 void bb_xfflush_stdout(void)
169 {
170         if (fflush(stdout)) {
171                 bb_perror_msg_and_die(bb_msg_standard_output);
172         }
173 }
174 #endif
175
176 #ifdef L_strlen
177 size_t bb_strlen(const char *string)
178 {
179             return(strlen(string));
180 }
181 #endif
182
183 /* END CODE */
184 /*
185 Local Variables:
186 c-file-style: "linux"
187 c-basic-offset: 4
188 tab-width: 4
189 End:
190 */