tst-ntoa.c 671 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <arpa/inet.h>
  4. #include <netinet/in.h>
  5. static int
  6. test (unsigned int inaddr, const char *expected)
  7. {
  8. struct in_addr addr;
  9. char *res;
  10. int fail;
  11. addr.s_addr = htonl (inaddr);
  12. res = inet_ntoa (addr);
  13. fail = strcmp (res, expected);
  14. printf ("%#010x -> \"%s\" -> %s%s\n", inaddr, res,
  15. fail ? "fail, expected" : "ok", fail ? expected : "");
  16. return fail;
  17. }
  18. int
  19. main (void)
  20. {
  21. int result = 0;
  22. result |= test (INADDR_LOOPBACK, "127.0.0.1");
  23. result |= test (INADDR_BROADCAST, "255.255.255.255");
  24. result |= test (INADDR_ANY, "0.0.0.0");
  25. result |= test (0xc0060746, "192.6.7.70");
  26. return result;
  27. }