-
Notifications
You must be signed in to change notification settings - Fork 0
/
child_process.c
61 lines (55 loc) · 1.6 KB
/
child_process.c
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
#include "simple_shell.h"
/**
* create_child - creates a sub process.
* @command: The pointer to tokenized command
* @name: The pointer to the name of shell.
* @env: The pointer to the enviromental variables.
* @cicles: Number of executed cicles.
*
* Return: Nothing.
*/
void create_child(char **command, char *name, char **env, int cicles)
{
int pid = 0;
int status = 0;
int wait_error = 0;
pid = fork();
if (pid < 0)
{
perror("Error: ");
free_exit(command);
}
else if (pid == 0)
{
execute(command, name, env, cicles);
free_dp(command);
}
else
{
wait_error = waitpid(pid, &status, 0);
if (wait_error < 0)
{
free_exit(command);
}
free_dp(command);
}
}
/**
* change_dir - changes working directory.
* @path: The new current working directory.
*
* Return: 0 on success, -1 on failure.
*/
int change_dir(const char *path)
{
char *buf = NULL;
size_t size = 1024;
if (path == NULL)
path = getcwd(buf, size);
if (chdir(path) == -1)
{
perror(path);
return (98);
}
return (1);
}