-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_search.c
More file actions
141 lines (103 loc) · 3.34 KB
/
admin_search.c
File metadata and controls
141 lines (103 loc) · 3.34 KB
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
void search(int * n,struct detail *u,int *login_status)
{
int new_socket=*n;
printf("Username: %s\nAccount Number: %lu\n",u->username,u->acc_no);
struct detail user;
int count=0,set=0;
int ofd = open("user_db.dat", O_RDWR, 0766);
if(ofd == -1){printf("cannot open file");}
do{
printf("read %d bytes:\n",count = read(ofd, &user, sizeof(user)));
if(count !=0)
{
if(strcmp(u->username,user.username)==0 && (u->acc_no==user.acc_no))
{
set=1;
break;
}
}
}while(count != 0);
send(new_socket,&set, sizeof(set),0);
char *out =NULL;
if(set==1)
{
out="User found !! press 1).Deletion 2).Modification 3).Exit";
send(new_socket,out, strlen(out),0);
int res;
read( new_socket,&res,sizeof(res));
lseek(ofd,(-1*sizeof(user)),SEEK_CUR); //taking pointer a struct back
//read(ofd,&user,sizeof(user));
struct flock fp;
fp.l_type=F_RDLCK;
fp.l_whence=SEEK_CUR;
fp.l_start=0;
fp.l_len=sizeof(u);
fp.l_pid=getpid();
//fcntl(ofd,F_GETLK,&fp);
if(res==1)
{
//deletion selected
memset(user.username,0,100);
//lseek(ofd,(-1*sizeof(user)),SEEK_CUR); //taking pointer a struct back
//locking the data before deletion
fp.l_whence=SEEK_CUR;
fp.l_start=0;
fp.l_len=(100*sizeof(char));
fp.l_type=F_WRLCK;
fcntl(ofd,F_SETLKW,&fp);
write(ofd,&user,sizeof(user));
lseek(ofd,(-1*sizeof(user)),SEEK_CUR);
fp.l_type=F_UNLCK;
fcntl(ofd,F_SETLKW,&fp);
out="User deleted !!";
send(new_socket,out, strlen(out),0);
}
if(res==2)
{
//modification selected
out="Select for modification :1.username 2.password 3.account number 4.Balance 5.Name";
send(new_socket,out, strlen(out),0);
int mod;
read( new_socket,&mod,sizeof(mod));
out="Enter new value";
send(new_socket,out, strlen(out),0);
//lseek(ofd,(-1*sizeof(user)),SEEK_CUR); //taking pointer a struct back
switch(mod)
{
case 1:read( new_socket,user.username,sizeof(user.username));
break;
case 2:read( new_socket,user.password,sizeof(user.password));
break;
case 3:read( new_socket,&user.acc_no,sizeof(user.acc_no));
break;
case 4:read( new_socket,&user.balance,sizeof(user.balance));
break;
case 5:read( new_socket,&user.name,sizeof(user.name));
break;
}
printf("Username: %s\npassword: %s\naccountno: %lu\nbalance: %lu\nname: %s\n",user.username,user.password,user.acc_no,user.balance,user.name);
fp.l_whence=SEEK_CUR;
fp.l_start=0;
fp.l_len=sizeof(user);
fp.l_type=F_WRLCK;
fcntl(ofd,F_SETLKW,&fp);
write(ofd,&user,sizeof(user));
lseek(ofd,(-1*sizeof(user)),SEEK_CUR);
fp.l_type=F_UNLCK;
fcntl(ofd,F_SETLKW,&fp);
out="Value Modified !!";
send(new_socket,out, strlen(out),0);
}
if(res==3)
{
*login_status=-1;
}
//return 0;
}
else
{
out="User NOT found !!";
send(new_socket,out, strlen(out),0);
//return -1;
}
}