aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-20 14:07:46 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-20 14:07:46 +0200
commit95e1552a898078b4dec1eae3fcf975b392cfad6d (patch)
treed19e2277a7e94c4004290eddc84529c7e3f66b8f
parente1f3547e236671697c66e27ba02b6a151e59af04 (diff)
downloadtar-95e1552a898078b4dec1eae3fcf975b392cfad6d.tar.gz
tar-95e1552a898078b4dec1eae3fcf975b392cfad6d.tar.bz2
tar-95e1552a898078b4dec1eae3fcf975b392cfad6d.zip
Writting header and files
-rw-r--r--Makefile4
-rw-r--r--inc/tar.h30
-rw-r--r--src/main.c77
3 files changed, 96 insertions, 15 deletions
diff --git a/Makefile b/Makefile
index 78da46f..defed2a 100644
--- a/Makefile
+++ b/Makefile
@@ -18,8 +18,8 @@ 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
+ @mkdir -vp $(OBJDIR)
+ @for subdir in $(OBJDIRS); do mkdir -vp $$subdir; done
$(NAME): $(OBJ)
@echo "Linking: $@"
diff --git a/inc/tar.h b/inc/tar.h
index 29bea4b..8663926 100644
--- a/inc/tar.h
+++ b/inc/tar.h
@@ -1,12 +1,27 @@
#ifndef TAR_H
# define TAR_H
-# include <stdio.h>
-# include <stdlib.h>
-# include <stdbool.h>
+# include <sys/types.h>
+# include <sys/stat.h>
+
+# ifndef S_IFDIR
+# define S_IFDIR __S_IFDIR
+# endif
+# ifndef S_IFMT
+# define S_IFMT __S_IFMT
+# endif
+
# include <unistd.h>
+# include <stdbool.h>
# include <getopt.h>
# include <fcntl.h>
+# include <stdio.h>
+# include <stdlib.h>
+# include <strings.h>
+# include <string.h>
+
+
+// # define FILE_NAME_MAX 100
// https://en.wikipedia.org/wiki/Tar_(computing)?oldformat=true#Header
typedef struct
@@ -18,8 +33,15 @@ typedef struct
char file_size[12];
char last_time[12];
char checksum[8];
- char link_indicator[1];
+ char file_type[1];
char link_file_name[100];
+ char ustar[6];
+ char ustar_version[2];
+ char user_name[32];
+ char group_name[32];
+ char device_major_number[8];
+ char device_minor_number[8];
+ char file_name_prefix[155];
} t_header;
diff --git a/src/main.c b/src/main.c
index d6df9d7..1161278 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,65 @@
#include "tar.h"
+// only first hard link
+// checksum positive and negative bytes
+
+int write_file(int fd, char *file_name)
+{
+ struct stat statbuf;
+ t_header header;
+
+ stat(file_name, &statbuf);
+ bzero(&header, sizeof(t_header));
+ strncpy(header.file_name, file_name, sizeof(header.file_name));
+ sprintf(header.file_mode, "%07o", statbuf.st_mode & 0777);
+ sprintf(header.user_id, "%07o", statbuf.st_uid);
+ sprintf(header.group_id, "%07o", statbuf.st_gid);
+ sprintf(header.file_size, "%011lo", statbuf.st_size);
+ sprintf(header.last_time, "%011lo", statbuf.st_mtime);
+ memset(header.checksum, ' ', sizeof(header.checksum));
+ header.file_type[0] = '0';
+
+ /* strncpy(header.ustar, "ustar", sizeof(header.ustar)); */
+ char *header_ptr = (char*)&header;
+ unsigned int sum = 0;
+ for (size_t i = 0; i < sizeof(t_header); i++)
+ sum += header_ptr[i];
+ sprintf(header.checksum, "%06o", sum);
+
+ char buf[512] = {0};
+ memcpy(buf, &header, sizeof(t_header));
+ write(fd, buf, 512);
+
+
+ int from;
+ char *content;
+ switch (statbuf.st_mode & S_IFMT)
+ {
+ case S_IFDIR: break;
+ default:
+ from = open(file_name, O_RDONLY);
+ content = calloc(statbuf.st_size + 1, sizeof(char));
+ read(from, content, statbuf.st_size);
+ write(fd, content, statbuf.st_size);
+ free(content);
+ close(from);
+ memset(buf, '\0', 512);
+ write(fd, buf, 512 - statbuf.st_size % 512);
+ }
+
+ // if dir
+ // write_dir
+ // else
+ // write content
+ return (0);
+}
+
+int write_directory(int fd, char *dirname)
+{
+ // for f in files
+ // write_file
+}
+
int main(int argc, char **argv)
{
int opt;
@@ -35,18 +95,17 @@ int main(int argc, char **argv)
}
}
- // pipe to fd
- printf("out: %s\n", output_file_name);
+ /* 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);
+ /* printf("%s\n", *files); */
+ write_file(fd, *files);
}
+ char buf[512] = {'\0'};
+ write(fd, buf, 512);
+ write(fd, buf, 512);
+ if (fd != STDOUT_FILENO)
+ close(fd);
return 0;
}