From 922eafb28ab4331e7183901a1eb2e5e2269629db Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Wed, 31 Aug 2011 20:33:27 +0200 Subject: pivot_root: add version & help option Including other necessary changes to usage(). Signed-off-by: Sami Kerola --- sys-utils/pivot_root.c | 60 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 10 deletions(-) (limited to 'sys-utils/pivot_root.c') diff --git a/sys-utils/pivot_root.c b/sys-utils/pivot_root.c index f2a6f4834..8669748a1 100644 --- a/sys-utils/pivot_root.c +++ b/sys-utils/pivot_root.c @@ -13,21 +13,61 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ +#include +#include +#include #include +#include #include #include +#include "c.h" +#include "nls.h" + #define pivot_root(new_root,put_old) syscall(SYS_pivot_root,new_root,put_old) -int main(int argc, const char **argv) +static void __attribute__ ((__noreturn__)) usage(FILE * out) { - if (argc != 3) { - fprintf(stderr, "usage: %s new_root put_old\n", argv[0]); - return 1; - } - if (pivot_root(argv[1], argv[2]) < 0) { - perror("pivot_root"); - return 1; - } - return 0; + fprintf(out, USAGE_HEADER); + fprintf(out, _(" %s [options] new_root put_old\n"), + program_invocation_short_name); + fprintf(out, USAGE_HELP); + fprintf(out, USAGE_VERSION); + fprintf(out, USAGE_BEGIN_TAIL); + fprintf(out, USAGE_MAN_TAIL, "pivot_root(8)"); + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); +} + +int main(int argc, char **argv) +{ + int ch; + static const struct option longopts[] = { + {"version", no_argument, NULL, 'V'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; + + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + + while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) + switch (ch) { + case 'V': + printf(UTIL_LINUX_VERSION); + return EXIT_SUCCESS; + case 'h': + usage(stdout); + default: + usage(stderr); + } + + if (argc != 3) + usage(stderr); + + if (pivot_root(argv[1], argv[2]) < 0) + err(EXIT_FAILURE, _("failed to change root from `%s' to `%s'"), + argv[1], argv[2]); + + return EXIT_SUCCESS; } -- cgit v1.2.3-55-g7522