aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbb/bb_asprintf.c89
-rw-r--r--libbb/xconnect.c5
2 files changed, 91 insertions, 3 deletions
diff --git a/libbb/bb_asprintf.c b/libbb/bb_asprintf.c
new file mode 100644
index 000000000..c0c5cdeda
--- /dev/null
+++ b/libbb/bb_asprintf.c
@@ -0,0 +1,89 @@
1/*
2 Copyright (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
3*/
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <stdarg.h>
8
9
10#ifdef TEST
11extern void *xrealloc(void *p, size_t size);
12#else
13#include "libbb.h" /* busybox source */
14#endif
15
16
17/* Exchange glibc vasprintf - minimize allocate memory version */
18/* libc5 and uclibc have not vasprintf function */
19void bb_vasprintf(char **string_ptr, const char *format, va_list args)
20{
21 int bs = 128;
22 char stack_buff[128];
23 char *buff = stack_buff;
24 int done;
25
26 /* two or more loop, first - calculate memory size only */
27 while(1) {
28 done = vsnprintf (buff, bs, format, args);
29/* Different libc have different interpretation vsnprintf returned value */
30 if(done >= 0) {
31 if(done < bs && buff != stack_buff) {
32 /* allocated */
33 *string_ptr = buff;
34 return;
35 } else {
36 /* true calculate memory size */
37 bs = done+1;
38 }
39 } else {
40 /*
41 * Old libc. Incrementaly test.
42 * Exact not minimize allocate memory.
43 */
44 bs += 128;
45 }
46 buff = xrealloc((buff == stack_buff ? NULL : buff), bs);
47 }
48}
49
50void bb_asprintf(char **string_ptr, const char *format, ...)
51{
52 va_list p;
53
54 va_start(p, format);
55 bb_vasprintf(string_ptr, format, p);
56 va_end(p);
57}
58
59#ifdef TEST
60int main(int argc, char **argv)
61{
62 char *out_buf;
63 char big_buf[200];
64 int i;
65
66 bb_asprintf(&out_buf, "Hi!\nargc=%d argv[0]=%s\n", argc, argv[0]);
67 printf(out_buf);
68 free(out_buf);
69
70 for(i=0; i < sizeof(big_buf)-1; i++)
71 big_buf[i]='x';
72 big_buf[i]=0;
73 bb_asprintf(&out_buf, "Test Big\n%s\n", big_buf);
74 printf(out_buf);
75 free(out_buf);
76
77 return 0;
78}
79
80void *xrealloc(void *p, size_t size)
81{
82 void *p2 = realloc(p, size);
83 if(p2==0) {
84 fprintf(stderr, "TEST: memory_exhausted\n");
85 exit(1);
86 }
87 return p2;
88}
89#endif
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 9e771495d..f3a1b4462 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -6,7 +6,6 @@
6 * 6 *
7 */ 7 */
8 8
9#include "inet_common.h"
10#include <unistd.h> 9#include <unistd.h>
11#include <string.h> 10#include <string.h>
12#include <stdlib.h> 11#include <stdlib.h>
@@ -58,7 +57,7 @@ int xconnect(const char *host, const char *port)
58 struct sockaddr_in s_addr; 57 struct sockaddr_in s_addr;
59 int s = socket(AF_INET, SOCK_STREAM, 0); 58 int s = socket(AF_INET, SOCK_STREAM, 0);
60 struct servent *tserv; 59 struct servent *tserv;
61 int port_nr=atoi(port); 60 int port_nr=htons(atoi(port));
62 struct hostent * he; 61 struct hostent * he;
63 62
64 if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL) 63 if (port_nr==0 && (tserv = getservbyname(port, "tcp")) != NULL)
@@ -66,7 +65,7 @@ int xconnect(const char *host, const char *port)
66 65
67 memset(&s_addr, 0, sizeof(struct sockaddr_in)); 66 memset(&s_addr, 0, sizeof(struct sockaddr_in));
68 s_addr.sin_family = AF_INET; 67 s_addr.sin_family = AF_INET;
69 s_addr.sin_port = htons(port_nr); 68 s_addr.sin_port = port_nr;
70 69
71 he = xgethostbyname(host); 70 he = xgethostbyname(host);
72 memcpy(&s_addr.sin_addr, he->h_addr, sizeof s_addr.sin_addr); 71 memcpy(&s_addr.sin_addr, he->h_addr, sizeof s_addr.sin_addr);