2 * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
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
);
50 if (write(file
, hostName
, nameSize
) != (ssize_t
)nameSize
51 || write(file
, "\n", 1) != 1) {
62 gethostname(char *hostName
, size_t nameSize
)
64 // look up hostname from network settings hostname file
66 char path
[B_PATH_NAME_LENGTH
];
67 if (get_path(path
, false) != B_OK
) {
72 int file
= open(path
, O_RDONLY
);
76 nameSize
= min_c(nameSize
, MAXHOSTNAMELEN
);
78 int length
= read(file
, hostName
, nameSize
- 1);
84 hostName
[length
] = '\0';
86 char *end
= strpbrk(hostName
, "\r\n\t");