-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay.c
133 lines (123 loc) · 3.15 KB
/
display.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* display.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ccharrie <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/06 16:21:19 by ccharrie #+# #+# */
/* Updated: 2018/02/05 16:09:58 by ccharrie ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int clearterm(t_env *env)
{
char **argv;
pid_t pid;
int status;
char **tabenv;
pid = fork();
tabenv = ft_list2tab(env);
if (!(argv = (char **)malloc(sizeof(char *) * 2)))
return (0);
argv[0] = ft_strdup("/usr/bin/clear");
argv[1] = NULL;
if (pid == -1)
{
ft_putformat("error : unable to fork 🤔", LIGHT_RED, NULL, "BOLD");
return (0);
}
else if (pid == 0)
{
execve(argv[0], argv, tabenv);
exit(1);
}
else
waitpid(pid, &status, 0);
ft_del2tab(tabenv, argv);
return (0);
}
void execcmd(char **args, char **env)
{
execve(args[0], args, env);
if (ft_strcmp(args[0], "exit"))
{
ft_putformat(args[0], LIGHT_RED, NULL, "BOLD");
if (access(args[0], F_OK) == -1)
{
ft_putformat(" error : command not found,", RED, NULL, "BOLD");
ft_putformat(" everyone make mistakes😉\n", RED, NULL, "BOLD");
}
else if (access(args[0], X_OK) == -1)
{
ft_putformat(" error : permission denied😔\n",
LIGHT_RED, NULL, "BOLD");
}
}
}
void findthebin(char **args, char **path, char **env)
{
int i;
DIR *dir;
struct dirent *file;
char *tmp;
i = 0;
while (path[i])
{
dir = opendir(path[i]);
while ((file = readdir(dir)))
{
if (ft_strcmp(file->d_name, args[0]) == 0)
{
tmp = ft_strjoin(path[i], "/");
args[0] = ft_strjoinnfree(tmp, args[0]);
execve(args[0], args, env);
}
}
closedir(dir);
i++;
}
ft_putformat(args[0], RED, NULL, "BOLD");
ft_putformat(" error : command not found, everyone make mistakes 😉\n",
RED, NULL, "BOLD");
}
void execbincmd(char **args, t_env *env)
{
char **path;
t_env *begin;
char **tenv;
begin = env;
path = NULL;
while (env)
{
if (ft_strcmp("PATH", env->name) == 0)
{
path = ft_strsplit(env->value, ':');
break ;
}
env = env->next;
}
if (env == NULL)
ft_putformat("error : command not found, everyone make mistakes 😉\n",
RED, NULL, "BOLD");
env = begin;
tenv = ft_list2tab(env);
findthebin(args, path, tenv);
ft_del2dtab(path);
ft_del2dtab(tenv);
}
void forknexec(char **cmd, char **path, char **env)
{
pid_t pid;
int status;
pid = fork();
if (pid == -1)
ft_putformat("error : unable to fork 🤔", LIGHT_RED, NULL, "BOLD");
else if (pid == 0)
{
findthebin(cmd, path, env);
exit(1);
}
else
waitpid(pid, &status, 0);
}