]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rtcwake: improve coding style
authorSami Kerola <kerolasa@iki.fi>
Sun, 11 Jan 2015 00:55:23 +0000 (00:55 +0000)
committerKarel Zak <kzak@redhat.com>
Mon, 29 Jun 2015 11:39:37 +0000 (13:39 +0200)
Reviewed-by: Benno Schulenberg <bensberg@justemail.net>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/rtcwake.c

index 1044c86db36ea804ce0709040631bbcc9d570ce9..e6b4a8dae24d2647b5dd26b68ebaae8b316e34b5 100644 (file)
  * That flag should not be needed on systems with adjtime support.
  */
 
-#include <stdio.h>
-#include <getopt.h>
+#include <errno.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <libgen.h>
+#include <linux/rtc.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <time.h>
-
 #include <sys/ioctl.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
 
-#include <linux/rtc.h>
-
+#include "c.h"
+#include "closestream.h"
 #include "nls.h"
-#include "xalloc.h"
 #include "pathnames.h"
 #include "strutils.h"
-#include "c.h"
-#include "closestream.h"
 #include "timeutils.h"
+#include "xalloc.h"
 
 #ifndef RTC_AF
 # define       RTC_AF  0x20    /* Alarm interrupt */
@@ -151,12 +149,10 @@ static int is_wakeup_enabled(const char *devname)
        fclose(f);
        if (!s)
                return 0;
-
        s = strchr(buf, '\n');
        if (!s)
                return 0;
        *s = 0;
-
        /* wakeup events could be disabled or not supported */
        return strcmp(buf, "enabled") == 0;
 }
@@ -166,14 +162,13 @@ static int get_basetimes(struct rtcwake_control *ctl, int fd)
        struct tm       tm = { 0 };
        struct rtc_time rtc;
 
-       /* this process works in RTC time, except when working
+       /* This process works in RTC time, except when working
         * with the system clock (which always uses UTC).
         */
        if (ctl->clock_mode == CM_UTC)
                setenv("TZ", "UTC", 1);
        tzset();
-
-       /* read rtc and system clocks "at the same time", or as
+       /* Read rtc and system clocks "at the same time", or as
         * precisely (+/- a second) as we can read them.
         */
        if (ioctl(fd, RTC_RD_TIME, &rtc) < 0) {
@@ -185,8 +180,7 @@ static int get_basetimes(struct rtcwake_control *ctl, int fd)
                warn(_("read system time failed"));
                return -1;
        }
-
-       /* convert rtc_time to normal arithmetic-friendly form,
+       /* Convert rtc_time to normal arithmetic-friendly form,
         * updating tm.tm_wday as used by asctime().
         */
        tm.tm_sec = rtc.tm_sec;
@@ -197,20 +191,16 @@ static int get_basetimes(struct rtcwake_control *ctl, int fd)
        tm.tm_year = rtc.tm_year;
        tm.tm_isdst = -1;  /* assume the system knows better than the RTC */
        ctl->rtc_time = mktime(&tm);
-
        if (ctl->rtc_time == (time_t)-1) {
                warn(_("convert rtc time failed"));
                return -1;
        }
-
        if (ctl->verbose) {
                /* Unless the system uses UTC, either delta or tzone
                 * reflects a seconds offset from UTC.  The value can
-                * help sort out problems like bugs in your C library.
-                */
+                * help sort out problems like bugs in your C library. */
                printf("\tdelta   = %ld\n", ctl->sys_time - ctl->rtc_time);
                printf("\ttzone   = %ld\n", timezone);
-
                printf("\ttzname  = %s\n", tzname[daylight]);
                gmtime_r(&ctl->rtc_time, &tm);
                printf("\tsystime = %ld, (UTC) %s",
@@ -218,7 +208,6 @@ static int get_basetimes(struct rtcwake_control *ctl, int fd)
                printf("\trtctime = %ld, (UTC) %s",
                                (long) ctl->rtc_time, asctime(&tm));
        }
-
        return 0;
 }
 
