Last active
November 22, 2015 01:12
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Author
treeherder
commented
Nov 22, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment