Updating for 1.0.2 release
[awesomized/libmemcached] / libtest / wait.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <config.h>
23
24 #include <cstdlib>
25 #include <fcntl.h>
26 #include <getopt.h>
27 #include <iostream>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include <libtest/wait.h>
33
34 static void version_command(const char *command_name, int major_version, int minor_version)
35 {
36 std::cout << command_name << " " << major_version << "." << minor_version << std::endl;
37 exit(EXIT_SUCCESS);
38 }
39
40 static void help_command(const char *command_name,
41 int major_version, int minor_version,
42 const struct option *long_options)
43 {
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;
46
47 for (uint32_t x= 0; long_options[x].name; x++)
48 {
49 std::cout << "\t --" << long_options[x].name << char(long_options[x].has_arg ? '=' : ' ') << std::endl;
50 }
51
52 std::cout << std::endl;
53 exit(EXIT_SUCCESS);
54 }
55
56 static void close_stdio(void)
57 {
58 int fd;
59 if ((fd = open("/dev/null", O_RDWR, 0)) < 0)
60 {
61 return;
62 }
63 else
64 {
65 if (dup2(fd, STDIN_FILENO) < 0)
66 {
67 return;
68 }
69
70 if (dup2(fd, STDOUT_FILENO) < 0)
71 {
72 return;
73 }
74
75 if (dup2(fd, STDERR_FILENO) < 0)
76 {
77 return;
78 }
79
80 if (fd > STDERR_FILENO)
81 {
82 close(fd);
83 }
84 }
85 }
86
87 enum {
88 OPT_HELP,
89 OPT_QUIET,
90 OPT_VERSION
91 };
92
93 static void options_parse(int argc, char *argv[])
94 {
95 static struct option long_options[]=
96 {
97 { "version", no_argument, NULL, OPT_VERSION},
98 { "help", no_argument, NULL, OPT_HELP},
99 { "quiet", no_argument, NULL, OPT_QUIET},
100 {0, 0, 0, 0},
101 };
102
103 bool opt_version= false;
104 bool opt_help= false;
105 int option_index= 0;
106
107 while (1)
108 {
109 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
110 if (option_rv == -1)
111 {
112 break;
113 }
114
115 switch (option_rv)
116 {
117 case OPT_HELP: /* --help or -h */
118 opt_help= true;
119 break;
120
121 case OPT_QUIET:
122 close_stdio();
123 break;
124
125 case '?':
126 /* getopt_long already printed an error message. */
127 exit(EXIT_SUCCESS);
128
129 default:
130 abort();
131 }
132 }
133
134 if (opt_version)
135 {
136 version_command(argv[0], 1, 0);
137 exit(EXIT_SUCCESS);
138 }
139
140 if (opt_help)
141 {
142 help_command(argv[0], 1, 0, long_options);
143 exit(EXIT_SUCCESS);
144 }
145 }
146
147 int main(int argc, char *argv[])
148 {
149 options_parse(argc, argv);
150
151 if (argc == 2)
152 {
153 libtest::Wait wait(argv[1]);
154
155 if (wait.successful())
156 return EXIT_SUCCESS;
157 }
158
159 return EXIT_FAILURE;
160 }