You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.8 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/c/util/example.c.util.gethostname/example.c.util.gethostname.c
  3. *
  4. * Purpose: Implementation file for the example.c.util.gethostname project.
  5. *
  6. * Created: 25th August 2008
  7. * Updated: 27th December 2010
  8. *
  9. * Status: Wizard-generated
  10. *
  11. * License: (Licensed under the Synesis Software Open License)
  12. *
  13. * Copyright (c) 2008-2010, Synesis Software Pty Ltd.
  14. * All rights reserved.
  15. *
  16. * www: http://www.synesis.com.au/software
  17. *
  18. * ////////////////////////////////////////////////////////////////////// */
  19. /* Pantheios Header Files */
  20. #include <pantheios/pantheios.h>
  21. #include <pantheios/util/system/hostname.h>
  22. #include <pantheios/internal/safestr.h>
  23. /* STLSoft Header Files */
  24. #include <stlsoft/stlsoft.h>
  25. #include <platformstl/platformstl.h>
  26. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  27. # include <winstl/error/error_functions.h>
  28. #endif /* OS */
  29. /* Standard C Header Files */
  30. #include <errno.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #if defined(_MSC_VER) && \
  35. defined(_DEBUG)
  36. # include <crtdbg.h>
  37. #endif /* _MSC_VER) && _DEBUG */
  38. /* ////////////////////////////////////////////////////////////////////// */
  39. int main(int argc, char** argv)
  40. {
  41. PAN_CHAR_T name[101];
  42. size_t cch = pantheios_getHostName(&name[0], STLSOFT_NUM_ELEMENTS(name));
  43. /* There are three possible return values:
  44. *
  45. * 0 - indicates that no host name is available, or there was an error
  46. * when retrieving it
  47. * 101 - the buffer is not long enough
  48. * [1, 101) - the name was successfully retrieved
  49. *
  50. * The only complication when processing this is that the last error
  51. * information is held in an operating-system-specific form. On Windows,
  52. * it is available via GetLastError(); otherwise use errno
  53. */
  54. if(0 == cch)
  55. {
  56. /* not available, or a problem retrieving it */
  57. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  58. DWORD error = GetLastError();
  59. char* message;
  60. if(0 == winstl_C_format_message_alloc_a(error, NULL, &message))
  61. {
  62. /* Could not retrieve a string-form of the error, so print
  63. * out the error code (in case that might help)
  64. */
  65. fprintf(stderr, "could not elicit hostname: %lu\n", error);
  66. }
  67. else
  68. {
  69. /* Print out the error string, and release it.
  70. *
  71. * Note: in C++, these function names are a lot cleaner
  72. */
  73. fprintf(stderr, "could not elicit hostname: %s\n", message);
  74. winstl_C_format_message_free_buff_a(message);
  75. }
  76. #else /* ? OS */
  77. # ifdef PANTHEIOS_USING_SAFE_STR_FUNCTIONS
  78. char err[1001];
  79. int r = strerror_s(err, STLSOFT_NUM_ELEMENTS(err) - 1, errno);
  80. if(0 != r)
  81. {
  82. err[0] = '\0';
  83. }
  84. else
  85. {
  86. err[STLSOFT_NUM_ELEMENTS(err) - 1] = '\0';
  87. }
  88. fprintf(stderr, "could not elicit hostname: %s\n", err);
  89. # else /* ? PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
  90. fprintf(stderr, "could not elicit hostname: %s\n", strerror(errno));
  91. # endif /* PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
  92. #endif /* OS */
  93. }
  94. else if(STLSOFT_NUM_ELEMENTS(name) == cch)
  95. {
  96. /* buffer too short */
  97. fprintf(stderr, "could not elicit hostname: name longer than given buffer\n");
  98. }
  99. else
  100. {
  101. /* Success. Print it out! */
  102. fprintf(stdout, "host name: %s\n", name);
  103. return EXIT_SUCCESS;
  104. }
  105. STLSOFT_SUPPRESS_UNUSED(argc);
  106. STLSOFT_SUPPRESS_UNUSED(argv);
  107. return EXIT_FAILURE;
  108. }
  109. /* ///////////////////////////// end of file //////////////////////////// */