diff options
author | John Ankarström <john@ankarstrom.se> | 2021-07-10 16:21:59 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-07-10 16:21:59 +0200 |
commit | e68a0b4707885be3c016f3b4f9443740e5dcc25b (patch) | |
tree | 63183a77fd00068efbc7bd2740fc89404a8b786c | |
parent | 25f9384a066b0afe0eaefc96c6e9360b81833ddd (diff) | |
download | re-name-e68a0b4707885be3c016f3b4f9443740e5dcc25b.tar.gz |
Add name
-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"); +} |