diff options
author | Florian Pritz <bluewind@xinu.at> | 2011-06-04 16:01:39 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2011-06-04 16:01:39 +0200 |
commit | b6189f1907e22aad1cda7ecf236529bd7b270734 (patch) | |
tree | ae7b218e5ec065e8232a9c0986635025e02ed3d2 | |
parent | 772c2fdc00319ce312845eba3df85115567f6aa5 (diff) |
remove dependency on curl binary
fb-upload is now called fb-helper which supports downloading and uploading
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | Makefile | 18 | ||||
-rw-r--r-- | fb-helper.c.in (renamed from fb-upload.c.in) | 85 | ||||
-rw-r--r-- | fb.in | 34 |
4 files changed, 69 insertions, 72 deletions
@@ -1,5 +1,5 @@ dist fb.1 fb -fb-upload -fb-upload.c +fb-helper +fb-helper.c @@ -5,40 +5,40 @@ LIBDIR=/usr/lib CC=gcc CFLAGS=-O2 -std=c99 -Wall -Wextra -pedantic -all: fb.1 fb fb-upload +all: fb.1 fb fb-helper fb: fb.in @[ -n "$(VERSION)" ] || (echo "Error: version detection failed"; exit 1) sed 's|@VERSION@|$(VERSION)|; s|@LIBDIR@|$(LIBDIR)|' fb.in > fb chmod 755 fb -fb-upload.c: fb-upload.c.in - sed 's/@VERSION@/$(VERSION)/' fb-upload.c.in > fb-upload.c +fb-helper.c: fb-helper.c.in + sed 's/@VERSION@/$(VERSION)/' fb-helper.c.in > fb-helper.c -fb-upload: fb-upload.c - $(CC) $(CFLAGS) -lcurl -lm -o fb-upload fb-upload.c +fb-helper: fb-helper.c + $(CC) $(CFLAGS) -lcurl -lm -o fb-helper fb-helper.c fb.1: fb.pod pod2man -c "" fb.pod fb.1 clean: - rm -f fb.1 fb fb-upload.c fb-upload + rm -f fb.1 fb fb-helper.c fb-helper rm -rf dist install: all install -Dm755 fb $(DESTDIR)$(BINDIR)/fb - install -Dm755 fb-upload $(DESTDIR)$(LIBDIR)/fb-upload + install -Dm755 fb-helper $(DESTDIR)$(LIBDIR)/fb-helper install -Dm644 fb.1 $(DESTDIR)$(MANDIR)/man1/fb.1 uninstall: rm -f $(DESTDIR)$(BINDIR)/fb - rm -f $(DESTDIR)$(LIBDIR)/fb-upload + rm -f $(DESTDIR)$(LIBDIR)/fb-helper rm -f $(DESTDIR)$(MANDIR)/man1/fb.1 dist: all @[ -n "$(VERSION)" ] || (echo "Error: version detection failed"; exit 1) mkdir -p dist/fb-$(VERSION) - cp -a fb-upload.c{,.in} fb{,.in} fb.pod fb.1 COPYING Makefile dist/fb-$(VERSION) + cp -a fb-helper.c{,.in} fb{,.in} fb.pod fb.1 COPYING Makefile dist/fb-$(VERSION) sed -i 's/^VERSION:=.*$$/VERSION:="'$(VERSION)'"/' dist/fb-$(VERSION)/Makefile cd dist; tar -czf fb-$(VERSION).tar.gz fb-$(VERSION) diff --git a/fb-upload.c.in b/fb-helper.c.in index b0a44ee..2d92caa 100644 --- a/fb-upload.c.in +++ b/fb-helper.c.in @@ -154,50 +154,61 @@ int main(int argc, char *argv[]) static const char buf[] = "Expect:"; struct curl_forms forms[4]; + char *mode = NULL; char *data = NULL; + char *url = NULL; + char *file = NULL; size_t data_size = 0; int ret = 0; - /* simple arg check */ - if(argc != 3) + if(argc == 0) return 1; + mode = argv[1]; + + if (strncmp(mode, "u", 1) == 0) + file = argv[3]; + + url = argv[2]; + if (curl_global_init(CURL_GLOBAL_ALL) != 0) { fprintf(stderr, "Error initializing curl"); return 10; } - if(stat(argv[2], &statbuf) == -1) { - perror("fb-upload"); - return 1; - } - - /* load files with 0 size (/proc files for example) into memory so we can - * determine their real length */ - if (statbuf.st_size == 0) { - if (load_file(argv[2], &data, &data_size) != 0) { + if (file) { + if(stat(file, &statbuf) == -1) { + perror("fb-upload"); return 1; } - forms[0].option = CURLFORM_BUFFER; - forms[0].value = basename(argv[2]); - forms[1].option = CURLFORM_BUFFERPTR; - forms[1].value = data; - forms[2].option = CURLFORM_BUFFERLENGTH; - forms[2].value = (char *)data_size; - forms[3].option = CURLFORM_END; - } else { - forms[0].option = CURLFORM_FILE; - forms[0].value = argv[2]; - forms[1].option = CURLFORM_END; - } - /* Fill in the file upload field */ - curl_formadd(&formpost, - &lastptr, - CURLFORM_COPYNAME, "file", - CURLFORM_ARRAY, forms, - CURLFORM_END); + /* load files with 0 size (/proc files for example) into memory so we can + * determine their real length */ + if (statbuf.st_size == 0) { + if (load_file(file, &data, &data_size) != 0) { + return 1; + } + forms[0].option = CURLFORM_BUFFER; + forms[0].value = basename(file); + forms[1].option = CURLFORM_BUFFERPTR; + forms[1].value = data; + forms[2].option = CURLFORM_BUFFERLENGTH; + forms[2].value = (char *)data_size; + forms[3].option = CURLFORM_END; + } else { + forms[0].option = CURLFORM_FILE; + forms[0].value = file; + forms[1].option = CURLFORM_END; + } + + /* Fill in the file upload field */ + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "file", + CURLFORM_ARRAY, forms, + CURLFORM_END); + } curl = curl_easy_init(); /* initialize custom header list (stating that Expect: 100-continue is not @@ -205,14 +216,17 @@ int main(int argc, char *argv[]) headerlist = curl_slist_append(headerlist, buf); if(curl) { /* what URL that receives this POST */ - curl_easy_setopt(curl, CURLOPT_URL, argv[1]); + curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); - curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &cb_data); - curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback); + if (file) { + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); + curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &cb_data); + curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback); + } curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent); - curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + if (formpost) + curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, (long)1); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, (long)30); gettimeofday(&cb_data.starttime, NULL); @@ -225,7 +239,8 @@ int main(int argc, char *argv[]) /* cleanup */ curl_easy_cleanup(curl); - curl_formfree(formpost); + if (formpost) + curl_formfree(formpost); curl_slist_free_all (headerlist); curl_global_cleanup(); free(data); @@ -7,8 +7,7 @@ # (see COPYING for full license text) # #---------------------------------------------------- -# Dependencies: curl -# Optional: xclip +# Optional dependency: xclip #---------------------------------------------------- VERSION="@VERSION@" @@ -56,7 +55,7 @@ do_upload() { TMPFILE=`mktemp "$TMPDIR/data.XXXXXX"` if [ `stat -c %s -- "$file"` -gt "$WARNSIZE" ]; then - WARNSIZE=`curl -s "$PASTEBIN/file/get_max_size"` + WARNSIZE=`$LIBDIR/fb-helper d "$PASTEBIN/file/get_max_size"` if [ `stat -c %s -- "$file"` -gt "$WARNSIZE" ]; then echo "Warning: Your upload is too big and would be rejected. Maximum size is: $WARNSIZE bytes. Skipping..." >&2 EXITCODE=1 @@ -64,26 +63,9 @@ do_upload() { fi fi - # fb-upload handles corner cases (comma in the filename) better - if [ -x "$LIBDIR/fb-upload" ]; then - if ! $LIBDIR/fb-upload "$PASTEBIN/file/do_upload" "$file" > $TMPFILE; then - EXITCODE=1 - return 1 - fi - else - CURLOPTS="-# -n -L -A $USERAGENT $EXTRA" - if [ `stat -c %s -- "$file"` -eq "0" ] || echo "$file" | grep -F -q ","; then - basefilename=`echo "$basefilename" | tr -d ,` - if ! curl $CURLOPTS -F "file=@-;filename=$basefilename" "$PASTEBIN/file/do_upload" < "$file" > $TMPFILE; then - EXITCODE=1 - return 1 - fi - else - if ! curl $CURLOPTS -F "file=@$file" "$PASTEBIN/file/do_upload" > $TMPFILE; then - EXITCODE=1 - return 1 - fi - fi + if ! $LIBDIR/fb-helper u "$PASTEBIN/file/do_upload" "$file" > $TMPFILE; then + EXITCODE=1 + return 1 fi sed '$d' $TMPFILE >&2 URL=`tail -1 $TMPFILE`"$EXTENSION" @@ -162,9 +144,9 @@ if [ "$DELETE" ] || [ "$GET" ]; then for i in "$@"; do i=$(id_from_arg "$i") if [ "$DELETE" ]; then - curl -n -L -A $USERAGENT "$PASTEBIN/file/delete/$i" || EXITCODE=1 + $LIBDIR/fb-helper d "$PASTEBIN/file/delete/$i" || EXITCODE=1 elif [ "$GET" ]; then - curl -s -o - -A $USERAGENT "$PASTEBIN/$i" || EXITCODE=1 + $LIBDIR/fb-helper d "$PASTEBIN/$i" || EXITCODE=1 fi done elif [ $# -eq 0 ]; then @@ -184,7 +166,7 @@ else for i in "$@"; do if echo "$i" | grep -qE "^(f|ht)tp(s)?://.+"; then cd $TMPDIR - if ! curl -# -A $USERAGENT -O "$i"; then + if ! $LIBDIR/fb-helper d "$i" > "`basename "$i"`"; then EXITCODE=1 continue fi |