chiark / gitweb /
path-util: unify code for detecting OS trees
[elogind.git] / src / shared / hwclock.c
index d9d5600ff34052db0bc03b5075a6e1ddcf24e71e..9076d8ffc76b850e20d280d876b6c241e86c8074 100644 (file)
@@ -41,6 +41,7 @@
 #include "log.h"
 #include "strv.h"
 #include "hwclock.h"
+#include "fileio.h"
 
 static int rtc_open(int flags) {
         int fd;
@@ -61,10 +62,11 @@ static int rtc_open(int flags) {
 
         for (;;) {
                 char *p, *v;
-                struct dirent buf, *de;
+                struct dirent *de;
+                union dirent_storage buf;
                 int r;
 
-                r = readdir_r(d, &buf, &de);
+                r = readdir_r(d, &buf.de, &de);
                 if (r != 0)
                         goto fallback;
 
@@ -93,6 +95,11 @@ static int rtc_open(int flags) {
                         continue;
 
                 p = strappend("/dev/", de->d_name);
+                if (!p) {
+                        closedir(d);
+                        return -ENOMEM;
+                }
+
                 fd = open(p, flags);
                 free(p);
 
@@ -154,9 +161,9 @@ int hwclock_set_time(const struct tm *tm) {
 
         return err;
 }
+
 int hwclock_is_localtime(void) {
-        FILE *f;
-        bool local = false;
+        FILE _cleanup_fclose_ *f;
 
         /*
          * The third line of adjtime is "UTC" or "LOCAL" or nothing.
@@ -173,33 +180,30 @@ int hwclock_is_localtime(void) {
                 b = fgets(line, sizeof(line), f) &&
                         fgets(line, sizeof(line), f) &&
                         fgets(line, sizeof(line), f);
-
-                fclose(f);
-
                 if (!b)
                         return -EIO;
 
                 truncate_nl(line);
-                local = streq(line, "LOCAL");
+                return streq(line, "LOCAL");
 
-        } else if (errno != -ENOENT)
+        } else if (errno != ENOENT)
                 return -errno;
 
-        return local;
+        return 0;
 }
 
 int hwclock_set_timezone(int *min) {
         const struct timeval *tv_null = NULL;
         struct timespec ts;
         struct tm *tm;
-        int minuteswest;
+        int minutesdelta;
         struct timezone tz;
 
         assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
         assert_se(tm = localtime(&ts.tv_sec));
-        minuteswest = tm->tm_gmtoff / 60;
+        minutesdelta = tm->tm_gmtoff / 60;
 
-        tz.tz_minuteswest = -minuteswest;
+        tz.tz_minuteswest = -minutesdelta;
         tz.tz_dsttime = 0; /* DST_NONE*/
 
         /*
@@ -210,7 +214,7 @@ int hwclock_set_timezone(int *min) {
         if (settimeofday(tv_null, &tz) < 0)
                 return -errno;
         if (min)
-                *min = minuteswest;
+                *min = minutesdelta;
         return 0;
 }