diff options
-rw-r--r-- | TODO.org | 8 | ||||
-rw-r--r-- | src/c_opts.c | 72 | ||||
-rw-r--r-- | src/c_opts.h | 7 | ||||
-rw-r--r-- | src/c_unixize.c | 4 | ||||
-rw-r--r-- | src/u_utils.c | 5 |
5 files changed, 76 insertions, 20 deletions
diff --git a/TODO.org b/TODO.org new file mode 100644 index 0000000..8c7c0d7 --- /dev/null +++ b/TODO.org @@ -0,0 +1,8 @@ +#+TITLE: TODO list for unixize + +* TODO remove malloc for recursive main [0/2] + - [ ] Remove them / go stack + - [ ] Check "= \0" int c_opts.c:c_recursive_parse + +* DONE scanf static-like issue + CLOSED: [2020-11-06 Fri 5:04] diff --git a/src/c_opts.c b/src/c_opts.c index 93bd5d8..e9548d4 100644 --- a/src/c_opts.c +++ b/src/c_opts.c @@ -66,10 +66,11 @@ c_dump_usage(void) ); } -static void +static bool_t c_prompt_confirm(void) { char c; + char first; char path[MAXPATHLEN]; if (getcwd(path, MAXPATHLEN) == NULL) { @@ -79,20 +80,58 @@ c_prompt_confirm(void) else { dprintf(STDERR_FILENO, "unixize %s? (y/n [n]) ", path); } - scanf("%c", &c); - if (c != 'y' && c != 'Y') { + c = getchar(); + first = c; + while (c != '\n' && c != EOF) { + c = getchar(); + } + if (first != 'y' && first != 'Y') { dprintf(STDERR_FILENO, "not unixized\n"); - exit(0); + return (FALSE); } + return (TRUE); } -void +static void +c_recursive_parse +(struct opts_s* opts, + const char* argv[]) +{ + char** ptr; + + ptr = (char**)argv; + ptr++; + while (*ptr != NULL) { + if ((*ptr)[1] == 'a') { + opts->hidden = TRUE; + } + else if ((*ptr)[1] == 'i') { + opts->confirm = TRUE; + } + else if ((*ptr)[1] == 'n') { + opts->hyphen = TRUE; + } + else if ((*ptr)[1] == 'p') { + opts->pretend = TRUE; + } + else if ((*ptr)[1] == 'R') { + opts->recursive = TRUE; + } + else if ((*ptr)[1] == 'v') { + opts->verbose = TRUE; + } + ptr++; + } +} + +bool_t c_get_opts (struct opts_s* opts, int argc, const char* argv[]) { int opt; + bool_t ret; opts->confirm = FALSE; opts->hidden = FALSE; @@ -101,12 +140,11 @@ c_get_opts opts->recursive = FALSE; opts->verbose = FALSE; while ( - argv[0][0] != 'r' && + argv[0][0] != C_RECURSIVE_CHAR && (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(); @@ -114,42 +152,46 @@ 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 (argv[0][0] != 'r' && optind < argc && argv[optind] != NULL) { + if ( + argv[0][0] != C_RECURSIVE_CHAR && + 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[0][0] == 'r' || argv[optind] == NULL) { + else if (argv[0][0] == C_RECURSIVE_CHAR || argv[optind] == NULL) { strlcpy(opts->dir, ".", 2 * sizeof(char)); } + if (argv[0][0] == C_RECURSIVE_CHAR) { + c_recursive_parse(opts, argv); + } + ret = TRUE; if (opts->confirm == TRUE) { - c_prompt_confirm(); + ret = c_prompt_confirm(); } if (opts->pretend == TRUE) { opts->verbose = TRUE; } + return (ret); } diff --git a/src/c_opts.h b/src/c_opts.h index b2c76ce..b7d734f 100644 --- a/src/c_opts.h +++ b/src/c_opts.h @@ -48,9 +48,10 @@ #include "c_unixize.h" -#define C_OPTS "ahinpRv" -#define C_USAGE_FMT "usage: unixize [-%s] [directory]\n" +#define C_OPTS "ahinpRv" +#define C_RECURSIVE_CHAR 'r' +#define C_USAGE_FMT "usage: unixize [-%s] [directory]\n" -void c_get_opts(struct opts_s*, int, const char*[]); +bool_t c_get_opts(struct opts_s*, int, const char*[]); #endif /* ifndef __C_OPTS_H__ */ diff --git a/src/c_unixize.c b/src/c_unixize.c index c941102..880f0a4 100644 --- a/src/c_unixize.c +++ b/src/c_unixize.c @@ -70,7 +70,9 @@ main int nargc; char** nargv; - c_get_opts(&opts, argc, argv); + if (c_get_opts(&opts, argc, argv) == FALSE) { + return (0); + } if (chdir((const char*)opts.dir) == -1) { u_dump_errno_path(opts.dir); return (1); diff --git a/src/u_utils.c b/src/u_utils.c index 4c1a9ed..b5f0b45 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -49,6 +49,7 @@ #include <string.h> #include <unistd.h> +#include "c_opts.h" #include "c_unixize.h" void @@ -139,12 +140,14 @@ u_get_nargv(struct opts_s* opts) u_dump_errno(); return (NULL); } - nargv[0] = strdup("r"); + nargv[0] = (char*)malloc(2 * sizeof(char)); nargv[1] = NULL; if (nargv[0] == NULL) { u_memdel((void*)&nargv); return (NULL); } + nargv[0][0] = C_RECURSIVE_CHAR; + nargv[0][1] = 0x00; nargv[1] = strdup("-R"); nargv[2] = NULL; if (nargv[1] == NULL) { |