|
发表于 2003-9-24 17:47:58
|
显示全部楼层
[code:1]
#define _GNU_SOURCE
#define MAX_ARGS 10
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
int main (int argc,char *argv[])
{
size_t size=0;
pid_t pid;
const char delimiters[]=" \t";
int result;
char *args=NULL;
char *myargv[MAX_ARGS+2];
char *token;
while(1)
{
if(args)
{
free(args);
args=NULL;
}
fputs("TEST>",stdout);
getline(&args,&size,stdin);
memset(strchr(args,'\n'),'\0',1);
token=strtok(args,delimiters);
int i=0;
do{
myargv[i++]=token;
if(i>MAX_ARGS+1) break;
}
while(token=strtok(NULL,delimiters));
myargv[i]=NULL;
if(!myargv[0]) continue;
if(!strcmp(myargv[0],"OUT")) break;
int j;
pid = fork ();
if (pid > 0)
{
waitpid (pid,&result,0);
}
else if (pid == 0)
{
if(execvp (myargv[0],&myargv[0])!=0)
_exit(EXIT_FAILURE);
}
else
printf ("fail\n");
if(result)
printf("ERROR!!\n");
}
free(args);
return 0;
}
[/code:1] |
|