title: libcurl: How to download url with original filename? (equivalent for "-O/--remote-name" )
body: <p>Question 1: When url is downloaded using libcurl, how to preserve the original name of downloaded file ?  LibCurl asks programmer to generate filename. Which is maybe easy when URL has the 
  e.g. in below url its easy to figure out target name is <strong>vimqrc.pdf</strong>.</p>
  
  <pre><code> http://tnerual.eriogerg.free.fr/vimqrc.pdf)  
  </code></pre>
  
  <p>but when URL is dynamically generating target name e.g.below URL downloads <strong>AdbeRdr1010_eu_ES.exe.</strong> with wget (no arguments except URL) and curl (argument -O)</p>
  
  <pre><code>http://get.adobe.com/reader/download/?installer=Reader_10.1_Basque_for_Windows&amp;standalone=1%22
  </code></pre>
  
  <p>How does curl (-O) or wget figures out name of </p>
  
  <pre><code>//invoked as ./a.out &lt;URL&gt;
  
  #include &lt;stdio.h&gt;
  #include &lt;curl/curl.h&gt;
  
  char *location = "/tmp/test/out";
  
  size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
      size_t written = fwrite(ptr, size, nmemb, stream);
      return written;
  }
  
  int main(int argc, char *argv[])
  {
      CURL        *curl;
      CURLcode    res;
      int         ret = -1;
  
  
      if (argc!= 2) {
          //invoked as ./a.out &lt;URL&gt;
          return -1;
      } 
  
      curl = curl_easy_init();
      if (!curl) {
          goto bail;
      }
  
      FILE *fp = fopen(location, "wb");
      curl_easy_setopt(curl, CURLOPT_URL, argv[1]); //invoked as ./a.out &lt;URL&gt;
      /* example.com is redirected, so we tell libcurl to follow redirection */
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  
      /* Perform the request, res will get the return code */
      res = curl_easy_perform(curl);
      /* Check for errors */
      if(res != CURLE_OK)
          fprintf(stderr, "curl_easy_perform() failed: %s\n",
                  curl_easy_strerror(res));
  
      /* always cleanup */
      curl_easy_cleanup(curl);
      ret = 0;
      fclose(fp);
  
  bail:
      return ret;
  }
  </code></pre>