diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-06-20 14:07:46 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-06-20 14:07:46 +0200 |
| commit | 95e1552a898078b4dec1eae3fcf975b392cfad6d (patch) | |
| tree | d19e2277a7e94c4004290eddc84529c7e3f66b8f /src/main.c | |
| parent | e1f3547e236671697c66e27ba02b6a151e59af04 (diff) | |
| download | tar-95e1552a898078b4dec1eae3fcf975b392cfad6d.tar.gz tar-95e1552a898078b4dec1eae3fcf975b392cfad6d.tar.bz2 tar-95e1552a898078b4dec1eae3fcf975b392cfad6d.zip | |
Writting header and files
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 77 |
1 files changed, 68 insertions, 9 deletions
@@ -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; } |
