Make code better

This commit is contained in:
(≧◡≦) 2022-07-15 11:16:15 +02:00
parent 9c8749e664
commit 955985b825
2 changed files with 40 additions and 42 deletions

View file

@ -2,52 +2,51 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <libconfig.h> #include <libconfig.h>
#include <curl/curl.h>
#include "sakisafecli.h" #include "sakisafecli.h"
size_t size_t
write_data(void *buffer, size_t size, size_t nmemb, write_data(void *buffer, size_t size, size_t nmemb, void *userp)
void *userp)
{ {
memcpy(userp, buffer, nmemb*size); memcpy(userp, buffer, nmemb * size);
return 0; return 0;
} }
void void
print_usage() print_usage()
{ {
printf("USAGE: sakisafecli [-p socks proxy|-P http proxy] [-x] [-s] [-6|-4] [--server] FILE\n"); printf("USAGE: sakisafecli [-p socks proxy|-P http proxy] [-x] [-s] "
"[-6|-4] [--server] FILE\n");
return; return;
} }
void void
print_help() print_help()
{ {
printf("-s|--server: specifies the sakisafe server\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s", printf("-s|--server: specifies the sakisafe "
"-t|--token: Authentication token (https://u.kalli.st)", "server\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
"-P|--http-proxy: http proxy to use e.g. http://127.0.0.1:4444", "-t|--token: Authentication token (https://u.kalli.st)",
"-p|--socks-proxy: SOCK proxy to use e.g. 127.0.0.1:9050", "-P|--http-proxy: http proxy to use e.g. http://127.0.0.1:4444",
"-6|--ipv6: uses IPv6 only", "-p|--socks-proxy: SOCK proxy to use e.g. 127.0.0.1:9050",
"-4|--ipv6: uses IPv4 only", "-6|--ipv6: uses IPv6 only",
"-S|--silent: doesn't print progress", "-4|--ipv6: uses IPv4 only",
"-x|--paste: read file from stdin", "-S|--silent: doesn't print progress",
"-C: print current settings", "-x|--paste: read file from stdin",
"-h|--help: print this message.\n"); "-C: print current settings",
"-h|--help: print this message.\n");
return; return;
} }
void void
progress(void *clientp, progress(
double dltotal, void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
double dlnow,
double ultotal,
double ulnow)
{ {
/* So I don't get a warning */ /* So I don't get a warning */
dltotal += 1; dltotal += 1;
dlnow += 1; dlnow += 1;
printf("\r%0.f uploaded of %0.f (\033[32;1m%0.f%%\033[30;0m)",ulnow,ultotal, printf("\r%0.f uploaded of %0.f (\033[32;1m%0.f%%\033[30;0m)",
ulnow*100/ultotal); ulnow,
ultotal,
ulnow * 100 / ultotal);
fflush(stdout); fflush(stdout);
} }

View file

@ -152,9 +152,7 @@ main(int argc, char **argv)
} }
if(access(argv[optind], F_OK) && !paste_flag) { if(access(argv[optind], F_OK) && !paste_flag) {
fprintf(stderr, "Error opening file: %s\n", fprintf(stderr, "Error opening file: %s\n", strerror(errno));
strerror(errno)
);
return -1; return -1;
} }
@ -166,12 +164,6 @@ main(int argc, char **argv)
int protocol = get_protocol(server); int protocol = get_protocol(server);
/* If using SCP, set the ssh key */
if(protocol == CURLPROTO_SCP && ssh_key_path != NULL) {
curl_easy_setopt(
easy_handle, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path);
}
/* Proxy options */ /* Proxy options */
if(socks_proxy_flag && http_proxy_flag) { if(socks_proxy_flag && http_proxy_flag) {
@ -201,8 +193,6 @@ main(int argc, char **argv)
curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag); curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag);
curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress); curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress);
/* File name */ /* File name */
/* TODO: make it iterate on args so you can upload multiple files /* TODO: make it iterate on args so you can upload multiple files
@ -214,7 +204,7 @@ main(int argc, char **argv)
if(protocol == CURLPROTO_HTTP) { if(protocol == CURLPROTO_HTTP) {
curl_mime *mime; curl_mime *mime;
mime = curl_mime_init(easy_handle); mime = curl_mime_init(easy_handle);
curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime); curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime);
if(!mime) { if(!mime) {
fprintf(stderr, "Error initializing curl_mime\n"); fprintf(stderr, "Error initializing curl_mime\n");
@ -241,23 +231,32 @@ main(int argc, char **argv)
} }
/* Process SCP uploads */ /* Process SCP uploads */
else if(protocol == CURLPROTO_SCP) { else if(protocol == CURLPROTO_SCP) {
curl_easy_setopt(
easy_handle, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path);
char path[256]; char path[256];
char *filename = argv[optind]; char *filename = argv[optind];
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, true); curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, true);
FILE *fp = fopen(filename, "r"); FILE *fp = fopen(filename, "r");
if(fp == NULL) {
fprintf(stderr, "%s",strerror(errno));
exit(-1);
}
struct stat st; struct stat st;
stat(argv[optind], &st); stat(argv[optind], &st);
snprintf(path, 256, "%s/%s", server, filename); snprintf(path, 256, "%s/%s", server, filename);
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp); curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
curl_easy_setopt( curl_easy_setopt(
easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size); easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size);
curl_easy_setopt(easy_handle, CURLOPT_URL, path); int ret = curl_easy_perform(easy_handle);
curl_easy_perform(easy_handle);
putchar('\n'); putchar('\n');
if(ret != 0) {
fprintf(stderr, "%i: %s\n", ret, curl_easy_strerror(ret));
}
} else { } else {
puts("Unsupported protocol"); puts("Unsupported protocol");
return -1; return -1;