diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-06-20 11:08:06 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-06-20 11:08:06 +0200 |
| commit | e1f3547e236671697c66e27ba02b6a151e59af04 (patch) | |
| tree | 342e3cfdc26697c7ef4c54346070dc646e3e1362 | |
| download | tar-e1f3547e236671697c66e27ba02b6a151e59af04.tar.gz tar-e1f3547e236671697c66e27ba02b6a151e59af04.tar.bz2 tar-e1f3547e236671697c66e27ba02b6a151e59af04.zip | |
Initial commit
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | Makefile | 44 | ||||
| -rw-r--r-- | inc/tar.h | 26 | ||||
| -rw-r--r-- | src/header.c | 3 | ||||
| -rw-r--r-- | src/main.c | 52 |
5 files changed, 130 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b90f99 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +tar +a.out +*.o +*.tar +*.gz diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..78da46f --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +RM = rm -f + +INCDIR = inc +SRCDIR = src +OBJDIR = obj +OBJDIRS = $(shell find $(SRCDIR) -type d | sed 's/src/$(OBJDIR)/') + +INC = $(shell find $(INCLUDEDIR) -name "*.h") +SRC = $(shell find $(SRCDIR) -name "*.c") +OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) + +CC = gcc +CCFLAGS = -g -I$(INCDIR) --std=c99 -Wall -Wextra -Wpedantic #-Werror +LDFLAGS = -L$(LIBFTDIR) -lft + +NAME = tar + +all: prebuild $(NAME) + +prebuild: + @echo "Making dir $$subdir"; mkdir -p $(OBJDIR) + @for subdir in $(OBJDIRS); do echo "Making dir $$subdir"; mkdir -p $$subdir; done + +$(NAME): $(OBJ) + @echo "Linking: $@" + @$(CC) -o $@ $(OBJ) $(LDFLAGS) + +$(OBJDIR)/%.o: $(SRCDIR)/%.c $(INC) + @echo "Compiling: $@" + @$(CC) $(CCFLAGS) -c -o $@ $< + +clean: + @echo "Removing objects" + @$(RM) -r $(OBJDIR) + +fclean: + @echo "Removing objects" + @$(RM) -r $(OBJDIR) + @echo "Removing exectable" + @$(RM) $(NAME) + +re: fclean all + +.PHONY: all prebuild clean fclean re diff --git a/inc/tar.h b/inc/tar.h new file mode 100644 index 0000000..29bea4b --- /dev/null +++ b/inc/tar.h @@ -0,0 +1,26 @@ +#ifndef TAR_H +# define TAR_H + +# include <stdio.h> +# include <stdlib.h> +# include <stdbool.h> +# include <unistd.h> +# include <getopt.h> +# include <fcntl.h> + +// https://en.wikipedia.org/wiki/Tar_(computing)?oldformat=true#Header +typedef struct +{ + char file_name[100]; + char file_mode[8]; + char user_id[8]; + char group_id[8]; + char file_size[12]; + char last_time[12]; + char checksum[8]; + char link_indicator[1]; + char link_file_name[100]; +} t_header; + + +#endif // TAR_H diff --git a/src/header.c b/src/header.c new file mode 100644 index 0000000..a0cf2fb --- /dev/null +++ b/src/header.c @@ -0,0 +1,3 @@ +#include "tar.h" + +/* int header_write(int fd, */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d6df9d7 --- /dev/null +++ b/src/main.c @@ -0,0 +1,52 @@ +#include "tar.h" + +int main(int argc, char **argv) +{ + int opt; + char *output_file_name = NULL; + + while ((opt = getopt(argc, argv, "cvtf:")) != -1) + { + switch (opt) + { + case 'c': + break; + case 'f': + output_file_name = optarg; + break; + case 'v': + break; + case 't': + break; + default: + return 1; + } + } + int fd = -1; + if (output_file_name == NULL) + fd = STDOUT_FILENO; + else + { + fd = open(output_file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd == -1) + { + perror(NULL); + return 1; + } + } + + // pipe to fd + printf("out: %s\n", output_file_name); + char **files = argv + optind; + for (; *files != NULL; files++) + { + // stat file + // write header to pipe + // if dir + // recursion on files in dir + // else + // write content to pipe + printf("%s\n", *files); + } + return 0; +} |
