diff --git a/src/main.c b/src/main.c index c10bef9..cce1a9c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,8 @@ #define _DEFAULT_SOURCE +#include #include #include +#include #include #include #include @@ -20,12 +22,14 @@ static const char *output_name = "out.cav"; * @dir: Opened directory * @pass_count: Number of passes made in file * @file_count: Number of files encountered including subdirectories + * @cur_off: Current offset tracker */ struct cav_state { int dir_fd; DIR *dir; uint8_t pass_count; size_t file_count; + uint32_t cur_off; }; static void @@ -53,6 +57,30 @@ version(void) ); } +/* + * Obtain file size from path + * + * @path: Path of file to obtain size from + * + * Returns number of bytes in file + */ +static size_t +get_file_size(const char *path) +{ + struct stat sb; + + if (path == NULL) { + return 0; + } + + if (stat(path, &sb) < 0) { + perror("stat"); + return 0; + } + + return sb.st_size; +} + /* * Initialize the cav state machine * @@ -133,7 +161,9 @@ consume_scan(const char *dir_path, struct cav_state *state, DIR *subdir) closedir(subdir); break; default: + snprintf(buf, sizeof(buf), "%s/%s", dir_path, dirent->d_name); ++state->file_count; + state->cur_off += get_file_size(buf); break; } } @@ -155,7 +185,11 @@ consume_pass(struct cav_state *state) case 0: /* Scan pass */ consume_scan(input_name, state, NULL); - printf("[pass 0] scanned %zu files\n", state->file_count); + printf( + "[pass 0] scanned %zu files [%d bytes]\n", + state->file_count, + state->cur_off + ); break; } }