-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver4.c
71 lines (65 loc) · 1.8 KB
/
server4.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server4.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgaspail <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/05/16 07:05:04 by mgaspail #+# #+# */
/* Updated: 2014/05/16 07:05:06 by mgaspail ### ########.fr */
/* */
/* ************************************************************************** */
#include "server.h"
int usage(char *str)
{
write(2, "Usage : ", 8);
write(2, str, ft_strlen(str));
write(2, " <port>\n", 8);
return (0);
}
void ft_get2(int cs, int r, int fd)
{
char buf[1024];
while ((r = read(fd, buf, 1023)) == 1023)
{
send(cs, buf, r, 0);
r = recv(cs, buf, 20, 0);
if (buf[0] == 'E')
r = -2;
if (r < 0)
break ;
}
if (r > 0)
{
send(cs, buf, r, 0);
r = recv(cs, buf, 20, 0);
if (buf[0] == 'S')
send(cs, "S", 1, 0);
else
send(cs, "2", 1, 0);
}
else if (r == -1)
send(cs, "1", 1, 0);
else
send(cs, "S", 1, 0);
}
void ft_get(char *name, int cs, char *pwd)
{
int fd;
char buf[1024];
int r;
char *path;
path = ft_strjoin(pwd, name);
if ((fd = open(path, O_RDONLY)) == -1)
{
send(cs, "E", 1, 0);
return ;
}
send(cs, "S", 1, 0);
r = recv(cs, buf, 20, 0);
if (buf[0] == 'S')
ft_get2(cs, r, fd);
else
ft_putendl("ERROR : cannot create file on server.");
close(fd);
}