]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rtcwake: improve read_clock_mode()
authorSami Kerola <kerolasa@iki.fi>
Sat, 10 Jan 2015 18:03:25 +0000 (18:03 +0000)
committerKarel Zak <kzak@redhat.com>
Mon, 29 Jun 2015 11:39:37 +0000 (13:39 +0200)
Make skipping two lines more robust, and add message about unexpected
adjfile contents when running with --verbose.

Reviewed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/strutils.h
lib/strutils.c
sys-utils/rtcwake.c

index 9eb9c81271fea3bc50e337040cf2e7c746b1167c..ec2a8acd67594c6498124c0c71d50c11c15e2753 100644 (file)
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <sys/types.h>
 #include <ctype.h>
+#include <stdio.h>
 
 /* default strtoxx_or_err() exit code */
 #ifndef STRTOXX_EXIT_CODE
@@ -207,4 +208,6 @@ extern char *strnappend(const char *s, const char *suffix, size_t b);
 extern char *strappend(const char *s, const char *suffix);
 extern const char *split(const char **state, size_t *l, const char *separator, int quoted);
 
+extern int skip_fline(FILE *fp);
+
 #endif
index cc90e043da222d8c505e19af8da0c3d1fa28e883..ebfc5110b92458d2f07e5ecd543349d69ffd9e1e 100644 (file)
@@ -859,6 +859,18 @@ const char *split(const char **state, size_t *l, const char *separator, int quot
         return current;
 }
 
+/* Rewind file pointer forward to new line.  */
+int skip_fline(FILE *fp)
+{
+       char ch;
+
+       do {
+               if ((ch = fgetc(fp)) == EOF)
+                       return 1;
+               if (ch == '\n')
+                       return 0;
+       } while (1);
+}
 
 #ifdef TEST_PROGRAM
 
index 3c8238d27511fe97387b0fc019deade33edd1c80..6d67fc26a74a56c214e386a2f0e1b437b4e86f90 100644 (file)
@@ -47,7 +47,7 @@
 #define        RTC_AF  0x20
 #define        RTC_UF  0x10
 
-#define MAX_LINE               1024
+#define ADJTIME_ZONE_STRLEN    8
 
 #define RTC_PATH               "/sys/class/rtc/%s/device/power/wakeup"
 #define SYS_POWER_STATE_PATH   "/sys/power/state"
@@ -294,30 +294,23 @@ static void suspend_system(struct rtcwake_control *ctl, int suspend)
                errx(EXIT_FAILURE, _("write error"));
 }
 
-
 static int read_clock_mode(struct rtcwake_control *ctl)
 {
        FILE *fp;
-       char linebuf[MAX_LINE];
+       char linebuf[ADJTIME_ZONE_STRLEN];
 
        fp = fopen(ctl->adjfile, "r");
        if (!fp)
                return -1;
 
-       /* skip first line */
-       if (!fgets(linebuf, MAX_LINE, fp)) {
-               fclose(fp);
-               return -1;
-       }
-
-       /* skip second line */
-       if (!fgets(linebuf, MAX_LINE, fp)) {
+       /* skip two lines */
+       if (skip_fline(fp) || skip_fline(fp)) {
                fclose(fp);
                return -1;
        }
 
        /* read third line */
-       if (!fgets(linebuf, MAX_LINE, fp)) {
+       if (!fgets(linebuf, sizeof linebuf, fp)) {
                fclose(fp);
                return -1;
        }
@@ -326,6 +319,8 @@ static int read_clock_mode(struct rtcwake_control *ctl)
                ctl->clock_mode = CM_UTC;
        else if (strncmp(linebuf, "LOCAL", 5) == 0)
                ctl->clock_mode = CM_LOCAL;
+       else if (ctl->verbose)
+               warnx(_("unexpected third line in: %s: %s"), ctl->adjfile, linebuf);
 
        fclose(fp);