-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagenda_server.c
140 lines (121 loc) · 3.17 KB
/
agenda_server.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
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "agenda.h"
#include <string.h>
#include <stdio.h>
FILE *file;
char fileName[100];
int size;
struct contact *loadFile(){
struct contact *contacts;
int i;
if((file = fopen(fileName,"ab+"))==NULL){
printf("Error: File %s not open.\n",fileName);
exit(0);
}
fseek(file,0,SEEK_END);
size = ftell(file)/sizeof(struct contact);
fseek(file,0,SEEK_SET);
contacts =(struct contact *) malloc(sizeof(struct contact)*size);
for(i=0;i<size;i++){
fread(&contacts[i],sizeof(struct contact),1,file);
}
fclose(file);
return contacts;
}
struct contact search(char *name){
int i;
struct contact *contacts;
contacts = loadFile();
i=0;
while(i<size && strcmp(name,contacts[i].name)!=0) i++;
if(i< size) return contacts[i];
struct contact cont;
memset(&cont, 0, sizeof(struct contact));
return cont;
}
int *
insertcontact_1_svc(struct contact *argp, struct svc_req *rqstp)
{
static int result;
int i, included = 0;
struct contact *contacts;
contacts = loadFile();
if((file = fopen(fileName,"wb"))==NULL){
printf("Error: File %s not open.\n",fileName);
return &result;
//exit(0);
}
i=0;
while(i<size && strcmp(argp->name,contacts[i].name)>0){
fwrite(&contacts[i],1,sizeof(struct contact),file);
i++;
}
if(strcmp(argp->name,contacts[i].name)!=0){
fwrite(argp,1,sizeof(struct contact),file);
included = 1;
}
while(i<size){
fwrite(&contacts[i],1,sizeof(struct contact),file);
i++;
}
fclose(file);
result = included;
return &result;
}
struct contact *
searchcontact_1_svc(char **argp, struct svc_req *rqstp)
{
static struct contact result;
int i;
struct contact *contacts;
contacts = loadFile();
i=0;
while(i<size && strcmp(*argp,contacts[i].name)!=0) i++;
if(i< size){
// result = (struct contact *) malloc(sizeof(struct contact));
result = contacts[i];
}
else memset(&result, 0, sizeof(struct contact));
return &result;
}
int *
removecontact_1_svc(char **argp, struct svc_req *rqstp)
{
static int result;
int i, deleted = 0;
struct contact contactRemove;
contactRemove = search(*argp);
if(strcmp(contactRemove.name,"")){
struct contact *contacts;
contacts = loadFile();
if((file = fopen(fileName,"wb"))==NULL){
printf("Error: File %s not open.\n",fileName);
exit(0);
}
i=0;
while(i<size && strcmp(contactRemove.name,contacts[i].name)>0){
fwrite(&contacts[i],1,sizeof(struct contact),file);
i++;
}
i++;
while(i<size){
fwrite(&contacts[i],1,sizeof(struct contact),file);
i++;
}
fclose(file);
deleted = 1;
}
result = deleted;
return &result;
}
void *
setfile_1_svc(char **argp, struct svc_req *rqstp)
{
static char * result;
strcpy(fileName,*argp);
return (void *) &result;
}