ss321c
ss321c

Reputation: 799

error in use of pointers to store command line arguments in argv1

I am trying to implement a shell and on command prompt when a user is entering multiple commands then I want those commands to to be stored in argv1 array as following

argv1[0]="ls -al"  
argv1[1]=command 2 with arguments  
argv1[2]=command 3 with arguments

what I have written is following

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LEN 1024

int main()
{
        char line[BUFFER_LEN];  //get command line
        char *argv[100];        //user command
        char **argv1[10];       //user command
        int n = 0;
        int argc;
        fgets(line, BUFFER_LEN, stdin);
        char *token;            //split command into separate strings
        token = strtok(line, " ");
        int i = 0;
        while (token != NULL) {
                argv[i] = token;
                token = strtok(NULL, " ");
                i++;
        }
        argv[i] = NULL;         //set last value to NULL for execvp
        argv1[n] = argv;        //we are storing commands in format argv1={"ls -al","wc","tee"}
        n++;                    //for argv1 tracking
        printf("value in argv1 is %s\n", argv[n - 1]);
        argc = i;               //get arg count
        for (i = 0; i < argc; i++) {
                printf("%s\n", argv[i]);        //print command/args
        }

}

I want this complete command with arguments to go in argv1[0] how to do
this. This is what I am not able to think of.

I want to do some thing of this sort

int
main(int argc, char *argv[])
{
        char *ls[] = {"ls", "-al", NULL};
        char *rev[] = {"rev", NULL};
        char *nl[] = {"nl", NULL};
        char *cat[] = {"cat", "-e", NULL};
        char **cmd[] = {ls, rev, nl, cat, NULL};

        pipeline(cmd);
        return (0);
}
~

Inside the pipeline function I will pass commands one by one to be executed in a while loop with muliple pipes and file descriptors open.The will replace the outputs in execlp calls from stdout to pipes in while loop, The commands in original code are coming in argv[0]=ls , argv[1]=-al, where as I want to implement
some_pointer=argv[0]+argv[1]
here some pointer is the command that I will pass on in main function pipeline i.e. pipeline(somepointer), rest code will execute on full command passed with arguments in pipleine function,this is what I want to achieve how to implement is what I am not able to understand. In the real implementation I will be taking input via fgets from user and not as charachter arrays *ls,*nl etc I mentioned above.

Upvotes: 1

Views: 140

Answers (2)

Fabien Lacorre
Fabien Lacorre

Reputation: 61

He doesn't want get argv in the main but read inputs after to emulate a shell.

Check getopt function maybe that can help you

Upvotes: 1

Andrew Jones
Andrew Jones

Reputation: 13

You will want to make use of argv and argc. Here's something I whipped up quickly based on your attempt.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LEN 256

int main(int argc, char *argv[])
{
  /* Command and arguments */
  char *cmd = NULL;
  char args[argc][BUFFER_LEN] ;

  /* Print command */
  printf("Command was: %s. ", argv[1]);

  /* Return early if no arguments */
  if(argc == 2) return 1;

  /* Copy arguments */
  for(int i=2; i<argc; i++)
  {
    strncpy(args[i], argv[i], sizeof(argv[i]));
  }

  /* Print args */
  for(int j=2; j<argc; j++)
  {
    printf("Arg %d is %s ", j, argv[j]);
  }

  return 0;
}

Sample:

$ ./test ls
Command was: ls.

$ ./test ls -al
Command was: ls. Arg 2 is -al

Upvotes: 1

Related Questions