util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: Karel Zak <kzak@redhat.com>
Cc: util-linux@vger.kernel.org
Subject: Re: [PATCH 05/16] lslogins: reject unknown time format arguments
Date: Fri, 19 Dec 2014 09:30:23 +0000 (GMT)	[thread overview]
Message-ID: <alpine.LNX.2.03.1412190927340.8955@iki.fi> (raw)
In-Reply-To: <20141215092852.GX19904@x2.net.home>

On Mon, 15 Dec 2014, Karel Zak wrote:

> On Sun, Dec 14, 2014 at 05:44:01PM +0000, Sami Kerola wrote:
>> @@ -1398,8 +1388,18 @@ int main(int argc, char *argv[])
>>  			break;
>>  		case OPT_TIME_FMT:
>>  			{
>> +				struct lslogins_timefmt {
>> +					const char *name;
>> +					const int val;
>> +				};
>> +				const struct lslogins_timefmt timefmts[] = {
>
> static const ....

Corrected.

>> +					{ "iso", TIME_ISO },
>> +					{ "full", TIME_FULL },
>> +					{ "short", TIME_SHORT },
>> +				};
>>  				size_t i;
>>
>> +				ctl->time_mode = TIME_INVALID;
>>  				for (i = 0; i < ARRAY_SIZE(timefmts); i++) {
>>  					if (strcmp(timefmts[i].name, optarg) == 0) {
>>  						ctl->time_mode = timefmts[i].val;
>> @@ -1407,7 +1407,7 @@ int main(int argc, char *argv[])
>>  					}
>>  				}
>>  				if (ctl->time_mode == TIME_INVALID)
>> -					usage(stderr);
>> +					errx(EXIT_FAILURE, _("unknown time format: %s"), optarg);
>
> This is not elegant solution, it would be better to move all the code
> to small function parse_time_mode() and keep the main() less "crowded".

Good idea. Here is updated version of the change.

https://github.com/kerolasa/lelux-utiliteetit/commit/cdf3896a0502b2773323ec293ccb4dfdbfc87cf8

--->8----
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 13 Dec 2014 17:11:04 +0000
Subject: [PATCH 05/18] lslogins: reject unknown time format arguments

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
  login-utils/lslogins.c | 40 +++++++++++++++++++---------------------
  1 file changed, 19 insertions(+), 21 deletions(-)

diff --git a/login-utils/lslogins.c b/login-utils/lslogins.c
index cacf83c..5e1ef17 100644
--- a/login-utils/lslogins.c
+++ b/login-utils/lslogins.c
@@ -1180,16 +1180,25 @@ static void free_user(void *f)
  	free(u);
  }

-struct lslogins_timefmt {
-	const char *name;
-	int val;
-};
+static int parse_time_mode(const char *optarg)
+{
+	struct lslogins_timefmt {
+		const char *name;
+		const int val;
+	};
+	static const struct lslogins_timefmt timefmts[] = {
+		{"iso", TIME_ISO},
+		{"full", TIME_FULL},
+		{"short", TIME_SHORT},
+	};
+	size_t i;

-static struct lslogins_timefmt timefmts[] = {
-	{ "short", TIME_SHORT },
-	{ "full", TIME_FULL },
-	{ "iso", TIME_ISO },
-};
+	for (i = 0; i < ARRAY_SIZE(timefmts); i++) {
+		if (strcmp(timefmts[i].name, optarg) == 0)
+			return timefmts[i].val;
+	}
+	errx(EXIT_FAILURE, _("unknown time format: %s"), optarg);
+}

  static void __attribute__((__noreturn__)) usage(FILE *out)
  {
@@ -1397,18 +1406,7 @@ int main(int argc, char *argv[])
  			ctl->noheadings = 1;
  			break;
  		case OPT_TIME_FMT:
-			{
-				size_t i;
-
-				for (i = 0; i < ARRAY_SIZE(timefmts); i++) {
-					if (strcmp(timefmts[i].name, optarg) == 0) {
-						ctl->time_mode = timefmts[i].val;
-						break;
-					}
-				}
-				if (ctl->time_mode == TIME_INVALID)
-					usage(stderr);
-			}
+			ctl->time_mode = parse_time_mode(optarg);
  			break;
  		case 'V':
  			printf(UTIL_LINUX_VERSION);
-- 
2.2.0


  reply	other threads:[~2014-12-19  9:30 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-14 17:43 [PATCH 00/16] pull: updates to lslogins and chfn Sami Kerola
2014-12-14 17:43 ` [PATCH 01/16] lslogins: allow changing password changed and expiration time formats Sami Kerola
2014-12-14 17:43 ` [PATCH 02/16] lslogins: make journald last logs time stamps to honor --time-format Sami Kerola
2014-12-14 17:43 ` [PATCH 03/16] lslogins: tell why command failed Sami Kerola
2014-12-14 17:44 ` [PATCH 04/16] lslogins: fix short options Sami Kerola
2014-12-14 17:44 ` [PATCH 05/16] lslogins: reject unknown time format arguments Sami Kerola
2014-12-15  9:28   ` Karel Zak
2014-12-19  9:30     ` Sami Kerola [this message]
2014-12-14 17:44 ` [PATCH 06/16] lslogins: add space to systemd journal header and message Sami Kerola
2014-12-14 17:44 ` [PATCH 07/16] lslogins: use hardcoded paths from pathnames.h Sami Kerola
2014-12-14 17:44 ` [PATCH 08/16] chfn: remove function prototypes Sami Kerola
2014-12-14 17:44 ` [PATCH 09/16] chfn: rewrite prompt() to use strutils Sami Kerola
2014-12-14 17:44 ` [PATCH 10/16] chfn: use xasprintf() rather than bunch of strlen() and malloc() calls Sami Kerola
2014-12-14 17:44 ` [PATCH 11/16] chfn: fix usage() regression Sami Kerola
2014-12-14 17:44 ` [PATCH 12/16] chfn: simplify parse_passwd() by using strsep() Sami Kerola
2014-12-14 17:44 ` [PATCH 13/16] chfn: add minimalistic struct chfn_control Sami Kerola
2014-12-14 17:44 ` [PATCH 14/16] chfn: clean up parse_argv() Sami Kerola
2014-12-15  9:45   ` Karel Zak
2014-12-14 17:44 ` [PATCH 15/16] chfn: move new and old finger structs to chfn control struct Sami Kerola
2014-12-15 10:07   ` Karel Zak
2014-12-19  9:41     ` Sami Kerola
2014-12-14 17:44 ` [PATCH 16/16] chfn: make command to obey login.defs CHFN_RESTRICT instructions Sami Kerola
2014-12-15 10:11   ` Karel Zak
2014-12-19 13:30 ` [PATCH 00/16] pull: updates to lslogins and chfn 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=alpine.LNX.2.03.1412190927340.8955@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=kzak@redhat.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).