Kyle Ramos Kyle Ramos - 1 month ago 6
C Question

C/ HTML - Printing the username and password from an HTML Form

We were asked to create a login page using HTML, C/CGI and CSS. I wanted to start with the basics, HTML and C/CGI first so I created this:

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

int main(void)
{
char *data;
char *user;
char *password;
printf("Content-type:text/html\r\n\r\n");
printf("<!DOCTYPE html><html><head><title>Welcome!</title></head><body>");
data = getenv("QUERY_STRING");
if (data) {
sscanf(data,"user=%s&password=%s", &user, &password);
printf("Hello Mr./Ms. %s\n",user);
printf("You entered your password %s\n",password);
}
printf("<form action='http://localhost/inch.html'>");
printf("<input type=submit value='Compute'>");
printf("</body></html>");
exit(EXIT_SUCCESS);
}


However, the compiler is showing me this problem:


warning: format ‘%s’ expects argument of type ‘char *’, but argument 3
has type ‘char **’ [-Wformat=]
sscanf(data,"user=%s&password=%s", &user, &password);


Also, the HTML is showing me Error 500.

Any ideas on how to fix this? I'm fairly new at this HTML/CGI thing, and I would appreciate help along the way. Thanks!

Answer

The problem is, sscanf needs a pointer to char:

int sscanf ( const char * s, const char * format, ...);

Try this:

char user[100];
char password[100];
// ...
sscanf(data,"user=%s&password=%s", user, password);
Comments