From 77084d56e34887fe1b19f7ba9a9a5b2508af89e3 Mon Sep 17 00:00:00 2001 From: jozan Date: Fri, 6 Nov 2020 03:12:33 +0000 Subject: Must reparse options FossilOrigin-Name: 7a4169603246baa42e24eca59517253be703abdaf3eeac79829e4e8db0341506 --- src/c_opts.c | 30 ++++++++++++++++++++-------- src/c_unixize.c | 12 ++++++++++-- src/u_utils.c | 61 +++++++++++++++++++++++++++++---------------------------- src/u_utils.h | 1 + 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/src/c_opts.c b/src/c_opts.c index 2c73288..93bd5d8 100644 --- a/src/c_opts.c +++ b/src/c_opts.c @@ -45,6 +45,8 @@ * This is where we handle command line options. */ +#include + #include #include #include @@ -52,6 +54,7 @@ #include "c_opts.h" #include "c_unixize.h" +#include "u_utils.h" static void c_dump_usage(void) @@ -64,15 +67,17 @@ c_dump_usage(void) } static void -c_ask_confirm(const char dir[]) +c_prompt_confirm(void) { char c; + char path[MAXPATHLEN]; - if (strncmp(dir, ".", 2 * sizeof(char)) == 0) { + if (getcwd(path, MAXPATHLEN) == NULL) { + u_dump_errno(); dprintf(STDERR_FILENO, "unixize current directory? (y/n [n]) "); } else { - dprintf(STDERR_FILENO, "unixize %s? (y/n [n]) ", dir); + dprintf(STDERR_FILENO, "unixize %s? (y/n [n]) ", path); } scanf("%c", &c); if (c != 'y' && c != 'Y') { @@ -95,9 +100,13 @@ c_get_opts opts->pretend = FALSE; opts->recursive = FALSE; opts->verbose = FALSE; - while ((opt = getopt(argc, (char *const *)argv, C_OPTS)) != -1) { + while ( + argv[0][0] != 'r' && + (opt = getopt(argc, (char *const *)argv, C_OPTS)) != -1 + ) { if (opt == 'a') { opts->hidden = TRUE; + printf("hidden\n"); } else if (opt == 'h') { c_dump_usage(); @@ -105,35 +114,40 @@ c_get_opts } else if (opt == 'i') { opts->confirm = TRUE; + printf("confirm\n"); } else if (opt == 'n') { opts->hyphen = TRUE; + printf("hyphen\n"); } else if (opt == 'p') { opts->pretend = TRUE; + printf("pretend\n"); } else if (opt == 'R') { opts->recursive = TRUE; + printf("recursive\n"); } else if (opt == 'v') { opts->verbose = TRUE; + printf("verbose\n"); } else if (opt == '?') { c_dump_usage(); exit(1); } } - if (optind < argc && argv[optind] != NULL) { + if (argv[0][0] != 'r' && optind < argc && argv[optind] != NULL) { strncpy(opts->dir, argv[optind], PATH_MAX); if (opts->dir[strlen(opts->dir) - 1] == '/') { opts->dir[strlen(opts->dir) - 1] = 0x00; } } - else if (argv[optind] == NULL) { - strncpy(opts->dir, ".", 2 * sizeof(char)); + else if (argv[0][0] == 'r' || argv[optind] == NULL) { + strlcpy(opts->dir, ".", 2 * sizeof(char)); } if (opts->confirm == TRUE) { - c_ask_confirm(opts->dir); + c_prompt_confirm(); } if (opts->pretend == TRUE) { opts->verbose = TRUE; diff --git a/src/c_unixize.c b/src/c_unixize.c index 45ce8d5..c941102 100644 --- a/src/c_unixize.c +++ b/src/c_unixize.c @@ -101,15 +101,23 @@ main nargv = u_get_nargv(&opts); if (nargv != NULL) { nargc = 0; - while (argv[nargc] != NULL) { + while (argv[nargc + 1] != NULL) { nargc++; } main(nargc, (const char**)nargv); + u_del_nargv(nargv); } chdir("../"); } } - /* printf("'%s' -> '%s'\n", og_files->filename, new_files->filename); */ + if (opts.verbose == TRUE) { + dprintf( + STDOUT_FILENO, + "'%s' -> '%s'\n", + og_files->filename, + new_files->filename + ); + } /* rename(); */ og_files = og_files->next; new_files = new_files->next; diff --git a/src/u_utils.c b/src/u_utils.c index 4af6579..4c1a9ed 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -79,6 +79,19 @@ u_dump_errno_path(const char path[]) ); } +void +u_del_nargv(char** nargv) +{ + char** ptr; + + ptr = nargv; + while (*ptr != NULL) { + u_memdel((void*)&nargv[ptr - nargv]); + ptr++; + } + u_memdel((void*)&nargv); +} + static char u_get_extra_args(char args[], struct opts_s* opts) { @@ -112,19 +125,6 @@ u_get_extra_args(char args[], struct opts_s* opts) return (i); } -static void -u_del_nargv(char** nargv) -{ - char** ptr; - - ptr = nargv; - while (*ptr != NULL) { - u_memdel((void*)&nargv[ptr - nargv]); - ptr++; - } - u_memdel((void*)&nargv); -} - char** u_get_nargv(struct opts_s* opts) { @@ -134,12 +134,12 @@ u_get_nargv(struct opts_s* opts) char* tok; i = u_get_extra_args(args, opts) + 2; - nargv = (char**)malloc((i + 1) * sizeof(char*)); + nargv = (char**)malloc((i + 2) * sizeof(char*)); if (nargv == NULL) { u_dump_errno(); return (NULL); } - nargv[0] = strdup("unixize"); + nargv[0] = strdup("r"); nargv[1] = NULL; if (nargv[0] == NULL) { u_memdel((void*)&nargv); @@ -152,24 +152,25 @@ u_get_nargv(struct opts_s* opts) u_memdel((void*)&nargv); return (NULL); } - if (i > 2) { - tok = strtok(args, ":"); - nargv[3] = strdup(tok); - if (nargv[3] == NULL) { + if (i <= 3) { + return (nargv); + } + tok = strtok(args, ":"); + nargv[2] = strdup(tok); + if (nargv[2] == NULL) { + u_del_nargv(nargv); + return (NULL); + } + nargv[3] = NULL; + i = 3; + while ((tok = strtok(NULL, ":")) != NULL) { + nargv[i] = strdup(tok); + if (nargv[i] == NULL) { u_del_nargv(nargv); return (NULL); } - nargv[4] = NULL; - i = 4; - while ((tok = strtok(NULL, ":")) != NULL) { - nargv[i] = strdup(tok); - if (nargv[i] != NULL) { - u_del_nargv(nargv); - return (NULL); - } - nargv[i + 1] = NULL; - i++; - } + nargv[i + 1] = NULL; + i++; } return (nargv); } diff --git a/src/u_utils.h b/src/u_utils.h index 4c298d6..5ea7534 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -48,4 +48,5 @@ void u_memdel(void**); void u_dump_errno(void); void u_dump_errno_path(const char[]); +void u_del_nargv(char** nargv); char** u_get_nargv(struct opts_s*); -- cgit v1.2.3