1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib525.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
13 #include <sys/types.h>
19 #define MAIN_LOOP_HANG_TIMEOUT 90 * 1000
20 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
29 struct_stat file_info
;
33 struct timeval ml_start
;
34 struct timeval mp_start
;
35 char ml_timedout
= FALSE
;
36 char mp_timedout
= FALSE
;
39 fprintf(stderr
, "Usage: lib525 [url] [uploadfile]\n");
43 /* get the file size of the local file */
44 hd
= open(libtest_arg2
, O_RDONLY
) ;
45 fstat(hd
, &file_info
);
48 /* get a FILE * of the same file, could also be made with
49 fdopen() from the previous descriptor, but hey this is just
51 hd_src
= fopen(libtest_arg2
, "rb");
54 fprintf(stderr
, "fopen() failed with error: %d %s\n",
55 error
, strerror(error
));
56 fprintf(stderr
, "Error opening file: %s\n", libtest_arg2
);
57 return TEST_ERR_MAJOR_BAD
;
60 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
) {
61 fprintf(stderr
, "curl_global_init() failed\n");
63 return TEST_ERR_MAJOR_BAD
;
66 if ((curl
= curl_easy_init()) == NULL
) {
67 fprintf(stderr
, "curl_easy_init() failed\n");
69 curl_global_cleanup();
70 return TEST_ERR_MAJOR_BAD
;
73 /* enable uploading */
74 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1L);
77 curl_easy_setopt(curl
,CURLOPT_URL
, URL
);
80 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, 1L);
83 curl_easy_setopt(curl
, CURLOPT_FTPPORT
, "-");
85 /* now specify which file to upload */
86 curl_easy_setopt(curl
, CURLOPT_READDATA
, hd_src
);
88 /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you
89 MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
90 do so will give you a crash since a DLL may not use the variable's memory
91 when passed in to it from an app like this. */
93 /* Set the size of the file to upload (optional). If you give a *_LARGE
94 option you MUST make sure that the type of the passed-in argument is a
95 curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
96 make sure that to pass in a type 'long' argument. */
97 curl_easy_setopt(curl
, CURLOPT_INFILESIZE_LARGE
,
98 (curl_off_t
)file_info
.st_size
);
100 if ((m
= curl_multi_init()) == NULL
) {
101 fprintf(stderr
, "curl_multi_init() failed\n");
102 curl_easy_cleanup(curl
);
103 curl_global_cleanup();
105 return TEST_ERR_MAJOR_BAD
;
108 if ((res
= (int)curl_multi_add_handle(m
, curl
)) != CURLM_OK
) {
109 fprintf(stderr
, "curl_multi_add_handle() failed, "
110 "with code %d\n", res
);
111 curl_multi_cleanup(m
);
112 curl_easy_cleanup(curl
);
113 curl_global_cleanup();
115 return TEST_ERR_MAJOR_BAD
;
119 ml_start
= tutil_tvnow();
124 struct timeval interval
;
127 interval
.tv_usec
= 0;
129 if (tutil_tvdiff(tutil_tvnow(), ml_start
) >
130 MAIN_LOOP_HANG_TIMEOUT
) {
135 mp_start
= tutil_tvnow();
137 while (res
== CURLM_CALL_MULTI_PERFORM
) {
138 res
= (int)curl_multi_perform(m
, &running
);
139 if (tutil_tvdiff(tutil_tvnow(), mp_start
) >
140 MULTI_PERFORM_HANG_TIMEOUT
) {
149 if (mp_timedout
|| done
)
152 if (res
!= CURLM_OK
) {
153 fprintf(stderr
, "not okay???\n");
162 if (curl_multi_fdset(m
, &rd
, &wr
, &exc
, &max_fd
) != CURLM_OK
) {
163 fprintf(stderr
, "unexpected failured of fdset.\n");
168 if (select_test(max_fd
+1, &rd
, &wr
, &exc
, &interval
) == -1) {
169 fprintf(stderr
, "bad select??\n");
174 res
= CURLM_CALL_MULTI_PERFORM
;
177 if (ml_timedout
|| mp_timedout
) {
178 if (ml_timedout
) fprintf(stderr
, "ml_timedout\n");
179 if (mp_timedout
) fprintf(stderr
, "mp_timedout\n");
180 fprintf(stderr
, "ABORTING TEST, since it seems "
181 "that it would have run forever.\n");
182 res
= TEST_ERR_RUNS_FOREVER
;
187 curl_multi_remove_handle(m
, curl
);
188 curl_multi_cleanup(m
);
189 curl_easy_cleanup(curl
);
192 curl_multi_remove_handle(m
, curl
);
193 curl_easy_cleanup(curl
);
194 curl_multi_cleanup(m
);
197 fclose(hd_src
); /* close the local file */
199 curl_global_cleanup();