From e68a0b4707885be3c016f3b4f9443740e5dcc25b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 10 Jul 2021 16:21:59 +0200 Subject: Add name --- name.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 name.c diff --git a/name.c b/name.c new file mode 100644 index 0000000..a53d7e0 --- /dev/null +++ b/name.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include + +#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"); +} -- cgit v1.2.3