From: Sami Kerola Date: Sun, 30 Oct 2011 13:08:33 +0000 (+0100) Subject: setsid: add long options and fix coding style X-Git-Tag: v2.21-rc1~194^2~19 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cbfa325318dc9ed7f47bffa9e0f3b823fbbb003c;p=thirdparty%2Futil-linux.git setsid: add long options and fix coding style Signed-off-by: Sami Kerola --- diff --git a/sys-utils/setsid.c b/sys-utils/setsid.c index baf3b0aae4..268871a8bd 100644 --- a/sys-utils/setsid.c +++ b/sys-utils/setsid.c @@ -11,38 +11,69 @@ * */ +#include #include -#include #include +#include + +#include "c.h" #include "nls.h" -int -main(int argc, char *argv[]) { +static void __attribute__ ((__noreturn__)) usage(FILE * out) +{ + fprintf(out, USAGE_HEADER); + fprintf(out, _(" %s [options] [arguments ...]\n"), + program_invocation_short_name); + fprintf(out, USAGE_OPTIONS); + fprintf(out, USAGE_HELP); + fprintf(out, USAGE_VERSION); + fprintf(out, USAGE_MAN_TAIL("setsid(1)")); + 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); - - if (argc < 2) { - fprintf(stderr, _("usage: %s program [arg ...]\n"), - argv[0]); - exit(1); - } + + 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 < 2) + usage(stderr); + if (getpgrp() == getpid()) { - switch(fork()){ + switch (fork()) { case -1: - perror("fork"); - exit(1); - case 0: /* child */ + err(EXIT_FAILURE, _("fork")); + case 0: + /* child */ break; - default: /* parent */ - exit(0); + default: + /* parent */ + return 0; } } - if (setsid() < 0) { - perror("setsid"); /* cannot happen */ - exit(1); - } + if (setsid() < 0) + /* cannot happen */ + err(EXIT_FAILURE, _("setsid failed")); + execvp(argv[1], argv + 1); - perror("execvp"); - exit(1); + err(EXIT_FAILURE, _("execvp failed")); }