Compare commits

..

No commits in common. "main" and "96f9ded185bc6d4455c1514365c31f08f87ecb51" have entirely different histories.

2 changed files with 49 additions and 66 deletions

View file

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

109
multif.c
View file

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