summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hostname/hostname.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/hostname/hostname.c b/hostname/hostname.c
index d19e4b8..b994a07 100644
--- a/hostname/hostname.c
+++ b/hostname/hostname.c
@@ -132,7 +132,7 @@ int do_set_nodename(bool file, const char *value)
/* This is insane. There is no validation whatsoever.
* However, strace on net-tools hostname(1) shows this to be
* the exact algorithm used, so... */
- char buf[1024];
+ char buf[1025];
int fildes = open(value, O_RDONLY);
ssize_t result = 0;
@@ -145,7 +145,8 @@ int do_set_nodename(bool file, const char *value)
while(true)
{
char *curr = buf;
- result = read(fildes, buf, sizeof buf);
+ memset(buf, 0, sizeof buf);
+ result = read(fildes, buf, sizeof buf - 1);
switch(result)
{
case -1:
@@ -158,14 +159,21 @@ int do_set_nodename(bool file, const char *value)
}
do
{
- len = result;
+ len = strnlen(curr, result);
char *pos = strchr(curr, '\n');
if(pos)
{
len = pos - curr;
}
- sethostname(curr, (len > 63) ? 63 : len);
- } while((curr = strchr(curr, '\n')));
+ if(len == 0 || *curr == '#') continue;
+ while(len > 63)
+ {
+ sethostname(curr, 63);
+ len -= 63;
+ curr += 63;
+ }
+ sethostname(curr, len);
+ } while(curr = strchr(curr, '\n') + 1, curr != 0x1);
}
}