-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
42 lines (39 loc) · 807 Bytes
/
shell.c
File metadata and controls
42 lines (39 loc) · 807 Bytes
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
/*************************************************************************
> File Name: shell.c
> Author: jianghechao
> Mail: 591378033@qq.com
> Created Time: Thu 24 Apr 2014 12:42:08 AM CST
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define MAXSIZE 100
int main()
{
char buf[MAXSIZE];
bzero(buf,sizeof(buf));
pid_t pid = 0;
int status;
printf("%% ");
while(fgets(buf,MAXSIZE,stdin)!=NULL)
{
if(buf[strlen(buf)-1]=='\n')
buf[strlen(buf)-1]='\0';
if(pid=fork()==0)
/**
* child process do
*/
{
execlp(buf,buf,NULL);
perror("error");
}
/**
* parent process do
*/
if(waitpid(pid,&status,0)<0)
perror("waitpid error");
printf("%% ");
}
return 0;
}