我正在为shell编写代码,但是当我尝试运行命令cat filename.c
时,它说:
cat:filename.c
:没有此类文件或目录
即使同一命令在外壳程序外部运行。对于其他命令,例如lsmod
,它什么也不打印。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 80 /* 80 chars per line, per command */
int main(void)
{
char *args[MAX_LINE/2 + 1]; /* command line (of 80) has max of 40 arguments */
int should_run = 1;
int i, upper, j;
char *w= (char *)malloc(80*sizeof(char)) ;
char *buffer;
size_t bufsize= 80;
while (should_run){
printf("osh> ");
fflush(stdout);
getline(&w, &bufsize , stdin);
buffer = strtok (w, " ");
i=0;
while (buffer)
{
args[i++] = buffer;
buffer = strtok (NULL, " ");
}
char *ls_arg[i+1];
for(j=0;j<i;j++)
{
ls_arg[j]=args[j];
}
ls_arg[j]=NULL;
int pid;
pid= fork();
if(!pid)//child process
{
execvp(ls_arg[0], ls_arg);
}
wait(5);
}
return 0;
}