-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins_2.c
73 lines (65 loc) · 1.29 KB
/
builtins_2.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
#include "minishell.h"
int msh_heredoc(struct s_process *ps)
{
int fd;
char *buff;
int r;
fd = open(ps->args[0], O_RDONLY, 0644);
buff = malloc(2);
r = 1;
while (r)
{
r = read(fd, buff, 2);
write(1, buff, r);
}
free(buff);
unlink(ps->args[0]);
exit(0);
return (1);
}
/* cd .
* ..
* ../path
* /path
*
* returns:
* 0 - malloc fail
* 1 - ok
* -1 - bad dir
*/
int msh_cd(struct s_process *ps)
{
char buf[MAXPATHLEN];
char *path;
int r;
(path = ps->args[1]) || (path = my_getenv(ps->env, "HOME"), ':');
(path || display_err_direct(ps->args[0], "HOME not set"));
if (!path)
return (0);
(r = chdir(path)) && display_err(ps);
if (r)
return (-1);
getcwd(buf, sizeof(buf));
r = dict_set_default(ps->env, ft_strdup("PWD"), ft_strdup(buf));
return (r);
}
int msh_exit(struct s_process *ps)
{
int code;
if (*ps->flag)
return (0);
printf("exit\n");
code = ps->shell->last_exit_code;
free_and_null(ps->shell->heredoc, ps->shell->line);
ft_lstclear(&ps->shell->env, del_dict);
ft_lstclear(&ps->shell->cmd, free_process);
exit(code);
return (1);
}
/* function return always ok because prints error directly */
int msh_pwd(struct s_process *ps)
{
char buf[MAXPATHLEN];
(getcwd(buf, sizeof(buf)) && printf("%s\n", buf)) || display_err(ps);
return (1);
}