-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver3.c
99 lines (89 loc) · 2.1 KB
/
server3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgaspail <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/05/16 07:04:57 by mgaspail #+# #+# */
/* Updated: 2014/05/16 07:04:59 by mgaspail ### ########.fr */
/* */
/* ************************************************************************** */
#include "server.h"
void ft_joinfree(char *tmp, char *cd)
{
char *tmp2;
tmp2 = tmp;
tmp = ft_strjoin(tmp2, cd);
free(tmp2);
tmp2 = tmp;
tmp = ft_strjoin(tmp2, "/");
free(tmp2);
}
char *cddotdot(char *tmp)
{
int len;
char *ret;
len = ft_strlen(tmp) - 2;
while (tmp[len] && tmp[len] != '/')
len--;
if (len > 0)
ret = ft_strsub(tmp, 0, len + 1);
else
ret = tmp;
return (ret);
}
void ft_cdsend(int ret, int cs)
{
if (ret == 1)
send(cs, "1", 1, 0);
else if (ret == 2)
send(cs, "2", 1, 0);
else
send(cs, "S", 1, 0);
}
int ft_cd2(char *tmp, char *cd)
{
int ret;
char *path;
DIR *dir;
ret = 0;
if ((path = ft_strjoin(tmp, cd)))
{
free(path);
if ((dir = opendir(path)))
ft_joinfree(tmp, cd);
else
ret = 2;
}
else
ret = 1;
ft_putendl(tmp);
return (ret);
}
void ft_cd(char *cmd, int cs, char **pwd)
{
char *tmp;
char **cd;
int i;
int ret;
ret = 0;
if (!(tmp = ft_strdup(*pwd)))
ret = 1;
if (!(cd = ft_strsplit(cmd, '/')))
ret = 1;
i = -1;
while (ret == 0 && cd[++i])
{
if (ft_strcmp(cd[i], "."))
{
if (ft_strcmp(cd[i], ".."))
ret = ft_cd2(tmp, cd[i]);
else if (ft_strlen(tmp) >= 2)
tmp = cddotdot(tmp);
}
}
if (ret == 0)
*pwd = ft_strdup(tmp);
ft_cdsend(ret, cs);
}