Support "-f -" to read/write archive stdin/stdout
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 6s
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 6s
This commit is contained in:
parent
613354034d
commit
dcff34e06b
3 changed files with 93 additions and 46 deletions
46
src/main.c
46
src/main.c
|
@ -46,6 +46,12 @@ int main(int argc, const char **argv) {
|
|||
|
||||
simple_archiver_parse_args(argc, argv, &parsed);
|
||||
|
||||
if (!parsed.filename && (parsed.flags & 0x10) == 0) {
|
||||
fprintf(stderr, "ERROR: Filename not specified!\n");
|
||||
simple_archiver_print_usage();
|
||||
return 6;
|
||||
}
|
||||
|
||||
if ((parsed.flags & 0x3) == 0 && (parsed.flags & 0x4) == 0) {
|
||||
FILE *file = fopen(parsed.filename, "r");
|
||||
if (file != NULL) {
|
||||
|
@ -68,6 +74,10 @@ int main(int argc, const char **argv) {
|
|||
}
|
||||
|
||||
if ((parsed.flags & 3) == 0) {
|
||||
// Is creating archive.
|
||||
__attribute__((cleanup(simple_archiver_free_state)))
|
||||
SDArchiverState *state = simple_archiver_init_state(&parsed);
|
||||
if ((parsed.flags & 0x10) == 0) {
|
||||
FILE *file = fopen(parsed.filename, "wb");
|
||||
if (!file) {
|
||||
fprintf(stderr, "ERROR: Failed to open \"%s\" for writing!\n",
|
||||
|
@ -75,9 +85,6 @@ int main(int argc, const char **argv) {
|
|||
return 2;
|
||||
}
|
||||
|
||||
__attribute__((cleanup(simple_archiver_free_state)))
|
||||
SDArchiverState *state = simple_archiver_init_state(&parsed);
|
||||
|
||||
int ret = simple_archiver_write_all(file, state, filenames);
|
||||
if (ret != SDAS_SUCCESS) {
|
||||
fprintf(stderr, "Error during writing.\n");
|
||||
|
@ -93,7 +100,17 @@ int main(int argc, const char **argv) {
|
|||
return 3;
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
int ret = simple_archiver_write_all(stdout, state, filenames);
|
||||
if (ret != SDAS_SUCCESS) {
|
||||
fprintf(stderr, "Error during writing.\n");
|
||||
char *error_str = simple_archiver_error_to_string(ret);
|
||||
fprintf(stderr, " %s\n", error_str);
|
||||
}
|
||||
}
|
||||
} else if ((parsed.flags & 3) == 2) {
|
||||
// Is checking archive.
|
||||
if ((parsed.flags & 0x10) == 0) {
|
||||
FILE *file = fopen(parsed.filename, "rb");
|
||||
if (!file) {
|
||||
fprintf(stderr, "ERROR: Failed to open \"%s\" for reading!\n",
|
||||
|
@ -108,7 +125,19 @@ int main(int argc, const char **argv) {
|
|||
fprintf(stderr, " %s\n", error_str);
|
||||
}
|
||||
fclose(file);
|
||||
} else {
|
||||
int ret = simple_archiver_parse_archive_info(stdin, 0, NULL);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Error during archive checking/examining.\n");
|
||||
char *error_str = simple_archiver_error_to_string(ret);
|
||||
fprintf(stderr, " %s\n", error_str);
|
||||
}
|
||||
}
|
||||
} else if ((parsed.flags & 3) == 1) {
|
||||
// Is extracting archive.
|
||||
__attribute__((cleanup(simple_archiver_free_state)))
|
||||
SDArchiverState *state = simple_archiver_init_state(&parsed);
|
||||
if ((parsed.flags & 0x10) == 0) {
|
||||
FILE *file = fopen(parsed.filename, "rb");
|
||||
if (!file) {
|
||||
fprintf(stderr, "ERROR: Failed to open \"%s\" for reading!\n",
|
||||
|
@ -116,9 +145,6 @@ int main(int argc, const char **argv) {
|
|||
return 5;
|
||||
}
|
||||
|
||||
__attribute__((cleanup(simple_archiver_free_state)))
|
||||
SDArchiverState *state = simple_archiver_init_state(&parsed);
|
||||
|
||||
int ret = simple_archiver_parse_archive_info(file, 1, state);
|
||||
if (ret != SDAS_SUCCESS) {
|
||||
fprintf(stderr, "Error during archive extracting.\n");
|
||||
|
@ -126,6 +152,14 @@ int main(int argc, const char **argv) {
|
|||
fprintf(stderr, " %s\n", error_str);
|
||||
}
|
||||
fclose(file);
|
||||
} else {
|
||||
int ret = simple_archiver_parse_archive_info(stdin, 1, state);
|
||||
if (ret != SDAS_SUCCESS) {
|
||||
fprintf(stderr, "Error during archive extracting.\n");
|
||||
char *error_str = simple_archiver_error_to_string(ret);
|
||||
fprintf(stderr, " %s\n", error_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
12
src/parser.c
12
src/parser.c
|
@ -139,6 +139,9 @@ void simple_archiver_print_usage(void) {
|
|||
fprintf(stderr, "-x : extract archive file\n");
|
||||
fprintf(stderr, "-t : examine archive file\n");
|
||||
fprintf(stderr, "-f <filename> : filename to work on\n");
|
||||
fprintf(stderr,
|
||||
" Use \"-f -\" to work on stdout when creating archive or stdin "
|
||||
"when reading archive\n");
|
||||
fprintf(stderr,
|
||||
"--compressor <full_compress_cmd> : requires --decompressor\n");
|
||||
fprintf(stderr,
|
||||
|
@ -210,9 +213,18 @@ int simple_archiver_parse_args(int argc, const char **argv,
|
|||
// set second bit.
|
||||
out->flags |= 0x2;
|
||||
} else if (strcmp(argv[0], "-f") == 0 && argc > 1) {
|
||||
if (strcmp(argv[1], "-") == 0) {
|
||||
out->flags |= 0x10;
|
||||
if (out->filename) {
|
||||
free(out->filename);
|
||||
}
|
||||
out->filename = NULL;
|
||||
} else {
|
||||
out->flags &= 0xFFFFFFEF;
|
||||
int size = strlen(argv[1]) + 1;
|
||||
out->filename = malloc(size);
|
||||
strncpy(out->filename, argv[1], size);
|
||||
}
|
||||
--argc;
|
||||
++argv;
|
||||
} else if (strcmp(argv[0], "--compressor") == 0 && argc > 1) {
|
||||
|
|
|
@ -29,6 +29,7 @@ typedef struct SDArchiverParsed {
|
|||
/// 0b xxxx x0xx - Do NOT allow create archive overwrite.
|
||||
/// 0b xxxx x1xx - Allow create archive overwrite.
|
||||
/// 0b xxxx 1xxx - Allow extract overwrite.
|
||||
/// 0b xxx1 xxxx - Create archive to stdout or read archive from stdin.
|
||||
unsigned int flags;
|
||||
/// Null-terminated string.
|
||||
char *filename;
|
||||
|
|
Loading…
Reference in a new issue