1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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");
}
|