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