- 论坛徽章:
- 13
|
代码如下:- #include <apue.h>
- #define MAXARGS 20
- #define ARGLEN 100
- int main()
- {
- char *arglist[MAXARGS+1]; //an array of ptrs*
- int numargs; //index into arrays
- char argbuf[ARGLEN]; //read stuff here
- char *makestring(); //malloc etc
- numargs = 0;
- while (numargs < MAXARGS)
- {
- printf("Arg[%d]?",numargs);
- if(fgets(argbuf,ARGLEN,stdin) && *argbuf!='\n')
- arglist[numargs++]=makestring(argbuf);
- else
- {
- if(numargs > 0){
- arglist[numargs]=NULL;
- execute(arglist);
- numargs=0;
- }
- }
- }
- return 0;
- }
- int execute(char *arglist[])
- {
- execvp(arglist[0],arglist);
- perror("execvp failed");
- exit(1);
- }
- char *makestring(char *buf)
- {
- char *cp;
- buf[strlen(buf)-1]='\0';
- cp=malloc(strlen(buf)+1);
- if(cp==NULL){
- fprintf(stderr,"no memory\n");
- exit(1);
- }
- strcpy(cp,buf);
- return cp;
- }
复制代码 说白了就是用exec* 在当前bash 下覆盖,exit退出,应该退出当前bash进程。
但是实际执行的时候不是如此:- [root@ system_program]# ./psh1
- Arg[0]?pwd
- Arg[1]?
- /usr/local/src/c/system_program
- [root@ system_program]#
复制代码 这是为什么?
是bash 现在的运行机制吗?
|
|