Good day, I tried parsing this query string:
pkname=Paras&key=password123
with this code:
printf("Content-type:text/html\n\n");
printf("<html><body>");
data = getenv("QUERY_STRING");
if(data){
sscanf(data, "pkname=%10[^&]&key=%30[^&]&", pkname, key);
printf("%s", pkname);
printf("%s", key);
}
printf("</html></body>");
But i can’t seem to get the key right, since the its output is always null:
Paras(null)
You don’t show the definition of the variable
keybut the fact that it prints out as(null)strongly suggests that its value is NULL, and that bothprintfandsscanfare trying to protect you by not segfaulting on NULL pointers. (The GNU libc implementations do that; printf prints NULL as(null)and sscanf appears to just stop parsing.)What you probably wanted was something like:
although you might also find the
mqualifier useful, since it avoids the need to specify arbitrary field length limits.