C command line password

So, I'm trying to create a C program where you have to enter a password on the command line, for example. / login password1 And if the password is password1, it will say something. If not, it prints another message. This is the code I have now:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  if (argc < 2) {
    printf("usage: %s <password>\n", argv[0]);
  }
  char pass = "password";
  if (argc == pass) {
    printf("Right\n");
  } else {
    printf("Wrong\n");
  }
}

But that will not work.

+3
source share
2 answers
char pass = "password";

You are trying to assign a string char. This will not work! Instead, you need to declare passas char[]follows:

char pass[] = "password";

The following problem:

if(argc == pass)

argc- the number of command line arguments passed to your program (including the name of the program as the first). You want argvone that contains the actual arguments. In particular, you probably want to argv[1].

argv[1] == pass, . , strcmp(). 0, ( , ). , , ; . (sniped from @caf)

, :

if (strcmp(argv[1], pass) == 0)

, . , . , .

+4

argc - "password".

argv[1] argc. strcmp, .

, - . (, "ps" ).

+1

Source: https://habr.com/ru/post/1783769/


All Articles