@@ -227,36 +216,31 @@ static int setup_alarm(struct rtcwake_control *ctl, int fd, time_t *wakeup)
        struct tm               *tm;
        struct rtc_wkalrm       wake;
 
-       /* The wakeup time is in POSIX time (more or less UTC).
-        * Ideally RTCs use that same time; but PCs can't do that
-        * if they need to boot MS-Windows.  Messy...
+       /* The wakeup time is in POSIX time (more or less UTC).  Ideally
+        * RTCs use that same time; but PCs can't do that if they need to
+        * boot MS-Windows.  Messy...
         *
-        * When clock_mode == CM_UTC this process's timezone is UTC,
-        * so we'll pass a UTC date to the RTC.
+        * When clock_mode == CM_UTC this process's timezone is UTC, so
+        * we'll pass a UTC date to the RTC.
         *
-        * Else clock_mode == CM_LOCAL so the time given to the RTC
-        * will instead use the local time zone.
-        */
+        * Else clock_mode == CM_LOCAL so the time given to the RTC will
+        * instead use the local time zone. */
        tm = localtime(wakeup);
-
        wake.time.tm_sec = tm->tm_sec;
        wake.time.tm_min = tm->tm_min;
        wake.time.tm_hour = tm->tm_hour;
        wake.time.tm_mday = tm->tm_mday;
        wake.time.tm_mon = tm->tm_mon;
        wake.time.tm_year = tm->tm_year;
-       /* wday, yday, and isdst fields are unused by Linux */
+       /* wday, yday, and isdst fields are unused */
        wake.time.tm_wday = -1;
        wake.time.tm_yday = -1;
        wake.time.tm_isdst = -1;
-
        wake.enabled = 1;
-
        if (!ctl->dryrun && ioctl(fd, RTC_WKALM_SET, &wake) < 0) {
                warn(_("set rtc wake alarm failed"));
                return -1;
        }
-
        return 0;
 }
 
@@ -268,12 +252,10 @@ static int is_suspend_available(const int suspend)
 
        if (!f)
                return -1;
-
        if (fgets(buf, sizeof buf, f) == NULL)
                rc = -1;
        else
                rc = strstr(buf, mode_str[suspend]) != NULL;
-
        fclose(f);
        return rc;
 }
@@ -286,12 +268,10 @@ static void suspend_system(struct rtcwake_control *ctl, int suspend)
                warn(_("cannot open %s"), SYS_POWER_STATE_PATH);
                return;
        }
-
        if (!ctl->dryrun) {
                fprintf(f, "%s\n", mode_str[suspend]);
                fflush(f);
        }
-
        /* this executes after wake from suspend */
        if (close_stream(f))
                errx(EXIT_FAILURE, _("write error"));
@@ -305,34 +285,26 @@ static int read_clock_mode(struct rtcwake_control *ctl)
        fp = fopen(ctl->adjfile, "r");
        if (!fp)
                return -1;
-
        /* skip two lines */
        if (skip_fline(fp) || skip_fline(fp)) {
                fclose(fp);
                return -1;
        }
-
        /* read third line */
        if (!fgets(linebuf, sizeof linebuf, fp)) {
                fclose(fp);
                return -1;
        }
-
        if (strncmp(linebuf, "UTC", 3) == 0)
                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);
-
        return 0;
 }
 
-/**
- * print basic alarm settings
- */
 static int print_alarm(struct rtcwake_control *ctl, int fd)
 {
        struct rtc_wkalrm wake;
@@ -343,12 +315,10 @@ static int print_alarm(struct rtcwake_control *ctl, int fd)
                warn(_("read rtc alarm failed"));
                return -1;
        }
-
        if (wake.enabled != 1 || wake.time.tm_year == -1) {
                printf(_("alarm: off\n"));
                return 0;
        }
