-
Notifications
You must be signed in to change notification settings - Fork 0
/
myBash.c
88 lines (88 loc) · 2.21 KB
/
myBash.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
//
// Created by seven on 16-7-23.
//
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#define MAXLINE 100
char userLine[MAXLINE];
char command[MAXLINE];
//split the command
int splitCommand(char* userLine, char* argv[]);
void getUserNameLine(char* userLine);
int userType();
int splitCommand(char* userLine, char* argv[]) {
int i = 0;
char* string;
argv[i++] = strtok(userLine, " ");
while ((string = strtok(NULL, " \n")) != NULL) {
argv[i++] = string;
printf("arg:%s\n", argv[i-1]);
}
return 0;
}
void getUserNameLine(char* userLine) {
userLine[0]='\0';
struct passwd *psw;
psw = getpwuid(getuid());
strcat(userLine, psw->pw_name);
strcat(userLine, "@");
char hostName[MAXLINE];
char wd[MAXLINE];
getwd(wd);
if(gethostname(hostName, sizeof(hostName)))
return;
else
strcat(userLine, hostName);
strcat(userLine, ":");
strcat(userLine, wd);
userType();
}
int userType() {
if(strcmp("root", getenv("USER")) == 0)
strcat(userLine, "#");
else
strcat(userLine, "$");
}
int main() {
int pipe_fd[2];
int status;
pipe(pipe_fd);
char *argv[MAXLINE];
pid_t child1, child2;
while (1) {
getUserNameLine(userLine);
printf("[%s]", userLine);
fgets(command, MAXLINE, stdin);
if(splitCommand(command, argv)) {
printf("split failed!\n");
}
if ((child1 = fork())) {
if ((child2) = fork() == 0) {
close(pipe_fd[1]);
close(fileno(stdin));
dup2(pipe_fd[0] ,fileno(stdin));
close(pipe_fd[0] );
execvp(argv[0], argv);
}
else {
close(pipe_fd[0]);
close(pipe_fd[1]);
waitpid(child2, &status , 0);
}
waitpid(child1, &status, 0);
}
else {
printf ("subshell 3cmd %d\n", getpid());
close (pipe_fd[0]);
close (fileno(stdout));
dup2 (pipe_fd[1], fileno(stdout));
close(pipe_fd[1]);
execvp(argv[0], argv);
}
}
return 0;
}