From 1aa1cbf8e5bc134b620ed153534b4891195d4b55 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 9 Jan 2013 16:56:51 +0100 Subject: fb-helper: Use getopt Signed-off-by: Florian Pritz --- fb-helper.c | 48 ++++++++++++++++++++++++++++-------------------- fb.in | 10 +++++++++- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/fb-helper.c b/fb-helper.c index 3c0e682..56f9429 100644 --- a/fb-helper.c +++ b/fb-helper.c @@ -13,7 +13,7 @@ * (see COPYING for full license text) */ -#define _POSIX_C_SOURCE 1 +#define _POSIX_C_SOURCE 2 #include #include @@ -47,6 +47,11 @@ struct progressData { int current_sample; }; +struct options { + char *url; + char *file; +}; + /* load the contents of file fn into data */ int load_file(const char *fn, char **data, size_t *data_size) { @@ -227,23 +232,27 @@ int main(int argc, char *argv[]) .current_sample = 0 }; - char *mode = NULL; char *data = NULL; - char *url = NULL; - char *file = NULL; int ret = 0; - /* process arguments */ - if(argc == 1) - return 1; + int opt; + struct options options = { + .file = NULL, + .url = NULL + }; - mode = argv[1]; + while ((opt = getopt(argc, argv, "u:f:m:")) != -1) { + switch (opt) { - if (strncmp(mode, "u", 1) == 0) - file = argv[3]; + case 'u': options.url = optarg; break; - url = argv[2]; + case 'f': options.file = optarg; break; + + default: + fprintf(stderr, "Error: unknown option %c", opt); + } + } /* initialize curl */ if (curl_global_init(CURL_GLOBAL_ALL) != 0) { @@ -260,11 +269,11 @@ int main(int argc, char *argv[]) } /* if we have a file to upload, add it as a POST request */ - if (file) { + if (options.file) { struct stat statbuf; - if(stat(file, &statbuf) == -1) { - fprintf(stderr, "fb-helper: %s: ", file); + if(stat(options.file, &statbuf) == -1) { + fprintf(stderr, "fb-helper: %s: ", options.file); perror(NULL); ret = 1; goto cleanup; @@ -275,19 +284,19 @@ int main(int argc, char *argv[]) if (statbuf.st_size == 0) { size_t data_size = 0; - if (load_file(file, &data, &data_size) != 0) { + if (load_file(options.file, &data, &data_size) != 0) { ret = 1; goto cleanup; } if (data_size == 0) { - fprintf(stderr, "Error: skipping 0-byte file: \"%s\"\n", file); + fprintf(stderr, "Error: skipping 0-byte file: \"%s\"\n", options.file); ret = 1; goto cleanup; } forms[0].option = CURLFORM_BUFFER; - forms[0].value = basename(file); + forms[0].value = basename(options.file); forms[1].option = CURLFORM_BUFFERPTR; forms[1].value = data; forms[2].option = CURLFORM_BUFFERLENGTH; @@ -295,7 +304,7 @@ int main(int argc, char *argv[]) forms[3].option = CURLFORM_END; } else { forms[0].option = CURLFORM_FILE; - forms[0].value = file; + forms[0].value = options.file; forms[1].option = CURLFORM_END; } @@ -320,13 +329,12 @@ int main(int argc, char *argv[]) headerlist = curl_slist_append(headerlist, buf); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); - curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_URL, options.url); curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent); /* use .netrc settings for authentication if available */ curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); - /* bail if the upload stalls for 30 seconds */ curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 30L); diff --git a/fb.in b/fb.in index b21a83c..ce56a6d 100644 --- a/fb.in +++ b/fb.in @@ -66,7 +66,15 @@ request_helper() { # if available use the external helper, else fall back to calling curl if [ -x "$libdir/fb-helper" ]; then - $libdir/fb-helper "$mode" "$url" "$file" + helperopts="" + + if [ "$mode" = "d" ]; then + $libdir/fb-helper $helperopts -u "$url" + fi + + if [ "$mode" = "u" ]; then + $libdir/fb-helper $helperopts -u "$url" -f "$file" + fi else useragent="fb-client/shell-$version" curlopts="-# -n -L -A $useragent --speed-time 30 --speed-limit 1 --connect-timeout 10" -- cgit v1.2.3-24-g4f1b