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
;
40 static void help_command(const char *command_name
,
41 int major_version
, int minor_version
,
42 const struct option
*long_options
)
44 std::cout
<< command_name
<< " " << major_version
<< "." << minor_version
<< std::endl
;
45 std::cout
<< "Current options. A '=' means the option takes a value." << std::endl
<< std::endl
;
47 for (uint32_t x
= 0; long_options
[x
].name
; x
++)
49 std::cout
<< "\t --" << long_options
[x
].name
<< char(long_options
[x
].has_arg
? '=' : ' ') << std::endl
;
52 std::cout
<< std::endl
;
56 static void close_stdio(void)
59 if ((fd
= open("/dev/null", O_RDWR
, 0)) < 0)
65 if (dup2(fd
, STDIN_FILENO
) < 0)
70 if (dup2(fd
, STDOUT_FILENO
) < 0)
75 if (dup2(fd
, STDERR_FILENO
) < 0)
80 if (fd
> STDERR_FILENO
)
93 static void options_parse(int argc
, char *argv
[])
95 static struct option long_options
[]=
97 { "version", no_argument
, NULL
, OPT_VERSION
},
98 { "help", no_argument
, NULL
, OPT_HELP
},
99 { "quiet", no_argument
, NULL
, OPT_QUIET
},
103 bool opt_version
= false;
104 bool opt_help
= false;
109 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
117 case OPT_HELP
: /* --help or -h */
126 /* getopt_long already printed an error message. */
136 version_command(argv
[0], 1, 0);
142 help_command(argv
[0], 1, 0, long_options
);
147 int main(int argc
, char *argv
[])
149 options_parse(argc
, argv
);
153 libtest::Wait
wait(argv
[1]);
155 if (wait
.successful())