Skip to content

Instantly share code, notes, and snippets.

@treeherder
Last active November 22, 2015 01:12
Show Gist options
  • Save treeherder/5299c2591e07b531f2aa to your computer and use it in GitHub Desktop.
Save treeherder/5299c2591e07b531f2aa to your computer and use it in GitHub Desktop.
using libcurl to achieve something like "curl ADDRESS --data-binary @-FILE" because it's more portable than using an OS call, and we need a reliable method of uploading, and soon downloading, files of various sizes and descriptions. long term goal is to make this part of the updater toolchain for 3d printer firmware. For now, it will live here a…
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int fd;
char *data;
struct stat sbuf;
CURL *curl = curl_easy_init();
if (argc != 3) {
fprintf(stderr, "usage: curler ADDRESS FILE\n");
exit(1);
}
if ((fd = open(argv[1], O_RDONLY)) == -1) {
perror("open");
exit(1);
}
if (stat(argv[1], &sbuf) == -1) {
perror("stat");
exit(1);
}
if ((data = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (caddr_t)(-1)) {
perror("mmap");
exit(1);
}
printf("curling\r\n");
curl_easy_setopt(curl, CURLOPT_URL, argv[2]);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_perform(curl);
munmap(data, sizeof(data));
return 0;
}
@treeherder
Copy link
Author

 [curler] curl -O https://gist.githubusercontent.com/treeherder/5299c2591e07b531f2aa/raw/831d9acf62167a49eb74e792d42492b810564ecf/curler.c
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   883  100   883    0     0   5130      0 --:--:-- --:--:-- --:--:--  5133
[curler] gcc -lcurl curler.c -o test
[curler] ./test
usage: curler ADDRESS FILE
[curler] ./test curler.c paste.click
curling
http://paste.click/SExZSq
[curler]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment