Issues fixed for Clang
[awesomized/libmemcached] / clients / utilities.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11 #include <config.h>
12
13 #include <clients/utilities.h>
14
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cstring>
18 #include <ctype.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24
25 long int timedif(struct timeval a, struct timeval b)
26 {
27 long us, s;
28
29 us = (int)(a.tv_usec - b.tv_usec);
30 us /= 1000;
31 s = (int)(a.tv_sec - b.tv_sec);
32 s *= 1000;
33 return s + us;
34 }
35
36 void version_command(const char *command_name)
37 {
38 printf("%s v%u.%u\n", command_name, 1U, 0U);
39 exit(EXIT_SUCCESS);
40 }
41
42 void close_stdio(void)
43 {
44 int fd;
45 if ((fd = open("/dev/null", O_RDWR, 0)) < 0)
46 {
47 return;
48 }
49 else
50 {
51 if (dup2(fd, STDIN_FILENO) < 0)
52 {
53 return;
54 }
55
56 if (dup2(fd, STDOUT_FILENO) < 0)
57 {
58 return;
59 }
60
61 if (dup2(fd, STDERR_FILENO) < 0)
62 {
63 return;
64 }
65
66 if (fd > STDERR_FILENO)
67 {
68 close(fd);
69 }
70 }
71 }
72
73
74 static const char *lookup_help(memcached_options option)
75 {
76 switch (option)
77 {
78 case OPT_SERVERS: return("List which servers you wish to connect to.");
79 case OPT_VERSION: return("Display the version of the application and then exit.");
80 case OPT_HELP: return("Display this message and then exit.");
81 case OPT_VERBOSE: return("Give more details on the progression of the application.");
82 case OPT_QUIET: return("stderr and stdin will be closed at application startup.");
83 case OPT_DEBUG: return("Provide output only useful for debugging.");
84 case OPT_FLAG: return("Provide flag information for storage operation.");
85 case OPT_EXPIRE: return("Set the expire option for the object.");
86 case OPT_SET: return("Use set command with memcached when storing.");
87 case OPT_REPLACE: return("Use replace command with memcached when storing.");
88 case OPT_ADD: return("Use add command with memcached when storing.");
89 case OPT_SLAP_EXECUTE_NUMBER: return("Number of times to execute the given test.");
90 case OPT_SLAP_INITIAL_LOAD: return("Number of key pairs to load before executing tests.");
91 case OPT_SLAP_TEST: return("Test to run (currently \"get\" or \"set\").");
92 case OPT_SLAP_CONCURRENCY: return("Number of users to simulate with load.");
93 case OPT_SLAP_NON_BLOCK: return("Set TCP up to use non-blocking IO.");
94 case OPT_SLAP_TCP_NODELAY: return("Set TCP socket up to use nodelay.");
95 case OPT_FLUSH: return("Flush servers before running tests.");
96 case OPT_HASH: return("Select hash type.");
97 case OPT_BINARY: return("Switch to binary protocol.");
98 case OPT_ANALYZE: return("Analyze the provided servers.");
99 case OPT_UDP: return("Use UDP protocol when communicating with server.");
100 case OPT_USERNAME: return "Username to use for SASL authentication";
101 case OPT_PASSWD: return "Password to use for SASL authentication";
102 case OPT_FILE: return "Path to file in which to save result";
103 case OPT_STAT_ARGS: return "Argument for statistics";
104 default: WATCHPOINT_ASSERT(0);
105 };
106
107 WATCHPOINT_ASSERT(0);
108 return "forgot to document this function :)";
109 }
110
111 void help_command(const char *command_name, const char *description,
112 const struct option *long_options,
113 memcached_programs_help_st *options)
114 {
115 unsigned int x;
116 (void)options;
117
118 printf("%s v%u.%u\n\n", command_name, 1U, 0U);
119 printf("\t%s\n\n", description);
120 printf("Current options. A '=' means the option takes a value.\n\n");
121
122 for (x= 0; long_options[x].name; x++)
123 {
124 const char *help_message;
125
126 printf("\t --%s%c\n", long_options[x].name,
127 long_options[x].has_arg ? '=' : ' ');
128 if ((help_message= lookup_help(memcached_options(long_options[x].val))))
129 printf("\t\t%s\n", help_message);
130 }
131
132 printf("\n");
133 exit(EXIT_SUCCESS);
134 }
135
136 void process_hash_option(memcached_st *memc, char *opt_hash)
137 {
138 uint64_t set;
139 memcached_return_t rc;
140
141 if (opt_hash == NULL)
142 return;
143
144 set= MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */
145 if (!strcasecmp(opt_hash, "CRC"))
146 {
147 set= MEMCACHED_HASH_CRC;
148 }
149 else if (!strcasecmp(opt_hash, "FNV1_64"))
150 {
151 set= MEMCACHED_HASH_FNV1_64;
152 }
153 else if (!strcasecmp(opt_hash, "FNV1A_64"))
154 {
155 set= MEMCACHED_HASH_FNV1A_64;
156 }
157 else if (!strcasecmp(opt_hash, "FNV1_32"))
158 {
159 set= MEMCACHED_HASH_FNV1_32;
160 }
161 else if (!strcasecmp(opt_hash, "FNV1A_32"))
162 {
163 set= MEMCACHED_HASH_FNV1A_32;
164 }
165 else
166 {
167 fprintf(stderr, "hash: type not recognized %s\n", opt_hash);
168 exit(EXIT_FAILURE);
169 }
170
171 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
172 if (rc != MEMCACHED_SUCCESS)
173 {
174 fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc));
175 exit(EXIT_FAILURE);
176 }
177 }
178
179 void initialize_sockets(void)
180 {
181 /* Define the function for all platforms to avoid #ifdefs in each program */
182 #if defined(WIN32) && WIN32
183 WSADATA wsaData;
184 if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
185 {
186 fprintf(stderr, "Socket Initialization Error. Program aborted\n");
187 exit(EXIT_FAILURE);
188 }
189 #endif
190 }