gethostname.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /* gethostname.c: minimal substitute for missing gethostname() function
  2. * created 2000-Mar-02 jmk
  3. * requires SVR4 uname() and -lc
  4. *
  5. * by Jim Knoble <[email protected]>
  6. * Copyright ? 2000 Jim Knoble
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software
  9. * and its documentation for any purpose is hereby granted without fee,
  10. * provided that the above copyright notice appear in all copies and
  11. * that both that copyright notice and this permission notice appear in
  12. * supporting documentation.
  13. *
  14. * This software is provided "as is", without warranty of any kind,
  15. * express or implied, including but not limited to the warranties of
  16. * merchantability, fitness for a particular purpose and
  17. * noninfringement. In no event shall the author(s) be liable for any
  18. * claim, damages or other liability, whether in an action of contract,
  19. * tort or otherwise, arising from, out of or in connection with the
  20. * software or the use or other dealings in the software.
  21. */
  22. #include <string.h>
  23. #include <sys/utsname.h>
  24. int gethostname(char *name, size_t len)
  25. {
  26. struct utsname u;
  27. int status = uname(&u);
  28. if (-1 != status) {
  29. strncpy(name, u.nodename, len);
  30. name[len - 1] = '\0';
  31. }
  32. return(status);
  33. }