linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Sandeen <sandeen@redhat.com>
To: linux-btrfs@vger.kernel.org
Cc: Eric Sandeen <sandeen@redhat.com>
Subject: [PATCH 01/17] btrfs-progs: Unify size-parsing
Date: Mon, 25 Feb 2013 16:54:34 -0600	[thread overview]
Message-ID: <1361832890-40921-2-git-send-email-sandeen@redhat.com> (raw)
In-Reply-To: <1361832890-40921-1-git-send-email-sandeen@redhat.com>

cmds-qgroup.c contained a parse_limit() function which
duplicates much of the functionality of parse_size.
The only unique behavior is to handle "none"; then we
can just pass it off to parse_size().

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---
 cmds-qgroup.c |   44 ++++++--------------------------------------
 utils.c       |    8 +++++++-
 utils.h       |    2 +-
 3 files changed, 14 insertions(+), 40 deletions(-)

diff --git a/cmds-qgroup.c b/cmds-qgroup.c
index 26f0ab0..ce013c8 100644
--- a/cmds-qgroup.c
+++ b/cmds-qgroup.c
@@ -198,43 +198,13 @@ done:
 	return ret;
 }
 
-static int parse_limit(const char *p, unsigned long long *s)
+static u64 parse_limit(const char *p)
 {
-	char *endptr;
-	unsigned long long size;
-
-	if (strcasecmp(p, "none") == 0) {
-		*s = 0;
-		return 1;
-	}
-	size = strtoull(p, &endptr, 10);
-	switch (*endptr) {
-	case 'T':
-	case 't':
-		size *= 1024;
-	case 'G':
-	case 'g':
-		size *= 1024;
-	case 'M':
-	case 'm':
-		size *= 1024;
-	case 'K':
-	case 'k':
-		size *= 1024;
-		++endptr;
-		break;
-	case 0:
-		break;
-	default:
-		return 0;
-	}
-
-	if (*endptr)
+	if (strcasecmp(p, "none") == 0)
 		return 0;
 
-	*s = size;
-
-	return 1;
+	/* parse_size() will exit() on any error */
+	return parse_size(p);
 }
 
 static const char * const cmd_qgroup_assign_usage[] = {
@@ -364,10 +334,8 @@ static int cmd_qgroup_limit(int argc, char **argv)
 	if (check_argc_min(argc - optind, 2))
 		usage(cmd_qgroup_limit_usage);
 
-	if (!parse_limit(argv[optind], &size)) {
-		fprintf(stderr, "Invalid size argument given\n");
-		return 1;
-	}
+	/* parse_limit will exit on any error */
+	size = parse_limit(argv[optind]);
 
 	memset(&args, 0, sizeof(args));
 	if (size) {
diff --git a/utils.c b/utils.c
index d660507..bc6d5fe 100644
--- a/utils.c
+++ b/utils.c
@@ -1251,7 +1251,7 @@ scan_again:
 	return 0;
 }
 
-u64 parse_size(char *s)
+u64 parse_size(const char *s)
 {
 	int i;
 	char c;
@@ -1268,16 +1268,22 @@ u64 parse_size(char *s)
 		switch (c) {
 		case 'e':
 			mult *= 1024;
+			/* Fallthrough */
 		case 'p':
 			mult *= 1024;
+			/* Fallthrough */
 		case 't':
 			mult *= 1024;
+			/* Fallthrough */
 		case 'g':
 			mult *= 1024;
+			/* Fallthrough */
 		case 'm':
 			mult *= 1024;
+			/* Fallthrough */
 		case 'k':
 			mult *= 1024;
+			/* Fallthrough */
 		case 'b':
 			break;
 		default:
diff --git a/utils.h b/utils.h
index 60a0fea..dcdf475 100644
--- a/utils.h
+++ b/utils.h
@@ -47,7 +47,7 @@ char *pretty_sizes(u64 size);
 int check_label(char *input);
 int get_mountpt(char *dev, char *mntpt, size_t size);
 int btrfs_scan_block_devices(int run_ioctl);
-u64 parse_size(char *s);
+u64 parse_size(const char *s);
 int open_file_or_dir(const char *fname);
 int get_device_info(int fd, u64 devid,
 		    struct btrfs_ioctl_dev_info_args *di_args);
-- 
1.7.1


  reply	other threads:[~2013-02-25 21:55 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-25 22:54 [PATCH 00/17] btrfs-progs: More misc fixes & cleanups Eric Sandeen
2013-02-25 22:54 ` Eric Sandeen [this message]
2013-02-25 23:26   ` [PATCH 01/17] btrfs-progs: Unify size-parsing Zach Brown
2013-02-25 23:37     ` Eric Sandeen
2013-02-26  0:26       ` Zach Brown
2013-02-26 18:50   ` Goffredo Baroncelli
2013-02-26 20:17     ` Eric Sandeen
2013-02-26 21:15       ` Goffredo Baroncelli
2013-02-25 22:54 ` [PATCH 02/17] btrfs-progs: fix btrfs_get_subvol cut/paste error Eric Sandeen
2013-02-25 22:54 ` [PATCH 03/17] btrfs-progs: Remove write-only var fdres in cmd_dev_stats() Eric Sandeen
2013-02-25 22:54 ` [PATCH 04/17] btrfs-progs: btrfs_list_get_path_rootid error handling Eric Sandeen
2013-02-25 22:54 ` [PATCH 05/17] btrfs-progs: avoid double-free in __btrfs_map_block Eric Sandeen
2013-02-25 22:54 ` [PATCH 06/17] btrfs-progs: fix open error test in cmd_start_replace Eric Sandeen
2013-02-25 22:54 ` [PATCH 07/17] btrfs-progs: fix close of error fd in scrub cancel Eric Sandeen
2013-02-25 22:54 ` [PATCH 08/17] btrfs-progs: more scrub cancel error handling Eric Sandeen
2013-02-25 22:54 ` [PATCH 09/17] btrfs-progs: free memory before error exit in read_whole_eb Eric Sandeen
2013-02-25 22:54 ` [PATCH 10/17] btrfs-progs: don't call close on error fd Eric Sandeen
2013-02-25 22:54 ` [PATCH 11/17] btrfs-progs: provide positive errno to strerror in cmd_restore Eric Sandeen
2013-02-25 22:54 ` [PATCH 12/17] btrfs-progs: free allocated di_args in cmd_start_replace Eric Sandeen
2013-02-25 22:54 ` [PATCH 13/17] btrfs-progs: close fd on cmd_subvol_get_default return Eric Sandeen
2013-02-25 22:54 ` [PATCH 14/17] btrfs-progs: fix mem leak in resolve_root Eric Sandeen
2013-02-26  0:36   ` Shilong Wang
2013-02-26  4:36     ` Eric Sandeen
2013-02-27 13:03       ` David Sterba
2013-02-27 13:12         ` Shilong Wang
2013-02-25 22:54 ` [PATCH 15/17] btrfs-progs: Tidy up resolve_root Eric Sandeen
2013-02-25 22:54 ` [PATCH 16/17] btrfs-progs: fix fd leak in cmd_subvol_set_default Eric Sandeen
2013-02-26 18:46   ` Goffredo Baroncelli
2013-02-26 20:10     ` Eric Sandeen
2013-02-26 21:04       ` Goffredo Baroncelli
2013-02-27 12:38         ` David Sterba
2013-02-25 22:54 ` [PATCH 17/17] btrfs-progs: replace strtok_r with strsep Eric Sandeen
2013-02-26 18:47   ` Goffredo Baroncelli
2013-02-26 20:13     ` Eric Sandeen
2013-02-26 20:20     ` [PATCH 17/17 V2] " Eric Sandeen
2013-02-26 20:40       ` Ilya Dryomov
2013-02-26 20:46         ` Eric Sandeen
2013-02-26 21:07           ` Ilya Dryomov
2013-02-26 21:50             ` [PATCH 17/17 V3] btrfs-progs: initialize save_ptr prior to strtok_r Eric Sandeen
2013-02-27 13:54 ` [PATCH 00/17] btrfs-progs: More misc fixes & cleanups David Sterba

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=1361832890-40921-2-git-send-email-sandeen@redhat.com \
    --to=sandeen@redhat.com \
    --cc=linux-btrfs@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).