From: Karel Zak <kzak@redhat.com>
To: Sami Kerola <kerolasa@iki.fi>
Cc: util-linux@vger.kernel.org
Subject: Re: [PATCH 02/13] rtcwake: enumerate constant mode strings
Date: Tue, 24 Feb 2015 13:34:30 +0100 [thread overview]
Message-ID: <20150224123430.GQ19430@ws.net.home> (raw)
In-Reply-To: <1424616139-638-3-git-send-email-kerolasa@iki.fi>
On Sun, Feb 22, 2015 at 02:42:08PM +0000, Sami Kerola wrote:
> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
> ---
> sys-utils/rtcwake.c | 126 ++++++++++++++++++++++++++++++++--------------------
> 1 file changed, 78 insertions(+), 48 deletions(-)
>
> diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c
> index 8b5f69c..372e620 100644
> --- a/sys-utils/rtcwake.c
> +++ b/sys-utils/rtcwake.c
> @@ -52,7 +52,37 @@
> #define RTC_PATH "/sys/class/rtc/%s/device/power/wakeup"
> #define SYS_POWER_STATE_PATH "/sys/power/state"
> #define DEFAULT_DEVICE "/dev/rtc0"
> -#define DEFAULT_MODE "standby"
> +
> +enum rtc_modes { /* manual page --mode option explains these. */
> + STANDBY_MODE = 0,
> + MEM_MODE,
> + FREEZE_MODE,
> + DISK_MODE, /* end of Documentation/power/states.txt modes */
> + OFF_MODE,
> + NO_MODE,
> + ON_MODE, /* smaller <- read the code */
> + DISABLE_MODE, /* greater <- to understand */
> + SHOW_MODE,
> + ERROR_MODE /* invalid user input */
> +};
Please, don't use ERROR_MODE or so, just return -EINVAL from the
parser.
> +static int get_mode(const char *optarg)
> +{
> + if (!strcmp(optarg, mode_str[STANDBY_MODE]))
> + return STANDBY_MODE;
> + if (!strcmp(optarg, mode_str[MEM_MODE]))
> + return MEM_MODE;
> + if (!strcmp(optarg, mode_str[DISK_MODE]))
> + return DISK_MODE;
> + if (!strcmp(optarg, mode_str[ON_MODE]))
> + return ON_MODE;
> + if (!strcmp(optarg, mode_str[NO_MODE]))
> + return NO_MODE;
> + if (!strcmp(optarg, mode_str[OFF_MODE]))
> + return OFF_MODE;
> + if (!strcmp(optarg, mode_str[FREEZE_MODE]))
> + return FREEZE_MODE;
> + if (!strcmp(optarg, mode_str[DISABLE_MODE]))
> + return DISABLE_MODE;
> + if (!strcmp(optarg, mode_str[SHOW_MODE]))
> + return SHOW_MODE;
{
for (i = 0; i < ARRAY_SIZE(mode_str); i++)
if (strcmp(optarg, mode_str[i])
return i;
return -EINVAL;
}
> - if (!alarm && !seconds && strcmp(suspend,"disable") &&
> - strcmp(suspend,"show")) {
> -
> + if (!alarm && !seconds && suspend < DISABLE_MODE) {
this ("<") is pretty fragile, use
&& (suspend != DISABLE_MODE || suspend != SHOW_MODE)
> warnx(_("must provide wake time (see -t and -s options)"));
> usage(stderr);
...
> - if (strcmp(suspend, "show") && strcmp(suspend, "disable")) {
> - if (strcmp(suspend, "no") && strcmp(suspend, "on") &&
> - strcmp(suspend, "off") && is_suspend_available(suspend) <= 0) {
> - errx(EXIT_FAILURE, _("suspend to \"%s\" unavailable"), suspend);
> + if (suspend < DISABLE_MODE) {
> + if (suspend < OFF_MODE && is_suspend_available(suspend) <= 0) {
> + errx(EXIT_FAILURE, _("suspend to \"%s\" unavailable"),
> + mode_str[suspend]);
}
the same situation
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
next prev parent reply other threads:[~2015-02-24 12:34 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-22 14:42 [PATCH 00/13] pull: rtcwake changes Sami Kerola
2015-02-22 14:42 ` [PATCH 01/13] rtcwake: add rtcwake_control and remove global variables Sami Kerola
2015-02-22 14:42 ` [PATCH 02/13] rtcwake: enumerate constant mode strings Sami Kerola
2015-02-24 12:34 ` Karel Zak [this message]
2015-03-01 12:23 ` Sami Kerola
2015-02-22 14:42 ` [PATCH 03/13] rtcwake: replace long if else statement with switch case Sami Kerola
2015-02-22 14:42 ` [PATCH 04/13] rtcwake: remove RTC_ALM_READ and RTC_ALM_SET compatibility Sami Kerola
2015-02-23 20:37 ` Benno Schulenberg
2015-03-01 11:24 ` Sami Kerola
2015-02-22 14:42 ` [PATCH 05/13] rtcwake: improve read_clock_mode() Sami Kerola
2015-02-22 14:42 ` [PATCH 06/13] rtcwake: add human readable --date timestamp format Sami Kerola
2015-02-23 20:32 ` Benno Schulenberg
2015-02-22 14:42 ` [PATCH 07/13] rtcwake: fix preprocessor redefinition Sami Kerola
2015-02-22 14:42 ` [PATCH 08/13] rtcwake: clean up struct tm initializations Sami Kerola
2015-02-22 14:42 ` [PATCH 09/13] rtcwake: do not overwrite device name Sami Kerola
2015-02-24 13:00 ` Karel Zak
2015-03-01 12:24 ` Sami Kerola
2015-02-22 14:42 ` [PATCH 10/13] bash-completion: add freeze mode to rtcwake Sami Kerola
2015-02-24 13:01 ` Karel Zak
2015-03-01 12:26 ` Sami Kerola
2015-02-22 14:42 ` [PATCH 11/13] rtcwake: improve coding style Sami Kerola
2015-02-23 20:40 ` Benno Schulenberg
2015-03-01 11:33 ` Sami Kerola
2015-03-02 20:48 ` Benno Schulenberg
2015-02-22 14:42 ` [PATCH 12/13] rtcwake: make some command line options mutually exclusive Sami Kerola
2015-02-22 14:42 ` [PATCH 13/13] rtcwake: read accepted mode strings from /sys/power/state Sami Kerola
2015-06-02 12:48 ` [PATCH 00/13] pull: rtcwake changes Karel Zak
2015-06-07 20:41 ` Sami Kerola
2015-06-29 13:21 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150224123430.GQ19430@ws.net.home \
--to=kzak@redhat.com \
--cc=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.