remove the CVSish $Id$ lines
[platform/upstream/curl.git] / tests / libtest / lib541.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #ifdef HAVE_SYS_SOCKET_H
13 #include <sys/socket.h>
14 #endif
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18 #ifdef HAVE_SYS_STAT_H
19 #include <sys/stat.h>
20 #endif
21 #ifdef HAVE_FCNTL_H
22 #include <fcntl.h>
23 #endif
24
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #include "memdebug.h"
30
31 /*
32  * Two FTP uploads, the second with no content sent.
33  */
34
35 int test(char *URL)
36 {
37   CURL *curl;
38   CURLcode res = CURLE_OK;
39   FILE *hd_src ;
40   int hd ;
41   struct_stat file_info;
42   int error;
43
44   if (!libtest_arg2) {
45     fprintf(stderr, "Usage: <url> <file-to-upload>\n");
46     return -1;
47   }
48
49   /* get the file size of the local file */
50   hd = stat(libtest_arg2, &file_info);
51   if(hd == -1) {
52     /* can't open file, bail out */
53     error = ERRNO;
54     fprintf(stderr, "stat() failed with error: %d %s\n",
55             error, strerror(error));
56     fprintf(stderr, "WARNING: cannot open file %s\n", libtest_arg2);
57     return -1;
58   }
59
60   if(! file_info.st_size) {
61     fprintf(stderr, "WARNING: file %s has no size!\n", libtest_arg2);
62     return -4;
63   }
64
65   /* get a FILE * of the same file, could also be made with
66      fdopen() from the previous descriptor, but hey this is just
67      an example! */
68   hd_src = fopen(libtest_arg2, "rb");
69   if(NULL == hd_src) {
70     error = ERRNO;
71     fprintf(stderr, "fopen() failed with error: %d %s\n",
72             error, strerror(error));
73     fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
74     return -2; /* if this happens things are major weird */
75   }
76
77   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
78     fprintf(stderr, "curl_global_init() failed\n");
79     fclose(hd_src);
80     return TEST_ERR_MAJOR_BAD;
81   }
82
83   /* get a curl handle */
84   if ((curl = curl_easy_init()) == NULL) {
85     fprintf(stderr, "curl_easy_init() failed\n");
86     curl_global_cleanup();
87     fclose(hd_src);
88     return TEST_ERR_MAJOR_BAD;
89   }
90
91   /* enable uploading */
92   test_setopt(curl, CURLOPT_UPLOAD, 1L);
93
94   /* enable verbose */
95   test_setopt(curl, CURLOPT_VERBOSE, 1L);
96
97   /* specify target */
98   test_setopt(curl,CURLOPT_URL, URL);
99
100   /* now specify which file to upload */
101   test_setopt(curl, CURLOPT_INFILE, hd_src);
102
103   /* Now run off and do what you've been told! */
104   res = curl_easy_perform(curl);
105
106   /* and now upload the exact same again, but without rewinding so it already
107      is at end of file */
108   res = curl_easy_perform(curl);
109
110 test_cleanup:
111
112   /* close the local file */
113   fclose(hd_src);
114
115   curl_easy_cleanup(curl);
116   curl_global_cleanup();
117
118   return res;
119 }