diff options
author | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-08-04 20:20:03 +0000 |
---|---|---|
committer | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-08-04 20:20:03 +0000 |
commit | 874366fa1c4f16d73afc592b069f6787d33e76ce (patch) | |
tree | 926640e408ce81adfa335bfda57824246f81b315 | |
parent | 23e1b0eb1031bf74bdefeadf233e49b20eafccf8 (diff) | |
download | busybox-w32-874366fa1c4f16d73afc592b069f6787d33e76ce.tar.gz busybox-w32-874366fa1c4f16d73afc592b069f6787d33e76ce.tar.bz2 busybox-w32-874366fa1c4f16d73afc592b069f6787d33e76ce.zip |
Add comments to all the functions in this file documenting what they're for.
git-svn-id: svn://busybox.net/trunk/busybox@15779 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | libbb/xfuncs.c | 109 |
1 files changed, 76 insertions, 33 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index a926beb83..cfb1c29ac 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -11,8 +11,13 @@ | |||
11 | 11 | ||
12 | #include "busybox.h" | 12 | #include "busybox.h" |
13 | 13 | ||
14 | /* All the functions starting with "x" call bb_error_msg_and_die() if they | ||
15 | * fail, so callers never need to check for errors. If it returned, it | ||
16 | * succeeded. */ | ||
17 | |||
14 | #ifndef DMALLOC | 18 | #ifndef DMALLOC |
15 | #ifdef L_xmalloc | 19 | #ifdef L_xmalloc |
20 | // Die if we can't allocate size bytes of memory. | ||
16 | void *xmalloc(size_t size) | 21 | void *xmalloc(size_t size) |
17 | { | 22 | { |
18 | void *ptr = malloc(size); | 23 | void *ptr = malloc(size); |
@@ -23,6 +28,9 @@ void *xmalloc(size_t size) | |||
23 | #endif | 28 | #endif |
24 | 29 | ||
25 | #ifdef L_xrealloc | 30 | #ifdef L_xrealloc |
31 | // Die if we can't resize previously allocated memory. (This returns a pointer | ||
32 | // to the new memory, which may or may not be the same as the old memory. | ||
33 | // It'll copy the contents to a new chunk and free the old one if necessary.) | ||
26 | void *xrealloc(void *ptr, size_t size) | 34 | void *xrealloc(void *ptr, size_t size) |
27 | { | 35 | { |
28 | ptr = realloc(ptr, size); | 36 | ptr = realloc(ptr, size); |
@@ -33,6 +41,7 @@ void *xrealloc(void *ptr, size_t size) | |||
33 | #endif | 41 | #endif |
34 | 42 | ||
35 | #ifdef L_xzalloc | 43 | #ifdef L_xzalloc |
44 | // Die if we can't allocate and zero size bytes of memory. | ||
36 | void *xzalloc(size_t size) | 45 | void *xzalloc(size_t size) |
37 | { | 46 | { |
38 | void *ptr = xmalloc(size); | 47 | void *ptr = xmalloc(size); |
@@ -44,7 +53,8 @@ void *xzalloc(size_t size) | |||
44 | #endif /* DMALLOC */ | 53 | #endif /* DMALLOC */ |
45 | 54 | ||
46 | #ifdef L_xstrdup | 55 | #ifdef L_xstrdup |
47 | char * xstrdup (const char *s) | 56 | // Die if we can't copy a string to freshly allocated memory. |
57 | char * xstrdup(const char *s) | ||
48 | { | 58 | { |
49 | char *t; | 59 | char *t; |
50 | 60 | ||
@@ -61,7 +71,9 @@ char * xstrdup (const char *s) | |||
61 | #endif | 71 | #endif |
62 | 72 | ||
63 | #ifdef L_xstrndup | 73 | #ifdef L_xstrndup |
64 | char * xstrndup (const char *s, int n) | 74 | // Die if we can't allocate n+1 bytes (space for the null terminator) and copy |
75 | // the (possibly truncated to length n) string into it. | ||
76 | char * xstrndup(const char *s, int n) | ||
65 | { | 77 | { |
66 | char *t; | 78 | char *t; |
67 | 79 | ||
@@ -75,6 +87,8 @@ char * xstrndup (const char *s, int n) | |||
75 | #endif | 87 | #endif |
76 | 88 | ||
77 | #ifdef L_xfopen | 89 | #ifdef L_xfopen |
90 | // Die if we can't open a file and return a FILE * to it. | ||
91 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. | ||
78 | FILE *xfopen(const char *path, const char *mode) | 92 | FILE *xfopen(const char *path, const char *mode) |
79 | { | 93 | { |
80 | FILE *fp; | 94 | FILE *fp; |
@@ -85,13 +99,18 @@ FILE *xfopen(const char *path, const char *mode) | |||
85 | #endif | 99 | #endif |
86 | 100 | ||
87 | #ifdef L_xopen | 101 | #ifdef L_xopen |
102 | // Die if we can't open an existing file and return an fd. | ||
88 | int xopen(const char *pathname, int flags) | 103 | int xopen(const char *pathname, int flags) |
89 | { | 104 | { |
105 | if (ENABLE_DEBUG && (flags && O_CREAT)) | ||
106 | bb_error_msg_and_die("xopen() with O_CREAT\n"); | ||
107 | |||
90 | return xopen3(pathname, flags, 0777); | 108 | return xopen3(pathname, flags, 0777); |
91 | } | 109 | } |
92 | #endif | 110 | #endif |
93 | 111 | ||
94 | #ifdef L_xopen3 | 112 | #ifdef L_xopen3 |
113 | // Die if we can't open a new file and return an fd. | ||
95 | int xopen3(const char *pathname, int flags, int mode) | 114 | int xopen3(const char *pathname, int flags, int mode) |
96 | { | 115 | { |
97 | int ret; | 116 | int ret; |
@@ -105,9 +124,7 @@ int xopen3(const char *pathname, int flags, int mode) | |||
105 | #endif | 124 | #endif |
106 | 125 | ||
107 | #ifdef L_xread | 126 | #ifdef L_xread |
108 | |||
109 | // Die with an error message if we can't read the entire buffer. | 127 | // Die with an error message if we can't read the entire buffer. |
110 | |||
111 | void xread(int fd, void *buf, size_t count) | 128 | void xread(int fd, void *buf, size_t count) |
112 | { | 129 | { |
113 | while (count) { | 130 | while (count) { |
@@ -122,9 +139,7 @@ void xread(int fd, void *buf, size_t count) | |||
122 | #endif | 139 | #endif |
123 | 140 | ||
124 | #ifdef L_xwrite | 141 | #ifdef L_xwrite |
125 | |||
126 | // Die with an error message if we can't write the entire buffer. | 142 | // Die with an error message if we can't write the entire buffer. |
127 | |||
128 | void xwrite(int fd, void *buf, size_t count) | 143 | void xwrite(int fd, void *buf, size_t count) |
129 | { | 144 | { |
130 | while (count) { | 145 | while (count) { |
@@ -139,9 +154,7 @@ void xwrite(int fd, void *buf, size_t count) | |||
139 | #endif | 154 | #endif |
140 | 155 | ||
141 | #ifdef L_xlseek | 156 | #ifdef L_xlseek |
142 | 157 | // Die with an error message if we can't lseek to the right spot. | |
143 | // Die if we can't lseek to the right spot. | ||
144 | |||
145 | void xlseek(int fd, off_t offset, int whence) | 158 | void xlseek(int fd, off_t offset, int whence) |
146 | { | 159 | { |
147 | if (offset != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek"); | 160 | if (offset != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek"); |
@@ -149,6 +162,7 @@ void xlseek(int fd, off_t offset, int whence) | |||
149 | #endif | 162 | #endif |
150 | 163 | ||
151 | #ifdef L_xread_char | 164 | #ifdef L_xread_char |
165 | // Die with an error message if we can't read one character. | ||
152 | unsigned char xread_char(int fd) | 166 | unsigned char xread_char(int fd) |
153 | { | 167 | { |
154 | char tmp; | 168 | char tmp; |
@@ -160,6 +174,7 @@ unsigned char xread_char(int fd) | |||
160 | #endif | 174 | #endif |
161 | 175 | ||
162 | #ifdef L_xferror | 176 | #ifdef L_xferror |
177 | // Die with supplied error message if this FILE * has ferror set. | ||
163 | void xferror(FILE *fp, const char *fn) | 178 | void xferror(FILE *fp, const char *fn) |
164 | { | 179 | { |
165 | if (ferror(fp)) { | 180 | if (ferror(fp)) { |
@@ -169,6 +184,7 @@ void xferror(FILE *fp, const char *fn) | |||
169 | #endif | 184 | #endif |
170 | 185 | ||
171 | #ifdef L_xferror_stdout | 186 | #ifdef L_xferror_stdout |
187 | // Die with an error message if stdout has ferror set. | ||
172 | void xferror_stdout(void) | 188 | void xferror_stdout(void) |
173 | { | 189 | { |
174 | xferror(stdout, bb_msg_standard_output); | 190 | xferror(stdout, bb_msg_standard_output); |
@@ -176,6 +192,7 @@ void xferror_stdout(void) | |||
176 | #endif | 192 | #endif |
177 | 193 | ||
178 | #ifdef L_xfflush_stdout | 194 | #ifdef L_xfflush_stdout |
195 | // Die with an error message if we have trouble flushing stdout. | ||
179 | void xfflush_stdout(void) | 196 | void xfflush_stdout(void) |
180 | { | 197 | { |
181 | if (fflush(stdout)) { | 198 | if (fflush(stdout)) { |
@@ -185,7 +202,8 @@ void xfflush_stdout(void) | |||
185 | #endif | 202 | #endif |
186 | 203 | ||
187 | #ifdef L_spawn | 204 | #ifdef L_spawn |
188 | // This does a fork/exec in one call, using vfork(). | 205 | // This does a fork/exec in one call, using vfork(). Return PID of new child, |
206 | // -1 for failure. Runs argv[0], searching path if that has no / in it. | ||
189 | pid_t spawn(char **argv) | 207 | pid_t spawn(char **argv) |
190 | { | 208 | { |
191 | static int failed; | 209 | static int failed; |
@@ -211,6 +229,7 @@ pid_t spawn(char **argv) | |||
211 | #endif | 229 | #endif |
212 | 230 | ||
213 | #ifdef L_xspawn | 231 | #ifdef L_xspawn |
232 | // Die with an error message if we can't spawn a child process. | ||
214 | pid_t xspawn(char **argv) | 233 | pid_t xspawn(char **argv) |
215 | { | 234 | { |
216 | pid_t pid = spawn(argv); | 235 | pid_t pid = spawn(argv); |
@@ -220,6 +239,7 @@ pid_t xspawn(char **argv) | |||
220 | #endif | 239 | #endif |
221 | 240 | ||
222 | #ifdef L_wait4 | 241 | #ifdef L_wait4 |
242 | // Wait for the specified child PID to exit, returning child's error return. | ||
223 | int wait4pid(int pid) | 243 | int wait4pid(int pid) |
224 | { | 244 | { |
225 | int status; | 245 | int status; |
@@ -232,11 +252,9 @@ int wait4pid(int pid) | |||
232 | #endif | 252 | #endif |
233 | 253 | ||
234 | #ifdef L_itoa | 254 | #ifdef L_itoa |
235 | // Largest 32 bit integer is -2 billion plus null terminator. | 255 | // Convert unsigned integer to ascii, writing into supplied buffer. A |
236 | // Int should always be 32 bits on a Unix-oid system, see | 256 | // truncated result is always null terminated (unless buflen is 0), and |
237 | // http://www.unix.org/whitepapers/64bit.html | 257 | // contains the first few digits of the result ala strncpy. |
238 | static char local_buf[12]; | ||
239 | |||
240 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) | 258 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
241 | { | 259 | { |
242 | int i, out = 0; | 260 | int i, out = 0; |
@@ -254,15 +272,7 @@ void utoa_to_buf(unsigned n, char *buf, unsigned buflen) | |||
254 | } | 272 | } |
255 | } | 273 | } |
256 | 274 | ||
257 | // Note: uses static buffer, calling it twice in a row will overwrite. | 275 | // Convert signed integer to ascii, like utoa_to_buf() |
258 | |||
259 | char *utoa(unsigned n) | ||
260 | { | ||
261 | utoa_to_buf(n, local_buf, sizeof(local_buf)); | ||
262 | |||
263 | return local_buf; | ||
264 | } | ||
265 | |||
266 | void itoa_to_buf(int n, char *buf, unsigned buflen) | 276 | void itoa_to_buf(int n, char *buf, unsigned buflen) |
267 | { | 277 | { |
268 | if (buflen && n<0) { | 278 | if (buflen && n<0) { |
@@ -273,8 +283,24 @@ void itoa_to_buf(int n, char *buf, unsigned buflen) | |||
273 | utoa_to_buf((unsigned)n, buf, buflen); | 283 | utoa_to_buf((unsigned)n, buf, buflen); |
274 | } | 284 | } |
275 | 285 | ||
276 | // Note: uses static buffer, calling it twice in a row will overwrite. | 286 | // The following two functions use a static buffer, so calling either one a |
287 | // second time will overwrite previous results. | ||
288 | // | ||
289 | // The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes. | ||
290 | // Int should always be 32 bits on any remotely Unix-like system, see | ||
291 | // http://www.unix.org/whitepapers/64bit.html for the reasons why. | ||
277 | 292 | ||
293 | static char local_buf[12]; | ||
294 | |||
295 | // Convert unsigned integer to ascii using a static buffer (returned). | ||
296 | char *utoa(unsigned n) | ||
297 | { | ||
298 | utoa_to_buf(n, local_buf, sizeof(local_buf)); | ||
299 | |||
300 | return local_buf; | ||
301 | } | ||
302 | |||
303 | // Convert signed integer to ascii using a static buffer (returned). | ||
278 | char *itoa(int n) | 304 | char *itoa(int n) |
279 | { | 305 | { |
280 | itoa_to_buf(n, local_buf, sizeof(local_buf)); | 306 | itoa_to_buf(n, local_buf, sizeof(local_buf)); |
@@ -284,11 +310,15 @@ char *itoa(int n) | |||
284 | #endif | 310 | #endif |
285 | 311 | ||
286 | #ifdef L_setuid | 312 | #ifdef L_setuid |
313 | // Die with an error message if we can't set gid. (Because resource limits may | ||
314 | // limit this user to a given number of processes, and if that fills up the | ||
315 | // setgid() will fail and we'll _still_be_root_, which is bad.) | ||
287 | void xsetgid(gid_t gid) | 316 | void xsetgid(gid_t gid) |
288 | { | 317 | { |
289 | if (setgid(gid)) bb_error_msg_and_die("setgid"); | 318 | if (setgid(gid)) bb_error_msg_and_die("setgid"); |
290 | } | 319 | } |
291 | 320 | ||
321 | // Die with an error message if we cant' set uid. (See xsetgid() for why.) | ||
292 | void xsetuid(uid_t uid) | 322 | void xsetuid(uid_t uid) |
293 | { | 323 | { |
294 | if (setuid(uid)) bb_error_msg_and_die("setuid"); | 324 | if (setuid(uid)) bb_error_msg_and_die("setuid"); |
@@ -296,6 +326,7 @@ void xsetuid(uid_t uid) | |||
296 | #endif | 326 | #endif |
297 | 327 | ||
298 | #ifdef L_fdlength | 328 | #ifdef L_fdlength |
329 | // Return how long the file at fd is, if there's any way to determine it. | ||
299 | off_t fdlength(int fd) | 330 | off_t fdlength(int fd) |
300 | { | 331 | { |
301 | off_t bottom = 0, top = 0, pos; | 332 | off_t bottom = 0, top = 0, pos; |
@@ -305,7 +336,8 @@ off_t fdlength(int fd) | |||
305 | 336 | ||
306 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; | 337 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; |
307 | 338 | ||
308 | // If not, do a binary search for the last location we can read. | 339 | // If not, do a binary search for the last location we can read. (Some |
340 | // block devices don't do BLKGETSIZE right.) | ||
309 | 341 | ||
310 | do { | 342 | do { |
311 | char temp; | 343 | char temp; |
@@ -334,6 +366,8 @@ off_t fdlength(int fd) | |||
334 | #endif | 366 | #endif |
335 | 367 | ||
336 | #ifdef L_xasprintf | 368 | #ifdef L_xasprintf |
369 | // Die with an error message if we can't malloc() enough space and do an | ||
370 | // sprintf() into that space. | ||
337 | char *xasprintf(const char *format, ...) | 371 | char *xasprintf(const char *format, ...) |
338 | { | 372 | { |
339 | va_list p; | 373 | va_list p; |
@@ -362,6 +396,8 @@ char *xasprintf(const char *format, ...) | |||
362 | #endif | 396 | #endif |
363 | 397 | ||
364 | #ifdef L_xprint_and_close_file | 398 | #ifdef L_xprint_and_close_file |
399 | // Die with an error message if we can't copy an entire FILE * to stdout, then | ||
400 | // close that file. | ||
365 | void xprint_and_close_file(FILE *file) | 401 | void xprint_and_close_file(FILE *file) |
366 | { | 402 | { |
367 | // copyfd outputs error messages for us. | 403 | // copyfd outputs error messages for us. |
@@ -372,6 +408,7 @@ void xprint_and_close_file(FILE *file) | |||
372 | #endif | 408 | #endif |
373 | 409 | ||
374 | #ifdef L_xchdir | 410 | #ifdef L_xchdir |
411 | // Die if we can't chdir to a new path. | ||
375 | void xchdir(const char *path) | 412 | void xchdir(const char *path) |
376 | { | 413 | { |
377 | if (chdir(path)) | 414 | if (chdir(path)) |
@@ -380,6 +417,7 @@ void xchdir(const char *path) | |||
380 | #endif | 417 | #endif |
381 | 418 | ||
382 | #ifdef L_warn_opendir | 419 | #ifdef L_warn_opendir |
420 | // Print a warning message if opendir() fails, but don't die. | ||
383 | DIR *warn_opendir(const char *path) | 421 | DIR *warn_opendir(const char *path) |
384 | { | 422 | { |
385 | DIR *dp; | 423 | DIR *dp; |
@@ -393,6 +431,7 @@ DIR *warn_opendir(const char *path) | |||
393 | #endif | 431 | #endif |
394 | 432 | ||
395 | #ifdef L_xopendir | 433 | #ifdef L_xopendir |
434 | // Die with an error message if opendir() fails. | ||
396 | DIR *xopendir(const char *path) | 435 | DIR *xopendir(const char *path) |
397 | { | 436 | { |
398 | DIR *dp; | 437 | DIR *dp; |
@@ -405,6 +444,7 @@ DIR *xopendir(const char *path) | |||
405 | 444 | ||
406 | #ifdef L_xdaemon | 445 | #ifdef L_xdaemon |
407 | #ifndef BB_NOMMU | 446 | #ifndef BB_NOMMU |
447 | // Die with an error message if we can't daemonize. | ||
408 | void xdaemon(int nochdir, int noclose) | 448 | void xdaemon(int nochdir, int noclose) |
409 | { | 449 | { |
410 | if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon"); | 450 | if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon"); |
@@ -412,14 +452,8 @@ void xdaemon(int nochdir, int noclose) | |||
412 | #endif | 452 | #endif |
413 | #endif | 453 | #endif |
414 | 454 | ||
415 | #ifdef L_xbind | ||
416 | void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) | ||
417 | { | ||
418 | if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind"); | ||
419 | } | ||
420 | #endif | ||
421 | |||
422 | #ifdef L_xsocket | 455 | #ifdef L_xsocket |
456 | // Die with an error message if we can't open a new socket. | ||
423 | int xsocket(int domain, int type, int protocol) | 457 | int xsocket(int domain, int type, int protocol) |
424 | { | 458 | { |
425 | int r = socket(domain, type, protocol); | 459 | int r = socket(domain, type, protocol); |
@@ -430,7 +464,16 @@ int xsocket(int domain, int type, int protocol) | |||
430 | } | 464 | } |
431 | #endif | 465 | #endif |
432 | 466 | ||
467 | #ifdef L_xbind | ||
468 | // Die with an error message if we can't bind a socket to an address. | ||
469 | void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) | ||
470 | { | ||
471 | if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind"); | ||
472 | } | ||
473 | #endif | ||
474 | |||
433 | #ifdef L_xlisten | 475 | #ifdef L_xlisten |
476 | // Die with an error message if we can't listen for connections on a socket. | ||
434 | void xlisten(int s, int backlog) | 477 | void xlisten(int s, int backlog) |
435 | { | 478 | { |
436 | if (listen(s, backlog)) bb_perror_msg_and_die("listen"); | 479 | if (listen(s, backlog)) bb_perror_msg_and_die("listen"); |