writer.c (727B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 /* dispatch incoming lines to specified FIFOs according to first field */ 6 7 int main(int argc, char **argv) { 8 char *line = NULL; 9 int res; 10 int i; 11 size_t size; 12 struct FILE **descriptors; 13 descriptors = malloc((argc-1)*sizeof(FILE*)); 14 15 for (i=1; i<argc; i++) 16 descriptors[i-1] = fopen(argv[i], "w"); 17 18 while ((res = getline(&line, (size_t*) &size, stdin)) != -1) { 19 for (i=0; i<argc-1; i++) { 20 if (strncmp(argv[i+1], line, strlen(argv[i+1])) == 0 21 && line[strlen(argv[i+1])] == ' ') { 22 fprintf(descriptors[i], "%s", line + strlen(argv[i+1]) + 1); 23 fflush(descriptors[i]); 24 break; 25 } 26 } 27 } 28 29 return 0; 30 } 31