- don't free user-supplied string (via -e)
[oweals/busybox.git] / util-linux / rtcwake.c
index c7d5a4c129a064df54878afa11cc30fb8c69f428..a9766caaa2666aea0084e0a8518aaa06474c18ca 100644 (file)
 
 #define SYS_RTC_PATH   "/sys/class/rtc/%s/device/power/wakeup"
 #define SYS_POWER_PATH "/sys/power/state"
-#define DEFAULT_MODE   "suspend"
+#define DEFAULT_MODE   "standby"
 
 static time_t rtc_time;
 
-static int may_wakeup(const char *rtcname)
+static bool may_wakeup(const char *rtcname)
 {
        ssize_t ret;
        char buf[128];
@@ -42,10 +42,10 @@ static int may_wakeup(const char *rtcname)
        snprintf(buf, sizeof(buf), SYS_RTC_PATH, rtcname);
        ret = open_read_close(buf, buf, sizeof(buf));
        if (ret < 0)
-               return 0;
+               return false;
 
        /* wakeup events could be disabled or not supported */
-       return strcmp(buf, "enabled\n") == 0;
+       return strncmp(buf, "enabled\n", 8) == 0;
 }
 
 static void setup_alarm(int fd, time_t *wakeup)
@@ -89,15 +89,6 @@ static void setup_alarm(int fd, time_t *wakeup)
        }
 }
 
-static void suspend_system(const char *suspend)
-{
-       FILE *f = xfopen(SYS_POWER_PATH, "w");
-       fprintf(f, "%s\n", suspend);
-       fflush(f);
-       /* this executes after wake from suspend */
-       fclose(f);
-}
-
 #define RTCWAKE_OPT_AUTO         0x01
 #define RTCWAKE_OPT_LOCAL        0x02
 #define RTCWAKE_OPT_UTC          0x04
@@ -107,7 +98,7 @@ static void suspend_system(const char *suspend)
 #define RTCWAKE_OPT_TIME         0x40
 
 int rtcwake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int rtcwake_main(int argc, char **argv)
+int rtcwake_main(int argc UNUSED_PARAM, char **argv)
 {
        unsigned opt;
        const char *rtcname = NULL;
@@ -159,11 +150,11 @@ int rtcwake_main(int argc, char **argv)
        /* the rtcname is relative to /dev */
        xchdir("/dev");
 
-       if (strcmp(suspend, "on") != 0 && !may_wakeup(rtcname))
-               bb_error_msg_and_die("%s not enabled for wakeup events", rtcname);
-
        /* this RTC must exist and (if we'll sleep) be wakeup-enabled */
-       fd = rtc_xopen(rtcname, O_RDONLY);
+       fd = rtc_xopen(&rtcname, O_RDONLY);
+
+       if (strcmp(suspend, "on") && !may_wakeup(rtcname))
+               bb_error_msg_and_die("%s not enabled for wakeup events", rtcname);
 
        /* relative or absolute alarm time, normalized to time_t */
        sys_time = time(0);
@@ -184,8 +175,8 @@ int rtcwake_main(int argc, char **argv)
        fflush(stdout);
        usleep(10 * 1000);
 
-       if (!strcmp(suspend, "on"))
-               suspend_system(suspend);
+       if (strcmp(suspend, "on"))
+               xopen_xwrite_close(SYS_POWER_PATH, suspend);
        else {
                /* "fake" suspend ... we'll do the delay ourselves */
                unsigned long data;