-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnon_inter_shellby.c
More file actions
75 lines (69 loc) · 1.42 KB
/
non_inter_shellby.c
File metadata and controls
75 lines (69 loc) · 1.42 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
#include "shellby.h"
/**
* non_inter_shellby - non interactive simple shell run.
* @argc: numer of input arguments.
* @argv: NULL terminated array of input arguments.
*
* Return: 0 on success.
*/
int non_inter_shellby(int argc, char *argv[])
{
char *args_temp[2] = {NULL, NULL};
int acce = 0, error = 0;
if (argc > 1)
{
/*file input*/
args_temp[0] = argv[1];
acce = access(argv[1], R_OK);
if (acce == 0)
child_exe(args_temp, argv[0], 1);
}
else
{
/*commands from pipe*/
error = non_inter_piped(argv[0]);
}
return (error);
}
/**
* non_inter_piped - runs non interactive shell when input is piped.
* @argv: name of the program.
*
* Return: 0 on success.
*/
int non_inter_piped(char *argv)
{
char **cmnds = NULL, *buffer = NULL, *token = NULL;
char *heap_token = NULL;
size_t size = 0;
pid_t child_pid;
int status, error = 0, count = 1;
while ((getline(&buffer, &size, stdin)) != -1)
{
token = strtok(buffer, " \n\t\r");
if (token != NULL)
{
if (check_built_ins(buffer, token) == 1)
continue;
heap_token = look_inPATH(&token);
cmnds = input_tokens(token, buffer);
child_pid = fork();
if (child_pid == 0)
{
child_exe(cmnds, argv, count);
}
else
{
waitpid(child_pid, &status, 0);
/*error = errno;*/
error = WEXITSTATUS(status);
}
free_all(buffer, cmnds, heap_token);
size = 0;
}
count++;
}
if (size != 0)
free(buffer);
return (error);
}