-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetSummary.c
80 lines (77 loc) · 2.12 KB
/
GetSummary.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#define PATH "/home/braude/Black_Friday/camp_partics.txt"
#define GET_ORDER_NUM "/home/braude/GetOrderNum"
#define GET_NUM_COMP "/home/braude/GetNumComp"
#define ORDER "GetOrderNum"
int main(int argc, char *argv[]){
int fd, rbytes, length, pid;
char* text, *line;
if(argc > 1){
printf("This command should not take any parameters");
exit(-1);
}
//create nre process
if((pid = fork()) == -1){
perror("fork :");
exit(-1);
}
else if(pid == 0){
execl(GET_NUM_COMP, "GetNumComp", NULL);
perror("execl: ");
exit(-1);
}
else{
wait();
}
//get the camp_partics.txt text - know which companies exists - they have orders
if((fd = open(PATH,O_RDONLY)) == -1){ //if company flyer does not exist print and exit
perror("open: ");
exit(-1);
}
if((length = lseek(fd, 0, SEEK_END)) == -1){
perror("seek to end\n");
close(fd);
exit(-1);
}
if (lseek(fd, 0, SEEK_SET) == -1) {
perror("seek to begining");
close(fd);
exit(-1);
}
if((text = (char*)malloc(sizeof(char) * length)) == NULL){
close(fd);
printf("Allocation for saving file's data length failed");
exit(-1);
}
if((rbytes = read(fd, text, length)) == -1){
perror("read failed");
free(text);
close(fd);
exit(-1);
}
text[rbytes] = '\0'; //delete all binary data or line breaks that might have been read
//because read also read bin data
//begin counting distinct lines in camp_partics.txt
line = strtok(text, "\n");
while(line != NULL){
if((pid = fork()) == -1){
perror("fork: ");
exit(-1);
}
else if(pid == 0){ //child process
execl(GET_ORDER_NUM, ORDER, line ,NULL); //print each company and how many orders it has
perror("execl : ");
exit(-1);
}
else{
wait(NULL);
}
line = strtok(NULL, "\n");
}
free(text);
return 0;
}