-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
86 lines (74 loc) · 1.51 KB
/
Copy pathexecute.c
File metadata and controls
86 lines (74 loc) · 1.51 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
//libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <dirent.h>
#include <sys/dir.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include<sys/utsname.h>
#include "declarations.h"
void execute()
{
int status;
pid_t wpid;//datatype
pid=fork();
//error in forking
if(pid<0)
printf("Cannot create child process\n");
//child process
else if (pid==0)
{
redirectioncheck();
if(in1i!=-1)
{
//printf("executing cmd\n");
if(execvp(buf[0],buf)==-1)//-1 if failed
printf("%s: command not found\n",buf[0]);
//dup2(pipeout,1);
//dup2(pipein,0);
}
if(in1==1)
close(in1i);
if(out1==1)
close(out1i);
if(out2==1)
close(out2i);
}
//parent process
else
{
//background process
if(bg==1)
{
bg_proc_initialize(pid,buf[0]);
printf("[%d] %d\n",maxbg,pid);
}
//foreground process
else//do while bacause we need it to run atleast once
{
waitpid(pid,&status,WUNTRACED);
/*do
{
wpid = waitpid(pid, &status, WUNTRACED);
//wuntraced to return if a child has stopped
//status stores status info
}
while (!WIFEXITED(status) && !WIFSIGNALED(status));
//waits for child to end
//WIFEXITED returns true if the child terminated normally
//WIFSIGNALED returns true if the child process was terminated by a signal.
*/
}
bgprocexecute();
}
}