-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.c
More file actions
116 lines (112 loc) · 2.57 KB
/
Copy pathpath.c
File metadata and controls
116 lines (112 loc) · 2.57 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
116
#include "shell.h"
/**
* exec_builtin_cmd - checks to see if command is a built in
* @cmd: command passed to main
* @args: arguments passed with command
* @info: the info_t struct
* Return: returns TRUE (1) on success, FALSE (0) on fail.
*/
int exec_builtin_cmd(char *cmd, char **args, info_t *info)
{
if (!cmd)
return (TRUE);
if (_strcmp(cmd, "cd") == 0)
change_directory(args, info);
else if (_strcmp(cmd, "exit") == 0)
exit_shell(NULL, args, info);
else if (_strcmp(cmd, "env") == 0 && !args[1])
print_env(&info->env_head, info);
else if (_strcmp(cmd, "setenv") == 0)
_setenv(args[1], args[2], info->env_head);
else if (_strcmp(cmd, "unsetenv") == 0)
; /* TODO */
else if (_strcmp(cmd, "history") == 0)
print_history(&info->hist_head);
else if (_strcmp(cmd, "help") == 0)
; /* TODO */
else if (_strcmp(cmd, "alias") == 0)
; /* TODO */
else if (_strcmp(cmd, "unalias") == 0)
; /* TODO */
else
return (FALSE);
return (TRUE);
}
/**
* exec_path_cmd - checks/executes commandss in the PATH directories
* @cmd: command to check for
* @args: arguments received by getline
* @path_head: pointer to first node of path_t list
* @info: pointer to shell information struct
* Return: TRUE (1) on success; FALSE (0) on failure
*/
int exec_path_cmd(char *cmd, char **args, path_t *path_head, info_t *info)
{
path_t *temp;
char *file_path;
pid_t pid;
int status;
char **env_array;
temp = path_head;
while (temp->next)
{
file_path = malloc(sizeof(char) * (_strlen(temp->path_dir) +
_strlen(cmd) + 1));
_strcpy(file_path, temp->path_dir);
_strncat(file_path, cmd, _strlen(cmd)); /* create custom _strncat(...)*/
_strcat(file_path, "\0");
if (access(file_path, X_OK) == 0)
{
pid = fork();
if (pid == 0)
{
env_array = env_list_to_array(info->env_head);
execve(file_path, args, env_array);
}
else if (pid < 0)
{
perror("Error");
exit(1000); /*TODO: handle exit values */
}
else
{
wait(&status);
}
free(file_path);
return (TRUE);
}
free(file_path);
temp = temp->next;
}
return (FALSE);
}
/**
* exec_filename - checks/executes commands
* @cmd: command to check for
* @args: arguments received by getline
* Return: TRUE (1) on success; FALSE (0) on failure
*/
int exec_filename(char *cmd, char **args)
{
pid_t pid;
int status;
if (access(cmd, X_OK) == 0)
{
pid = fork();
if (pid == 0)
{
execve(cmd, args, environ);
}
else if (pid < 0)
{
perror("Error");
exit(1000); /*TODO: handle exit values */
}
else
{
wait(&status);
}
return (TRUE);
}
return (FALSE);
}