util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] mountpoint: refactor exit path
@ 2011-10-10 23:14 Dave Reisner
  2011-10-10 23:14 ` [PATCH 2/2] mountpoint: support symbolic and relative paths Dave Reisner
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Dave Reisner @ 2011-10-10 23:14 UTC (permalink / raw)
  To: util-linux; +Cc: Dave Reisner

There's only one condition for which we declare success, but many for
failure. Initialize rc as failure and set to success on this single
condition. In all cases, jump to a label to exit instead of exiting
immediately. This will be used later on to ease cleanup of any heap
allocations.

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
---
v2: use warn{,x} instead of err{,x} to avoid leaks.

 sys-utils/mountpoint.c |   19 +++++++++++--------
 1 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/sys-utils/mountpoint.c b/sys-utils/mountpoint.c
index 1297d82..e075b83 100644
--- a/sys-utils/mountpoint.c
+++ b/sys-utils/mountpoint.c
@@ -105,7 +105,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 
 int main(int argc, char **argv)
 {
-	int c, fs_devno = 0, dev_devno = 0, rc = 0;
+	int c, fs_devno = 0, dev_devno = 0, rc = EXIT_FAILURE;
 	char *spec;
 	struct stat st;
 
@@ -151,8 +151,8 @@ int main(int argc, char **argv)
 
 	if (stat(spec, &st)) {
 		if (!quiet)
-			err(EXIT_FAILURE, "%s", spec);
-		return EXIT_FAILURE;
+			warn("%s", spec);
+		goto finish;
 	}
 	if (dev_devno)
 		rc = print_devno(spec, &st);
@@ -161,20 +161,23 @@ int main(int argc, char **argv)
 
 		if (!S_ISDIR(st.st_mode)) {
 			if (!quiet)
-				errx(EXIT_FAILURE, _("%s: not a directory"), spec);
-			return EXIT_FAILURE;
+				warnx(_("%s: not a directory"), spec);
+			goto finish;
 		}
 		src = dir_to_device(spec);
 		if (src == (dev_t)-1) {
 			if (!quiet)
 				printf(_("%s is not a mountpoint\n"), spec);
-			return EXIT_FAILURE;
+			goto finish;
 		}
 		if (fs_devno)
 			printf("%u:%u\n", major(src), minor(src));
-		else if (!quiet)
+		else if (!quiet) {
 			printf(_("%s is a mountpoint\n"), spec);
+			rc = EXIT_SUCCESS;
+		}
 	}
 
-	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+finish:
+	return rc;
 }
-- 
1.7.7


^ permalink raw reply related	[flat|nested] 12+ messages in thread
* [PATCH 1/2] mountpoint: refactor exit path
@ 2011-10-09 15:12 Dave Reisner
  2011-10-10 19:45 ` Dave Reisner
  0 siblings, 1 reply; 12+ messages in thread
From: Dave Reisner @ 2011-10-09 15:12 UTC (permalink / raw)
  To: util-linux; +Cc: Dave Reisner

There's only one condition for which we declare success, but many for
failure. Initialize rc as failure and set to success on this single
condition. In all cases, jump to a label to exit instead of exiting
immediately. This will be used later on to ease cleanup of any heap
allocations.

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
---
 sys-utils/mountpoint.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/sys-utils/mountpoint.c b/sys-utils/mountpoint.c
index 1297d82..e021c70 100644
--- a/sys-utils/mountpoint.c
+++ b/sys-utils/mountpoint.c
@@ -105,7 +105,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 
 int main(int argc, char **argv)
 {
-	int c, fs_devno = 0, dev_devno = 0, rc = 0;
+	int c, fs_devno = 0, dev_devno = 0, rc = EXIT_FAILURE;
 	char *spec;
 	struct stat st;
 
@@ -152,7 +152,7 @@ int main(int argc, char **argv)
 	if (stat(spec, &st)) {
 		if (!quiet)
 			err(EXIT_FAILURE, "%s", spec);
-		return EXIT_FAILURE;
+		goto finish;
 	}
 	if (dev_devno)
 		rc = print_devno(spec, &st);
@@ -162,19 +162,22 @@ int main(int argc, char **argv)
 		if (!S_ISDIR(st.st_mode)) {
 			if (!quiet)
 				errx(EXIT_FAILURE, _("%s: not a directory"), spec);
-			return EXIT_FAILURE;
+			goto finish;
 		}
 		src = dir_to_device(spec);
 		if (src == (dev_t)-1) {
 			if (!quiet)
 				printf(_("%s is not a mountpoint\n"), spec);
-			return EXIT_FAILURE;
+			goto finish;
 		}
 		if (fs_devno)
 			printf("%u:%u\n", major(src), minor(src));
-		else if (!quiet)
+		else if (!quiet) {
 			printf(_("%s is a mountpoint\n"), spec);
+			rc = EXIT_SUCCESS;
+		}
 	}
 
-	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+finish:
+	return rc;
 }
-- 
1.7.7


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2011-10-11  9:34 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-10 23:14 [PATCH 1/2] mountpoint: refactor exit path Dave Reisner
2011-10-10 23:14 ` [PATCH 2/2] mountpoint: support symbolic and relative paths Dave Reisner
2011-10-11  9:34   ` Karel Zak
2011-10-11  7:07 ` [PATCH 1/2] mountpoint: refactor exit path Voelker, Bernhard
2011-10-11  7:44   ` Karel Zak
2011-10-11  7:55     ` Voelker, Bernhard
2011-10-11  8:14       ` Karel Zak
2011-10-11  9:06 ` Karel Zak
2011-10-11  9:31   ` Karel Zak
  -- strict thread matches above, loose matches on Subject: below --
2011-10-09 15:12 Dave Reisner
2011-10-10 19:45 ` Dave Reisner
2011-10-10 23:05   ` Karel Zak

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).