Merge
[awesomized/libmemcached] / src / memcp.c
index 62c6020780beb33611e1ab9d3e981f615ea53fd6..57d0a70da61282d5207b12c0e34d7f17db35c2b6 100644 (file)
@@ -1,4 +1,6 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <sys/types.h>
@@ -7,6 +9,8 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <strings.h>
+#include <string.h>
+#include <assert.h>
 
 #include <memcached.h>
 #include "client_options.h"
@@ -20,8 +24,9 @@ void options_parse(int argc, char *argv[]);
 
 static int opt_verbose= 0;
 static char *opt_servers= NULL;
+static char *opt_hash= NULL;
 static int opt_method= OPT_SET;
-static uint16_t opt_flags= 0;
+static uint32_t opt_flags= 0;
 static time_t opt_expires= 0;
 
 int main(int argc, char *argv[])
@@ -33,14 +38,22 @@ int main(int argc, char *argv[])
   options_parse(argc, argv);
 
   memc= memcached_create(NULL);
+  process_hash_option(memc, opt_hash);
 
   if (!opt_servers)
-    return 0;
+  {
+    char *temp;
+
+    if ((temp= getenv("MEMCACHED_SERVERS")))
+      opt_servers= strdup(temp);
+    else
+      exit(1);
+  }
 
   if (opt_servers)
-    servers= parse_opt_servers(opt_servers);
+    servers= memcached_servers_parse(opt_servers);
   else
-    servers= parse_opt_servers(argv[--argc]);
+    servers= memcached_servers_parse(argv[--argc]);
 
   memcached_server_push(memc, servers);
   memcached_server_list_free(servers);
@@ -105,8 +118,13 @@ int main(int argc, char *argv[])
                         opt_expires, opt_flags);
 
     if (rc != MEMCACHED_SUCCESS)
-      fprintf(stderr, "memcp: %s: memcache error %s\n", 
+    {
+      fprintf(stderr, "memcp: %s: memcache error %s", 
              ptr, memcached_strerror(memc, rc));
+      if (memc->cached_errno)
+       fprintf(stderr, " system error %s", strerror(memc->cached_errno));
+      fprintf(stderr, "\n");
+    }
 
     free(file_buffer_ptr);
     close(fd);
@@ -115,7 +133,10 @@ int main(int argc, char *argv[])
 
   memcached_free(memc);
 
-  free(opt_servers);
+  if (opt_servers)
+    free(opt_servers);
+  if (opt_hash)
+    free(opt_hash);
 
   return 0;
 }
@@ -142,6 +163,7 @@ void options_parse(int argc, char *argv[])
       {"set",  no_argument, NULL, OPT_SET},
       {"add",  no_argument, NULL, OPT_ADD},
       {"replace",  no_argument, NULL, OPT_REPLACE},
+      {"hash", required_argument, NULL, OPT_HASH},
       {0, 0, 0, 0},
     };
 
@@ -171,7 +193,7 @@ void options_parse(int argc, char *argv[])
       opt_servers= strdup(optarg);
       break;
     case OPT_FLAG: /* --flag */
-      opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16);
+      opt_flags= (uint32_t)strtol(optarg, (char **)NULL, 16);
       break;
     case OPT_EXPIRE: /* --expire */
       opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);
@@ -184,6 +206,8 @@ void options_parse(int argc, char *argv[])
       break;
     case OPT_ADD:
       opt_method= OPT_ADD;
+    case OPT_HASH:
+      opt_hash= strdup(optarg);
       break;
     case '?':
       /* getopt_long already printed an error message. */