-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32190750_myshell.c
115 lines (103 loc) · 2.31 KB
/
32190750_myshell.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* * myshell.c
* * @author : 컴퓨터공학과 32190750 김수연(3분반)
* * @email : [email protected]
* * @version : 1.0
* * @date : 2022.12.09
* **/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdbool.h>
#define MAX 1024
/*--------------문자열 parsing--------------*/
int Tokenize(char *buf,char * tokens[]){
char * token;
int tokenCount = 0;
token = strtok(buf, " ");
while(token != NULL) {
tokens[tokenCount++] = token;
token = strtok(NULL, " ");
}
tokens[tokenCount] = NULL;
return tokenCount;
}
/*--------------cd명령어(내부 명령어) 구현--------------*/
void innercmd(char *token) { //
if(chdir(token) == -1)
perror("cd");
}
/*--------------리다이렉션일 때--------------*/
void rd(char *token[]) {
pid_t pid;
int fd_rd;
printf("redirection!!\n");
if((pid = fork()) == 0) {
fd_rd = open(token[3], O_RDWR | O_CREAT | O_TRUNC, 0641);
dup2(fd_rd, STDOUT_FILENO);
close(fd_rd);
execvp(token[0], token);
exit(0);
}
wait();
}
/*--------------백그라운드일 때--------------*/
void bg(char *token[]) {
pid_t pid;
int fd_bg;
printf("!!! : %s\n",token[0]);
if((pid = fork()) == 0) {
fd_bg = open("/dev/null", O_RDONLY);
dup2(fd_bg, STDOUT_FILENO);
execvp(token[0],token);
exit(0);
}
}
/*--------------실행--------------*/
bool execute(char *cmd) {
int tokenCount;
int i = 0;
char * tokens[MAX];
pid_t pid;
int additionalFlag = 0;
tokenCount = Tokenize(cmd,tokens);
if(strcmp(tokens[0], "cd") == 0) {
innercmd(tokens[1]);
return;
}
for(i = 0; i < tokenCount; i++) {
if(!strcmp(tokens[i],">")) {
additionalFlag++;
rd(tokens);
}
if(!strcmp(tokens[i],"&")) {
additionalFlag++;
bg(tokens);
}
}
if(!additionalFlag) {
if((pid = fork()) == 0) {
execvp(tokens[0],tokens);
exit(0);
}
wait();
}
memset(tokens, '\0', MAX);
return 1;
}
/*--------------메인--------------*/
int main() {
char cmd[MAX];
while(1) {
printf("%s$ ",get_current_dir_name());
fgets(cmd, sizeof(cmd) - 1, stdin);
if(strncmp("exit",cmd,4) == 0) {
printf("Bye!");
break;
}
execute(cmd);
}
}