-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_cd.c
62 lines (57 loc) · 1.72 KB
/
ft_cd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rlutsch <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/06 13:41:09 by rlutsch #+# #+# */
/* Updated: 2016/11/14 14:24:53 by rlutsch ### ########.fr */
/* */
/* ************************************************************************** */
#include "21sh.h"
char *ft_cd(char **av)
{
char *pwd;
struct stat statbuf;
pwd = malloc(sizeof(char*) * 256);
if (!av[1])
{
pwd = ft_strjoin(pwd, getenv("HOME"));
stat(pwd, &statbuf);
}
else if (av[1])
stat(av[1], &statbuf);
printf("%d\n", ft_strcmp(av[1], "-"));
if (((S_ISDIR(statbuf.st_mode)) == 1) || (ft_strcmp(av[1],"-") == 0))
{
if (av[1])
{
pwd = ft_getpwd(av[1], pwd);
pwd = ft_strjoin(pwd, "/");
pwd = ft_strjoin(pwd, av[1]);
}
chdir(pwd);
}
else ft_putstr("invalid directory\n");
return (pwd);
}
char *ft_getpwd(char *av, char *pwd)
{
char **tmp;
tmp = NULL;
struct stat statbuf;
if (ft_strcmp(av, "-") == 0)
{
pwd = ft_strjoin(pwd, getenv("HOME"));
stat(pwd, &statbuf);
}
else
{
pwd = ft_getenv("OLDPWD");
tmp = ft_strsplit(pwd,'=');
printf("%s\n", tmp[1]);
chdir(tmp[1]);
}
return (pwd);
}