-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
110 lines (101 loc) · 2.71 KB
/
client.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgaspail <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/05/14 21:16:29 by mgaspail #+# #+# */
/* Updated: 2014/05/16 07:41:12 by mgaspail ### ########.fr */
/* */
/* ************************************************************************** */
#include "client.h"
int usage(char *str)
{
write(2, "Usage : ", 8);
write(2, str, ft_strlen(str));
write(2, " <machine> <port>\n", 18);
return (0);
}
int create_client(int port, char *addr)
{
int sock;
struct protoent *proto;
struct sockaddr_in sin;
proto = getprotobyname("tcp");
if (proto == 0)
return (-1);
sock = socket(PF_INET, SOCK_STREAM, proto->p_proto);
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = inet_addr(addr);
connect(sock, (struct sockaddr *)&sin, sizeof(sin));
return (sock);
}
void ft_send2(int sock, char *buf)
{
if (ft_strncmp(buf, "get ", 4) == 0)
{
send(sock, buf, ft_strlen(buf), 0);
ft_get(buf, sock);
}
else if (ft_strncmp(buf, "put ", 4) == 0)
ft_put(sock, buf);
else if (ft_strncmp(buf, "cd ", 3) == 0)
ft_cd(sock, buf);
else if (ft_strcmp(buf, "help") == 0)
ft_help();
else if (ft_strncmp(buf, "mkdir ", 6) == 0)
ft_mkdir(sock, buf);
else
{
ft_putstr("ERROR : ");
ft_putstr(buf);
ft_putendl(" command not found.");
}
}
void ft_sending(int sock, char *path)
{
char buf[1024];
int r;
char *prompt;
prompt = ft_strjoin(path, " $>");
while (ft_putstr(prompt), 1)
{
r = read(0, buf, 1023);
if (r > 0)
{
buf[r - 1] = '\0';
if (ft_strcmp(buf, "quit") == 0)
{
send(sock, buf, r, 0);
break ;
}
else if (ft_strcmp(buf, "pwd") == 0)
ft_pwd(sock, buf);
else if (ft_strcmp(buf, "ls") == 0)
ft_ls(sock, buf);
else
ft_send2(sock, &buf[0]);
}
}
}
int main(int argc, char **argv)
{
int port;
int sock;
int r;
char buff[1024];
char *path;
if (argc != 3)
return (usage(argv[0]));
port = ft_atoi(argv[2]);
sock = create_client(port, argv[1]);
r = recv(sock, buff, 1024, 0);
ft_putendl(buff);
path = ft_strjoin(argv[1], ":");
path = ft_strjoin(path, argv[2]);
ft_sending(sock, path);
close(sock);
return (0);
}