0

I created a parseCmd method for my simpleShell program in C, and store each argument before a delimiter whitespace to be stored in my args[] array. However, I am trying to add arguments with their respective parameters into a linked list, but I am having trouble obtaining them.

For example, if I type ls, I want:

args[0] = "ls";

And when I type ls -l, I want;

args[0] = "ls";
args[1] = "-l";

What I am trying to do here is: if a "-" argument is detected, append it to the previous argument "ls" and save as a separate string "ls -l" to be stored into a linkedList (already implemented).

Here is my method.

void parseCmd(char* cmd, char** args)
{       
    int i;

    for(i = 0; i < MAX_LINE; i++) {
        args[i] = strsep(&cmd, " ");

        if (args[i] != NULL)
            printf("--> %s \n",args[i]);

        if(args[i] == NULL) break;
    }
}

EDIT:

I tried the following

if (strchr(args[i], '-'))
    printf("--> %s \n", args[i]);

But I am getting a seg fault.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sean
  • 1,283
  • 9
  • 27
  • 43
  • 1
    and your question is what? – Tony The Lion Sep 12 '15 at 19:48
  • What is the question? You might find the answer just at the same time you will find the question. – 4pie0 Sep 12 '15 at 19:49
  • I am trying to concatenate two strings in a string array, only if the 2nd value in the string array begins with a '-' so I may append the -(parameter) with its subsequent arguements – Sean Sep 12 '15 at 19:52

1 Answers1

0

String is an array of characters. You realize that args is a char**, so basically it is an array of arrays. You can check if args entry contains '-', if it is true then you can do a simple string concat and add that value to args. Check the value of the first character of string.

Programmatically,

if(args[i][0] == '-')
    <Insert code for concatenation>
abhishek_M
  • 1,110
  • 11
  • 19