diff --git a/src/mcsfile/args.c b/src/mcsfile/args.c index cb46c14..f01d5d6 100644 --- a/src/mcsfile/args.c +++ b/src/mcsfile/args.c @@ -17,6 +17,8 @@ * along with p7utils; if not, see . * ************************************************************************** */ #include "main.h" +#include +#include #include /* --- @@ -26,7 +28,7 @@ /* Help message. */ static const char *help_start = -"Usage: mcsfile [--version|-v] [--help|-h] \n" +"Usage: mcsfile [--version|-v] [--help|-h] ...\n" "\n" "Reads mcs files in a g1m file.\n" "\n" @@ -127,23 +129,28 @@ static void put_version(void) * @return if execution should stop. */ -int parse_args(int ac, char **av, const char **path) +int parse_args(int ac, char **av, int *num, const char ***paths) { - /* getopt elements */ + int c, help = 0, version = 0, pc; + char **pv; const char *optstring = "hv"; const struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, - {} + {NULL, 0, NULL, 0} }; - /* get options */ - int c; opterr = 0; - int help = 0, version = 0; + /* Get options. */ + + opterr = 0; while ((c = getopt_long(ac, av, optstring, longopts, NULL)) != -1) switch (c) { - case 'h': help = 1; break; - case 'v': version = 1; break; + case 'h': + help = 1; + break; + case 'v': + version = 1; + break; default: switch (optopt) { default: fprintf(stderr, "-%c: unknown option.\n", optopt); @@ -151,15 +158,20 @@ int parse_args(int ac, char **av, const char **path) return (1); } - /* check parameters */ - int pc = ac - optind; - char **pv = &av[optind]; - if (pc != 1) - help = 1; - else - *path = *pv; + /* Check parameters. */ + + pc = ac - optind; + pv = &av[optind]; + + if (!pc) + help = 1; + else { + *num = pc; + *paths = (const char **)pv; + } + + /* Display version or help message. */ - /* display version or help message */ if (version) { put_version(); return (1); @@ -168,6 +180,5 @@ int parse_args(int ac, char **av, const char **path) return (1); } - /* no error */ return (0); } diff --git a/src/mcsfile/main.c b/src/mcsfile/main.c index 0978fd5..5a659f7 100644 --- a/src/mcsfile/main.c +++ b/src/mcsfile/main.c @@ -17,10 +17,16 @@ * along with p7utils; if not, see . * ************************************************************************** */ #include "main.h" +#include #include #include #include -#define cry(S, ...) fprintf(stderr, S "\n", ##__VA_ARGS__) + +struct entry { + int valid; + casio_file_t *handle; + const char *path; +}; /** * main: @@ -33,40 +39,86 @@ int main(int ac, char **av) { - const char *path; - casio_file_t *handle; - int err; + int num, i, err, fst; + const char **paths; + struct entry *entries, *ep; /* Set the locale and parse arguments. */ setlocale(LC_ALL, ""); - if (parse_args(ac, av, &path)) + if (parse_args(ac, av, &num, &paths)) return (0); - /* parse */ - if ((err = casio_open_file(&handle, path, casio_filetype_mcs))) - switch (err) { - case casio_error_wrong: - cry("An MCS file was expected (g1m/g1r, g1m/g2r, g3m)"); - return (1); - case casio_error_nostream: - cry("Could not open file: %s", strerror(errno)); - return (1); - case casio_error_magic: - cry("Magic error: file might be corrupted"); - return (1); - case casio_error_eof: - cry("Unexpected end of file"); - return (1); - default: - cry("Unknown error: %s", casio_strerror(err)); - return (1); + /* Prepare the list. */ + + if (!(entries = malloc(sizeof(struct entry) * num))) { + fprintf(stderr, "error: a memory allocation has failed\n"); + return (1); } - /* Read the files, free the handle and exit. */ + for (ep = entries, i = num; i; ep++, i--) { + ep->valid = 0; + ep->handle = NULL; + ep->path = paths[num - i]; - print_files(handle->casio_file_mcs); - casio_free_file(handle); + /* Decode the file. */ + + err = casio_open_file(&ep->handle, ep->path, casio_filetype_mcs); + + if (err) { + fprintf(stderr, "error: %s: ", ep->path); + switch (err) { + case casio_error_wrong: + fprintf(stderr, "an MCS file was expected " + "(g1m, g1r, g2m, g2r, g3m)\n"); + break; + + case casio_error_nostream: + fprintf(stderr, "could not open file: %s\n", strerror(errno)); + break; + + case casio_error_magic: + case casio_error_eof: + fprintf(stderr, "file might be corrupted\n"); + break; + + default: + fprintf(stderr, "%s\n", casio_strerror(err)); + break; + } + + continue; + } + + ep->valid = 1; + } + + /* List files in all of the given archives, and free the + * file handles while we're at it. */ + + fst = 1; + for (ep = entries, i = num; i; ep++, i--) { + if (!ep->valid) + continue; + + /* Print the header, with some spacing if not the first entry. */ + + if (fst) + fst = 0; + else + fputc('\n', stdout); + + if (num > 1) + fprintf(stdout, "%s:\n\n", ep->path); + + /* Print the entries in the archive. */ + + print_files(ep->handle->casio_file_mcs); + + /* Free the handle. */ + + casio_free_file(ep->handle); + } return (0); } diff --git a/src/mcsfile/main.h b/src/mcsfile/main.h index d810700..ad0413d 100644 --- a/src/mcsfile/main.h +++ b/src/mcsfile/main.h @@ -21,6 +21,7 @@ # include /* Some printf types */ + # ifdef _WIN64 # define PRIuSIZE "l64u" # elif _WIN32 @@ -29,8 +30,9 @@ # define PRIuSIZE "zu" # endif -/* Prototypes */ -int parse_args(int ac, char **av, const char **paths); +/* Prototypes. */ + +int parse_args(int ac, char **av, int *num, const char ***paths); void print_files(casio_mcs_t *handle); #endif /* MAIN_H */ diff --git a/src/mcsfile/vars.mk b/src/mcsfile/vars.mk index ce38114..41148b4 100755 --- a/src/mcsfile/vars.mk +++ b/src/mcsfile/vars.mk @@ -1,4 +1,4 @@ #!/usr/bin/make -f -disable: +#disable: libs: @echo libcasio diff --git a/src/p7/vars.mk b/src/p7/vars.mk index 372e49b..41148b4 100755 --- a/src/p7/vars.mk +++ b/src/p7/vars.mk @@ -1,3 +1,4 @@ #!/usr/bin/make -f +#disable: libs: @echo libcasio diff --git a/src/p7screen/vars.mk b/src/p7screen/vars.mk index c030138..1aea089 100755 --- a/src/p7screen/vars.mk +++ b/src/p7screen/vars.mk @@ -1,3 +1,4 @@ #!/usr/bin/make -f +#disable: libs: @echo libcasio sdl