gethstnm.c 667 B

123456789101112131415161718192021222324252627282930313233343536
  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(strlen)
  11. libc_hidden_proto(strcpy)
  12. libc_hidden_proto(uname)
  13. int
  14. gethostname(char *name, size_t len)
  15. {
  16. struct utsname uts;
  17. if (name == NULL) {
  18. __set_errno(EINVAL);
  19. return -1;
  20. }
  21. if (uname(&uts) == -1) return -1;
  22. if (strlen(uts.nodename)+1 > len) {
  23. __set_errno(EINVAL);
  24. return -1;
  25. }
  26. strcpy(name, uts.nodename);
  27. return 0;
  28. }
  29. libc_hidden_proto(gethostname)
  30. libc_hidden_def(gethostname)