#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <glob.h>
//#include <string.h>
#define DELIMS " \t\n"
static void prompt(void)
{
printf("mysh-0.1$ ");
}
static int parse(const char* line,glob_t *globres)
{
printf("line:%s\n",line);
char *tok;
int i =0;
while(1)
{
tok = strsep(&line,DELIMS);
if( tok == NULL)
break;
// printf("tok:%s:len:%ld\n",tok,strlen(tok));
if( tok[0] == '\0')
continue;
glob(tok,GLOB_NOCHECK|GLOB_APPEND * i,NULL,globres);
i = 1;
}
return 1;
}
int main(int argc,char ** argv)
{
char * linebuf = NULL;
size_t linebuf_size = 0;
glob_t globres;
while(1)
{
prompt();
if (getline(&linebuf,&linebuf_size,stdin)<0)
break;
parse(linebuf,&globres);
if(0)
{
}
else
{
pid_t pid = fork();
if( pid <0)
{
perror("fork()");
}
if( pid == 0)
{
execvp(globres.gl_pathv[0],globres.gl_pathv);
perror("execvp()");
exit(0);
}
}
}
exit(0);
}
如果不添加#include<string.h> 虽然编译通过但是会在 if( tok[0] == '\0') 提示段错误