1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 2016, 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 https://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 ***************************************************************************/
22 #include "tool_setup.h"
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
30 #include "tool_cfgable.h"
31 #include "tool_convert.h"
32 #include "tool_doswin.h"
33 #include "tool_operhlp.h"
34 #include "tool_metalink.h"
36 #include "memdebug.h" /* keep this as LAST include */
38 void clean_getout(struct OperationConfig *config)
41 struct getout *node = config->url_list;
45 Curl_safefree(node->url);
46 Curl_safefree(node->outfile);
47 Curl_safefree(node->infile);
51 config->url_list = NULL;
54 bool output_expected(const char *url, const char *uploadfile)
57 return TRUE; /* download */
58 if(checkprefix("http://", url) || checkprefix("https://", url))
59 return TRUE; /* HTTP(S) upload */
61 return FALSE; /* non-HTTP upload, probably no output should be expected */
64 bool stdin_upload(const char *uploadfile)
66 return (!strcmp(uploadfile, "-") ||
67 !strcmp(uploadfile, ".")) ? TRUE : FALSE;
71 * Adds the file name to the URL if it doesn't already have one.
72 * url will be freed before return if the returned pointer is different
74 char *add_file_name_to_url(CURL *curl, char *url, const char *filename)
76 /* If no file name part is given in the URL, we add this file name */
77 char *ptr = strstr(url, "://");
82 ptr = strrchr(ptr, '/');
83 if(!ptr || !strlen(++ptr)) {
84 /* The URL has no file name part, add the local file name. In order
85 to be able to do so, we have to create a new URL in another
88 /* We only want the part of the local path that is on the right
89 side of the rightmost slash and backslash. */
90 const char *filep = strrchr(filename, '/');
91 char *file2 = strrchr(filep?filep:filename, '\\');
101 /* URL encode the file name */
102 encfile = curl_easy_escape(curl, filep, 0 /* use strlen */);
106 /* there is a trailing slash on the URL */
107 urlbuffer = aprintf("%s%s", url, encfile);
109 /* there is no trailing slash on the URL */
110 urlbuffer = aprintf("%s/%s", url, encfile);
118 url = urlbuffer; /* use our new URL instead! */
126 /* Extracts the name portion of the URL.
127 * Returns a pointer to a heap-allocated string or NULL if
128 * no name part, at location indicated by first argument.
130 CURLcode get_url_file_name(char **filename, const char *url)
132 const char *pc, *pc2;
136 /* Find and get the remote file name */
137 pc = strstr(url, "://");
143 pc2 = strrchr(pc, '\\');
144 pc = strrchr(pc, '/');
145 if(pc2 && (!pc || pc < pc2))
149 /* duplicate the string beyond the slash */
152 /* no slash => empty string */
155 *filename = strdup(pc);
157 return CURLE_OUT_OF_MEMORY;
159 #if defined(MSDOS) || defined(WIN32)
162 SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
163 Curl_safefree(*filename);
165 return CURLE_URL_MALFORMAT;
166 *filename = sanitized;
168 #endif /* MSDOS || WIN32 */
170 /* in case we built debug enabled, we allow an environment variable
171 * named CURL_TESTDIR to prefix the given file name to put it into a
176 char *tdir = curlx_getenv("CURL_TESTDIR");
178 char buffer[512]; /* suitably large */
179 snprintf(buffer, sizeof(buffer), "%s/%s", tdir, *filename);
180 Curl_safefree(*filename);
181 *filename = strdup(buffer); /* clone the buffer */
184 return CURLE_OUT_OF_MEMORY;