diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-06-22 19:13:19 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-06-22 19:13:19 +0200 |
| commit | 88ab5a5273e13066ce3f496a690f10d20a278bb4 (patch) | |
| tree | d4587015207c4ed0d1872b14b737d0c42d881f33 /src | |
| parent | 86e8aeb5bb66ae5625002533b529752a7814e6c0 (diff) | |
| download | tar-88ab5a5273e13066ce3f496a690f10d20a278bb4.tar.gz tar-88ab5a5273e13066ce3f496a690f10d20a278bb4.tar.bz2 tar-88ab5a5273e13066ce3f496a690f10d20a278bb4.zip | |
Added better option handling
Diffstat (limited to 'src')
| -rw-r--r-- | src/archive.c | 77 | ||||
| -rw-r--r-- | src/args.c | 45 | ||||
| -rw-r--r-- | src/fs.c | 2 | ||||
| -rw-r--r-- | src/main.c | 46 |
4 files changed, 97 insertions, 73 deletions
diff --git a/src/archive.c b/src/archive.c index a3addd3..48fa55d 100644 --- a/src/archive.c +++ b/src/archive.c @@ -1,59 +1,28 @@ #include "tar.h" -int archive_write(char *archive_file_name, char **files) +int archive_create(int archive_fd, char **files) { - int fd; char file_name[PATH_MAX]; struct stat root_statbuf; - if (archive_file_name == NULL) - fd = STDOUT_FILENO; - else - { - fd = open(archive_file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd == -1) - { - perror("achive_write"); - return -1; - } - } for (; *files != NULL; files++) { bzero(file_name, PATH_MAX); strcpy(file_name, *files); - file_write(fd, file_name); + file_write(archive_fd, file_name); } if (stat("/", &root_statbuf) == -1) { perror("achive_write"); - close(fd); return -1; } - if (record_write_blank(fd, 2 + (2 * root_statbuf.st_blksize) / RECORD_SIZE) == -1) - { - close(fd); + if (record_write_blank(archive_fd, 2 + (2 * root_statbuf.st_blksize) / RECORD_SIZE) == -1) return -1; - } - if (fd != STDOUT_FILENO) - close(fd); return 0; } -int archive_read(char *archive_file_name) +int archive_extract(int archive_fd) { - int archive_fd; - - if (archive_file_name == NULL) - archive_fd = STDIN_FILENO; - else - { - if ((archive_fd = open(archive_file_name, O_RDONLY)) == -1) - { - perror("achive_write"); - return -1; - } - } - char record[RECORD_SIZE]; t_header header; int fd; @@ -80,7 +49,6 @@ int archive_read(char *archive_file_name) case '5': if (mkdir(header.file_name, statbuf.st_mode) == -1) { - close(archive_fd); perror(NULL); return -1; } @@ -106,6 +74,41 @@ int archive_read(char *archive_file_name) return -1; } } - close(archive_fd); + return 0; +} + +int archive_get_fd(t_args *args) +{ + int fd; + bool is_read; + + is_read = args->action == ACTION_DIFF || + args->action == ACTION_LIST || + args->action == ACTION_EXTRACT; + if (args->archive_name == NULL) + return is_read ? STDIN_FILENO : STDOUT_FILENO; + if (is_read) + fd = open(args->archive_name, O_RDONLY); + else + fd = open(args->archive_name, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_UMASK); + if (fd == -1) + { + perror("archive fd"); + return -1; + } + return fd; +} + +int archive_dispatch_action(int archive_fd, t_args *args) +{ + switch (args->action) + { + case ACTION_CREATE: + return archive_create(archive_fd, args->files); + case ACTION_EXTRACT: + return archive_extract(archive_fd); + default: + return -1; + } return 0; } diff --git a/src/args.c b/src/args.c new file mode 100644 index 0000000..1600e0e --- /dev/null +++ b/src/args.c @@ -0,0 +1,45 @@ +#include "tar.h" + +bool args_parse(int argc, char **argv, t_args *args) +{ + int opt; + int action_counter = 0; + + args->archive_name = NULL; + args->files = NULL; + while ((opt = getopt(argc, argv, "Acdtruxvf:")) != -1) + { + switch (opt) + { + case 'A': args->action = ACTION_CONCAT; break; + case 'c': args->action = ACTION_CREATE; break; + case 'd': args->action = ACTION_DIFF; break; + case 't': args->action = ACTION_LIST; break; + case 'r': args->action = ACTION_APPEND; break; + case 'u': args->action = ACTION_UPDATE; break; + case 'x': args->action = ACTION_EXTRACT; break; + + case 'v': args->flags |= FLAG_VERBOSE; break; + case 'f': + args->flags |= FLAG_FILE; + args->archive_name = optarg; + break; + default: + return false; + } + if (strchr("Acdtrux", opt) != NULL) + action_counter++; + } + if (action_counter != 1) + { + fprintf(stderr, "%s: You way not specify more than one '-Acdtrux' option\n", argv[0]); + return false; + } + if (!(args->flags & FLAG_FILE) && isatty(STDOUT_FILENO)) + { + fprintf(stderr, "%s: Refusing to write archive contents to terminal (missing -f option?)\n", argv[0]); + return false; + } + args->files = argv + optind; + return true; +} @@ -28,7 +28,7 @@ int file_write(int fd, char file_name[PATH_MAX]) int file_fd; struct stat statbuf; - fprintf(stderr, "|%s|\n", file_name); + /* fprintf(stderr, "|%s|\n", file_name); */ if (stat(file_name, &statbuf) == -1) { perror("file_write stat"); @@ -5,40 +5,16 @@ int main(int argc, char **argv) { - int opt; - char *archive_file_name = NULL; - bool create; + t_args args; + int archive_fd; + int ret; - while ((opt = getopt(argc, argv, "Oxcvtf:")) != -1) - { - switch (opt) - { - case 'c': - create = true; - break; - case 'x': - create = false; - break; - case 'f': - archive_file_name = optarg; - break; - case 'v': - break; - case 't': - break; - default: - return 1; - } - } - if (create) - { - if (archive_write(archive_file_name, argv + optind) == -1) - return 1; - } - else - { - if (archive_read(archive_file_name) == -1) - return 1; - } - return 0; + if (!args_parse(argc, argv, &args) || + (archive_fd = archive_get_fd(&args)) == -1) + return EXIT_FAILURE; + + ret = archive_dispatch_action(archive_fd, &args); + if (archive_fd != STDOUT_FILENO && archive_fd != STDIN_FILENO) + close(archive_fd); + return ret == -1 ? 1 : 0; } |