-
        tm.tm_sec = wake.time.tm_sec;
        tm.tm_min = wake.time.tm_min;
        tm.tm_hour = wake.time.tm_hour;
@@ -356,16 +326,13 @@ static int print_alarm(struct rtcwake_control *ctl, int fd)
        tm.tm_mon = wake.time.tm_mon;
        tm.tm_year = wake.time.tm_year;
        tm.tm_isdst = -1;  /* assume the system knows better than the RTC */
-
        alarm = mktime(&tm);
        if (alarm == (time_t)-1) {
                warn(_("convert time failed"));
                return -1;
        }
-
        /* 0 if both UTC, or expresses diff if RTC in local time */
        alarm += ctl->sys_time - ctl->rtc_time;
-
        printf(_("alarm: on  %s"), ctime(&alarm));
        return 0;
 }
@@ -412,21 +379,17 @@ int main(int argc, char **argv)
                .clock_mode = CM_AUTO,
                0
        };
-
-       char            *devname = DEFAULT_DEVICE;
-       unsigned        seconds = 0;
-       int             suspend = STANDBY_MODE;
-
-       int             rc = EXIT_SUCCESS;
-       int             t;
-       int             fd;
-       time_t          alarm = 0;
-
+       char *devname = DEFAULT_DEVICE;
+       unsigned seconds = 0;
+       int suspend = STANDBY_MODE;
+       int rc = EXIT_SUCCESS;
+       int t;
+       int fd;
+       time_t alarm = 0;
        enum {
                OPT_DATE = CHAR_MAX + 1,
                OPT_LIST
        };
