Repository navigation
Expand file tree
/
Copy pathshell.c
More file actions
115 lines (103 loc) · 3.06 KB
/
Copy pathshell.c
File metadata and controls
115 lines (103 loc) · 3.06 KB
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
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkellum <nkellum@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/08/01 13:04:45 by nkellum #+# #+# */
/* Updated: 2019/08/07 12:47:43 by nkellum ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*
** DESCRIPTION:
** This function allocates and initializes a new instance of
** the shell struct.
**
** RETURN VALUE:
** If the allocation succeeds, the new instance of a shell
** struct is returned. Otherwise, 0 is returned.
*/
t_shell *init_shell(void)
{
t_shell *shell;
char cwd[1024];
extern char **environ;
if ((shell = malloc(sizeof(t_shell))) == NULL)
return (0);
shell->environ = string_arr_cpy(environ);
shell->home = ft_getenv(shell, "HOME");
getcwd(cwd, 1024);
shell->pwd = ft_strdup(cwd);
shell->oldpwd = ft_strdup(cwd);
return (shell);
}
/*
** DESCRIPTION:
** This function displays the minishell prompt which contains
** the current working directory. If the HOME directory is found
** in the CWD, it is replaced by tilde.
*/
void display_prompt(t_shell *shell)
{
char cwd[1024];
char *cwd_home;
getcwd(cwd, 1024);
cwd_home = replace_substring(cwd, shell->home, "~");
free(shell->home);
shell->home = NULL;
ft_printf(LIGHTBLUE"%s >"RESET" ", cwd_home);
free(cwd_home);
}
/*
** DESCRIPTION:
** This function frees the contents of the shell struct as
** well as the struct itself.
*/
void free_shell_vars(t_shell *shell)
{
if (shell->home)
free(shell->home);
if (shell->pwd)
free(shell->pwd);
if (shell->oldpwd)
free(shell->oldpwd);
if (shell->environ)
free_string_array(shell->environ);
if (shell->input)
free_string_array(shell->input);
free(shell);
}
/*
** DESCRIPTION:
** This function executes the binary given to it with
** the args and environment variables specified. It will
** create a new child process with fork() to execute the
** binary. Once the binary is done running, the process
** is terminated. The parent process will wait for the
** child process to finish by using waitpid().
**
** RETURN VALUE:
** If the new process was created succesfully, 1 is returned.
** Otherwise, 0 is returned.
*/
int run(char *filename, char **args, char **environ)
{
pid_t pid;
pid = fork();
if (pid == 0)
{
if (execve(filename, args, environ) == -1)
ft_printf("Error while executing command.\n");
exit(0);
}
else if (pid < 0)
{
ft_printf("Error creating new process.\n");
return (0);
}
else
waitpid(pid, NULL, 0);
return (1);
}