From: Banana Date: Mon, 13 Apr 2020 22:01:54 +0000 (+0200) Subject: working on the c client X-Git-Tag: v1.0^2~6 X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=db6e1c0333080c0b1f36dc7deafcea58cd5b9ec1;p=selfpaste.git working on the c client --- 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 5bf5268..0000000 Binary files a/client/c-client/selfpaste and /dev/null differ 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