fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. int get_value_from_response(char *dst, int len, const char *response, const char *key) {
  7. // make a copy of response since strtok_r modifies input
  8. const char kPairDelimiter[]="\n";
  9. char *resp = strdup(response);
  10. char *saveptr = NULL;
  11. char *line = strtok_r(resp, kPairDelimiter, &saveptr);
  12. size_t key_len = strlen(key);
  13. while (line) {
  14. if (!strncmp(line, key, key_len)) {
  15. snprintf(dst, len, "%s", line + key_len);
  16. free(resp);
  17. return 0;
  18. }
  19. line = strtok_r(nullptr, kPairDelimiter, &saveptr);
  20. }
  21. free(resp);
  22. return -1;
  23. }
  24.  
  25.  
  26. int main(void) {
  27. char resp[]="bssid=00:11:22:33:44:55\nssid=hello\npassword=world";
  28. char bssid[20]={0};
  29. size_t bssid_len = 15;
  30. const char kKeyBSSID[] = "bssid=";
  31. get_value_from_response(bssid, bssid_len, resp, kKeyBSSID);
  32. printf("bssid: %s", bssid);
  33. // your code goes here
  34. return 0;
  35. }
  36.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'get_value_from_response':
prog.c:9:16: warning: implicit declaration of function 'strdup' [-Wimplicit-function-declaration]
   char *resp = strdup(response);
                ^
prog.c:9:16: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
prog.c:11:16: warning: implicit declaration of function 'strtok_r' [-Wimplicit-function-declaration]
   char *line = strtok_r(resp, kPairDelimiter, &saveptr);
                ^
prog.c:11:16: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
prog.c:19:21: error: 'nullptr' undeclared (first use in this function)
     line = strtok_r(nullptr, kPairDelimiter, &saveptr);
                     ^
prog.c:19:21: note: each undeclared identifier is reported only once for each function it appears in
stdout
Standard output is empty