diff --git a/bin/main b/bin/main index efe8b24..962ad28 100755 Binary files a/bin/main and b/bin/main differ diff --git a/src/main.c b/src/main.c index 40e7a01..b381f0d 100644 --- a/src/main.c +++ b/src/main.c @@ -21,46 +21,18 @@ void check_args(int argc, char *argv[]) { } scanned_path=realpath(_scanned_path, NULL); } -// ulong file_size(char *path) { -// struct stat file_stat; +// Helper to create child paths safely +char* make_child_path(const char* parent, const char* child) { + size_t parent_len = strlen(parent); + size_t child_len = strlen(child); + size_t total = parent_len + child_len + 2; // +2 for '/' and null term -// if (stat(path, &file_stat) == -1) { -// perror("stat failed"); -// return -1; // Return error -// } + char* path = malloc(total); + if (!path) return NULL; -// if (!S_ISREG(file_stat->st_mode)) { -// fputs("Not a regular file\n", stderr); -// return -1; // Not a regular file -// } - -// return file_stat->st_size; // Return size in bytes -// } -// bool is_dir(char *path) { -// struct stat file_stat; - -// if (stat(path, &file_stat) == -1) { -// perror("stat failed"); -// return false; // Return error -// } - -// return S_ISDIR(file_stat->st_mode); -// } -// ulong cluster_size(char *path) { -// struct stat file_stat; - -// if (stat(path, &file_stat) == -1) { -// perror("stat failed"); -// return -1; // Return error -// } - -// if (!S_ISREG(file_stat->st_mode)) { -// fputs("Not a regular file\n", stderr); -// return -1; // Not a regular file -// } - -// return file_stat->st_blocks * 512; -// } + snprintf(path, total, "%s/%s", parent, child); + return path; +} void ls(struct path_node *node, char *path) { // path if(node->path==NULL) node->path=path; @@ -100,36 +72,38 @@ void ls(struct path_node *node, char *path) { if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) continue; - // Print filename (no path, no metadata) - // - char *child_path=node->path; - strcat(child_path, "/"); - strcat(child_path, d->d_name); - - struct stat *file_stat=malloc(sizeof(struct stat)); - // bool is_dir; - if (stat(child_path, file_stat) == -1) { - perror("stat failed"); + // Create child path safely + char* child_path = make_child_path(node->path, d->d_name); + if (!child_path) { + perror("malloc failed"); + continue; } + struct stat file_stat; + if (lstat(child_path, &file_stat) == -1) { // Use lstat to avoid following symlinks + perror("stat failed"); + free(child_path); + continue; + } + + // Determine file type + const char* type = "OTHER"; + long path_size=0; - switch (file_stat->st_mode & S_IFMT) { - case S_IFREG: - path_size=file_stat->st_size; - printf("FILE\t%ld\t%s\n", path_size, child_path); - break; - case S_IFDIR: - path_size=file_stat->st_blocks*512; - printf("DIR\t%ld\t%s/\n", path_size, child_path); - break; - default: - printf("UNKNOWN\t%ld\t%s\n", path_size, child_path); - break; + + if (S_ISREG(file_stat.st_mode)) { + type = "FILE"; + path_size = file_stat.st_size; + } + else if (S_ISDIR(file_stat.st_mode)) { + type = "DIR"; + path_size = file_stat.st_blocks * 512; } + // Add more types (LNK, FIFO, etc) later? + + // store to struct + printf("%s\t%ld\t%s\n", type, path_size, d->d_name); free(child_path); - // free(file_stat); - // free(d); - printf("next...\n"); } } diff --git a/src/tmp1.c b/src/tmp1.c new file mode 100644 index 0000000..747bd38 --- /dev/null +++ b/src/tmp1.c @@ -0,0 +1,57 @@ +/* +// Helper to create child paths safely +char* make_child_path(const char* parent, const char* child) { + size_t parent_len = strlen(parent); + size_t child_len = strlen(child); + size_t total = parent_len + child_len + 2; // +2 for '/' and null term + + char* path = malloc(total); + if (!path) return NULL; + + snprintf(path, total, "%s/%s", parent, child); + return path; +} + +// Inside your directory processing loop: +d = (struct linux_dirent64*)(buf + bpos); +bpos += d->d_reclen; + +// Skip . and .. +if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) + continue; + +// Create child path safely +char* child_path = make_child_path(node->path, d->d_name); +if (!child_path) { + perror("malloc failed"); + continue; +} + +// Use stack variable for stat (no allocation) +struct stat file_stat; +if (lstat(child_path, &file_stat) == -1) { // Use lstat to avoid following symlinks + perror("stat failed"); + free(child_path); + continue; +} + +// Determine file type +const char* type = "OTHER"; +long path_size = 0; + +if (S_ISREG(file_stat.st_mode)) { + type = "FILE"; + path_size = file_stat.st_size; +} +else if (S_ISDIR(file_stat.st_mode)) { + type = "DIR"; + // For directory metadata size only (not contents!) + path_size = file_stat.st_blocks * 512; +} +// Add more types (LNK, FIFO, etc) as needed + +printf("%s\t%ld\t%s\n", type, path_size, d->d_name); + +// Cleanup +free(child_path); +*/ \ No newline at end of file diff --git a/src/tmp2.c b/src/tmp2.c new file mode 100644 index 0000000..68b3d11 --- /dev/null +++ b/src/tmp2.c @@ -0,0 +1,39 @@ +/* +d = (struct linux_dirent64 *)(buf + bpos); +bpos += d->d_reclen; + +// Skip . and .. entries +if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) + continue; + +// Print filename (no path, no metadata) +// +char *child_path=node->path; +strcat(child_path, "/"); +strcat(child_path, d->d_name); + +struct stat *file_stat=malloc(sizeof(struct stat)); +// bool is_dir; +if (stat(child_path, file_stat) == -1) { + perror("stat failed"); +} + +long path_size = 0; +switch (file_stat->st_mode & S_IFMT) { + case S_IFREG: + path_size=file_stat->st_size; + printf("FILE\t%ld\t%s\n", path_size, child_path); + break; + case S_IFDIR: + path_size=file_stat->st_blocks*512; + printf("DIR\t%ld\t%s/\n", path_size, child_path); + break; + default: + printf("UNKNOWN\t%ld\t%s\n", path_size, child_path); + break; +} +free(child_path); +// free(file_stat); +// free(d); +printf("next...\n"); +*/ \ No newline at end of file