vm: fix a null dereference on out-of-memory
[minix.git] / lib / libc / gen / minix / gethostname.c
blobeba3f28400da137ce56ce1c0faa8232fab569dbf
1 /* gethostname(2) system call emulation */
2 #include <sys/cdefs.h>
3 #include "namespace.h"
5 #include <sys/types.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
11 #ifdef __weak_alias
12 __weak_alias(gethostname, _gethostname)
13 #endif
15 #define HOSTNAME_FILE "/etc/hostname.file"
17 int gethostname(char *buf, size_t len)
19 int fd;
20 int r;
21 char *nl;
23 if ((fd= open(HOSTNAME_FILE, O_RDONLY)) < 0) return -1;
25 r= read(fd, buf, len);
26 close(fd);
27 if (r == -1) return -1;
29 buf[len-1]= '\0';
30 if ((nl= strchr(buf, '\n')) != NULL) *nl= '\0';
31 return 0;