-
        static const struct option long_options[] = {
                {"adjfile",     required_argument,      0, 'A'},
                {"auto",        no_argument,            0, 'a'},
@@ -449,7 +412,6 @@ int main(int argc, char **argv)
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
        atexit(close_stdout);
-
        while ((t = getopt_long(argc, argv, "A:ahd:lm:ns:t:uVv",
                                        long_options, NULL)) != EOF) {
                switch (t) {
@@ -457,15 +419,12 @@ int main(int argc, char **argv)
                        /* for better compatibility with hwclock */
                        ctl.adjfile = optarg;
                        break;
-
                case 'a':
                        ctl.clock_mode = CM_AUTO;
                        break;
-
                case 'd':
                        devname = optarg;
                        break;
-
                case 'l':
                        ctl.clock_mode = CM_LOCAL;
                        break;
@@ -478,24 +437,19 @@ int main(int argc, char **argv)
                        if ((suspend = get_mode(optarg)) < 0)
                                errx(EXIT_FAILURE, _("unrecognized suspend state '%s'"), optarg);
                        break;
-
                case 'n':
                        ctl.dryrun = 1;
                        break;
-
                case 's':
                        /* alarm time, seconds-to-sleep (relative) */
                        seconds = strtou32_or_err(optarg, _("invalid seconds argument"));
                        break;
-
                case 't':
                        /* alarm time, time_t (absolute, seconds since epoc) */
                        alarm = strtou32_or_err(optarg, _("invalid time argument"));
                        break;
-
                case OPT_DATE:
-               {
-                       /* alarm time, see timestamp format from manual */
+               {       /* alarm time, see timestamp format from manual */
                        usec_t p;
                        if (parse_timestamp(optarg, &p) < 0)
                                errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg);
@@ -505,22 +459,18 @@ int main(int argc, char **argv)
                case 'u':
                        ctl.clock_mode = CM_UTC;
                        break;
-
                case 'v':
                        ctl.verbose = 1;
                        break;
-
                case 'V':
                        printf(UTIL_LINUX_VERSION);
                        exit(EXIT_SUCCESS);
-
                case 'h':
                        usage(stdout);
                default:
                        usage(stderr);
                }
        }
-
        if (ctl.clock_mode == CM_AUTO) {
                if (read_clock_mode(&ctl) < 0) {
                        printf(_("%s: assuming RTC uses UTC ...\n"),
@@ -531,25 +481,20 @@ int main(int argc, char **argv)
        if (ctl.verbose)
                printf("%s",  ctl.clock_mode == CM_UTC ? _("Using UTC time.\n") :
                                _("Using local time.\n"));
-
        if (!alarm && !seconds && suspend != DISABLE_MODE && suspend != SHOW_MODE) {
                warnx(_("must provide wake time (see --seconds, --time, and --date options)"));
                usage(stderr);
        }
-
        /* device must exist and (if we'll sleep) be wakeup-enabled */
        fd = open_dev_rtc(devname);
-
        if (suspend != ON_MODE && suspend != NO_MODE && !is_wakeup_enabled(devname))
                errx(EXIT_FAILURE, _("%s not enabled for wakeup events"), devname);
-
        /* relative or absolute alarm time, normalized to time_t */
        if (get_basetimes(&ctl, fd) < 0)
                exit(EXIT_FAILURE);
        if (ctl.verbose)
                printf(_("alarm %ld, sys_time %ld, rtc_time %ld, seconds %u\n"),
                                alarm, ctl.sys_time, ctl.rtc_time, seconds);
-
        if (suspend != DISABLE_MODE && suspend != SHOW_MODE) {
                if ((suspend == STANDBY_MODE || suspend == MEM_MODE
                     || suspend == FREEZE_MODE || suspend == DISK_MODE)
@@ -567,10 +512,8 @@ int main(int argc, char **argv)
                        alarm += ctl.sys_time - ctl.rtc_time;
                } else
                        alarm = ctl.rtc_time + seconds + 1;
-
                if (setup_alarm(&ctl, fd, &alarm) < 0)
                        exit(EXIT_FAILURE);
-
                if (suspend == NO_MODE || suspend == ON_MODE)
                        printf(_("%s: wakeup using %s at %s"),
                                program_invocation_short_name, devname,
@@ -582,14 +525,12 @@ int main(int argc, char **argv)
                fflush(stdout);
                xusleep(10 * 1000);
        }
-
        switch (suspend) {
        case NO_MODE:
                if (ctl.verbose)
                        printf(_("suspend mode: no; leaving\n"));
                ctl.dryrun = 1; /* to skip disabling alarm at the end */
                break;
-
        case OFF_MODE:
        {
                char *arg[5];
@@ -603,10 +544,8 @@ int main(int argc, char **argv)
                arg[i++] = "-P";
                arg[i++] = "now";
                arg[i]   = NULL;
-
                if (!ctl.dryrun) {
                        execv(arg[0], arg);
-
                        warn(_("failed to execute %s"), _PATH_SHUTDOWN);
                        rc = EXIT_FAILURE;
                }
@@ -618,7 +557,6 @@ int main(int argc, char **argv)
 
                if (ctl.verbose)
                        printf(_("suspend mode: on; reading rtc\n"));
-
                if (!ctl.dryrun) {
                        do {
                                t = read(fd, &data, sizeof data);
@@ -637,7 +575,6 @@ int main(int argc, char **argv)
                if (ctl.verbose)
                        printf(_("suspend mode: disable; disabling alarm\n"));
                break;
-
        case SHOW_MODE:
                if (ctl.verbose)
                        printf(_("suspend mode: show; printing alarm info\n"));
@@ -645,7 +582,6 @@ int main(int argc, char **argv)
                        rc = EXIT_FAILURE;
                ctl.dryrun = 1; /* don't really disable alarm in the end, just show */
                break;
-
        default:
                if (ctl.verbose)
                        printf(_("suspend mode: %s; suspending system\n"), mode_str[suspend]);
@@ -667,7 +603,6 @@ int main(int argc, char **argv)
                        }
                }
        }
-
        close(fd);
        return rc;
 }