From 3906f08d7acd25eab42361c0d7a52ea9c23a8cb3 Mon Sep 17 00:00:00 2001 From: xfnw Date: Fri, 31 Dec 2021 15:51:42 -0500 Subject: [PATCH] stdin stuff --- multif.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 multif.c diff --git a/multif.c b/multif.c new file mode 100644 index 0000000..42ac1f3 --- /dev/null +++ b/multif.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc < 2) + { + fprintf(stderr, "Error : You must specify a directory\n"); + return 1; + } + DIR* FD; + struct dirent* in_file; + FILE *entry_file; + char buffer[BUFSIZ]; + char *line = NULL; + size_t len = 0; + ssize_t lineSize = 0; + + char directory[4096]; + strcpy(directory, strdup(argv[1])); + strcat(directory, "/"); + + //lineSize = getline(&line, &len, stdin); + while (lineSize != -1) + { + /* Scanning the in directory */ + if (NULL == (FD = opendir (argv[1]))) + { + fprintf(stderr, "Error : Failed to open input directory - %s\n", strerror(errno)); + + return 1; + } + while ((in_file = readdir(FD))) + { + + char *line = NULL; + if (!strcmp (in_file->d_name, ".") || !strcmp (in_file->d_name, "..")) + continue; + + char entry_path[4096]; + strcpy(entry_path,directory); + strcat(entry_path,in_file->d_name); + + entry_file = fopen(entry_path, "a"); + if (entry_file == NULL) + { + fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno)); + + return 1; + } + + lineSize = getline(&line, &len, stdin); + if (lineSize == -1) + return 0; + + fprintf(entry_file, line); + + /* When you finish with the file, close it */ + fclose(entry_file); + free(line); + } + } + return 0; +}