2

This method takes a vector (inputVector, member variable) and splits it into char*[]'s. Whenever you come across a ";" in the vector, print out the last set of char*s stored in args. Even though the vector size is 14, the loop exits on the 5th loop.

Vector data (newline separates items):
/bin/echo
killroy
was
here;
;
xyzzy
;
nonexistent-program
;
/bin/true
;
/bin/false
;

void TrivialShell::splitArguments() {

    char* args[MAX_ARGS];

    int inputVectorIdx = 0;
    int currentArgsIdx = 0;
    int startingArgsIdx = 0;

    while (inputVectorIdx < inputVector.size()) {

        if (inputVector[inputVectorIdx] == ";") {
            for (int k = startingArgsIdx; k <= currentArgsIdx; k++) {
                cout << args[k];
            }
            startingArgsIdx = currentArgsIdx + 1;
        }

        else {
            args[currentArgsIdx] = 
                const_cast<char*>(inputVector[inputVectorIdx].c_str());
        }

        inputVectorIdx++;
        currentArgsIdx++;
    }
}
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • But does it print the first four properly before exiting? –  Apr 11 '13 at 23:45
  • 2
    There's not enough code here to determine how it works – Patashu Apr 11 '13 at 23:46
  • Is this a homework assignment? Is there a reason you're converting your `std::string`s to `char*`s for this function? – JBentley Apr 11 '13 at 23:47
  • what type is your inputVector? – 4pie0 Apr 11 '13 at 23:49
  • It prints the first 4 correctly. How is this not enough code? The vector is populated correctly. This is part of a homework assignment, I am converting the strings so that I can pass them into execv(args[0], args); – user2145808 Apr 11 '13 at 23:49
  • show how you declare & initialize vector – 4pie0 Apr 11 '13 at 23:50
  • Have a look at [this answer](http://stackoverflow.com/a/236803/1227469) which solves your problem without C-style code in a much simpler way. Just use `std::string::c_str` at the point you actually need the `char*`, rather than in your function. – JBentley Apr 11 '13 at 23:51

2 Answers2

1
for (int k = startingArgsIdx; k < currentArgsIdx; k++) {
 //                             ^^
                cout << args[k];
}

You are trying to print one too much. You haven't said it but I am sure it doesn't just skip the loop but exits the program.

  • Of course! Thanks so much, I knew it was something minor. Fixed it – user2145808 Apr 12 '13 at 00:01
  • btw: in my case on my machine it contnues, it doesn't exit – 4pie0 Apr 12 '13 at 00:10
  • @cf16 Maybe it might not exit. I think if it can print whatever garbage is in that location the it will just give you jibberish and continue. Maybe IDK. –  Apr 12 '13 at 00:12
1

you have a bug. when you enter your loop

if (inputVector[inputVectorIdx] == ";") {
            for (int k = startingArgsIdx; k <= currentArgsIdx; k++) {
                cout << args[k];
            }
            startingArgsIdx = currentArgsIdx + 1;
        }

you have your index iterator currentArgsIdx bigger than your actual size of data in array args. So you are doing cout<<args[3] when there are some garbages there already. It is because you ++ your index at the end of while loop:

inputVectorIdx++;
currentArgsIdx++;

arrange this in different way or change condition to be:

for (int k = startingArgsIdx; k < currentArgsIdx; k++) {
                               ^^^
                cout << args[k];
            }
4pie0
  • 29,204
  • 9
  • 82
  • 118