From db6e1c0333080c0b1f36dc7deafcea58cd5b9ec1 Mon Sep 17 00:00:00 2001 From: Banana Date: Tue, 14 Apr 2020 00:01:54 +0200 Subject: [PATCH] working on the c client --- client/c-client/.gitignore | 1 + client/c-client/Makefile | 5 ++ client/c-client/selfpaste | Bin 16608 -> 0 bytes client/c-client/selfpaste.c | 165 +++++++++++++++++++++++++++++++++++- 4 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 client/c-client/.gitignore create mode 100644 client/c-client/Makefile delete mode 100755 client/c-client/selfpaste diff --git a/client/c-client/.gitignore b/client/c-client/.gitignore new file mode 100644 index 0000000..969e400 --- /dev/null +++ b/client/c-client/.gitignore @@ -0,0 +1 @@ +selfpaste \ No newline at end of file diff --git a/client/c-client/Makefile b/client/c-client/Makefile new file mode 100644 index 0000000..d5320e7 --- /dev/null +++ b/client/c-client/Makefile @@ -0,0 +1,5 @@ +CC=gcc +CFLAGS=-O2 --machine-arch=native -pipe + +all: selfpaste.c + $(CC) $(CFLAGS) -o selfpaste selfpaste.c \ No newline at end of file diff --git a/client/c-client/selfpaste b/client/c-client/selfpaste deleted file mode 100755 index 5bf526897285f7540255321ab8521ec082c37954..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16608 zcmeHOU2Ggz6~4P0$4-;1oiqVA0X&2RB@nNjKi8pkv+Lh+WhW+e8WoyM*50*uVSlXM zS+D~VAx^8TT9-b6#0xLbhgPaksQSQ5AyGnABvg?HL?oy%B>|@e)rAtJK)swZbH4S= zc#RMeh=;k>?sw1cJ@?!@vv)lAeywk)KNtuol#qHzF*VX;bD|F^ni$vk?mG1XXcEEXQ4~TqnPWW?8{CTmL zWL$u(%Vp%2nc(_fjj5IRpv_i5*nO7lIL?2!AnnJcz18y4Eb#-SyQ(dd9_u*RmPxc` z(z()f>vU&FYsbMzF(27)^_%^(?!eI#R!BU*kXnv;3w@Z!@_!8d{tqu6dj0l}{_{^? z`egm^D;M*5`D^7X96Smld{Nvk1SPrS`F_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Myz|V)r?OHHJK|##!6{A<&{pT z2vezeF{L7jnH>4E(Ja^!dOJ}}=W{N?AXy=qiL-&!rZT3ASlx_JH8Pne(o9a%H%}*0 z%3G<9BvUehQVDVb$~LuUl5K%HwGhvy$Eo?e$(|`V9PSxytdYkAmX zy$IxW0~f>(J>VBf#?J19C;nhDzL|M$c;8)QrT@o^&9rUw@Bi&S5Z-Iw^0)k z06XAcNZ_>BqPC_c_K4GK5BhzourI`q^;x^vqaEl=IzXx=sxeR0t9;n6_WJ=pjMceY wwWjyla43mqKRl#}2lthA#6#>3b2ZXoQ}rI_XcxM?Os!>qvQz9m1|CxV6SqYZ@&Et; diff --git a/client/c-client/selfpaste.c b/client/c-client/selfpaste.c index fecf39f..10890a7 100644 --- a/client/c-client/selfpaste.c +++ b/client/c-client/selfpaste.c @@ -8,11 +8,168 @@ * 2019 - 2020 https://://www.bananas-playground.net/projekt/selfpaste */ +/* + * !WARNING! + * This is a very simple, with limited experience written, c program. + * Use at own risk and feel free to improve + */ + #include +#include +#include +#include +#include +#include +#include -int main() { - // https://www.programmingsimplified.com/c-program-examples - printf("Writing comments is very useful.\n"); +// https://www.gnu.org/software/libc/manual/html_node/Argp-Example-3.html#Argp-Example-3 +const char *argp_program_version = "1.0"; +const char *argp_program_bug_address = "https://://www.bananas-playground.net/projekt/selfpaste"; +static char doc[] = "selfpaste. Upload given file to your selfpaste installation."; +static char args_doc[] = "file"; - return(0); +// The options we understand. +static struct argp_option options[] = { + {"verbose",'v', 0, 0, "Produce verbose output" }, + {"quiet", 'q', 0, 0, "Don't produce any output" }, + {"output", 'o', "FILE", 0, "Output to FILE instead of standard output" }, + {"create-config-file", 'c', 0, 0, "Create default config file" }, + { 0 } +}; + +struct arguments { + char *args[1]; + int quiet, verbose, create_config_file; + char *output_file; +}; + +// Parse a single option. +static error_t +parse_opt (int key, char *arg, struct argp_state *state) { + //Get the input argument from argp_parse, which we know is a pointer to our arguments structure. + struct arguments *arguments = state->input; + + switch (key) + { + case 'q': + arguments->quiet = 1; + break; + case 'v': + arguments->verbose = 1; + break; + case 'o': + arguments->output_file = arg; + break; + case 'c': + arguments->create_config_file = 1; + break; + + case ARGP_KEY_ARG: + if (state->arg_num >= 1) + // Too many arguments. + argp_usage (state); + + arguments->args[state->arg_num] = arg; + break; + + case ARGP_KEY_END: + if (state->arg_num < 1 && arguments->create_config_file == 0) + // Not enough arguments. + argp_usage (state); + break; + + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +static struct argp argp = { options, parse_opt, args_doc, doc }; + +// some random string creation stuff +const char availableChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; +int intN(int n) { return rand() % n; } +char *randomString(int len) { + char *rstr = malloc((len + 1) * sizeof(char)); + int i; + for (i = 0; i < len; i++) { + rstr[i] = availableChars[intN(strlen(availableChars))]; + } + rstr[len] = '\0'; + return rstr; } + + + +int main(int argc, char *argv[]) { + struct arguments arguments; + srand(time(NULL)); + + // argument parsing and default values + arguments.quiet = 0; + arguments.verbose = 0; + arguments.output_file = "-"; + arguments.create_config_file = 0; + + argp_parse (&argp, argc, argv, 0, 0, &arguments); + + if(arguments.verbose) { + printf ("File = %s\nOutputfile = %s\n" + "Verbose = %s\nQuiet = %s\n", + arguments.args[0], + arguments.output_file, + arguments.verbose ? "yes" : "no", + arguments.quiet ? "yes" : "no", + arguments.create_config_file ? "yes" : "no" + ); + } + + // checking and finding config file + char* homedir = getenv("HOME"); + if ( homedir == NULL ) { + homedir = getpwuid(getuid())->pw_dir; + } + if(homedir[0] == '\0') { + printf("ERROR: $HOME directory not found?\n"); + return(1); + } + if(arguments.verbose) printf("Homedir: %s\n", homedir); + + char configFileName[15] = "/.selfpaste.cfg"; + if(arguments.verbose) printf("Config file name: '%s'\n", configFileName); + char configFilePath[strlen(homedir) + strlen(configFileName)]; + + strcpy(configFilePath, homedir); + strcat(configFilePath, configFileName); + + if(arguments.verbose) printf("Configfilepath: '%s'\n", configFilePath); + + if( access( configFilePath, F_OK ) != -1 ) { + if(arguments.verbose) printf("Using configfile: '%s'\n", configFilePath); + if(arguments.create_config_file == 1) { + printf("INFO: Re creating configfile by manually deleting it.\n"); + return(1); + } + } else { + printf("ERROR: Configfile '%s' not found.\n",configFilePath); + if(arguments.create_config_file == 1) { + if(arguments.verbose) printf("Creating configfile: '%s'\n", configFilePath); + + FILE *fp = fopen(configFilePath, "w"); + if (fp) { + fputs("# selfpaste config file.\n", fp); + fprintf(fp, "# See %s for more details.\n", argp_program_bug_address); + fprintf(fp, "# Version: %s\n", argp_program_version); + fprintf(fp, "SELFPASTE_SECRET=%s\n", randomString(50)); + fclose(fp); + if(arguments.verbose) printf("Config file '%s' created.\n", configFilePath); + } + else { + printf("ERROR: Configfile '%s' could not be written.\n",configFilePath); + return(1); + } + } + } + + return(0); +} \ No newline at end of file -- 2.39.5