From 4d57b14b5b874a34f81119fdd880a98df1175b76 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Wed, 22 May 2013 12:37:47 +0200 Subject: [PATCH] Bugfix chomp() and set_hostname() chomp(): Removed first \n instead of last \n set_hostname(): If /etc/hostname exists we need to realloc the size of the current hostname variable. Also, only try to set the hostname if !NULL Signed-off-by: Joachim Nilsson --- helpers.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/helpers.c b/helpers.c index ba73c339..6cd34b12 100644 --- a/helpers.c +++ b/helpers.c @@ -640,9 +640,9 @@ int getgroup(char *group) void chomp(char *str) { - char *x; + char *x = strrchr(str, 0x0a); - if ((x = strchr((str), 0x0a)) != NULL) + if (x) *x = 0; } @@ -652,12 +652,20 @@ void set_hostname(char *hostname) _d("Set hostname: %s", hostname); if ((fp = fopen("/etc/hostname", "r")) != NULL) { - fgets(hostname, HOSTNAME_SIZE, fp); + struct stat st; + + fstat(fileno(fp), &st); + hostname = realloc(hostname, st.st_size); + if (!hostname) + return; + + fgets(hostname, st.st_size, fp); chomp(hostname); fclose(fp); } - sethostname(hostname, strlen(hostname)); + if (hostname) + sethostname(hostname, strlen(hostname)); } /**