16030b585SAxel Dörfler /*
2*2dba0007SAxel Dörfler * Copyright 2002-2014, Axel Dörfler, axeld@pinc-software.de.
36030b585SAxel Dörfler * Distributed under the terms of the MIT License.
46030b585SAxel Dörfler */
56030b585SAxel Dörfler
66030b585SAxel Dörfler
76030b585SAxel Dörfler #include <errno.h>
86030b585SAxel Dörfler #include <fcntl.h>
96030b585SAxel Dörfler #include <string.h>
106030b585SAxel Dörfler #include <unistd.h>
116030b585SAxel Dörfler
126030b585SAxel Dörfler #include <FindDirectory.h>
136030b585SAxel Dörfler #include <StorageDefs.h>
146030b585SAxel Dörfler
15ae901935SOliver Tappe #include <errno_private.h>
16de49e349SIngo Weinhold #include <find_directory_private.h>
17ae901935SOliver Tappe
186030b585SAxel Dörfler
196030b585SAxel Dörfler static status_t
get_path(char * path,bool create)206030b585SAxel Dörfler get_path(char *path, bool create)
216030b585SAxel Dörfler {
22de49e349SIngo Weinhold status_t status = __find_directory(B_SYSTEM_SETTINGS_DIRECTORY, -1, create,
236030b585SAxel Dörfler path, B_PATH_NAME_LENGTH);
246030b585SAxel Dörfler if (status != B_OK)
256030b585SAxel Dörfler return status;
266030b585SAxel Dörfler
276030b585SAxel Dörfler strlcat(path, "/network", B_PATH_NAME_LENGTH);
286030b585SAxel Dörfler if (create)
296030b585SAxel Dörfler mkdir(path, 0755);
306030b585SAxel Dörfler strlcat(path, "/hostname", B_PATH_NAME_LENGTH);
316030b585SAxel Dörfler return B_OK;
326030b585SAxel Dörfler }
336030b585SAxel Dörfler
346030b585SAxel Dörfler
356030b585SAxel Dörfler extern "C" int
sethostname(const char * hostName,size_t nameSize)366030b585SAxel Dörfler sethostname(const char *hostName, size_t nameSize)
376030b585SAxel Dörfler {
386030b585SAxel Dörfler char path[B_PATH_NAME_LENGTH];
396030b585SAxel Dörfler if (get_path(path, false) != B_OK) {
40ae901935SOliver Tappe __set_errno(B_ERROR);
416030b585SAxel Dörfler return -1;
426030b585SAxel Dörfler }
436030b585SAxel Dörfler
446030b585SAxel Dörfler int file = open(path, O_WRONLY | O_CREAT, 0644);
456030b585SAxel Dörfler if (file < 0)
466030b585SAxel Dörfler return -1;
476030b585SAxel Dörfler
486030b585SAxel Dörfler nameSize = min_c(nameSize, MAXHOSTNAMELEN);
49*2dba0007SAxel Dörfler ftruncate(file, nameSize + 1);
506030b585SAxel Dörfler
516030b585SAxel Dörfler if (write(file, hostName, nameSize) != (ssize_t)nameSize
526030b585SAxel Dörfler || write(file, "\n", 1) != 1) {
536030b585SAxel Dörfler close(file);
546030b585SAxel Dörfler return -1;
556030b585SAxel Dörfler }
566030b585SAxel Dörfler
576030b585SAxel Dörfler close(file);
586030b585SAxel Dörfler return 0;
596030b585SAxel Dörfler }
606030b585SAxel Dörfler
616030b585SAxel Dörfler
626030b585SAxel Dörfler extern "C" int
gethostname(char * hostName,size_t nameSize)636030b585SAxel Dörfler gethostname(char *hostName, size_t nameSize)
646030b585SAxel Dörfler {
656030b585SAxel Dörfler // look up hostname from network settings hostname file
666030b585SAxel Dörfler
676030b585SAxel Dörfler char path[B_PATH_NAME_LENGTH];
686030b585SAxel Dörfler if (get_path(path, false) != B_OK) {
69ae901935SOliver Tappe __set_errno(B_ERROR);
706030b585SAxel Dörfler return -1;
716030b585SAxel Dörfler }
726030b585SAxel Dörfler
736030b585SAxel Dörfler int file = open(path, O_RDONLY);
746030b585SAxel Dörfler if (file < 0)
756030b585SAxel Dörfler return -1;
766030b585SAxel Dörfler
776030b585SAxel Dörfler nameSize = min_c(nameSize, MAXHOSTNAMELEN);
786030b585SAxel Dörfler
796030b585SAxel Dörfler int length = read(file, hostName, nameSize - 1);
806030b585SAxel Dörfler close(file);
816030b585SAxel Dörfler
826030b585SAxel Dörfler if (length < 0)
836030b585SAxel Dörfler return -1;
846030b585SAxel Dörfler
856030b585SAxel Dörfler hostName[length] = '\0';
866030b585SAxel Dörfler
876030b585SAxel Dörfler char *end = strpbrk(hostName, "\r\n\t");
886030b585SAxel Dörfler if (end != NULL)
896030b585SAxel Dörfler end[0] = '\0';
906030b585SAxel Dörfler
916030b585SAxel Dörfler return 0;
926030b585SAxel Dörfler }
936030b585SAxel Dörfler
94