Not quite compiler independent, but we've never tried to be gcc independent
[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 #include "busybox.h"
18
19 #ifndef DMALLOC
20 #ifdef L_xmalloc
21 void *xmalloc(size_t size)
22 {
23         void *ptr = malloc(size);
24         if (ptr == NULL && size != 0)
25                 bb_error_msg_and_die(bb_msg_memory_exhausted);
26         return ptr;
27 }
28 #endif
29
30 #ifdef L_xrealloc
31 void *xrealloc(void *ptr, size_t size)
32 {
33         ptr = realloc(ptr, size);
34         if (ptr == NULL && size != 0)
35                 bb_error_msg_and_die(bb_msg_memory_exhausted);
36         return ptr;
37 }
38 #endif
39
40 #ifdef L_xcalloc
41 void *xcalloc(size_t nmemb, size_t size)
42 {
43         void *ptr = calloc(nmemb, size);
44         if (ptr == NULL && nmemb != 0 && size != 0)
45                 bb_error_msg_and_die(bb_msg_memory_exhausted);
46         return ptr;
47 }
48 #endif
49 #endif /* DMALLOC */
50
51 #ifdef L_xstrdup
52 char * bb_xstrdup (const char *s)
53 {
54         char *t;
55
56         if (s == NULL)
57                 return NULL;
58
59         t = strdup (s);
60
61         if (t == NULL)
62                 bb_error_msg_and_die(bb_msg_memory_exhausted);
63
64         return t;
65 }
66 #endif
67
68 #ifdef L_xstrndup
69 char * bb_xstrndup (const char *s, int n)
70 {
71         char *t;
72
73         if (s == NULL)
74                 bb_error_msg_and_die("bb_xstrndup bug");
75
76         t = xmalloc(++n);
77
78         return safe_strncpy(t,s,n);
79 }
80 #endif
81
82 #ifdef L_xfopen
83 FILE *bb_xfopen(const char *path, const char *mode)
84 {
85         FILE *fp;
86         if ((fp = fopen(path, mode)) == NULL)
87                 bb_perror_msg_and_die("%s", path);
88         return fp;
89 }
90 #endif
91
92 #ifdef L_xopen
93 int bb_xopen(const char *pathname, int flags)
94 {
95         return bb_xopen3(pathname, flags, 0777);
96 }
97 #endif
98
99 #ifdef L_xopen3
100 int bb_xopen3(const char *pathname, int flags, int mode)
101 {
102         int ret;
103
104         ret = open(pathname, flags, mode);
105         if (ret < 0) {
106                 bb_perror_msg_and_die("%s", pathname);
107         }
108         return ret;
109 }
110 #endif
111
112 #ifdef L_xread
113 ssize_t bb_xread(int fd, void *buf, size_t count)
114 {
115         ssize_t size;
116
117         size = read(fd, buf, count);
118         if (size < 0) {
119                 bb_perror_msg_and_die(bb_msg_read_error);
120         }
121         return(size);
122 }
123 #endif
124
125 #ifdef L_xread_all
126 void bb_xread_all(int fd, void *buf, size_t count)
127 {
128         ssize_t size;
129
130         while (count) {
131                 if ((size = bb_xread(fd, buf, count)) == 0) {   /* EOF */
132                         bb_error_msg_and_die("Short read");
133                 }
134                 count -= size;
135                 buf = ((char *) buf) + size;
136         }
137         return;
138 }
139 #endif
140
141 #ifdef L_xread_char
142 unsigned char bb_xread_char(int fd)
143 {
144         char tmp;
145
146         bb_xread_all(fd, &tmp, 1);
147
148         return(tmp);
149 }
150 #endif
151
152 #ifdef L_xferror
153 void bb_xferror(FILE *fp, const char *fn)
154 {
155         if (ferror(fp)) {
156                 bb_error_msg_and_die("%s", fn);
157         }
158 }
159 #endif
160
161 #ifdef L_xferror_stdout
162 void bb_xferror_stdout(void)
163 {
164         bb_xferror(stdout, bb_msg_standard_output);
165 }
166 #endif
167
168 #ifdef L_xfflush_stdout
169 void bb_xfflush_stdout(void)
170 {
171         if (fflush(stdout)) {
172                 bb_perror_msg_and_die(bb_msg_standard_output);
173         }
174 }
175 #endif
176
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.
179 #ifdef L_strlen
180 size_t bb_strlen(const char *string)
181 {
182             return(__builtin_strlen(string));
183 }
184 #endif
185
186 /* END CODE */
187 /*
188 Local Variables:
189 c-file-style: "linux"
190 c-basic-offset: 4
191 tab-width: 4
192 End:
193 */