Skip to content
Snippets Groups Projects
Verified Commit ae925488 authored by Rafael László's avatar Rafael László :speech_balloon:
Browse files

Task2

parent ad5eaf24
No related branches found
No related tags found
No related merge requests found
......@@ -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[]) {
struct pollfd pfds[2] = {{.fd = open("/tmp/opened", O_RDONLY), .events = POLLIN},{.fd = 0, .events = POLLIN}};
int pipeout = open("/tmp/write", O_WRONLY);
while (1) {
char buf[10];
int ready;
ready = poll(pfds, 2, -1);
if (ready == -1) {
perror("poll");
return 1;
}
int main(int argc, char *argv[]) {
if(argc == 1) {
printf("Not enough parameters provided!");
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;
} else {
DIR * dir;
struct dirent * entry;
}
dir = opendir(argv[1]);
if (!dir) {
return 0;
write(1, buf, s);
} else {
break;
}
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);
} else {
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;
}
close(pfds[0].fd);
close(pipeout);
return 0;
}
\ No newline at end of file
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment