1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
32 #include <libtest/wait.h>
34 static void version_command(const char *command_name
, int major_version
, int minor_version
)
36 std::cout
<< command_name
<< " " << major_version
<< "." << minor_version
<< std::endl
;
39 static void help_command(const char *command_name
,
40 int major_version
, int minor_version
,
41 const struct option
*long_options
)
43 std::cout
<< command_name
<< " " << major_version
<< "." << minor_version
<< std::endl
;
44 std::cout
<< "Current options. A '=' means the option takes a value." << std::endl
<< std::endl
;
46 for (uint32_t x
= 0; long_options
[x
].name
; x
++)
48 std::cout
<< "\t --" << long_options
[x
].name
<< char(long_options
[x
].has_arg
? '=' : ' ') << std::endl
;
51 std::cout
<< std::endl
;
54 static void close_stdio(void)
57 if ((fd
= open("/dev/null", O_RDWR
, 0)) < 0)
63 if (dup2(fd
, STDIN_FILENO
) < 0)
68 if (dup2(fd
, STDOUT_FILENO
) < 0)
73 if (dup2(fd
, STDERR_FILENO
) < 0)
78 if (fd
> STDERR_FILENO
)
91 static void options_parse(int argc
, char *argv
[])
93 static struct option long_options
[]=
95 { "version", no_argument
, NULL
, OPT_VERSION
},
96 { "help", no_argument
, NULL
, OPT_HELP
},
97 { "quiet", no_argument
, NULL
, OPT_QUIET
},
101 bool opt_version
= false;
102 bool opt_help
= false;
103 bool opt_quiet
= false;
108 int option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
116 case OPT_HELP
: /* --help or -h */
120 case OPT_VERSION
: /* --version or -v */
129 /* getopt_long already printed an error message. */
133 help_command(argv
[0], 1, 0, long_options
);
145 version_command(argv
[0], 1, 0);
151 help_command(argv
[0], 1, 0, long_options
);
156 int main(int argc
, char *argv
[])
163 options_parse(argc
, argv
);
165 int ret
= EXIT_FAILURE
;
166 while (optind
< argc
)
168 libtest::Wait
wait(argv
[optind
++]);
170 if (wait
.successful() == false)