--INCLUDES = -I$(top_builddir)/include\r
++INCLUDES = -I$(top_builddir)/include -Wall\r
LIBS =\r
\r
lib_LTLIBRARIES = libmemcached.la\r
void memcached_deinit(memcached_st *ptr)
{
unsigned int x;
-- memcached_host_st *host_ptr;
if (ptr->hosts)
{
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"%s %.*s %u\r\n", verb,
-- key_length, key,
++ (int)key_length, key,
offset);
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
- {
- fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
-
+ if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
- {
- fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
-
return MEMCACHED_WRITE_FAILURE;
-- }
memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
send_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
unsigned int x;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
-- memcached_host_st *host_ptr;
if (ptr->connected)
return MEMCACHED_SUCCESS;
for (x= 0; x < ptr->number_of_hosts; x++)
{
if ((h= gethostbyname(ptr->hosts[x].hostname)) == NULL)
-- {
-- fprintf(stderr, "unknown host '%s'\n", ptr->hosts[x].hostname);
return MEMCACHED_HOST_LOCKUP_FAILURE;
-- }
servAddr.sin_family= h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
/* Create the socket */
if ((ptr->hosts[0].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
-- {
-- fprintf(stderr, "cannot open socket");
return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
-- }
/* bind any port number */
localAddr.sin_port = htons(0);
if (bind(ptr->hosts[0].fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0)
-- {
-- fprintf(stderr, "cannot bind port TCP %u\n", ptr->hosts[x].port);
return(MEMCACHED_CONNECTION_BIND_FAILURE);
-- }
/* connect to server */
if (connect(ptr->hosts[0].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
-- {
-- fprintf(stderr, "cannot connect to host '%s' (%u) (error: %s)\n", ptr->hosts[x].hostname,
-- ptr->hosts[x].port,
-- strerror(errno));
return MEMCACHED_HOST_LOCKUP_FAILURE;
-- }
}
ptr->connected= 1;
if (expiration)
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
-- "delete %.*s %u\r\n", key_length, key, expiration);
++ "delete %.*s %llu\r\n", (int)key_length, key,
++ (unsigned long long)expiration);
else
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
-- "delete %.*s\r\n", key_length, key);
-- if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
-- {
-- fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
++ "delete %.*s\r\n", (int)key_length, key);
++ if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
return MEMCACHED_WRITE_FAILURE;
-- }
return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
}
{
if (expiration)
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
-- "flush_all %u\r\n", expiration);
++ "flush_all %llu\r\n", (unsigned long long)expiration);
else
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"flush_all\r\n");
- if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1))
- {
- fprintf(stderr, "failed flush_all TCP\n");
-
+ if ((write(ptr->hosts[x].fd, buffer, send_length) == -1))
- {
- fprintf(stderr, "failed flush_all TCP\n");
-
return MEMCACHED_WRITE_FAILURE;
-- }
rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n",
-- key_length, key);
++ (int)key_length, key);
if (*error != MEMCACHED_SUCCESS)
return NULL;
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
+ if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
{
-- fprintf(stderr, "failed fetch on %.*s TCP\n", key_length+1, key);
*error= MEMCACHED_WRITE_FAILURE;
return NULL;
}
memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args,
char *hostname, unsigned int port)
{
-- size_t send_length;
memcached_return rc;
-- char buffer[HUGE_STRING_LEN];
memcached_st memc;
memcached_init(&memc);
server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
- send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
- "%s %.*s %u %u %u\r\n", verb,
- key_length, key, flags, expiration, value_length);
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
- {
- fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
-
+ write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
- "%s %.*s %u %u %u\r\n", verb,
- key_length, key, flags, expiration, value_length);
++ "%s %.*s %x %llu %zu\r\n", verb,
++ (int)key_length, key, flags,
++ (unsigned long long)expiration, value_length);
+ if ((sent_length= write(ptr->hosts[server_key].fd, buffer, write_length)) == -1)
- {
- fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno));
-
return MEMCACHED_WRITE_FAILURE;
-- }
+ assert(write_length == sent_length);
- WATCHPOINT;
- printf("About to push %.*s\n", value_length, value);
- WATCHPOINT;
- send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
- "%.*s\r\n",
- value_length, value);
- if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
- {
- fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
+ if ((sent_length= write(ptr->hosts[server_key].fd, value, value_length)) == -1)
- {
- fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno));
-
+ return MEMCACHED_WRITE_FAILURE;
- }
+ assert(value_length == sent_length);
- if ((sent_length= write(ptr->hosts[server_key].fd, "\r\n", 2)) == -1)
- {
- fprintf(stderr, "failed %s on %.*s: %s\n", verb, key_length+1, key, strerror(errno));
++ if ((sent_length= write(ptr->hosts[server_key].fd, "\r\n", 2)) == -1)
return MEMCACHED_WRITE_FAILURE;
-- }
-
- send_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
+ assert(2 == sent_length);
+
+ sent_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
- if (send_length && buffer[0] == 'S') /* STORED */
+ if (sent_length && buffer[0] == 'S') /* STORED */
return MEMCACHED_SUCCESS;
- else if (send_length && buffer[0] == 'N') /* NOT_STORED */
+ else if (write_length && buffer[0] == 'N') /* NOT_STORED */
return MEMCACHED_NOTSTORED;
else
return MEMCACHED_READ_FAILURE;
case MEMCACHED_PARTIAL_READ:
return "PARTIAL READ";
case MEMCACHED_SOME_ERRORS:
-- return "SOME ERRORS WERE REPORTED ";
++ return "SOME ERRORS WERE REPORTED";
};
++
++ return "COMPLETELY UNKNOWN ERROR, SOMEONE FORGOT TO UPDATE ERROR MESSAGES";
}
{
memcached_return rc;
- if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1))
- {
- fprintf(stderr, "failed verbosity\n");
-
+ if ((write(ptr->hosts[x].fd, buffer, send_length) == -1))
- {
- fprintf(stderr, "failed verbosity\n");
-
return MEMCACHED_WRITE_FAILURE;
-- }
rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
--INCLUDES = -I$(top_builddir)/include\r
++INCLUDES = -I$(top_builddir)/include -Wall\r
LDADDS = ../lib/libmemcached.la\r
\r
bin_PROGRAMS = memcat memcp memstat memrm\r
/* Prototypes */\r
void options_parse(int argc, char *argv[]);\r
\r
--static int opt_verbose;\r
--static int opt_displayflag;\r
++static int opt_verbose= 0;\r
++static int opt_displayflag= 0;\r
static char *opt_servers;\r
\r
int main(int argc, char *argv[])\r
{\r
if (opt_displayflag)\r
{\r
-- if (opt_verbose)\r
-- printf("key: %s\nflags: ", argv[optind]);\r
- printf("%u\n", flags);\r
- printf("%x\n", flags);\r
++ if (opt_verbose)\r
++ printf("key: %s\nflags: ", argv[optind]);\r
++ printf("%x\n", flags);\r
}\r
else \r
{\r
-- if (opt_verbose)\r
- printf("key: %s\nflags: %u\nlength: %uz\nvalue: ",\r
- printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",\r
-- argv[optind], flags, string_length);\r
-- printf("%.*s\n", string_length, string);\r
++ if (opt_verbose)\r
++ printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",\r
++ argv[optind], flags, string_length);\r
++ printf("%.*s\n", (int)string_length, string);\r
free(string);\r
}\r
}\r
else\r
{\r
fprintf(stderr, "memcat: %s: memcache error %s\n", \r
-- argv[optind], memcached_strerror(memc, rc));\r
++ argv[optind], memcached_strerror(memc, rc));\r
}\r
optind++;\r
}\r
\r
memcached_deinit(memc);\r
\r
++ free(opt_servers);\r
++\r
return 0;\r
};\r
\r
exit(0);\r
break;\r
case OPT_SERVERS: /* --servers or -s */\r
-- opt_servers= optarg;\r
++ opt_servers= strdup(optarg);\r
break;\r
case '?':\r
/* getopt_long already printed an error message. */\r
\r
#include <memcached.h>\r
#include "client_options.h"\r
++#include "utilities.h"\r
\r
/* Prototypes */\r
void options_parse(int argc, char *argv[]);\r
\r
-static int opt_verbose;\r
-static char *opt_servers;\r
-static int opt_replace;\r
-uint16_t opt_flags= 0;\r
-time_t opt_expires= 0;\r
+static int opt_verbose= 0;\r
+static char *opt_servers= NULL;\r
- static int opt_method= 0;\r
- uint16_t opt_flags= 0;\r
- time_t opt_expires= 0;\r
++static int opt_method= OPT_SET;\r
++static uint16_t opt_flags= 0;\r
++static time_t opt_expires= 0;\r
\r
int main(int argc, char *argv[])\r
{\r
memcached_st *memc;\r
-- char *string;\r
-- size_t string_length;\r
memcached_return rc;\r
+ \r
options_parse(argc, argv);\r
\r
memc= memcached_init(NULL);\r
- parse_opt_servers(memc, opt_servers);\r
+ \r
- while (optind <= argc) \r
+ if (opt_servers)\r
+ parse_opt_servers(memc, opt_servers);\r
+ else\r
+ parse_opt_servers(memc, argv[--argc]);\r
+\r
+ while (optind < argc) \r
{\r
- char *mptr;\r
struct stat sbuf;\r
int fd;\r
char *ptr;\r
+ ssize_t read_length;\r
+ char *file_buffer_ptr;\r
\r
fd= open(argv[optind], O_RDONLY);\r
- if (fd < 0) \r
+ if (fd < 0)\r
{\r
fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));\r
optind++;\r
else\r
ptr= argv[optind];\r
\r
- if (opt_verbose)\r
+ if (opt_verbose) \r
{\r
- static char *opstr[]= { "set", "add", "replace" };\r
+ static char *opstr[] = { "set", "add", "replace" };\r
printf("op: %s\nsource file: %s\nlength: %zu\n"\r
- "key: %s\nflags: %u\n expires: %llu\n",\r
- opstr[opt_method], argv[optind], sbuf.st_size,\r
- "key: %s\nflags: %x\n expires: %ld\n",\r
- opstr[opt_replace], argv[optind], sbuf.st_size,\r
-- ptr, opt_flags, opt_expires);\r
++ "key: %s\nflags: %x\n expires: %llu\n",\r
++ opstr[opt_method], argv[optind], (size_t)sbuf.st_size,\r
++ ptr, opt_flags, (unsigned long long)opt_expires);\r
}\r
\r
- if (opt_replace == 0)\r
- rc= memcached_set(memc, ptr, strlen(ptr),\r
- mptr, sbuf.st_size,\r
- opt_expires, opt_flags);\r
- else if (opt_replace == 1)\r
+ if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)\r
+ {\r
+ fprintf(stderr, "malloc: %s\n", strerror(errno)); \r
+ exit(1);\r
+ }\r
+\r
+ if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)\r
+ {\r
+ fprintf(stderr, "read: %s\n", strerror(errno)); \r
+ exit(1);\r
+ }\r
+ assert(read_length == sbuf.st_size);\r
+\r
+ if (opt_method == OPT_ADD)\r
rc= memcached_add(memc, ptr, strlen(ptr),\r
- mptr, sbuf.st_size,\r
+ file_buffer_ptr, sbuf.st_size,\r
opt_expires, opt_flags);\r
- else if (opt_replace == 2)\r
+ else if (opt_method == OPT_REPLACE)\r
rc= memcached_replace(memc, ptr, strlen(ptr),\r
- mptr, sbuf.st_size,\r
+ file_buffer_ptr, sbuf.st_size,\r
opt_expires, opt_flags);\r
else\r
- abort();\r
+ rc= memcached_set(memc, ptr, strlen(ptr),\r
+ file_buffer_ptr, sbuf.st_size,\r
+ opt_expires, opt_flags);\r
\r
if (rc != MEMCACHED_SUCCESS)\r
- {\r
fprintf(stderr, "memcp: %s: memcache error %s\n", \r
ptr, memcached_strerror(memc, rc));\r
- }\r
\r
- munmap(mptr, sbuf.st_size);\r
+ WATCHPOINT;\r
+ free(file_buffer_ptr);\r
close(fd);\r
optind++;\r
}\r
\r
memcached_deinit(memc);\r
\r
- cleanup();\r
++ free(opt_servers);\r
+\r
return 0;\r
- };\r
+ }\r
\r
void options_parse(int argc, char *argv[])\r
{\r
printf("useful help messages go here\n");\r
exit(0);\r
case OPT_SERVERS: /* --servers or -s */\r
- opt_servers= strdup_cleanup(optarg);\r
- opt_servers= optarg;\r
++ opt_servers= strdup(optarg);\r
break;\r
case OPT_FLAG: /* --flag */\r
- opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10);\r
+ opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16);\r
break;\r
case OPT_EXPIRE: /* --expire */\r
-- opt_expires= (time_t)strtol(optarg, (char **)NULL, 10);\r
++ opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);\r
+ break;\r
+ case OPT_SET:\r
+ opt_method= OPT_SET;\r
+ break;\r
+ case OPT_REPLACE:\r
+ opt_method= OPT_REPLACE;\r
+ break;\r
+ case OPT_ADD:\r
+ opt_method= OPT_ADD;\r
break;\r
case '?':\r
/* getopt_long already printed an error message. */\r
#include <getopt.h>\r
#include <memcached.h>\r
#include "client_options.h"\r
++#include "utilities.h"\r
\r
--static int opt_verbose;\r
--static char *opt_servers;\r
++static int opt_verbose= 0;\r
++static time_t opt_expire= 0;\r
++static char *opt_servers= NULL;\r
++\r
++/* Prototypes */\r
++void options_parse(int argc, char *argv[]);\r
\r
int main(int argc, char *argv[])\r
{\r
memcached_st *memc;\r
-- char *string;\r
-- size_t string_length;\r
- time_t expires = 0;\r
- time_t expires= 0;\r
memcached_return rc;\r
\r
- static struct option long_options[] =\r
- static struct option long_options[]=\r
++ options_parse(argc, argv);\r
++\r
++ memc= memcached_init(NULL);\r
++\r
++ parse_opt_servers(memc, opt_servers);\r
++\r
++ if (opt_servers)\r
++ parse_opt_servers(memc, opt_servers);\r
++ \r
++ while (optind <= argc) \r
++ {\r
++ if (opt_verbose) \r
++ printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);\r
++ rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);\r
++\r
++ if (rc != MEMCACHED_SUCCESS) \r
{\r
-- {"version", no_argument, NULL, OPT_VERSION},\r
-- {"help", no_argument, NULL, OPT_HELP},\r
- {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},\r
- {"debug", no_argument, &opt_verbose, OPT_DEBUG},\r
- {"verbose", no_argument, &opt_verbose, 1},\r
- {"debug", no_argument, &opt_verbose, 2},\r
-- {"servers", required_argument, NULL, OPT_SERVERS},\r
-- {"expire", required_argument, NULL, OPT_EXPIRE},\r
-- {0, 0, 0, 0},\r
-- };\r
- int option_index = 0;\r
++ fprintf(stderr, "memrm: %s: memcache error %s\n", \r
++ argv[optind], memcached_strerror(memc, rc));\r
++ }\r
++\r
++ optind++;\r
++ }\r
++\r
++ memcached_deinit(memc);\r
++\r
++ free(opt_servers);\r
++\r
++ return 0;\r
++}\r
++\r
++\r
++void options_parse(int argc, char *argv[])\r
++{\r
++ static struct option long_options[]=\r
++ {\r
++ {"version", no_argument, NULL, OPT_VERSION},\r
++ {"help", no_argument, NULL, OPT_HELP},\r
++ {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},\r
++ {"debug", no_argument, &opt_verbose, OPT_DEBUG},\r
++ {"servers", required_argument, NULL, OPT_SERVERS},\r
++ {"expire", required_argument, NULL, OPT_EXPIRE},\r
++ {0, 0, 0, 0},\r
++ };\r
+ int option_index= 0;\r
int option_rv;\r
\r
while (1) \r
exit(0);\r
break;\r
case OPT_SERVERS: /* --servers or -s */\r
- opt_servers = optarg;\r
- opt_servers= optarg;\r
++ opt_servers= strdup(optarg);\r
break;\r
case OPT_EXPIRE: /* --expire */\r
- expires = (time_t)strtol(optarg, (char **)NULL, 10);\r
- expires= (time_t)strtol(optarg, (char **)NULL, 10);\r
++ opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);\r
break;\r
case '?':\r
/* getopt_long already printed an error message. */\r
abort();\r
}\r
}\r
--\r
-- memc= memcached_init(NULL);\r
-- parse_opt_servers(memc, opt_servers);\r
-- \r
-- while (optind <= argc) \r
-- {\r
- if (opt_verbose) \r
- printf("key: %s\nexpires: %llu\n", argv[optind], expires);\r
- rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), expires);\r
- if (opt_verbose)\r
- printf("key: %s\nexpires: %ld\n", argv[optind], expires);\r
--\r
- if (rc != MEMCACHED_SUCCESS) \r
- {\r
- rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), expires);\r
-\r
- if (rc != MEMCACHED_SUCCESS)\r
-- fprintf(stderr, "memrm: %s: memcache error %s\n", \r
-- argv[optind], memcached_strerror(memc, rc));\r
- }\r
--\r
-- optind++;\r
-- }\r
--\r
-- memcached_deinit(memc);\r
--\r
-- return 0;\r
- };\r
+ }\r
#include <memcached.h>
- static char **cleanup_list= NULL;
- static char cleanup_list_length= 0;
-
void parse_opt_servers(memcached_st *memc,
- char *opt_servers)
+ char *server_strings)
{
char *string;
unsigned int port;
}
}
}
-
- void cleanup(void)
- {
- unsigned int x;
- for (x= 0; x < cleanup_list_length; x++)
- free(cleanup_list[x]);
-
- free(cleanup_list);
- }
-
- char *strdup_cleanup(const char *str)
- {
- char *ptr;
-
- ptr= strdup(str);
-
- cleanup_list= (char **)realloc(cleanup_list, sizeof(char *) * (cleanup_list_length+1));
- cleanup_list[cleanup_list_length]= ptr;
- cleanup_list_length++;
--
- return ptr;
- }