2 * Copyright 2002-2014, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
12 #include <FindDirectory.h>
13 #include <StorageDefs.h>
15 #include <errno_private.h>
16 #include <find_directory_private.h>
20 get_path(char *path
, bool create
)
22 status_t status
= __find_directory(B_SYSTEM_SETTINGS_DIRECTORY
, -1, create
,
23 path
, B_PATH_NAME_LENGTH
);
27 strlcat(path
, "/network", B_PATH_NAME_LENGTH
);
30 strlcat(path
, "/hostname", B_PATH_NAME_LENGTH
);
36 sethostname(const char *hostName
, size_t nameSize
)
38 char path
[B_PATH_NAME_LENGTH
];
39 if (get_path(path
, false) != B_OK
) {
44 int file
= open(path
, O_WRONLY
| O_CREAT
, 0644);
48 nameSize
= min_c(nameSize
, MAXHOSTNAMELEN
);
49 ftruncate(file
, nameSize
+ 1);
51 if (write(file
, hostName
, nameSize
) != (ssize_t
)nameSize
52 || write(file
, "\n", 1) != 1) {
63 gethostname(char *hostName
, size_t nameSize
)
65 // look up hostname from network settings hostname file
67 char path
[B_PATH_NAME_LENGTH
];
68 if (get_path(path
, false) != B_OK
) {
73 int file
= open(path
, O_RDONLY
);
77 nameSize
= min_c(nameSize
, MAXHOSTNAMELEN
);
79 int length
= read(file
, hostName
, nameSize
- 1);
85 hostName
[length
] = '\0';
87 char *end
= strpbrk(hostName
, "\r\n\t");