简体   繁体   中英

execvp() not executing any command in my own shell

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LINE 80 /* Maximum  length of command*/
struct node
{
 char commandName[41];
 struct node *next;
}*start=NULL;


void insert_at_beg(char cmd[])
{
 struct node *new_node,*current;

 new_node=(struct node *)malloc(sizeof(struct node));

 if(new_node == NULL)
 printf("nFailed to Allocate Memory");

 strcpy(new_node->commandName, cmd);
 new_node->next=NULL;

 if(start==NULL)
 {
  start=new_node;
  current=new_node;
 }
 else
 {
  new_node->next=start;
  start=new_node;
 }
}
void display()
{
 struct node *temp;
 temp=start;
 while(temp!=NULL)
 {
  printf("\t%s\n",temp->commandName);
  temp=temp->next;
 }
}

The above code is a linked list which is implemented using structure to implement history feature in my own shell. insert_at_beg() is used to add commands that we enter on shell as a node into the list and display() is used to display list.

Below is the main function. In this function I have created my own shell

int main()
{
 char *args[MAX_LINE/2+1]; /* Command Line Argument*/
 int should_run=1, status, i, num, error;
 pid_t pid;
 char str[41];
 char teststr[10]={"history\0"};
 char temprory[41];
 const char delimiter[2]=" ";
 char *token;

 while(should_run)
 {
i=0;
printf("osh>");
fflush(stdout);

fgets(str, sizeof str, stdin);
str[strlen(str)-1]='\0';
strncpy(temprory, str, sizeof(temprory));

token=strtok(str, " ");

    while(token)
    {
       args[i]=strdup(token);

       i++;
       token=strtok(NULL, " ");
    }

    insert_at_beg(args[0]);

    if((strcmp(args[0],teststr))==0)
    {
        display();
    }
    else
    {
        ;
    }

    pid=fork();
    if(pid<0)   // error in creating child process
    {
       printf("\tError in creating child process\n");
    }       
    else if(pid==0)    //child process will execute this block
    {

       error=execvp(args[0], args);   //execvp() always return -1 for any  
        if(error==-1)             //command I type in i.e. that command
        {                         //is not found and hence not executed.
            printf("bash:command not found\n");
        }
                 exit(1);
    }
    else        //parent process will execute this block
    {
       pid=wait(&status);

    if(!strcmp(args[0], "exit"))
    {
            should_run=0;
    }
    }
    }
   return 0;
 }

How to proceed on this issue and make it work? I am stuck in this situation since long time. Need help here.

Quoted from man 3 execvp :

The execv(), execvp(), and execvpe() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by a null pointer.

While you didn't terminate the array of args with a null pointer.

Adding this line:

args[i] = NULL;

after your loop of:

while(token)
{
   args[i]=strdup(token);
   i++;
   token=strtok(NULL, " ");
}

solves your problem. (At least on my laptop the problem is solved..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM