]>
Commit | Line | Data |
---|---|---|
5fbef188 | 1 | /* Get a host configuration item kept as the whole contents of a file. |
04277e02 | 2 | Copyright (C) 1996-2019 Free Software Foundation, Inc. |
c84142e8 | 3 | This file is part of the GNU C Library. |
5fbef188 | 4 | |
c84142e8 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
5fbef188 | 9 | |
c84142e8 UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
5fbef188 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
5fbef188 | 18 | |
a5d96b25 | 19 | #include <fcntl.h> |
5fbef188 | 20 | #include <hurd.h> |
b6bebf51 | 21 | #include <hurd/lookup.h> |
5fbef188 | 22 | #include "hurdhost.h" |
8a0746ae | 23 | #include <string.h> |
5fbef188 RM |
24 | |
25 | ssize_t | |
26 | _hurd_get_host_config (const char *item, char *buf, size_t buflen) | |
27 | { | |
28 | error_t err; | |
29 | char *data; | |
30 | mach_msg_type_number_t nread, more; | |
1d721dae RM |
31 | file_t config; |
32 | ||
33 | err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0, | |
34 | item, O_RDONLY, 0, &config); | |
35 | switch (err) | |
36 | { | |
37 | case 0: /* Success; read file contents below. */ | |
38 | break; | |
39 | ||
40 | case ENOENT: /* ? Others? All errors? */ | |
41 | /* The file does not exist, so no value has been set. Rather than | |
42 | causing gethostname et al to fail with ENOENT, give an empty value | |
43 | as other systems do before sethostname has been called. */ | |
44 | if (buflen != 0) | |
45 | *buf = '\0'; | |
46 | return 0; | |
47 | ||
48 | default: | |
49 | return __hurd_fail (err); | |
50 | } | |
5fbef188 RM |
51 | |
52 | data = buf; | |
d5a0160b | 53 | nread = buflen; |
5fbef188 RM |
54 | err = __io_read (config, &data, &nread, -1, buflen); |
55 | if (! err) | |
56 | /* Check if there is more in the file we didn't read. */ | |
57 | err = __io_readable (config, &more); | |
58 | __mach_port_deallocate (__mach_task_self (), config); | |
59 | if (err) | |
60 | return __hurd_fail (err); | |
61 | if (data != buf) | |
62 | { | |
63 | memcpy (buf, data, nread); | |
64 | __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread); | |
65 | } | |
66 | ||
dcc7b756 | 67 | /* If the file is empty, give an empty value. */ |
507fe027 | 68 | if (nread == 0 && more == 0) |
dcc7b756 RM |
69 | { |
70 | if (buflen != 0) | |
71 | *buf = '\0'; | |
72 | return 0; | |
73 | } | |
fbfdeab3 | 74 | |
5fbef188 | 75 | /* Remove newlines in case someone wrote the file by hand. */ |
8c654609 | 76 | while (nread > 0 && buf[nread - 1] == '\n') |
5fbef188 RM |
77 | buf[--nread] = '\0'; |
78 | ||
fbfdeab3 MK |
79 | /* Null-terminate the result if there is enough space. */ |
80 | if (nread < buflen) | |
81 | buf[nread] = '\0'; | |
82 | else | |
507fe027 | 83 | if (nread != 0 && buf[nread - 1] != '\0') |
fbfdeab3 MK |
84 | more = 1; |
85 | ||
5fbef188 RM |
86 | if (more) |
87 | /* If we didn't read the whole file, tell the caller to use a bigger | |
88 | buffer next time. */ | |
89 | return __hurd_fail (ENAMETOOLONG); | |
90 | ||
91 | return nread; | |
92 | } |