aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-09-14 19:52:07 +0000
committerRob Landley <rob@landley.net>2006-09-14 19:52:07 +0000
commitda9d1d079efe0b024cdedfd85bd4ffadfbe57abf (patch)
tree044a56fbd057d36cac408d8f15b834560445eb2f
parent7ccf5cc0644ab4399b570781a42f3aeff7b98a07 (diff)
downloadbusybox-w32-da9d1d079efe0b024cdedfd85bd4ffadfbe57abf.tar.gz
busybox-w32-da9d1d079efe0b024cdedfd85bd4ffadfbe57abf.tar.bz2
busybox-w32-da9d1d079efe0b024cdedfd85bd4ffadfbe57abf.zip
Strangely, using // in the comments I added was not a persistent accident.
-rw-r--r--libbb/xfuncs.c127
1 files changed, 62 insertions, 65 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 0f0faaf74..3baf7b251 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -21,7 +21,7 @@
21 * included after these prototypes in libbb.h, all is well. 21 * included after these prototypes in libbb.h, all is well.
22 */ 22 */
23#ifdef L_xmalloc 23#ifdef L_xmalloc
24/* Die if we can't allocate size bytes of memory. */ 24// Die if we can't allocate size bytes of memory.
25void *xmalloc(size_t size) 25void *xmalloc(size_t size)
26{ 26{
27 void *ptr = malloc(size); 27 void *ptr = malloc(size);
@@ -32,9 +32,9 @@ void *xmalloc(size_t size)
32#endif 32#endif
33 33
34#ifdef L_xrealloc 34#ifdef L_xrealloc
35/* Die if we can't resize previously allocated memory. (This returns a pointer 35// Die if we can't resize previously allocated memory. (This returns a pointer
36 * to the new memory, which may or may not be the same as the old memory. 36// to the new memory, which may or may not be the same as the old memory.
37 * It'll copy the contents to a new chunk and free the old one if necessary.) */ 37// It'll copy the contents to a new chunk and free the old one if necessary.)
38void *xrealloc(void *ptr, size_t size) 38void *xrealloc(void *ptr, size_t size)
39{ 39{
40 ptr = realloc(ptr, size); 40 ptr = realloc(ptr, size);
@@ -47,7 +47,7 @@ void *xrealloc(void *ptr, size_t size)
47 47
48 48
49#ifdef L_xzalloc 49#ifdef L_xzalloc
50/* Die if we can't allocate and zero size bytes of memory. */ 50// Die if we can't allocate and zero size bytes of memory.
51void *xzalloc(size_t size) 51void *xzalloc(size_t size)
52{ 52{
53 void *ptr = xmalloc(size); 53 void *ptr = xmalloc(size);
@@ -57,7 +57,7 @@ void *xzalloc(size_t size)
57#endif 57#endif
58 58
59#ifdef L_xstrdup 59#ifdef L_xstrdup
60/* Die if we can't copy a string to freshly allocated memory. */ 60// Die if we can't copy a string to freshly allocated memory.
61char * xstrdup(const char *s) 61char * xstrdup(const char *s)
62{ 62{
63 char *t; 63 char *t;
@@ -75,9 +75,8 @@ char * xstrdup(const char *s)
75#endif 75#endif
76 76
77#ifdef L_xstrndup 77#ifdef L_xstrndup
78/* Die if we can't allocate n+1 bytes (space for the null terminator) and copy 78// Die if we can't allocate n+1 bytes (space for the null terminator) and copy
79 * the (possibly truncated to length n) string into it. 79// the (possibly truncated to length n) string into it.
80 */
81char * xstrndup(const char *s, int n) 80char * xstrndup(const char *s, int n)
82{ 81{
83 char *t; 82 char *t;
@@ -92,9 +91,8 @@ char * xstrndup(const char *s, int n)
92#endif 91#endif
93 92
94#ifdef L_xfopen 93#ifdef L_xfopen
95/* Die if we can't open a file and return a FILE * to it. 94// Die if we can't open a file and return a FILE * to it.
96 * Notice we haven't got xfread(), This is for use with fscanf() and friends. 95// Notice we haven't got xfread(), This is for use with fscanf() and friends.
97 */
98FILE *xfopen(const char *path, const char *mode) 96FILE *xfopen(const char *path, const char *mode)
99{ 97{
100 FILE *fp; 98 FILE *fp;
@@ -105,7 +103,7 @@ FILE *xfopen(const char *path, const char *mode)
105#endif 103#endif
106 104
107#ifdef L_xopen 105#ifdef L_xopen
108/* Die if we can't open an existing file and return an fd. */ 106// Die if we can't open an existing file and return an fd.
109int xopen(const char *pathname, int flags) 107int xopen(const char *pathname, int flags)
110{ 108{
111 if (ENABLE_DEBUG && (flags & O_CREAT)) 109 if (ENABLE_DEBUG && (flags & O_CREAT))
@@ -116,7 +114,7 @@ int xopen(const char *pathname, int flags)
116#endif 114#endif
117 115
118#ifdef L_xopen3 116#ifdef L_xopen3
119/* Die if we can't open a new file and return an fd. */ 117// Die if we can't open a new file and return an fd.
120int xopen3(const char *pathname, int flags, int mode) 118int xopen3(const char *pathname, int flags, int mode)
121{ 119{
122 int ret; 120 int ret;
@@ -130,7 +128,7 @@ int xopen3(const char *pathname, int flags, int mode)
130#endif 128#endif
131 129
132#ifdef L_xread 130#ifdef L_xread
133/* Die with an error message if we can't read the entire buffer. */ 131// Die with an error message if we can't read the entire buffer.
134void xread(int fd, void *buf, size_t count) 132void xread(int fd, void *buf, size_t count)
135{ 133{
136 while (count) { 134 while (count) {
@@ -145,7 +143,7 @@ void xread(int fd, void *buf, size_t count)
145#endif 143#endif
146 144
147#ifdef L_xwrite 145#ifdef L_xwrite
148/* Die with an error message if we can't write the entire buffer. */ 146// Die with an error message if we can't write the entire buffer.
149void xwrite(int fd, void *buf, size_t count) 147void xwrite(int fd, void *buf, size_t count)
150{ 148{
151 while (count) { 149 while (count) {
@@ -160,7 +158,7 @@ void xwrite(int fd, void *buf, size_t count)
160#endif 158#endif
161 159
162#ifdef L_xlseek 160#ifdef L_xlseek
163/* Die with an error message if we can't lseek to the right spot. */ 161// Die with an error message if we can't lseek to the right spot.
164void xlseek(int fd, off_t offset, int whence) 162void xlseek(int fd, off_t offset, int whence)
165{ 163{
166 if (offset != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek"); 164 if (offset != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek");
@@ -168,7 +166,7 @@ void xlseek(int fd, off_t offset, int whence)
168#endif 166#endif
169 167
170#ifdef L_xread_char 168#ifdef L_xread_char
171/* Die with an error message if we can't read one character. */ 169// Die with an error message if we can't read one character.
172unsigned char xread_char(int fd) 170unsigned char xread_char(int fd)
173{ 171{
174 char tmp; 172 char tmp;
@@ -180,7 +178,7 @@ unsigned char xread_char(int fd)
180#endif 178#endif
181 179
182#ifdef L_xferror 180#ifdef L_xferror
183/* Die with supplied error message if this FILE * has ferror set. */ 181// Die with supplied error message if this FILE * has ferror set.
184void xferror(FILE *fp, const char *fn) 182void xferror(FILE *fp, const char *fn)
185{ 183{
186 if (ferror(fp)) { 184 if (ferror(fp)) {
@@ -190,7 +188,7 @@ void xferror(FILE *fp, const char *fn)
190#endif 188#endif
191 189
192#ifdef L_xferror_stdout 190#ifdef L_xferror_stdout
193/* Die with an error message if stdout has ferror set. */ 191// Die with an error message if stdout has ferror set.
194void xferror_stdout(void) 192void xferror_stdout(void)
195{ 193{
196 xferror(stdout, bb_msg_standard_output); 194 xferror(stdout, bb_msg_standard_output);
@@ -198,7 +196,7 @@ void xferror_stdout(void)
198#endif 196#endif
199 197
200#ifdef L_xfflush_stdout 198#ifdef L_xfflush_stdout
201/* Die with an error message if we have trouble flushing stdout. */ 199// Die with an error message if we have trouble flushing stdout.
202void xfflush_stdout(void) 200void xfflush_stdout(void)
203{ 201{
204 if (fflush(stdout)) { 202 if (fflush(stdout)) {
@@ -208,25 +206,24 @@ void xfflush_stdout(void)
208#endif 206#endif
209 207
210#ifdef L_spawn 208#ifdef L_spawn
211/* This does a fork/exec in one call, using vfork(). Return PID of new child, 209// This does a fork/exec in one call, using vfork(). Return PID of new child,
212 * -1 for failure. Runs argv[0], searching path if that has no / in it. 210// -1 for failure. Runs argv[0], searching path if that has no / in it.
213 */
214pid_t spawn(char **argv) 211pid_t spawn(char **argv)
215{ 212{
216 static int failed; 213 static int failed;
217 pid_t pid; 214 pid_t pid;
218 void *app = ENABLE_FEATURE_SH_STANDALONE_SHELL ? find_applet_by_name(argv[0]) : 0; 215 void *app = ENABLE_FEATURE_SH_STANDALONE_SHELL ? find_applet_by_name(argv[0]) : 0;
219 216
220 /* Be nice to nommu machines. */ 217 // Be nice to nommu machines.
221 failed = 0; 218 failed = 0;
222 pid = vfork(); 219 pid = vfork();
223 if (pid < 0) return pid; 220 if (pid < 0) return pid;
224 if (!pid) { 221 if (!pid) {
225 execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv); 222 execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv);
226 223
227 /* We're sharing a stack with blocked parent, let parent know we failed 224 // We're sharing a stack with blocked parent, let parent know we failed
228 * and then exit to unblock parent (but don't run atexit() stuff, which 225 // and then exit to unblock parent (but don't run atexit() stuff, which
229 would screw up parent.) */ 226 // would screw up parent.)
230 227
231 failed = -1; 228 failed = -1;
232 _exit(0); 229 _exit(0);
@@ -236,7 +233,7 @@ pid_t spawn(char **argv)
236#endif 233#endif
237 234
238#ifdef L_xspawn 235#ifdef L_xspawn
239/* Die with an error message if we can't spawn a child process. */ 236// Die with an error message if we can't spawn a child process.
240pid_t xspawn(char **argv) 237pid_t xspawn(char **argv)
241{ 238{
242 pid_t pid = spawn(argv); 239 pid_t pid = spawn(argv);
@@ -246,7 +243,7 @@ pid_t xspawn(char **argv)
246#endif 243#endif
247 244
248#ifdef L_wait4 245#ifdef L_wait4
249/* Wait for the specified child PID to exit, returning child's error return. */ 246// Wait for the specified child PID to exit, returning child's error return.
250int wait4pid(int pid) 247int wait4pid(int pid)
251{ 248{
252 int status; 249 int status;
@@ -259,9 +256,9 @@ int wait4pid(int pid)
259#endif 256#endif
260 257
261#ifdef L_itoa 258#ifdef L_itoa
262/* Convert unsigned integer to ascii, writing into supplied buffer. A 259// Convert unsigned integer to ascii, writing into supplied buffer. A
263 * truncated result is always null terminated (unless buflen is 0), and 260// truncated result is always null terminated (unless buflen is 0), and
264 * contains the first few digits of the result ala strncpy. */ 261// contains the first few digits of the result ala strncpy.
265void utoa_to_buf(unsigned n, char *buf, unsigned buflen) 262void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
266{ 263{
267 int i, out = 0; 264 int i, out = 0;
@@ -279,7 +276,7 @@ void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
279 } 276 }
280} 277}
281 278
282/* Convert signed integer to ascii, like utoa_to_buf() */ 279// Convert signed integer to ascii, like utoa_to_buf()
283void itoa_to_buf(int n, char *buf, unsigned buflen) 280void itoa_to_buf(int n, char *buf, unsigned buflen)
284{ 281{
285 if (buflen && n<0) { 282 if (buflen && n<0) {
@@ -290,16 +287,16 @@ void itoa_to_buf(int n, char *buf, unsigned buflen)
290 utoa_to_buf((unsigned)n, buf, buflen); 287 utoa_to_buf((unsigned)n, buf, buflen);
291} 288}
292 289
293/* The following two functions use a static buffer, so calling either one a 290// The following two functions use a static buffer, so calling either one a
294 * second time will overwrite previous results. 291// second time will overwrite previous results.
295 * 292//
296 * The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes. 293// The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes.
297 * Int should always be 32 bits on any remotely Unix-like system, see 294// Int should always be 32 bits on any remotely Unix-like system, see
298 * http://www.unix.org/whitepapers/64bit.html for the reasons why. 295// http://www.unix.org/whitepapers/64bit.html for the reasons why.
299*/ 296
300static char local_buf[12]; 297static char local_buf[12];
301 298
302/* Convert unsigned integer to ascii using a static buffer (returned). */ 299// Convert unsigned integer to ascii using a static buffer (returned).
303char *utoa(unsigned n) 300char *utoa(unsigned n)
304{ 301{
305 utoa_to_buf(n, local_buf, sizeof(local_buf)); 302 utoa_to_buf(n, local_buf, sizeof(local_buf));
@@ -307,7 +304,7 @@ char *utoa(unsigned n)
307 return local_buf; 304 return local_buf;
308} 305}
309 306
310/* Convert signed integer to ascii using a static buffer (returned). */ 307// Convert signed integer to ascii using a static buffer (returned).
311char *itoa(int n) 308char *itoa(int n)
312{ 309{
313 itoa_to_buf(n, local_buf, sizeof(local_buf)); 310 itoa_to_buf(n, local_buf, sizeof(local_buf));
@@ -317,15 +314,15 @@ char *itoa(int n)
317#endif 314#endif
318 315
319#ifdef L_setuid 316#ifdef L_setuid
320/* Die with an error message if we can't set gid. (Because resource limits may 317// Die with an error message if we can't set gid. (Because resource limits may
321 * limit this user to a given number of processes, and if that fills up the 318// limit this user to a given number of processes, and if that fills up the
322 * setgid() will fail and we'll _still_be_root_, which is bad.) */ 319// setgid() will fail and we'll _still_be_root_, which is bad.)
323void xsetgid(gid_t gid) 320void xsetgid(gid_t gid)
324{ 321{
325 if (setgid(gid)) bb_error_msg_and_die("setgid"); 322 if (setgid(gid)) bb_error_msg_and_die("setgid");
326} 323}
327 324
328/* Die with an error message if we cant' set uid. (See xsetgid() for why.) */ 325// Die with an error message if we cant' set uid. (See xsetgid() for why.)
329void xsetuid(uid_t uid) 326void xsetuid(uid_t uid)
330{ 327{
331 if (setuid(uid)) bb_error_msg_and_die("setuid"); 328 if (setuid(uid)) bb_error_msg_and_die("setuid");
@@ -333,31 +330,31 @@ void xsetuid(uid_t uid)
333#endif 330#endif
334 331
335#ifdef L_fdlength 332#ifdef L_fdlength
336/* Return how long the file at fd is, if there's any way to determine it. */ 333// Return how long the file at fd is, if there's any way to determine it.
337off_t fdlength(int fd) 334off_t fdlength(int fd)
338{ 335{
339 off_t bottom = 0, top = 0, pos; 336 off_t bottom = 0, top = 0, pos;
340 long size; 337 long size;
341 338
342 /* If the ioctl works for this, return it. */ 339 // If the ioctl works for this, return it.
343 340
344 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; 341 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
345 342
346 /* If not, do a binary search for the last location we can read. (Some 343 // If not, do a binary search for the last location we can read. (Some
347 * block devices don't do BLKGETSIZE right.) */ 344 // block devices don't do BLKGETSIZE right.)
348 345
349 do { 346 do {
350 char temp; 347 char temp;
351 348
352 pos = bottom + (top - bottom) / 2; 349 pos = bottom + (top - bottom) / 2;
353 350
354 /* If we can read from the current location, it's bigger. */ 351 // If we can read from the current location, it's bigger.
355 352
356 if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) { 353 if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) {
357 if (bottom == top) bottom = top = (top+1) * 2; 354 if (bottom == top) bottom = top = (top+1) * 2;
358 else bottom = pos; 355 else bottom = pos;
359 356
360 /* If we can't, it's smaller. */ 357 // If we can't, it's smaller.
361 358
362 } else { 359 } else {
363 if (bottom == top) { 360 if (bottom == top) {
@@ -373,8 +370,8 @@ off_t fdlength(int fd)
373#endif 370#endif
374 371
375#ifdef L_xasprintf 372#ifdef L_xasprintf
376/* Die with an error message if we can't malloc() enough space and do an 373// Die with an error message if we can't malloc() enough space and do an
377 * sprintf() into that space. */ 374// sprintf() into that space.
378char *xasprintf(const char *format, ...) 375char *xasprintf(const char *format, ...)
379{ 376{
380 va_list p; 377 va_list p;
@@ -403,8 +400,8 @@ char *xasprintf(const char *format, ...)
403#endif 400#endif
404 401
405#ifdef L_xprint_and_close_file 402#ifdef L_xprint_and_close_file
406/* Die with an error message if we can't copy an entire FILE * to stdout, then 403// Die with an error message if we can't copy an entire FILE * to stdout, then
407 * close that file. */ 404// close that file.
408void xprint_and_close_file(FILE *file) 405void xprint_and_close_file(FILE *file)
409{ 406{
410 // copyfd outputs error messages for us. 407 // copyfd outputs error messages for us.
@@ -416,7 +413,7 @@ void xprint_and_close_file(FILE *file)
416#endif 413#endif
417 414
418#ifdef L_xchdir 415#ifdef L_xchdir
419/* Die if we can't chdir to a new path. */ 416// Die if we can't chdir to a new path.
420void xchdir(const char *path) 417void xchdir(const char *path)
421{ 418{
422 if (chdir(path)) 419 if (chdir(path))
@@ -425,7 +422,7 @@ void xchdir(const char *path)
425#endif 422#endif
426 423
427#ifdef L_warn_opendir 424#ifdef L_warn_opendir
428/* Print a warning message if opendir() fails, but don't die. */ 425// Print a warning message if opendir() fails, but don't die.
429DIR *warn_opendir(const char *path) 426DIR *warn_opendir(const char *path)
430{ 427{
431 DIR *dp; 428 DIR *dp;
@@ -439,7 +436,7 @@ DIR *warn_opendir(const char *path)
439#endif 436#endif
440 437
441#ifdef L_xopendir 438#ifdef L_xopendir
442/* Die with an error message if opendir() fails. */ 439// Die with an error message if opendir() fails.
443DIR *xopendir(const char *path) 440DIR *xopendir(const char *path)
444{ 441{
445 DIR *dp; 442 DIR *dp;
@@ -452,7 +449,7 @@ DIR *xopendir(const char *path)
452 449
453#ifdef L_xdaemon 450#ifdef L_xdaemon
454#ifndef BB_NOMMU 451#ifndef BB_NOMMU
455/* Die with an error message if we can't daemonize. */ 452// Die with an error message if we can't daemonize.
456void xdaemon(int nochdir, int noclose) 453void xdaemon(int nochdir, int noclose)
457{ 454{
458 if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon"); 455 if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon");
@@ -461,7 +458,7 @@ void xdaemon(int nochdir, int noclose)
461#endif 458#endif
462 459
463#ifdef L_xsocket 460#ifdef L_xsocket
464/* Die with an error message if we can't open a new socket. */ 461// Die with an error message if we can't open a new socket.
465int xsocket(int domain, int type, int protocol) 462int xsocket(int domain, int type, int protocol)
466{ 463{
467 int r = socket(domain, type, protocol); 464 int r = socket(domain, type, protocol);
@@ -473,7 +470,7 @@ int xsocket(int domain, int type, int protocol)
473#endif 470#endif
474 471
475#ifdef L_xbind 472#ifdef L_xbind
476/* Die with an error message if we can't bind a socket to an address. */ 473// Die with an error message if we can't bind a socket to an address.
477void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) 474void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
478{ 475{
479 if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind"); 476 if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
@@ -481,7 +478,7 @@ void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
481#endif 478#endif
482 479
483#ifdef L_xlisten 480#ifdef L_xlisten
484/* Die with an error message if we can't listen for connections on a socket. */ 481// Die with an error message if we can't listen for connections on a socket.
485void xlisten(int s, int backlog) 482void xlisten(int s, int backlog)
486{ 483{
487 if (listen(s, backlog)) bb_perror_msg_and_die("listen"); 484 if (listen(s, backlog)) bb_perror_msg_and_die("listen");
@@ -489,7 +486,7 @@ void xlisten(int s, int backlog)
489#endif 486#endif
490 487
491#ifdef L_xstat 488#ifdef L_xstat
492/* xstat() - a stat() which dies on failure with meaningful error message */ 489// xstat() - a stat() which dies on failure with meaningful error message
493void xstat(char *name, struct stat *stat_buf) 490void xstat(char *name, struct stat *stat_buf)
494{ 491{
495 if (stat(name, stat_buf)) 492 if (stat(name, stat_buf))