-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_command.c
More file actions
55 lines (52 loc) · 855 Bytes
/
run_command.c
File metadata and controls
55 lines (52 loc) · 855 Bytes
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
#include "shell.h"
/**
* run_command - executes given input
*
* @args: arguments given
* Return: 0
*/
int run_command(char **args)
{
pid_t child_pid;
int status;
if (args == NULL)
return (-1);
child_pid = fork();
if (child_pid < 0)
{
printf("Error");
return (-1);
}
else if (child_pid == 0)
{
if (execve(args[0], args, NULL) == -1)
return (-1);
}
else
child_pid = wait(&status);
return (0);
}
/**
* getline_func - reads input from user
*
* @buffer: storage space for input
* @size: size in bytes of input
* Return: Pointer to the buffer
*/
char *getline_func(char *buffer, size_t size)
{
int x;
x = getline(&buffer, &size, stdin);
if (x == EOF)
{
free(buffer);
write(STDOUT_FILENO, "\n", 1);
exit(0);
}
if (x == 1 || x == -1 || buffer == NULL)
{
free(buffer);
return (NULL);
}
return (buffer);
}