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
120
src/main.c
120
src/main.c
|
@ -46,6 +46,12 @@ int main(int argc, const char **argv) {
|
||||||
|
|
||||||
simple_archiver_parse_args(argc, argv, &parsed);
|
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) {
|
if ((parsed.flags & 0x3) == 0 && (parsed.flags & 0x4) == 0) {
|
||||||
FILE *file = fopen(parsed.filename, "r");
|
FILE *file = fopen(parsed.filename, "r");
|
||||||
if (file != NULL) {
|
if (file != NULL) {
|
||||||
|
@ -68,64 +74,92 @@ int main(int argc, const char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((parsed.flags & 3) == 0) {
|
if ((parsed.flags & 3) == 0) {
|
||||||
FILE *file = fopen(parsed.filename, "wb");
|
// Is creating archive.
|
||||||
if (!file) {
|
|
||||||
fprintf(stderr, "ERROR: Failed to open \"%s\" for writing!\n",
|
|
||||||
parsed.filename);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
__attribute__((cleanup(simple_archiver_free_state)))
|
__attribute__((cleanup(simple_archiver_free_state)))
|
||||||
SDArchiverState *state = simple_archiver_init_state(&parsed);
|
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",
|
||||||
|
parsed.filename);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
int ret = simple_archiver_write_all(file, state, filenames);
|
int ret = simple_archiver_write_all(file, state, filenames);
|
||||||
if (ret != SDAS_SUCCESS) {
|
if (ret != SDAS_SUCCESS) {
|
||||||
fprintf(stderr, "Error during writing.\n");
|
fprintf(stderr, "Error during writing.\n");
|
||||||
char *error_str = simple_archiver_error_to_string(ret);
|
char *error_str = simple_archiver_error_to_string(ret);
|
||||||
fprintf(stderr, " %s\n", error_str);
|
fprintf(stderr, " %s\n", error_str);
|
||||||
}
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
|
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
|
||||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
|
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
|
||||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
|
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
|
||||||
if (ret != SDAS_SUCCESS) {
|
if (ret != SDAS_SUCCESS) {
|
||||||
unlink(parsed.filename);
|
unlink(parsed.filename);
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
#endif
|
#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) {
|
} else if ((parsed.flags & 3) == 2) {
|
||||||
FILE *file = fopen(parsed.filename, "rb");
|
// Is checking archive.
|
||||||
if (!file) {
|
if ((parsed.flags & 0x10) == 0) {
|
||||||
fprintf(stderr, "ERROR: Failed to open \"%s\" for reading!\n",
|
FILE *file = fopen(parsed.filename, "rb");
|
||||||
parsed.filename);
|
if (!file) {
|
||||||
return 4;
|
fprintf(stderr, "ERROR: Failed to open \"%s\" for reading!\n",
|
||||||
}
|
parsed.filename);
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
int ret = simple_archiver_parse_archive_info(file, 0, NULL);
|
int ret = simple_archiver_parse_archive_info(file, 0, NULL);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
fprintf(stderr, "Error during archive checking/examining.\n");
|
fprintf(stderr, "Error during archive checking/examining.\n");
|
||||||
char *error_str = simple_archiver_error_to_string(ret);
|
char *error_str = simple_archiver_error_to_string(ret);
|
||||||
fprintf(stderr, " %s\n", error_str);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fclose(file);
|
|
||||||
} else if ((parsed.flags & 3) == 1) {
|
} else if ((parsed.flags & 3) == 1) {
|
||||||
FILE *file = fopen(parsed.filename, "rb");
|
// Is extracting archive.
|
||||||
if (!file) {
|
|
||||||
fprintf(stderr, "ERROR: Failed to open \"%s\" for reading!\n",
|
|
||||||
parsed.filename);
|
|
||||||
return 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
__attribute__((cleanup(simple_archiver_free_state)))
|
__attribute__((cleanup(simple_archiver_free_state)))
|
||||||
SDArchiverState *state = simple_archiver_init_state(&parsed);
|
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",
|
||||||
|
parsed.filename);
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
int ret = simple_archiver_parse_archive_info(file, 1, state);
|
int ret = simple_archiver_parse_archive_info(file, 1, state);
|
||||||
if (ret != SDAS_SUCCESS) {
|
if (ret != SDAS_SUCCESS) {
|
||||||
fprintf(stderr, "Error during archive extracting.\n");
|
fprintf(stderr, "Error during archive extracting.\n");
|
||||||
char *error_str = simple_archiver_error_to_string(ret);
|
char *error_str = simple_archiver_error_to_string(ret);
|
||||||
fprintf(stderr, " %s\n", error_str);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fclose(file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
18
src/parser.c
18
src/parser.c
|
@ -139,6 +139,9 @@ void simple_archiver_print_usage(void) {
|
||||||
fprintf(stderr, "-x : extract archive file\n");
|
fprintf(stderr, "-x : extract archive file\n");
|
||||||
fprintf(stderr, "-t : examine archive file\n");
|
fprintf(stderr, "-t : examine archive file\n");
|
||||||
fprintf(stderr, "-f <filename> : filename to work on\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,
|
fprintf(stderr,
|
||||||
"--compressor <full_compress_cmd> : requires --decompressor\n");
|
"--compressor <full_compress_cmd> : requires --decompressor\n");
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
|
@ -210,9 +213,18 @@ int simple_archiver_parse_args(int argc, const char **argv,
|
||||||
// set second bit.
|
// set second bit.
|
||||||
out->flags |= 0x2;
|
out->flags |= 0x2;
|
||||||
} else if (strcmp(argv[0], "-f") == 0 && argc > 1) {
|
} else if (strcmp(argv[0], "-f") == 0 && argc > 1) {
|
||||||
int size = strlen(argv[1]) + 1;
|
if (strcmp(argv[1], "-") == 0) {
|
||||||
out->filename = malloc(size);
|
out->flags |= 0x10;
|
||||||
strncpy(out->filename, argv[1], size);
|
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;
|
--argc;
|
||||||
++argv;
|
++argv;
|
||||||
} else if (strcmp(argv[0], "--compressor") == 0 && argc > 1) {
|
} 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 x0xx - Do NOT allow create archive overwrite.
|
||||||
/// 0b xxxx x1xx - Allow create archive overwrite.
|
/// 0b xxxx x1xx - Allow create archive overwrite.
|
||||||
/// 0b xxxx 1xxx - Allow extract overwrite.
|
/// 0b xxxx 1xxx - Allow extract overwrite.
|
||||||
|
/// 0b xxx1 xxxx - Create archive to stdout or read archive from stdin.
|
||||||
unsigned int flags;
|
unsigned int flags;
|
||||||
/// Null-terminated string.
|
/// Null-terminated string.
|
||||||
char *filename;
|
char *filename;
|
||||||
|
|
Loading…
Reference in a new issue