gethostname.c 560 B

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