Skip to content

Commit da4f451

Browse files
committed
gives execution to program
1 parent 8c07687 commit da4f451

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

DL_execute.c

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
#include "header.h"
22

33
/**
4-
* _execute - code that executes the execve commands
5-
* @args: pointer to string array
6-
* @envp: array to pointer of environment variable
4+
* execute_command - function to handle command execution based on user input
5+
* @get_address: input line from user
6+
* @env: environment variable
77
*
8-
* Return: 0 successful
8+
* Return: output executed command else -1
99
*/
1010

11-
int _execute(char *args, char **envp)
11+
int execute_command(char *get_address, char __attribute__((__unused__)) **env)
1212
{
13-
char **argv;
14-
/*int kai;*/
15-
struct stat buff;
13+
char *delim = " \n\t\r";
14+
char **tokens = NULL, *command_path = NULL;
15+
int status = 0, ex = 0;
1616

17-
argv = malloc(sizeof(char *));
18-
if (argv == NULL)
17+
tokens = tokenize(get_address, delim);
18+
if (!tokens || tokens[0] == NULL)
1919
{
20-
perror("Error: ");
21-
exit(EXIT_FAILURE);
20+
perror("Error ");
21+
return (ex);
2222
}
23-
argv[0] = strtok(args, " \t\n");
24-
if (argv[0] == NULL)
23+
if (_strcmp(tokens[0], "exit") == 0)
2524
{
26-
free(argv);
27-
return (0);
25+
if (tokens[1] != NULL)
26+
{
27+
status = atoi(tokens[1]);
28+
}
29+
free(tokens);
30+
free(command_path);
31+
exit(status);
2832
}
29-
/*check if file exists*/
30-
if (stat(argv[0], &buff) == -1)
33+
if (_strcmp(tokens[0], "env") == 0)
3134
{
32-
perror("Error: ");
33-
free(argv);
34-
exit(EXIT_FAILURE);
35+
_printenv();
36+
free(tokens);
37+
free(command_path);
38+
return (ex);
3539
}
36-
execve(argv[0], argv, envp);
37-
/*print error if execve failed*/
38-
perror("Error execve:");
39-
free(argv);
40-
exit(EXIT_FAILURE);
40+
command_path = locate_path(tokens[0]);
41+
if (!command_path)
42+
{
43+
perror("command not found");
44+
free(tokens);
45+
ex = 127;
46+
return (ex);
47+
}
48+
execute_and_wait(command_path, tokens, &ex);
49+
return (ex);
4150
}

0 commit comments

Comments
 (0)