Commit cee61c26 authored by Ulrich Huber's avatar Ulrich Huber
Browse files

[add] Syscalls gethostname and sethostname

parent f1451490
......@@ -7,4 +7,8 @@
#include <sys/types.h>
#define HOST_NAME_MAX 64
int gethostname(char *name, size_t len);
int sethostname(const char *name, size_t len);
#endif //TOKI_UNISTD_H
......@@ -7,6 +7,7 @@ target_sources(
"${CMAKE_CURRENT_LIST_DIR}/atof.c"
"${CMAKE_CURRENT_LIST_DIR}/atoi.c"
"${CMAKE_CURRENT_LIST_DIR}/calloc.c"
"${CMAKE_CURRENT_LIST_DIR}/hostname.c"
"${CMAKE_CURRENT_LIST_DIR}/rand.c"
"${CMAKE_CURRENT_LIST_DIR}/realloc.c"
"${CMAKE_CURRENT_LIST_DIR}/strtol.c"
......
#include <stddef.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
char hostname[HOST_NAME_MAX + 1];
int gethostname(char *name, size_t len) {
size_t hostname_len = strlen(hostname);
if (name == NULL) {
errno = EFAULT;
return -1;
}
if (len < 0) {
errno = EINVAL;
return -1;
}
if (hostname_len >= len) {
errno = ENAMETOOLONG;
return -1;
}
// Copy hostname
strncpy(name, hostname, len);
return 0;
}
int sethostname(const char *name, size_t len) {
if (name == NULL) {
errno = EFAULT;
return -1;
}
if (len < 0 || len > HOST_NAME_MAX) {
errno = EINVAL;
return -1;
}
// We only copy the hostname without terminating character as we can not trust for one to exist
strncpy(hostname, name, len);
// Terminate the hostname - we already made sure it has a valid length
hostname[len] = '\0';
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment