aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-10 16:21:59 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-10 16:21:59 +0200
commite68a0b4707885be3c016f3b4f9443740e5dcc25b (patch)
tree63183a77fd00068efbc7bd2740fc89404a8b786c
parent25f9384a066b0afe0eaefc96c6e9360b81833ddd (diff)
downloadre-name-e68a0b4707885be3c016f3b4f9443740e5dcc25b.tar.gz
Add name
-rw-r--r--name.c67
1 files changed, 67 insertions, 0 deletions
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 <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");
+}