fork(1) download
  1. int get_system_ns(char **ns) {
  2. int ret = -1;
  3. int i = 0;
  4. char *buf = NULL;
  5. char addr[INET_ADDRSTRLEN] = { 0 };
  6.  
  7. // Init resolver
  8. ret = res_init();
  9.  
  10. if (0 != ret) {
  11. debug("Can't initialize resolver");
  12. } else {
  13. // Maximum required memory to store all nameserver addresses
  14. buf = calloc(INET_ADDRSTRLEN * MAXNS, sizeof(char));
  15.  
  16. if (NULL == buf) {
  17. debug("Memory allocaton error");
  18.  
  19. } else {
  20. for (i = 0; i < MAXNS; i++) {
  21.  
  22. // Try to get human readable nameserver address
  23. const char *s = inet_ntop(AF_INET,
  24. &_res.nsaddr_list[i].sin_addr,
  25. addr,
  26. INET_ADDRSTRLEN);
  27. if (NULL == s) {
  28. debug("Can't get address of nameserver #%d: %s", i + 1, strerror(errno));
  29. break;
  30.  
  31. } else {
  32. debug("Found address of nameserver #%d: %s", i + 1, addr);
  33.  
  34. if (i > 0) {
  35. strcat(buf, ","); // Prepend next address by comma first
  36. }
  37.  
  38. strcat(buf, addr);
  39. }
  40. }
  41.  
  42. // Copy the result to output argument
  43. *ns = calloc(strlen(buf) + 1, sizeof(char));
  44.  
  45. if (NULL == *ns) {
  46. debug("Memory allocation error");
  47. } else {
  48. strcpy(*ns, buf);
  49. ret = 0;
  50. }
  51.  
  52. // Cleanup
  53. free(buf);
  54. }
  55. }
  56.  
  57. return ret;
  58. }
  59.  
Compilation error #stdin compilation error #stdout 0s 2292KB
stdin
Standard input is empty
compilation info
prog.c: In function 'get_system_ns':
prog.c:4:15: error: 'NULL' undeclared (first use in this function)
   char *buf = NULL;
               ^
prog.c:4:15: note: each undeclared identifier is reported only once for each function it appears in
prog.c:5:14: error: 'INET_ADDRSTRLEN' undeclared (first use in this function)
   char  addr[INET_ADDRSTRLEN] = { 0 };
              ^
prog.c:8:9: warning: implicit declaration of function 'res_init' [-Wimplicit-function-declaration]
   ret = res_init();
         ^
prog.c:11:5: warning: implicit declaration of function 'debug' [-Wimplicit-function-declaration]
     debug("Can't initialize resolver");
     ^
prog.c:14:11: warning: implicit declaration of function 'calloc' [-Wimplicit-function-declaration]
     buf = calloc(INET_ADDRSTRLEN * MAXNS, sizeof(char));
           ^
prog.c:14:11: warning: incompatible implicit declaration of built-in function 'calloc'
prog.c:14:11: note: include '<stdlib.h>' or provide a declaration of 'calloc'
prog.c:14:36: error: 'MAXNS' undeclared (first use in this function)
     buf = calloc(INET_ADDRSTRLEN * MAXNS, sizeof(char));
                                    ^
prog.c:23:25: warning: implicit declaration of function 'inet_ntop' [-Wimplicit-function-declaration]
         const char *s = inet_ntop(AF_INET,
                         ^
prog.c:23:35: error: 'AF_INET' undeclared (first use in this function)
         const char *s = inet_ntop(AF_INET,
                                   ^
prog.c:24:36: error: '_res' undeclared (first use in this function)
                                   &_res.nsaddr_list[i].sin_addr,
                                    ^
prog.c:28:67: warning: implicit declaration of function 'strerror' [-Wimplicit-function-declaration]
           debug("Can't get address of nameserver #%d: %s", i + 1, strerror(errno));
                                                                   ^
prog.c:28:76: error: 'errno' undeclared (first use in this function)
           debug("Can't get address of nameserver #%d: %s", i + 1, strerror(errno));
                                                                            ^
prog.c:35:13: warning: implicit declaration of function 'strcat' [-Wimplicit-function-declaration]
             strcat(buf, ","); // Prepend next address by comma first
             ^
prog.c:35:13: warning: incompatible implicit declaration of built-in function 'strcat'
prog.c:35:13: note: include '<string.h>' or provide a declaration of 'strcat'
prog.c:38:11: warning: incompatible implicit declaration of built-in function 'strcat'
           strcat(buf, addr);
           ^
prog.c:38:11: note: include '<string.h>' or provide a declaration of 'strcat'
prog.c:43:20: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
       *ns = calloc(strlen(buf) + 1, sizeof(char));
                    ^
prog.c:43:20: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:43:20: note: include '<string.h>' or provide a declaration of 'strlen'
prog.c:48:9: warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration]
         strcpy(*ns, buf);
         ^
prog.c:48:9: warning: incompatible implicit declaration of built-in function 'strcpy'
prog.c:48:9: note: include '<string.h>' or provide a declaration of 'strcpy'
prog.c:53:7: warning: implicit declaration of function 'free' [-Wimplicit-function-declaration]
       free(buf);
       ^
prog.c:53:7: warning: incompatible implicit declaration of built-in function 'free'
prog.c:53:7: note: include '<stdlib.h>' or provide a declaration of 'free'
prog.c:5:9: warning: unused variable 'addr' [-Wunused-variable]
   char  addr[INET_ADDRSTRLEN] = { 0 };
         ^
stdout
Standard output is empty