find ./ -name .cvsignore | xargs svn rm
[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 char * bb_xstrdup (const char *s)
61 {
62         char *t;
63
64         if (s == NULL)
65                 return NULL;
66
67         t = strdup (s);
68
69         if (t == NULL)
70                 bb_error_msg_and_die(bb_msg_memory_exhausted);
71
72         return t;
73 }
74 #endif
75
76 #ifdef L_xstrndup
77 char * bb_xstrndup (const char *s, int n)
78 {
79         char *t;
80
81         if (s == NULL)
82                 bb_error_msg_and_die("bb_xstrndup bug");
83
84         t = xmalloc(++n);
85
86         return safe_strncpy(t,s,n);
87 }
88 #endif
89
90 #ifdef L_xfopen
91 FILE *bb_xfopen(const char *path, const char *mode)
92 {
93         FILE *fp;
94         if ((fp = fopen(path, mode)) == NULL)
95                 bb_perror_msg_and_die("%s", path);
96         return fp;
97 }
98 #endif
99
100 #ifdef L_xopen
101 int bb_xopen(const char *pathname, int flags)
102 {
103         int ret;
104
105         ret = open(pathname, flags, 0777);
106         if (ret == -1) {
107                 bb_perror_msg_and_die("%s", pathname);
108         }
109         return ret;
110 }
111 #endif
112
113 #ifdef L_xread
114 ssize_t bb_xread(int fd, void *buf, size_t count)
115 {
116         ssize_t size;
117
118         size = read(fd, buf, count);
119         if (size == -1) {
120                 bb_perror_msg_and_die(bb_msg_read_error);
121         }
122         return(size);
123 }
124 #endif
125
126 #ifdef L_xread_all
127 void bb_xread_all(int fd, void *buf, size_t count)
128 {
129         ssize_t size;
130
131         while (count) {
132                 if ((size = bb_xread(fd, buf, count)) == 0) {   /* EOF */
133                         bb_error_msg_and_die("Short read");
134                 }
135                 count -= size;
136                 buf = ((char *) buf) + size;
137         }
138         return;
139 }
140 #endif
141
142 #ifdef L_xread_char
143 unsigned char bb_xread_char(int fd)
144 {
145         char tmp;
146
147         bb_xread_all(fd, &tmp, 1);
148
149         return(tmp);
150 }
151 #endif
152
153 #ifdef L_xferror
154 void bb_xferror(FILE *fp, const char *fn)
155 {
156         if (ferror(fp)) {
157                 bb_error_msg_and_die("%s", fn);
158         }
159 }
160 #endif
161
162 #ifdef L_xferror_stdout
163 void bb_xferror_stdout(void)
164 {
165         bb_xferror(stdout, bb_msg_standard_output);
166 }
167 #endif
168
169 #ifdef L_xfflush_stdout
170 void bb_xfflush_stdout(void)
171 {
172         if (fflush(stdout)) {
173                 bb_perror_msg_and_die(bb_msg_standard_output);
174         }
175 }
176 #endif
177
178 #ifdef L_strlen
179 size_t bb_strlen(const char *string)
180 {
181             return(strlen(string));
182 }
183 #endif
184
185 /* END CODE */
186 /*
187 Local Variables:
188 c-file-style: "linux"
189 c-basic-offset: 4
190 tab-width: 4
191 End:
192 */