fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6. const char *data_ptr = "user=foo&password=bar"; // getenv("QUERY_STRING");
  7. char data[strlen(data_ptr) + 1]; // +1 for the string terminator
  8. strcpy(data, data_ptr);
  9.  
  10. char *name = strtok(data, "&");
  11. while (name != NULL)
  12. {
  13. char *value_sep = strchr(name, '=');
  14. if (value_sep != NULL)
  15. {
  16. *value_sep = '\0';
  17. char *value = ++value_sep;
  18.  
  19. printf("Name = %s\r\n", name);
  20. printf("Value = %s\r\n", value);
  21. }
  22. else
  23. {
  24. printf("Malformed query string\r\n");
  25. }
  26.  
  27. name = strtok(NULL, "&");
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
Name = user
Value = foo
Name = password
Value = bar