Removed hardcoded -rpath from client libs. libtool should be handling this appropriat...
[m6w6/libmemcached] / clients / memcp.c
index 780514c1bc56b7bf7daac5878f85cd8449a9af5f..716bfefd03482e94c40d8b881260e598c2d55206 100644 (file)
@@ -1,6 +1,7 @@
+#include "libmemcached/common.h"
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <sys/types.h>
@@ -10,7 +11,6 @@
 #include <errno.h>
 #include <strings.h>
 #include <string.h>
-#include <assert.h>
 
 #include <libmemcached/memcached.h>
 
@@ -23,6 +23,7 @@
 /* Prototypes */
 void options_parse(int argc, char *argv[]);
 
+static int opt_binary=0;
 static int opt_verbose= 0;
 static char *opt_servers= NULL;
 static char *opt_hash= NULL;
@@ -61,6 +62,8 @@ int main(int argc, char *argv[])
 
   memcached_server_push(memc, servers);
   memcached_server_list_free(servers);
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
+                         (uint64_t)opt_binary);
 
   while (optind < argc) 
   {
@@ -88,37 +91,42 @@ int main(int argc, char *argv[])
 
     if (opt_verbose) 
     {
-      static char *opstr[] = { "set", "add", "replace" };
+      static const char *opstr[] = { "set", "add", "replace" };
       printf("op: %s\nsource file: %s\nlength: %zu\n"
             "key: %s\nflags: %x\nexpires: %llu\n",
             opstr[opt_method - OPT_SET], argv[optind], (size_t)sbuf.st_size,
             ptr, opt_flags, (unsigned long long)opt_expires);
     }
 
-    if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)
+    if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
     {
       fprintf(stderr, "malloc: %s\n", strerror(errno)); 
       exit(1);
     }
 
-    if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)
+    if ((read_length= read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
     {
       fprintf(stderr, "read: %s\n", strerror(errno)); 
       exit(1);
     }
-    assert(read_length == sbuf.st_size);
+
+    if (read_length != sbuf.st_size)
+    {
+      fprintf(stderr, "Failure reading from file\n");
+      exit(1);
+    }
 
     if (opt_method == OPT_ADD)
       rc= memcached_add(memc, ptr, strlen(ptr),
-                        file_buffer_ptr, sbuf.st_size,
+                        file_buffer_ptr, (size_t)sbuf.st_size,
                        opt_expires, opt_flags);
     else if (opt_method == OPT_REPLACE)
       rc= memcached_replace(memc, ptr, strlen(ptr),
-                           file_buffer_ptr, sbuf.st_size,
+                           file_buffer_ptr, (size_t)sbuf.st_size,
                            opt_expires, opt_flags);
     else
       rc= memcached_set(memc, ptr, strlen(ptr),
-                        file_buffer_ptr, sbuf.st_size,
+                        file_buffer_ptr, (size_t)sbuf.st_size,
                         opt_expires, opt_flags);
 
     if (rc != MEMCACHED_SUCCESS)
@@ -157,17 +165,18 @@ void options_parse(int argc, char *argv[])
 
   static struct option long_options[]=
     {
-      {"version", no_argument, NULL, OPT_VERSION},
-      {"help", no_argument, NULL, OPT_HELP},
-      {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
-      {"debug", no_argument, &opt_verbose, OPT_DEBUG},
-      {"servers", required_argument, NULL, OPT_SERVERS},
-      {"flag", required_argument, NULL, OPT_FLAG},
-      {"expire", required_argument, NULL, OPT_EXPIRE},
-      {"set",  no_argument, NULL, OPT_SET},
-      {"add",  no_argument, NULL, OPT_ADD},
-      {"replace",  no_argument, NULL, OPT_REPLACE},
-      {"hash", required_argument, NULL, OPT_HASH},
+      {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
+      {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
+      {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
+      {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
+      {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
+      {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG},
+      {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
+      {(OPTIONSTRING)"set",  no_argument, NULL, OPT_SET},
+      {(OPTIONSTRING)"add",  no_argument, NULL, OPT_ADD},
+      {(OPTIONSTRING)"replace",  no_argument, NULL, OPT_REPLACE},
+      {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
+      {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
       {0, 0, 0, 0},
     };
 
@@ -181,6 +190,9 @@ void options_parse(int argc, char *argv[])
     {
     case 0:
       break;
+    case OPT_BINARY:
+      opt_binary = 1;
+      break;
     case OPT_VERBOSE: /* --verbose or -v */
       opt_verbose = OPT_VERBOSE;
       break;