summaryrefslogtreecommitdiff
path: root/hostname-setup.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2010-05-08 15:00:53 +0200
committerLennart Poettering <lennart@poettering.net>2010-05-08 15:00:53 +0200
commitea6145dabe707ae721d036c6764c34a493cff401 (patch)
tree799217b30b0d3addbad28b61eeaa0fff0f527f10 /hostname-setup.c
parent924b4d5bcc97ee2dc395723c3b73a53f3772e2ed (diff)
hostname: unify hostname configuration file parsers a little
Diffstat (limited to 'hostname-setup.c')
-rw-r--r--hostname-setup.c73
1 files changed, 13 insertions, 60 deletions
diff --git a/hostname-setup.c b/hostname-setup.c
index 67891a52e6..4d7f32d0a7 100644
--- a/hostname-setup.c
+++ b/hostname-setup.c
@@ -32,56 +32,25 @@
#define LINE_MAX 4096
-static int read_hostname(char **hn) {
-
#if defined(TARGET_FEDORA)
- int r;
- FILE *f;
-
- assert(hn);
-
- if (!(f = fopen("/etc/sysconfig/network", "re")))
- return -errno;
-
- for (;;) {
- char line[LINE_MAX];
- char *s, *k;
-
- if (!fgets(line, sizeof(line), f)) {
- if (feof(f))
- break;
-
- r = -errno;
- goto finish;
- }
-
- s = strstrip(line);
-
- if (!startswith(s, "HOSTNAME="))
- continue;
-
- if (!(k = strdup(s+9))) {
- r = -ENOMEM;
- goto finish;
- }
-
- *hn = k;
- break;
- }
-
- r = 0;
+#define FILENAME "/etc/sysconfig/network"
+#elif defined(TARGET_SUSE)
+#define FILENAME "/etc/HOSTNAME"
+#elif defined(TARGET_DEBIAN)
+#define FILENAME "/etc/hostname"
+#elif defined(TARGET_ARCH)
+#define FILENAME "/etc/rc.conf"
+#endif
-finish:
- fclose(f);
- return r;
+static int read_hostname(char **hn) {
-#elif defined(TARGET_ARCH)
+#if defined(TARGET_FEDORA) || defined(TARGET_ARCH)
int r;
FILE *f;
assert(hn);
- if (!(f = fopen("/etc/rc.conf", "re")))
+ if (!(f = fopen(FILENAME, "re")))
return -errno;
for (;;) {
@@ -116,13 +85,13 @@ finish:
fclose(f);
return r;
-#elif defined(TARGET_SUSE)
+#elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN)
int r;
char *s, *k;
assert(hn);
- if ((r = read_one_line_file("/etc/HOSTNAME", &s)) < 0)
+ if ((r = read_one_line_file(FILENAME, &s)) < 0)
return r;
k = strdup(strstrip(s));
@@ -133,22 +102,6 @@ finish:
*hn = k;
-#elif defined(TARGET_DEBIAN)
- int r;
- char *s, *k;
-
- assert(hn);
-
- if ((r = read_one_line_file("/etc/hostname", &s)) < 0)
- return r;
-
- k = strdup(strstrip(s));
- free(s);
-
- if (!k)
- return -ENOMEM;
-
- *hn = k;
#else
#warning "Don't know how to read the hostname"