feat: implemented getopt_long

This commit is contained in:
Renge 2022-03-04 22:22:08 -05:00
parent c63ef04b44
commit e917cbc49e

View File

@ -362,7 +362,7 @@ static int setValue(int *val, char *arg, char *name)
return 1; return 1;
} }
static int setOptions(int argc, char **argv, int *widthbak, int *prefixbak, int *suffixbak, int * hangbak, int *lastbak, int *minbak) static int setOptions(int argc, char **argv, int *widthbak, int *prefixbak, int *suffixbak, int *hangbak, int *lastbak, int *minbak)
{ {
FILE *stream; FILE *stream;
char *buf; char *buf;
@ -472,75 +472,91 @@ static int setOptions(int argc, char **argv, int *widthbak, int *prefixbak, int
int original_main(int argc, const char *const *argv) int original_main(int argc, const char *const *argv)
{ {
int width, widthbak = -1, prefix, prefixbak = -1, suffix, suffixbak = -1, int width, widthbak = -1, prefix, prefixbak = -1, suffix, suffixbak = -1,
hang, hangbak = -1, last, lastbak = -1, min, minbak = -1, c, argc_env; hang, hangbak = -1, last, lastbak = -1, min, minbak = -1, c, argc_env, option_code;
char *parinit, *picopy = NULL, *opt, **inlines = NULL, **outlines = NULL, char *parinit, *picopy = NULL, *opt, **inlines = NULL, **outlines = NULL,
**line, **argv_env, *tmp; **line, **argv_env = NULL, *tmp;
const char *const whitechars = " \f\n\r\t\v"; const char *const whitechars = " \f\n\r\t\v";
// parinit = getenv("PARINIT"); // parinit = getenv("PARINIT");
// if (parinit) { // if (parinit)
// picopy = malloc((strlen(parinit) + 1) * sizeof (char)); // {
// if (!picopy) { // picopy = malloc((strlen(parinit) + 1) * sizeof(char));
// if (!picopy)
// {
// set_error(outofmem); // set_error(outofmem);
// goto parcleanup; // goto parcleanup;
// } // }
// strcpy(picopy,parinit); // strcpy(picopy, parinit);
// opt = strtok(picopy,whitechars); // opt = strtok(picopy, whitechars);
// while (opt) { // while (opt)
// {
// parseopt(opt, &widthbak, &prefixbak, // parseopt(opt, &widthbak, &prefixbak,
// &suffixbak, &hangbak, &lastbak, &minbak); // &suffixbak, &hangbak, &lastbak, &minbak);
// if (is_error()) goto parcleanup; // if (is_error())
// opt = strtok(NULL,whitechars); // goto parcleanup;
// opt = strtok(NULL, whitechars);
// } // }
// free(picopy); // free(picopy);
// picopy = NULL; // picopy = NULL;
// } // }
// while (*++argv) { // while (*++argv)
// {
// parseopt(*argv, &widthbak, &prefixbak, // parseopt(*argv, &widthbak, &prefixbak,
// &suffixbak, &hangbak, &lastbak, &minbak); // &suffixbak, &hangbak, &lastbak, &minbak);
// if (is_error()) goto parcleanup; // if (is_error())
// goto parcleanup;
// } // }
// parinit = getenv("PARINIT"); parinit = getenv("PARINIT");
// if (parinit) if (parinit)
// { {
// picopy = malloc((strlen(parinit) + 1) * sizeof (char)); picopy = malloc((strlen(parinit) + 1) * sizeof(char));
// if (!picopy) { if (!picopy)
// set_error(outofmem); {
// goto parcleanup; set_error(outofmem);
// } goto parcleanup;
// argc_env = 1; }
// argv_env = malloc((argc_env) * sizeof (char*)); argc_env = 1;
// argv_env[0] = malloc((strlen(argv[0])+1) * sizeof(char)); argv_env = malloc((argc_env) * sizeof(char *));
// tmp = argv_env[0]; argv_env[0] = malloc((strlen(argv[0]) + 1) * sizeof(char));
// strcpy(tmp, argv[0]); tmp = argv_env[0];
// strcpy(picopy,parinit); strcpy(tmp, argv[0]);
// opt = strtok(picopy,whitechars); strcpy(picopy, parinit);
// while (opt) { opt = strtok(picopy, whitechars);
// argc_env ++; while (opt)
// argv_env = realloc(argv_env, (argc_env + 1)*sizeof(char*)); {
// argv_env[argc_env-1] = malloc((strlen(opt)+1)*sizeof(char)); argc_env++;
// tmp = argv_env[argc_env-1]; argv_env = realloc(argv_env, (argc_env + 1) * sizeof(char *));
// strcpy(tmp, opt); argv_env[argc_env - 1] = malloc((strlen(opt) + 1) * sizeof(char));
// opt = strtok(NULL,whitechars); tmp = argv_env[argc_env - 1];
// } strcpy(tmp, opt);
// argv_env = realloc(argv_env, (argc_env + 1)*sizeof(char*)); opt = strtok(NULL, whitechars);
// argv_env[argc_env] = NULL; }
// // for (size_t i = 0; i < argc_env; i++) for (size_t i = 1; i < argc; i++)
// // { {
// // printf("%s\n", argv_env[i]); argc_env++;
// // } argv_env = realloc(argv_env, (argc_env + 1) * sizeof(char *));
argv_env[argc_env - 1] = malloc((strlen(argv[i]) + 1) * sizeof(char));
tmp = argv_env[argc_env - 1];
strcpy(tmp, argv[i]);
}
argv_env = realloc(argv_env, (argc_env + 1) * sizeof(char *));
argv_env[argc_env] = NULL;
option_code = setOptions(argc_env, (char **)argv_env, &widthbak, &prefixbak, &suffixbak, &hangbak, &lastbak, &minbak);
// if (!setOptions(argc_env, (char **)argv_env, &widthbak, &prefixbak, &suffixbak, &hangbak, &lastbak, &minbak)) if (option_code == 0 || option_code == 2)
// goto parcleanup; goto parcleanup;
// freelines(argv_env); freelines(argv_env);
// } argv_env = NULL;
int option_code = setOptions(argc, (char **)argv, &widthbak, &prefixbak, &suffixbak, &hangbak, &lastbak, &minbak); }
if (option_code == 0 || option_code==2) else
goto parcleanup; {
option_code = setOptions(argc, (char **)argv, &widthbak, &prefixbak, &suffixbak, &hangbak, &lastbak, &minbak);
if (option_code == 0 || option_code == 2)
goto parcleanup;
}
// printf("width: %d, prefix: %d, suffix: %d, hang: %d, last: %d, min: %d", widthbak, prefixbak, suffixbak, hangbak, lastbak, minbak); // printf("width: %d, prefix: %d, suffix: %d, hang: %d, last: %d, min: %d", widthbak, prefixbak, suffixbak, hangbak, lastbak, minbak);
@ -599,6 +615,8 @@ parcleanup:
freelines(inlines); freelines(inlines);
if (outlines) if (outlines)
freelines(outlines); freelines(outlines);
if (argv_env)
freelines(argv_env);
if (is_error()) if (is_error())
{ {
@ -608,7 +626,7 @@ parcleanup:
{ {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }