1
/***************************************************************************
3
* Project ___| | | | _ \| |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
10
* This software is licensed as described in the file COPYING, which
11
* you should have received as part of this distribution. The terms
12
* are also available at http://curl.haxx.se/docs/copyright.html.
14
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
* copies of the Software, and permit persons to whom the Software is
16
* furnished to do so, under the terms of the COPYING file.
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
* KIND, either express or implied.
21
***************************************************************************/
24
#include <curl/curl.h>
28
#define ENABLE_CURLX_PRINTF
29
/* use our own printf() functions */
32
#include "tool_cfgable.h"
33
#include "tool_convert.h"
34
#include "tool_operhlp.h"
35
#include "tool_version.h"
37
#include "memdebug.h" /* keep this as LAST include */
40
* my_useragent: returns allocated string with default user agent
42
char *my_useragent(void)
44
char useragent[256]; /* we don't want a larger default user agent */
46
snprintf(useragent, sizeof(useragent),
47
CURL_NAME "/" CURL_VERSION " (" OS ") " "%s", curl_version());
49
return strdup(useragent);
53
* Print list of OpenSSL supported engines
55
void list_engines(const struct curl_slist *engines)
57
puts("Build-time engines:");
62
for(; engines; engines = engines->next)
63
printf(" %s\n", engines->data);
66
void clean_getout(struct Configurable *config)
69
struct getout *node = config->url_list;
73
Curl_safefree(node->url);
74
Curl_safefree(node->outfile);
75
Curl_safefree(node->infile);
79
config->url_list = NULL;
82
bool output_expected(const char *url, const char *uploadfile)
85
return TRUE; /* download */
86
if(checkprefix("http://", url) || checkprefix("https://", url))
87
return TRUE; /* HTTP(S) upload */
89
return FALSE; /* non-HTTP upload, probably no output should be expected */
92
bool stdin_upload(const char *uploadfile)
94
return (curlx_strequal(uploadfile, "-") ||
95
curlx_strequal(uploadfile, ".")) ? TRUE : FALSE;
99
* Adds the file name to the URL if it doesn't already have one.
100
* url will be freed before return if the returned pointer is different
102
char *add_file_name_to_url(CURL *curl, char *url, const char *filename)
104
/* If no file name part is given in the URL, we add this file name */
105
char *ptr = strstr(url, "://");
110
ptr = strrchr(ptr, '/');
111
if(!ptr || !strlen(++ptr)) {
112
/* The URL has no file name part, add the local file name. In order
113
to be able to do so, we have to create a new URL in another
116
/* We only want the part of the local path that is on the right
117
side of the rightmost slash and backslash. */
118
const char *filep = strrchr(filename, '/');
119
char *file2 = strrchr(filep?filep:filename, '\\');
129
/* URL encode the file name */
130
encfile = curl_easy_escape(curl, filep, 0 /* use strlen */);
132
char *urlbuffer = malloc(strlen(url) + strlen(encfile) + 3);
139
/* there is a trailing slash on the URL */
140
sprintf(urlbuffer, "%s%s", url, encfile);
142
/* there is no trailing slash on the URL */
143
sprintf(urlbuffer, "%s/%s", url, encfile);
148
url = urlbuffer; /* use our new URL instead! */
154
/* Extracts the name portion of the URL.
155
* Returns a pointer to a heap-allocated string or NULL if
156
* no name part, at location indicated by first argument.
158
CURLcode get_url_file_name(char **filename, const char *url)
164
/* Find and get the remote file name */
165
pc = strstr(url, "://");
170
pc = strrchr(pc, '/');
173
/* duplicate the string beyond the slash */
176
*filename = strdup(pc);
178
return CURLE_OUT_OF_MEMORY;
182
/* in case we built debug enabled, we allow an environment variable
183
* named CURL_TESTDIR to prefix the given file name to put it into a
188
char *tdir = curlx_getenv("CURL_TESTDIR");
190
char buffer[512]; /* suitably large */
191
snprintf(buffer, sizeof(buffer), "%s/%s", tdir, *filename);
192
Curl_safefree(*filename);
193
*filename = strdup(buffer); /* clone the buffer */
203
* This is the main global constructor for the app. Call this before
204
* _any_ libcurl usage. If this fails, *NO* libcurl functions may be
205
* used, or havoc may be the result.
207
CURLcode main_init(void)
209
#if defined(__DJGPP__) || defined(__GO32__)
210
/* stop stat() wasting time */
211
_djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
214
return curl_global_init(CURL_GLOBAL_DEFAULT);
218
* This is the main global destructor for the app. Call this after
219
* _all_ libcurl usage is done.
223
curl_global_cleanup();
228
void memory_tracking_init(void)
231
/* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
232
env = curlx_getenv("CURL_MEMDEBUG");
234
/* use the value as file name */
235
char fname[CURL_MT_LOGFNAME_BUFSIZE];
236
if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
237
env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
240
curl_memdebug(fname);
241
/* this weird stuff here is to make curl_free() get called
242
before curl_memdebug() as otherwise memory tracking will
243
log a free() without an alloc! */
245
/* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
246
env = curlx_getenv("CURL_MEMLIMIT");
249
long num = strtol(env, &endptr, 10);
250
if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))