diff options
Diffstat (limited to 'name.c')
-rw-r--r-- | name.c | 67 |
1 files changed, 67 insertions, 0 deletions
@@ -0,0 +1,67 @@ +#include <err.h> +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <sys/select.h> + +#define MAXFILES 100 /* Maximum number of files. */ +#define MAXFILE 255 /* Maximum file name length. */ +#define MAXLINE MAXFILE+1 + +void waitstdin(void); + +int +main(int argc, char *argv[]) +{ + char file[30]; + char from[MAXLINE], *froms[MAXFILES], to[MAXLINE], *tos[MAXFILES]; + FILE *fp; + int i, j; + + /* Allocate memory. */ + for (i = 0; i < MAXFILES; i++) + if (!(froms[i]=malloc(MAXFILE)) || !(tos[i]=malloc(MAXFILE))) + err(1, "malloc"); + + sprintf(file, "/var/tmp/re.%d", getppid()); + + if (waitstdin(), access(file, F_OK)) { + fprintf(stderr, "%s: %s does not exist\n", argv[0], file); + return 1; + } + + if (!(fp = fopen(file, "r"))) + err(1, "fopen"); + + for (i = 0; fgets(from, MAXLINE, fp); i++) { + from[strcspn(from, "\n")] = 0; + strcpy(froms[i], from); + printf("from %s\n", from); + } + + for (j = 0; fgets(to, MAXLINE, stdin); j++) { + to[strcspn(to, "\n")] = 0; + strcpy(tos[j], to); + printf("to %s\n", to); + } + + if (i != j) { + fprintf(stderr, "%s: too %s lines\n", + argv[0], j > i ? "many" : "few"); + return 1; + } + + fclose(fp); +} + +void +waitstdin() +{ + fd_set readfds; + + FD_ZERO(&readfds); + FD_SET(0, &readfds); + if (select(1, &readfds, NULL, NULL, NULL) == -1) + err(1, "select"); +} |