mirror of
https://github.com/troglobit/finit.git
synced 2026-10-14 10:19:36 +00:00
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 <troglobit@gmail.com>
This commit is contained in:
1 parent
263036045a
commit
4d57b14b5b
1 file changed
+12
-4
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user