Improve quality of uname/gethostname/getdomainname

This commit is contained in:
Justine Tunney 2022-09-03 19:07:19 -07:00
parent c5c4dfcd21
commit b66bd064d8
13 changed files with 334 additions and 151 deletions

View file

@ -17,26 +17,19 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall_support-sysv.internal.h"
#include "libc/errno.h"
#include "libc/str/str.h"
#define CTL_KERN 1
#define KERN_HOSTNAME 10
#define CTL_KERN 1
int gethostname_bsd(char *name, size_t len) {
char *p;
int cmd[2];
char buf[254];
size_t buflen;
cmd[0] = CTL_KERN;
cmd[1] = KERN_HOSTNAME;
buflen = sizeof(buf);
if (sysctl(cmd, 2, buf, &buflen, NULL, 0) == -1) {
if (errno == ENOMEM) errno = ENAMETOOLONG;
int gethostname_bsd(char *name, size_t len, int kind) {
int cmd[2] = {CTL_KERN, kind};
if (sysctl(cmd, 2, name, &len, 0, 0) != -1) {
return 0;
} else {
if (errno == ENOMEM) {
errno = ENAMETOOLONG;
}
return -1;
}
strncpy(name, buf, len);
name[len - 1] = '\0';
if ((p = strchr(name, '.'))) *p = '\0';
return 0;
}