tool_operhlp.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2014, 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #include "strcase.h"
00025 
00026 #define ENABLE_CURLX_PRINTF
00027 /* use our own printf() functions */
00028 #include "curlx.h"
00029 
00030 #include "tool_cfgable.h"
00031 #include "tool_convert.h"
00032 #include "tool_doswin.h"
00033 #include "tool_operhlp.h"
00034 #include "tool_metalink.h"
00035 
00036 #include "memdebug.h" /* keep this as LAST include */
00037 
00038 void clean_getout(struct OperationConfig *config)
00039 {
00040   struct getout *next;
00041   struct getout *node = config->url_list;
00042 
00043   while(node) {
00044     next = node->next;
00045     Curl_safefree(node->url);
00046     Curl_safefree(node->outfile);
00047     Curl_safefree(node->infile);
00048     Curl_safefree(node);
00049     node = next;
00050   }
00051   config->url_list = NULL;
00052 }
00053 
00054 bool output_expected(const char *url, const char *uploadfile)
00055 {
00056   if(!uploadfile)
00057     return TRUE;  /* download */
00058   if(checkprefix("http://", url) || checkprefix("https://", url))
00059     return TRUE;   /* HTTP(S) upload */
00060 
00061   return FALSE; /* non-HTTP upload, probably no output should be expected */
00062 }
00063 
00064 bool stdin_upload(const char *uploadfile)
00065 {
00066   return (!strcmp(uploadfile, "-") ||
00067           !strcmp(uploadfile, ".")) ? TRUE : FALSE;
00068 }
00069 
00070 /*
00071  * Adds the file name to the URL if it doesn't already have one.
00072  * url will be freed before return if the returned pointer is different
00073  */
00074 char *add_file_name_to_url(CURL *curl, char *url, const char *filename)
00075 {
00076   /* If no file name part is given in the URL, we add this file name */
00077   char *ptr = strstr(url, "://");
00078   if(ptr)
00079     ptr += 3;
00080   else
00081     ptr = url;
00082   ptr = strrchr(ptr, '/');
00083   if(!ptr || !strlen(++ptr)) {
00084     /* The URL has no file name part, add the local file name. In order
00085        to be able to do so, we have to create a new URL in another
00086        buffer.*/
00087 
00088     /* We only want the part of the local path that is on the right
00089        side of the rightmost slash and backslash. */
00090     const char *filep = strrchr(filename, '/');
00091     char *file2 = strrchr(filep?filep:filename, '\\');
00092     char *encfile;
00093 
00094     if(file2)
00095       filep = file2 + 1;
00096     else if(filep)
00097       filep++;
00098     else
00099       filep = filename;
00100 
00101     /* URL encode the file name */
00102     encfile = curl_easy_escape(curl, filep, 0 /* use strlen */);
00103     if(encfile) {
00104       char *urlbuffer;
00105       if(ptr)
00106         /* there is a trailing slash on the URL */
00107         urlbuffer = aprintf("%s%s", url, encfile);
00108       else
00109         /* there is no trailing slash on the URL */
00110         urlbuffer = aprintf("%s/%s", url, encfile);
00111 
00112       curl_free(encfile);
00113       Curl_safefree(url);
00114 
00115       if(!urlbuffer)
00116         return NULL;
00117 
00118       url = urlbuffer; /* use our new URL instead! */
00119     }
00120     else
00121       Curl_safefree(url);
00122   }
00123   return url;
00124 }
00125 
00126 /* Extracts the name portion of the URL.
00127  * Returns a pointer to a heap-allocated string or NULL if
00128  * no name part, at location indicated by first argument.
00129  */
00130 CURLcode get_url_file_name(char **filename, const char *url)
00131 {
00132   const char *pc, *pc2;
00133 
00134   *filename = NULL;
00135 
00136   /* Find and get the remote file name */
00137   pc = strstr(url, "://");
00138   if(pc)
00139     pc += 3;
00140   else
00141     pc = url;
00142 
00143   pc2 = strrchr(pc, '\\');
00144   pc = strrchr(pc, '/');
00145   if(pc2 && (!pc || pc < pc2))
00146     pc = pc2;
00147 
00148   if(pc)
00149     /* duplicate the string beyond the slash */
00150     pc++;
00151   else
00152     /* no slash => empty string */
00153     pc = "";
00154 
00155   *filename = strdup(pc);
00156   if(!*filename)
00157     return CURLE_OUT_OF_MEMORY;
00158 
00159 #if defined(MSDOS) || defined(WIN32)
00160   {
00161     char *sanitized;
00162     SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
00163     Curl_safefree(*filename);
00164     if(sc)
00165       return CURLE_URL_MALFORMAT;
00166     *filename = sanitized;
00167   }
00168 #endif /* MSDOS || WIN32 */
00169 
00170   /* in case we built debug enabled, we allow an environment variable
00171    * named CURL_TESTDIR to prefix the given file name to put it into a
00172    * specific directory
00173    */
00174 #ifdef DEBUGBUILD
00175   {
00176     char *tdir = curlx_getenv("CURL_TESTDIR");
00177     if(tdir) {
00178       char buffer[512]; /* suitably large */
00179       snprintf(buffer, sizeof(buffer), "%s/%s", tdir, *filename);
00180       Curl_safefree(*filename);
00181       *filename = strdup(buffer); /* clone the buffer */
00182       curl_free(tdir);
00183       if(!*filename)
00184         return CURLE_OUT_OF_MEMORY;
00185     }
00186   }
00187 #endif
00188 
00189   return CURLE_OK;
00190 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07