Make code better

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

View File

@ -152,9 +152,7 @@ main(int argc, char **argv)
}
if(access(argv[optind], F_OK) && !paste_flag) {
fprintf(stderr, "Error opening file: %s\n",
strerror(errno)
);
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
return -1;
}
@ -166,12 +164,6 @@ main(int argc, char **argv)
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 */
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_PROGRESSFUNCTION, progress);
/* File name */
/* 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) {
curl_mime *mime;
mime = curl_mime_init(easy_handle);
curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime);
if(!mime) {
fprintf(stderr, "Error initializing curl_mime\n");
@ -241,23 +231,32 @@ main(int argc, char **argv)
}
/* Process SCP uploads */
else if(protocol == CURLPROTO_SCP) {
curl_easy_setopt(
easy_handle, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path);
char path[256];
char *filename = argv[optind];
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, true);
FILE *fp = fopen(filename, "r");
if(fp == NULL) {
fprintf(stderr, "%s",strerror(errno));
exit(-1);
}
struct stat st;
stat(argv[optind], &st);
snprintf(path, 256, "%s/%s", server, filename);
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
curl_easy_setopt(
easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size);
curl_easy_setopt(easy_handle, CURLOPT_URL, path);
curl_easy_perform(easy_handle);
int ret = curl_easy_perform(easy_handle);
putchar('\n');
if(ret != 0) {
fprintf(stderr, "%i: %s\n", ret, curl_easy_strerror(ret));
}
} else {
puts("Unsupported protocol");
return -1;