chiark / gitweb /
hostname: read hostname for Gentoo
[elogind.git] / hostname-setup.c
index 5f3ee77ac01a61d3ffc94745d5609f967f47ba46..b8551d9802feb48715d8ebffdfa47c2d0c9b8777 100644 (file)
 
 #define LINE_MAX 4096
 
+#if defined(TARGET_FEDORA)
+#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"
+#elif defined(TARGET_GENTOO)
+#define FILENAME "/etc/conf.d/hostname"
+#endif
+
 static int read_hostname(char **hn) {
 
-#if defined(TARGET_FEDORA)
+#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
         int r;
         FILE *f;
 
         assert(hn);
 
-        if (!(f = fopen("/etc/sysconfig/network", "re")))
+        if (!(f = fopen(FILENAME, "re")))
                 return -errno;
 
         for (;;) {
@@ -57,7 +69,7 @@ static int read_hostname(char **hn) {
 
                 s = strstrip(line);
 
-                if (!startswith(s, "HOSTNAME="))
+                if (!startswith_no_case(s, "HOSTNAME="))
                         continue;
 
                 if (!(k = strdup(s+9))) {
@@ -65,6 +77,11 @@ static int read_hostname(char **hn) {
                         goto finish;
                 }
 
+                if (!(k = delete_chars(k, "\"\'"))) {
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
                 *hn = k;
                 break;
         }
@@ -75,13 +92,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));
@@ -92,22 +109,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"