Re: [PATCH] mount: Fix losetup usage()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jul 30, 2010 at 06:36:29PM +0530, Kamalesh Babulal wrote:
> Currently usage() of losetup returns 1 instead of 0, when
> invoked with --help and invalid usage. Following patch
> alters usage() to return 0, when invoked with --help and
> not treat it as an invalid command usage.

 Applied a little different version, see below. Thanks.

    Karel


>From 108591d2b7860ca0b8d27d114bed1e504a31fa72 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@xxxxxxxxxx>
Date: Wed, 4 Aug 2010 11:52:41 +0200
Subject: [PATCH] losetup: use stdout and return 0 for --help

Reported-by: Kamalesh Babulal <kamalesh@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Karel Zak <kzak@xxxxxxxxxx>
---
 mount/lomount.c |   34 +++++++++++++++++++---------------
 1 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/mount/lomount.c b/mount/lomount.c
index 6130be1..54b9f8e 100644
--- a/mount/lomount.c
+++ b/mount/lomount.c
@@ -884,8 +884,8 @@ find_unused_loop_device (void) {
 #include "strtosize.h"
 
 static void
-usage(void) {
-	fprintf(stderr, _("\nUsage:\n"
+usage(FILE *f) {
+	fprintf(f, _("\nUsage:\n"
   " %1$s loop_device                             give info\n"
   " %1$s -a | --all                              list all used\n"
   " %1$s -d | --detach <loopdev> [<loopdev> ...] delete\n"
@@ -895,7 +895,7 @@ usage(void) {
   " %1$s [ options ] {-f|--find|loopdev} <file>  setup\n"),
 		progname);
 
-	fprintf(stderr, _("\nOptions:\n"
+	fprintf(f, _("\nOptions:\n"
   " -e | --encryption <type> enable data encryption with specified <name/num>\n"
   " -h | --help              this help\n"
   " -o | --offset <num>      start at offset <num> into file\n"
@@ -904,7 +904,8 @@ usage(void) {
   " -r | --read-only         setup read-only loop device\n"
   "      --show              print device name (with -f <file>)\n"
   " -v | --verbose           verbose mode\n\n"));
-	exit(1);
+
+	exit(f == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
  }
 
 int
@@ -966,6 +967,9 @@ main(int argc, char **argv) {
 		case 'f':
 			find = 1;
 			break;
+		case 'h':
+			usage(stdout);
+			break;
 		case 'j':
 			assoc = optarg;
 			break;
@@ -987,42 +991,42 @@ main(int argc, char **argv) {
                         break;
 
 		default:
-			usage();
+			usage(stderr);
 		}
 	}
 
 	if (argc == 1) {
-		usage();
+		usage(stderr);
 	} else if (delete) {
 		if (argc < optind+1 || encryption || offset || sizelimit ||
 		    capacity || find || all || showdev || assoc || ro)
-			usage();
+			usage(stderr);
 	} else if (find) {
 		if (capacity || all || assoc || argc < optind || argc > optind+1)
-			usage();
+			usage(stderr);
 	} else if (all) {
 		if (argc > 2)
-			usage();
+			usage(stderr);
 	} else if (assoc) {
 		if (capacity || encryption || showdev || passfd || ro)
-			usage();
+			usage(stderr);
 	} else if (capacity) {
 		if (argc != optind + 1 || encryption || offset || sizelimit ||
 		    showdev || ro)
-			usage();
+			usage(stderr);
 	} else {
 		if (argc < optind+1 || argc > optind+2)
-			usage();
+			usage(stderr);
 	}
 
 	if (offset && strtosize(offset, &off)) {
 		error(_("%s: invalid offset '%s' specified"), progname, offset);
-		usage();
+		usage(stderr);
 	}
 	if (sizelimit && strtosize(sizelimit, &slimit)) {
 		error(_("%s: invalid sizelimit '%s' specified"),
 					progname, sizelimit);
-		usage();
+		usage(stderr);
 	}
 
 	if (all)
@@ -1057,7 +1061,7 @@ main(int argc, char **argv) {
 		res = show_loop(device);
 	else {
 		if (passfd && sscanf(passfd, "%d", &pfd) != 1)
-			usage();
+			usage(stderr);
 		do {
 			res = set_loop(device, file, off, slimit, encryption, pfd, &ro);
 			if (res == 2 && find) {
-- 
1.7.2

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux