Skip to content

Writing the software

Marcus Ottosson edited this page Jan 25, 2015 · 3 revisions
/*Download file from a terminal

Usage:
    $ lc_download http://google.com

*/

#include <stdio.h>
#include <libgen.h> // For basename()
#include <curl/curl.h>


int main(int argc, char* args[]) {
    
    // Assume a single argument
    if (argc != 2) {
        printf("Please enter a URL");
        return 1;
    }

    char *url = args[1];

    // Declare a pointer to a CURL *easy* handle
    // http://curl.haxx.se/libcurl/c/curl_easy_init.html
    CURL *curl;
    
    // If there is any error, this is the variable in which it will be stored.
    // http://curl.haxx.se/libcurl/c/curl_easy_strerror.html
    CURLcode res;

    // This (file-handle) is what we'll use to write to disk
    FILE *fp;

    // Apparently, this function must be the first function call.
    // http://curl.haxx.se/libcurl/c/curl_easy_init.html
    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);

        // Now that we've passed the full url, let's
        // get the last part of the URL, which will
        // become our file-name.
        char *output = basename(args[1]);
        fp = fopen(output, "wb");
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

        // Perform the request, res will get the return code
        res = curl_easy_perform(curl);
        if(res != CURLE_OK)
          fprintf(stderr, "Could not download file: %s\n",
                  curl_easy_strerror(res));

        // Always cleanup
        curl_easy_cleanup(curl);
    }

    return 0;
}

Conclusion

We've now written our Terminal Download Application and are ready to start using it!

$ ./lc_download https://raw.githubusercontent.com/learnclang/current/master/img/c.png

You should now have the file c.png in your current directory.

Success!

Clone this wiki locally