-1

I was writing a user validation program and after sucessful login I am providing a shell like prompt:

int main()
{
    int argc;
    char **argv;
    char *username = malloc(50);
    char *passwd = malloc(32);
    char command[200];

    printf("Enter username:");
    scanf("%s", username);
    printf("\n");

    printf("Enter password:");
    get_passwd(passwd); //User defined function for taking the password without echoing.

// Then a authentication module is called;
    int success =  authenticate_user(username, passwd);

//After successful authentication of user I will print like:

    printf("test @ %s >", username);

// To look user that he is working on program's own terminal where user can input some command as string

   gets(command);

//Here is the main problem
// I tried using strtok() and g_shell_parse_argv () but not getting the desired result.
   return 0;

}

Other team members written program for further action based on the command string parsing. They conveyed that I have to some how parse it into argc and argv variable like in main(int argc, int **argv) function signatures formal variable.

Is there any C library to do that or any best practice hint or sample to proceed.

Finally I have to parse a string in argc and argv.

Any help is highly appreciated. Thanks.

Abhijatya Singh
  • 642
  • 6
  • 23
  • Please show your research effort till time. Please read [Ask] page first. – Sourav Ghosh Feb 02 '16 at 11:35
  • `argc` and `argv` are parameters of the `main` function that are filled in by the operating system when your program is run. In your code, they are uniniliatised local variables. How do you expect them to hold sensible data? Just naming them `argc` and `argv` doesn't make them magically refer to the command line arguments. – M Oehm Feb 02 '16 at 11:41
  • Please note, the prototype for `main` is `int main(int argc, char **argv);`. In your question you say `int **argv` – David Hoelzer Feb 02 '16 at 11:55

1 Answers1

0
  • For command line argument you have to use this,

    int main(int argc, char **argv)
    
  • In your code , you can do it like this.

    int main(int argc, char **argv)
    {
       //your remaining code
       int success =  authenticate_user(argv[1], argv[2]);
    }
    
  • So for example when you run your program like ,

    ./demo username password  (./demo abhijatya 1234)
    
  • So, argv[0] = ./demo , argv[1] = username(abhijatya ) and argv[2] = password(1234).

zion blade
  • 13
  • 7
Sagar Patel
  • 864
  • 1
  • 11
  • 22