gethostname.c 400 B

12345678910111213141516171819202122232425
  1. /* posix */
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. int
  8. gethostname(char *name, int namelen)
  9. {
  10. int n, fd;
  11. char buf[128];
  12. fd = open("/dev/sysname", O_RDONLY);
  13. if(fd < 0)
  14. return -1;
  15. n = read(fd, buf, sizeof(buf)-1);
  16. close(fd);
  17. if(n <= 0)
  18. return -1;
  19. buf[n] = 0;
  20. strncpy(name, buf, namelen);
  21. name[namelen-1] = 0;
  22. return 0;
  23. }