-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.c
79 lines (73 loc) · 2.04 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
** cd.c for minishell1 in /home/nicolaspolomack/shell/PSU_2016_minishell1
**
** Made by Nicolas Polomack
** Login <nicolas.polomack@epitech.eu>
**
** Started on Mon Jan 9 11:06:20 2017 Nicolas Polomack
** Last update Sun May 21 03:58:03 2017 Nicolas Polomack
*/
#include <stdlib.h>
#include <unistd.h>
#include "shell.h"
#include "my.h"
#include "get_next_line.h"
int check_dir(char *path)
{
struct stat stats;
if (stat(path, &stats) == -1)
return (-1);
if (!S_ISDIR(stats.st_mode))
return (-2);
return (0);
}
int move_dir2(char **final, int ac, t_shell *shell)
{
char *temp;
int check;
if (ac == 2 && my_strcmp(final[1], "-") == 0)
{
if (shell->prev == NULL)
return (my_print_err(": No such file or directory.\n"));
temp = my_strdup(shell->prev);
shell->prev = getcwd(NULL, 512);
if (chdir(temp) == -1)
return (my_print_err(temp) +
my_print_err(": No such file or directory.\n") - 1);
free(temp);
}
else
if ((check = check_dir((ac == 2) ? final[1] : final[2])) == 0)
{
shell->prev = getcwd(NULL, 256);
chdir((ac == 2) ? final[1] : final[2]);
}
else
return (my_print_err((ac == 2) ? final[1] : final[2]) +
my_print_err((check == -2) ? ": Not a directory.\n" :
": No such file or directory.\n") - 1);
return (0);
}
int move_dir(char **final, int ac, t_shell *shell)
{
if (ac > 3 || (ac == 3 && my_strcmp(final[1], "--")))
{
my_print_err("cd: Too many arguments.\n");
return (1);
}
else if (ac == 1 || (ac == 2 && !my_strcmp(final[1], "--")))
{
shell->prev = getcwd(NULL, 512);
if (shell->home == NULL)
return (my_print_err("cd: No home directory.\n"));
else if (chdir(shell->home) == -1)
return (my_print_err("cd: Can't change to home directory.\n"));
shell->current = get_current(shell->current, shell->home);
return (0);
}
else
if (move_dir2(final, ac, shell))
return (1);
shell->current = get_current(shell->current, shell->home);
return (0);
}