-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute_command.c
More file actions
134 lines (114 loc) · 3.05 KB
/
execute_command.c
File metadata and controls
134 lines (114 loc) · 3.05 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "header.h"
/**
* execute_command - execute a command
*@arg_array: arguments to pass to command
*@line: command line to free if exit is called
*@tokens: array of directories in PATH
*/
void execute_command(char **arg_array, char *line, char **tokens)
{
int status = 0;
int pathCount, cmdLen, pathLen;
char *path = NULL;
/*checks for and executes built-ins */
builtIns(arg_array, line, tokens, status);
if (fork() == 0)
{
/* try to execute function as is */
if ((execve(arg_array[0], arg_array, NULL)) == -1)
{
/* try execve cmd using the different directories in PATH using tokens */
for (pathCount = 0; tokens[pathCount] != NULL; pathCount++)
{
/* find string length of path directory and command */
/* malloc enough size for new path string */
cmdLen = getLength(arg_array[0]);
pathLen = getLength(tokens[pathCount]);
path = malloc(sizeof(char) * (cmdLen + pathLen + 0));
/* combine path from token to command to get new path */
/* path = token + "/" + cmd */
newPath(path, tokens, pathLen, cmdLen, pathCount, arg_array[0]);
execve(path, arg_array, NULL);
free(path);
}
status = 127;
free(arg_array);
/* print "error" using echo to close pid */
perror(arg_array[0]);
_exit(status);
}
}
else
wait(&status);
/* free(path); */
}
/**
* builtIns - runs builtIns if command is detected
*@arg_array: command line split into an array (slit at " ")
*@line: pointer to command line
*@status: exit status
*@tokens: array of directories in PATH
*/
void builtIns(char **arg_array, char *line, char **tokens, int status)
{
int i;
char *env = *environ; /* grabs local enviorn with external variable */
int letters = 0; /* counter for printing environ */
char c[1];
/* check for exit command */
if (_strcmp(arg_array[0], "exit") == 0)
{
free(arg_array);
free(line);
free(tokens);
exit(status);
}
/* check for env command */
if (_strcmp(arg_array[0], "env") == 0)
{
for (i = 1; env != NULL; i++)
{
for (letters = 0; (c[0] = env[letters]) != '\0'; letters++)
{
write(1, c, 1);
}
write(1, "\n", 1);
env = *(environ + i);
}
return;
}
}
/**
* newPath - create a new path for a command using PATH
*@path: pointer to the new path
*@tok: array of directories from PATH
*@pathL: length of current directory from PATH in tokens
*@cmdL: length of the command line
*@pathC: count of directories in PATH
*@cmd: command string
*
* Return: path
*/
void newPath(char *path, char **tok, int pathL, int cmdL, int pathC, char *cmd)
{
int newPathTok, newPathCmd; /* index counters to create new path string */
for (newPathTok = 0; newPathTok < pathL; newPathTok++)
path[newPathTok] = tok[pathC][newPathTok];
path[newPathTok] = '/';
newPathTok++;
for (newPathCmd = 0; newPathCmd <= cmdL; newPathCmd++)
path[newPathTok + newPathCmd] = cmd[newPathCmd];
}
/**
* getLength - find length of a string
*@str: target string
*
* Return: length of str
*/
int getLength(char *str)
{
int index;
for (index = 0; str[index] != '\0'; index++)
;
return (index);
}