0

I have an assignment for my class to write a basic C Shell. I have found many pages that explain parts of it and some fully implemented code. I'm not going to copy, i'm just using it right now as a way to get started.

So I'm at the beginning of this project obviously. One example I am looking at parses the user's input and stores a pointer to the beginning of the argument in char *argv[3];

Am I just misunderstanding this or would this technically break if there are more than 3 arguments? (say /a.out arg1 arg2 arg3 etc). Would I wanna malloc this somehow?

I know a.out is considered argv[0], and the arguments argv[1] - however many there are. It's probably bad practice to have too many arguments for a program, but I still would at least want to address it as I don't know what my TA's are going to use to test my shell.

  • That example doesn't appear to be using command line arguments. I think he's getting input instead from `gets`. But yes, leave the declaration as `char *argv[]`. – JustinBlaber Nov 10 '13 at 23:22
  • "I know a.out is considered `argv[0]`, and the arguments `argv[1]` - however many there are." - maybe I am misunderstanding the question, but that's not how it works. The arguments are placed to `argv[1]...argv[N]`. The more arguments there are, the longer `argv` is. –  Nov 10 '13 at 23:23
  • what happened to argc ? are you not making any use of that ? – Raiyan Nov 10 '13 at 23:24
  • In the example, he uses 3 to allocate the space given to argv, which would mean that only a.out and 2 arguments are allowed, right? – user2278644 Nov 10 '13 at 23:28

2 Answers2

0

This is generally what you would use argc for; to check that the number of arguments passed to the program does not exceed what you were expecting.

int main(int argc, char** argv) {
  if (argc > 4) { // arbitrary number used here
    fprintf(stderr, "Too many arguments!");
    exit(1);
  }
}
yamafontes
  • 5,552
  • 1
  • 18
  • 18
0

It might or might not break, but it's buggy. What's happening if >3 arguments are present, is that you'll write past the reserved array length of 3. I.e. some arguments will be in memory that the operating system thinks is unallocated, and it might allocate other objects there.

There should be a check in parse() to see if more than 3 arguments are present, and if so stop parsing with an error message.

thpani
  • 403
  • 6
  • 15
  • That's what I thought. Would there be a way to use malloc to allow for a variable number of arguments or should I go along the lines of allocating 5-10 positions and then using a check in the parse to prevent the user from entering more? – user2278644 Nov 10 '13 at 23:34
  • I don't think this is right. When an array is passed to a function, a copy of the pointer to the first element of the array is passed. I actually think having `*argv[3]` in a function declaration does nothing, it should be the same as `*argv[]`, but either way `*argv[]` or `**argv` should be used. – JustinBlaber Nov 10 '13 at 23:39
  • @user2278644: either that, or allocate a variable length array in parse(): http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html From a technical perspective all three approaches (constant size + check, variable size, malloc) are correct, it depends on your spec / expectations of your TAs. – thpani Nov 11 '13 at 09:08
  • @jucestain You cannot define an array variable in C without size nor initializer. – thpani Nov 11 '13 at 09:12