Posted on 2007-11-06 23:18
山泉弯延 阅读(293)
评论(0) 编辑 收藏 引用 所属分类:
UNIX Programming
#include "apue.h"
#include <sys/wait.h>
extern char **environ;
static void sign_Int(int signnal)
{printf("Be interrupted\n%%");}
static void sign_Quit(int signnal)
{printf("quit\n%%");
}
int
main(void)
{
char *argv[20];
pid_t pid;
int status;
if(signal(SIGINT,sign_Int)==SIG_ERR)
err_sys("signal error\n");
if(signal(SIGQUIT,sign_Quit)==SIG_ERR)
err_sys("signal error\n");
printf("%%");
while (1)
{ char buf[MAXLINE];
int index=0,i=0;
if(fgets(buf,MAXLINE,stdin)==NULL)
break;
while( buf[i] != '\0'&&buf[i]!='\n' )
{
if(buf[i] != ' ' )
{
argv[index]= buf+i;
index++;
for(;buf[i]!= ' '&&buf[i]!='\n';i++);
if(buf[i]=='\n')
break;
buf[i]='\0';
i++;
while(buf[i]==' ')
i++;
}
}
buf[i]='\0';
argv[index] = NULL;
if((pid = fork() ) < 0 )
err_sys("fork error\n");
else if( pid == 0 )
{
execve(argv[0],argv,environ);
err_ret("coundn't execute:%s",argv[0]);
exit(127);
}
if(( pid = waitpid(pid,&status,0)) < 0)
err_sys("waitpid error\n");
printf("%%");
}
exit(0);
}