c++: Fix possible string truncation
[awesomized/libmemcached] / libtest / has.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "libtest/yatlcon.h"
38 #include <libtest/common.h>
39
40 #include <cstdio>
41 #include <cstdlib>
42 #include <unistd.h>
43
44 namespace libtest {
45
46 bool has_libmemcached_sasl(void)
47 {
48 return false;
49 }
50
51 bool has_libmemcached(void)
52 {
53 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
54 if (HAVE_LIBMEMCACHED)
55 {
56 return true;
57 }
58 #endif
59
60 return false;
61 }
62
63 bool has_libdrizzle(void)
64 {
65 #if defined(HAVE_LIBDRIZZLE) && HAVE_LIBDRIZZLE
66 if (HAVE_LIBDRIZZLE)
67 {
68 return true;
69 }
70 #endif
71
72 return false;
73 }
74
75 bool has_postgres_support(void)
76 {
77 char *getenv_ptr;
78 if (bool((getenv_ptr= getenv("POSTGES_IS_RUNNING_AND_SETUP"))))
79 {
80 (void)(getenv_ptr);
81 if (HAVE_LIBPQ)
82 {
83 return true;
84 }
85 }
86
87 return false;
88 }
89
90
91 bool has_gearmand()
92 {
93 #if defined(GEARMAND_BINARY) && defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
94 if (HAVE_GEARMAND_BINARY)
95 {
96 std::stringstream arg_buffer;
97
98 char *getenv_ptr;
99 if (bool((getenv_ptr= getenv("PWD"))) and
100 ((strcmp(GEARMAND_BINARY, "./gearmand/gearmand") == 0) or (strcmp(GEARMAND_BINARY, "gearmand/gearmand") == 0)))
101 {
102 arg_buffer << getenv_ptr;
103 arg_buffer << "/";
104 }
105 arg_buffer << GEARMAND_BINARY;
106
107 if (access(arg_buffer.str().c_str(), X_OK) == 0)
108 {
109 return true;
110 }
111 }
112 #endif
113
114 return false;
115 }
116
117 bool has_drizzled()
118 {
119 #if defined(DRIZZLED_BINARY) && defined(HAVE_DRIZZLED_BINARY) && HAVE_DRIZZLED_BINARY
120 if (HAVE_DRIZZLED_BINARY)
121 {
122 if (access(DRIZZLED_BINARY, X_OK) == 0)
123 {
124 return true;
125 }
126 }
127 #endif
128
129 return false;
130 }
131
132 bool has_mysqld()
133 {
134 #if defined(MYSQLD_BINARY) && defined(HAVE_MYSQLD_BUILD) && HAVE_MYSQLD_BUILD
135 if (HAVE_MYSQLD_BUILD)
136 {
137 if (access(MYSQLD_BINARY, X_OK) == 0)
138 {
139 return true;
140 }
141 }
142 #endif
143
144 return false;
145 }
146
147 static char memcached_binary_path[FILENAME_MAX];
148
149 static void initialize_memcached_binary_path()
150 {
151 memcached_binary_path[0]= 0;
152
153 #if defined(MEMCACHED_BINARY) && defined(HAVE_MEMCACHED_BINARY) && HAVE_MEMCACHED_BINARY
154 if (HAVE_MEMCACHED_BINARY)
155 {
156 std::stringstream arg_buffer;
157
158 char *getenv_ptr;
159 if (bool((getenv_ptr= getenv("PWD"))) and strcmp(MEMCACHED_BINARY, "memcached/memcached") == 0)
160 {
161 arg_buffer << getenv_ptr;
162 arg_buffer << "/";
163 }
164 arg_buffer << MEMCACHED_BINARY;
165
166 if (access(arg_buffer.str().c_str(), X_OK) == 0)
167 {
168 strncpy(memcached_binary_path, arg_buffer.str().c_str(), FILENAME_MAX-1);
169 }
170 }
171 #endif
172 }
173
174 static pthread_once_t memcached_binary_once= PTHREAD_ONCE_INIT;
175 static void initialize_memcached_binary(void)
176 {
177 int ret;
178 if ((ret= pthread_once(&memcached_binary_once, initialize_memcached_binary_path)) != 0)
179 {
180 FATAL(strerror(ret));
181 }
182 }
183
184 bool has_memcached()
185 {
186 initialize_memcached_binary();
187
188 if (memcached_binary_path[0] and (strlen(memcached_binary_path) > 0))
189 {
190 return true;
191 }
192
193 return false;
194 }
195
196 const char* memcached_binary()
197 {
198 initialize_memcached_binary();
199
200 if (memcached_binary_path[0])
201 {
202 return memcached_binary_path;
203 }
204
205 return NULL;
206 }
207
208 const char *gearmand_binary()
209 {
210 #if defined(GEARMAND_BINARY)
211 return GEARMAND_BINARY;
212 #else
213 return NULL;
214 #endif
215 }
216
217 const char *drizzled_binary()
218 {
219 #if defined(DRIZZLED_BINARY)
220 return DRIZZLED_BINARY;
221 #else
222 return NULL;
223 #endif
224 }
225
226 } // namespace libtest