gethostname.c 722 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <sys/utsname.h>
  9. #include <errno.h>
  10. libc_hidden_proto(gethostname)
  11. /* Experimentally off - libc_hidden_proto(strlen) */
  12. /* Experimentally off - libc_hidden_proto(strcpy) */
  13. libc_hidden_proto(uname)
  14. int
  15. gethostname(char *name, size_t len)
  16. {
  17. struct utsname uts;
  18. if (name == NULL) {
  19. __set_errno(EINVAL);
  20. return -1;
  21. }
  22. if (uname(&uts) == -1) return -1;
  23. if (strlen(uts.nodename)+1 > len) {
  24. __set_errno(EINVAL);
  25. return -1;
  26. }
  27. strcpy(name, uts.nodename);
  28. return 0;
  29. }
  30. libc_hidden_def(gethostname)