-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.c
More file actions
76 lines (63 loc) · 1.82 KB
/
terminal.c
File metadata and controls
76 lines (63 loc) · 1.82 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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#define MAX 401
#define true 1
const int N=10;
void spiltCmdString(char* commend, char** x);
int executeTheCommend(char** var);
int main()
{
char cmd[MAX+7];
char* var[N+1];
int cmdCount = 0;
char* user = getenv("USER"); // "getenv" for Get username
printf("\n\n ============ Welcome %s =============\n\n\n" ,user); //print user name
while(true)
{
char* username = getenv("USER"); // "getenv" Get username
if(strcmp(username,user)!=0)
printf("\n\n ============ Welcome %s =============\n\n\n" ,username);
printf("%s@line:%d> \a", username, ++cmdCount);
if(fgets(cmd, sizeof(cmd), stdin) == NULL) break; //get input from user..
if(cmd[strlen(cmd)-1] == '\n') //remove newline and replace with null("\0");
cmd[strlen(cmd)-1] = '\0';
spiltCmdString(cmd, var);
if(strcmp(var[0], "exit") == 0) break;
if(strcmp(var[0], "hang") == 0)
{
while(true){ pid_t p=fork(); }
}
if(executeTheCommend(var) == 0) break;
}
return 0;
}
void spiltCmdString(char* commend, char** x)
{ int i=0;
for(i = 0; i < N; i++) {
x[i] = strsep(&commend, " ");
if(x[i] == NULL) break;
}
}
int executeTheCommend(char** var)
{
pid_t pid = fork();
if (pid < 0) {
char* error = strerror(errno);
printf("fork: %s\n", error);
return 1;
}
else if (pid == 0) {
execvp(var[0], var); //execute cmd
char* error = strerror(errno); // Error occurred
printf("shell: %s: %s\n", var[0], error);
return 0;
}
else {
int childStatus;
waitpid(pid, &childStatus, 0); //wait for child
return 1;
}
}