-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.c
100 lines (81 loc) · 2.12 KB
/
run.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
#include "cscshell.h"
// COMPLETE
int cd_cscshell(const char *target_dir){
if (target_dir == NULL) {
char user_buff[MAX_USER_BUF];
if (getlogin_r(user_buff, MAX_USER_BUF) != 0) {
perror("run_command");
return -1;
}
struct passwd *pw_data = getpwnam((char *)user_buff);
if (pw_data == NULL) {
perror("run_command");
return -1;
}
target_dir = pw_data->pw_dir;
}
if(chdir(target_dir) < 0){
perror("cd_cscshell");
return -1;
}
return 0;
}
int *execute_line(Command *head){
#ifdef DEBUG
printf("\n***********************\n");
printf("BEGIN: Executing line...\n");
#endif
#ifdef DEBUG
printf("All children created\n");
#endif
// Wait for all the children to finish
#ifdef DEBUG
printf("All children finished\n");
#endif
#ifdef DEBUG
printf("END: Executing line...\n");
printf("***********************\n\n");
#endif
return NULL;
}
/*
** Forks a new process and execs the command
** making sure all file descriptors are set up correctly.
**
** Parent process returns -1 on error.
** Any child processes should not return.
*/
int run_command(Command *command){
#ifdef DEBUG
printf("Running command: %s\n", command->exec_path);
printf("Argvs: ");
if (command->args == NULL){
printf("NULL\n");
}
else if (command->args[0] == NULL){
printf("Empty\n");
}
else {
for (int i=0; command->args[i] != NULL; i++){
printf("%d: [%s] ", i+1, command->args[i]);
}
}
printf("\n");
printf("Redir out: %s\n Redir in: %s\n",
command->redir_out_path, command->redir_in_path);
printf("Stdin fd: %d | Stdout fd: %d\n",
command->stdin_fd, command->stdout_fd);
#endif
#ifdef DEBUG
printf("Parent process created child PID [%d] for %s\n", pid, command->exec_path);
#endif
return -1;
}
int run_script(char *file_path, Variable **root){
return -1;
}
void free_command(Command *command){
}