diff --git a/main.c b/main.c index cf988033db75f01bb2e2489c19ebf3c360fd5803..0bf5f8360c581834648e4841e694d22a276f3f5e 100644 --- a/main.c +++ b/main.c @@ -3,63 +3,60 @@ #include <dirent.h> #include <string.h> #include <malloc.h> +#include <poll.h> +#include <fcntl.h> +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> -int count_files_in_dir(char* path) { - int file_count = 0; - DIR * dir; - struct dirent * entry; - - dir = opendir(path); - while ((entry = readdir(dir)) != NULL) { - if (strcmp(entry->d_name,".") == 0 || strcmp(entry->d_name,"..") == 0) { - continue; - } - file_count++; - } - closedir(dir); - return file_count; -} int main(int argc, char *argv[]) { - if(argc == 1) { - printf("Not enough parameters provided!"); - return 1; - } else { - DIR * dir; - struct dirent * entry; + struct pollfd pfds[2] = {{.fd = open("/tmp/opened", O_RDONLY), .events = POLLIN},{.fd = 0, .events = POLLIN}}; + int pipeout = open("/tmp/write", O_WRONLY); - dir = opendir(argv[1]); - if (!dir) { - return 0; + while (1) { + char buf[10]; + int ready; + + ready = poll(pfds, 2, -1); + if (ready == -1) { + perror("poll"); + return 1; } - while ((entry = readdir(dir)) != NULL) { - if (strcmp(entry->d_name,".") == 0 || strcmp(entry->d_name,"..") == 0) { - continue; - } - char * entry_path; // string concatenation - if((entry_path = malloc(strlen(argv[1]) + strlen(entry->d_name) + 2)) != NULL){ - entry_path[0] = '\0'; - strcat(entry_path, argv[1]); - strcat(entry_path, "/"); - strcat(entry_path, entry->d_name); + + if (pfds[0].revents != 0) { + if (pfds[0].revents & POLLIN) { + ssize_t s = read(pfds[0].fd, buf, sizeof(buf)); + if (s == -1){ + perror("read 0"); + return 1; + } + + write(1, buf, s); + } else { - return 1; + break; } + } + printf("%d\n",pfds[1].revents); + if (pfds[1].revents != 0) { + if (pfds[1].revents & POLLIN) { + ssize_t s = read(pfds[1].fd, buf, sizeof(buf)); + if (s == -1){ + perror("read 0"); + return 1; + } - if (entry->d_type == DT_REG) { - unlink(entry_path); - } else if (entry->d_type == DT_DIR && count_files_in_dir(entry_path) == 0) { - rmdir(entry_path); + write(pipeout, buf, s); + } else { + break; } - free(entry_path); } - closedir(dir); - if(count_files_in_dir(argv[1]) == 0) { - rmdir(argv[1]); - } else { - printf("Couldn't delete all files, because there was a non empty folder!\n"); - return 1; - } - return 0; } + + close(pfds[0].fd); + close(pipeout); + + return 0; } \ No newline at end of file