Compare commits

...

3 commits

Author SHA1 Message Date
9e6e2457e2 fix indention
no more mixing tabs and spaces lol
2022-01-03 13:24:37 -05:00
8c09af80fb add option for bursting lines 2022-01-02 21:15:45 -05:00
08d65b254e fix file descriptor leak 2021-12-31 16:59:25 -05:00
2 changed files with 66 additions and 49 deletions

6
README.md Normal file
View file

@ -0,0 +1,6 @@
# multif
a multiplexer for distributing lines over a
directory of files

109
multif.c
View file

@ -8,61 +8,72 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc < 2) DIR* FD;
{ struct dirent* in_file;
fprintf(stderr, "Error : You must specify a directory\n"); FILE *entry_file;
return 1; char buffer[BUFSIZ];
} char *line = NULL;
DIR* FD; size_t len = 0;
struct dirent* in_file; ssize_t lineSize = 0;
FILE *entry_file; int burst = 1;
char buffer[BUFSIZ]; if (argc < 2)
char *line = NULL; {
size_t len = 0; fprintf(stderr, "Error : You must specify a directory\n");
ssize_t lineSize = 0; return 1;
}
if (argc >= 3)
burst = atoi(argv[2]);
if (burst <= 0)
{
fprintf(stderr, "Error : burst must be an integer greater than 0\n");
return 1;
}
char *directory = argv[1]; char *directory = argv[1];
strcat(directory, "/"); strcat(directory, "/");
/* Scanning the in directory */
//lineSize = getline(&line, &len, stdin); if (NULL == (FD = opendir (argv[1])))
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)); fprintf(stderr, "Error : Failed to open input directory - %s\n", strerror(errno));
return 1; return 1;
} }
while ((in_file = readdir(FD)))
{
char *line = NULL; while (lineSize != -1)
if (!strcmp (in_file->d_name, ".") || !strcmp (in_file->d_name, "..")) {
continue; while ((in_file = readdir(FD)))
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; 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);
int c;
for (c = 0; c < burst; c++) {
char *line = NULL;
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);
}
} }
rewinddir(FD);
lineSize = getline(&line, &len, stdin); }
if (lineSize == -1) return 0;
return 0;
fprintf(entry_file, line);
/* When you finish with the file, close it */
fclose(entry_file);
free(line);
}
}
return 0;
} }