/
filerdr2.c
212 lines (193 loc) · 3.83 KB
/
filerdr2.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
/*
* static buffer to read a line, max of 1k bytes
*/
#define MAX_LINE_SIZE 1024
char linebuf[MAX_LINE_SIZE];
char * letters="abcdefghijklmnopqrstuvwxyz";
/*
* convert letter to number
*/
int lettertonumber(char arg)
{
int i = 0;
int k = 0;
k = strlen(letters);
while(i<k)
{
if(letters[i]==arg)
{
return i+1;
}
i++;
}
return -1;
}
/*
* read a line from the file and print it out with 'id'
* which should identify which partner this is
*/
void readline(pid_t id, FILE *file)
{
char *e = fgets(linebuf, MAX_LINE_SIZE, file);
int i =0,k=0;
while(linebuf[i]!='\n' && linebuf[i]!='\0')
{
i++;
k++;
if(/*isalpha(linebuf[i]) &&*/ lettertonumber(linebuf[i])>9){
printf("K ADD\n");
k++;
}
}
/*
* Allocate memory and copy line
*/
char * line = malloc((k)*sizeof(char));
line[0]='\0';
i=0;
char * number=malloc(5*sizeof(char));
char * ch = malloc(2*sizeof(char));
ch[1]='\0';
while(linebuf[i]!='\n' && linebuf[i]!='\0')
{
if(isalpha(linebuf[i]))
{
sprintf(number,"%d",lettertonumber(linebuf[i]));
strcat(line,number);
}
else
{
ch[0]=linebuf[i];
strcat(line,ch);
}
i++;
}
if(e == NULL)
{
printf("[%d] *** NO LINE ***\n", id);
}
else
{
printf("%s %d\n",line,id);
//printf("length: %d\n",strlen(line));
fflush(stdout);
}
fflush(stdout);
}
/*
* write the position of the file to the other partner
*/
void writeposition(off_t pos, int fdes)
{
write(fdes, &pos, sizeof(off_t));
}
/*
* read the position of the file from the other partner
*/
off_t readposition(int fdes)
{
off_t pos;
read(fdes, &pos, sizeof(off_t));
return pos;
}
int main(int argc, char **argv)
{
/* pipe to parent, pipe to child */
int p_prnt[2], p_chld[2];
/* default filename to read, source code of this program */
char *filename = "FILE1";
/* filehandle of file we are reading */
FILE *file;
/* process 'id', just a string of parent or child */
char *id;
/* pid used when seeing the fork result */
int pid;
/* pipe used in this process, assigned after fork */
int mypipe[2];
/* position of file, read from other partner */
off_t pos;
/* stat info, so we don't fseek past the end */
struct stat finfo;
/* all using a separate file if a name is given */
if(argc > 1)
{
filename = argv[1];
}
/* init the pipes */
if(pipe(p_prnt) || pipe(p_chld))
{
perror("pipe");
exit(1);
}
/* open the file */
if((file = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "Failed to open '%s': %s\n",
filename, strerror(errno));
exit(1);
}
/* get the stat/size of the file */
if(fstat(fileno(file), &finfo))
{
perror("fstat");
exit(1);
}
/* fork and assign 'id' and 'mypipe' */
if((pid = fork()) < 0)
{
perror("fork");
exit(1);
}
else if(pid == 0)
{
id = "CHLD";
mypipe[0] = p_chld[0];
mypipe[1] = p_prnt[1];
/*
* the child should wait until the parent reads the first line
* and then position itself for the loop to begin with the second line
*/
pos = readposition(mypipe[0]);
fseek(file, pos, SEEK_SET);
}
else
{
id = "PRNT";
mypipe[0] = p_prnt[0];
mypipe[1] = p_chld[1];
}
/*
* main loop to
* read the line
* write the position to the other partner
* wait for the position from the other partner
* (it will read the next line in the mean-time)
* seek to the new position
*/
while(pos < finfo.st_size)
{
/* read */
readline(getpid(), file);
/* send pos */
writeposition(ftell(file), mypipe[1]);
/* get next */
pos = readposition(mypipe[0]);
/* seek */
fseek(file, pos, SEEK_SET);
}
/* get other side to close down */
writeposition(ftell(file), mypipe[1]);
/* clean up and exit */
close(mypipe[0]);
close(mypipe[1]);
fclose(file);
return(0);
}