mach_name.c 866 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <unistd.h>
  2. #include <grass/gis.h>
  3. #include <grass/config.h>
  4. #ifdef HAVE_SYS_UTSNAME_H
  5. #include <sys/utsname.h>
  6. #endif
  7. /* this routine returns a name for the machine
  8. * it returns the empty string, if this info
  9. * not available (it never returns a NULL pointer)
  10. *
  11. * the name is stored in a static array and the pointer to this
  12. * array is returned.
  13. *
  14. */
  15. const char *G__machine_name(void)
  16. {
  17. static int initialized;
  18. static char name[128];
  19. if (G_is_initialized(&initialized))
  20. return name;
  21. #if defined(HAVE_GETHOSTNAME)
  22. gethostname(name, sizeof(name));
  23. name[sizeof(name) - 1] = 0; /* make sure NUL terminated */
  24. #elif defined(HAVE_SYS_UTSNAME_H)
  25. {
  26. struct utsname attname;
  27. uname(&attname);
  28. strcpy(name, attname.nodename);
  29. }
  30. #else
  31. strcpy(name, "unknown");
  32. #endif
  33. G_initialize_done(&initialized);
  34. return name;
  35. }