CONSTANTS documentation.
[m6w6/libmemcached] / src / memcp.c
index 3cc0109ab4974e827b50381c7233441ea80bf993..f1054ea9d1518d2a969afcf5b32cead3099f02a1 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,33 +9,52 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <strings.h>
+#include <string.h>
+#include <assert.h>
 
 #include <memcached.h>
 #include "client_options.h"
 #include "utilities.h"
 
+#define PROGRAM_NAME "memcp"
+#define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
+
 /* Prototypes */
 void options_parse(int argc, char *argv[]);
 
 static int opt_verbose= 0;
 static char *opt_servers= 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[])
 {
   memcached_st *memc;
   memcached_return rc;
+  memcached_server_st *servers;
 
   options_parse(argc, argv);
 
-  memc= memcached_init(NULL);
+  memc= memcached_create(NULL);
+
+  if (!opt_servers)
+  {
+    char *temp;
+
+    if ((temp= getenv("MEMCACHED_SERVERS")))
+      opt_servers= strdup(temp);
+    else
+      exit(1);
+  }
 
   if (opt_servers)
-    parse_opt_servers(memc, opt_servers);
+    servers= memcached_servers_parse(opt_servers);
   else
-    parse_opt_servers(memc, argv[--argc]);
+    servers= memcached_servers_parse(argv[--argc]);
+
+  memcached_server_push(memc, servers);
+  memcached_server_list_free(servers);
 
   while (optind < argc) 
   {
@@ -95,15 +116,20 @@ 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);
     optind++;
   }
 
-  memcached_deinit(memc);
+  memcached_free(memc);
 
   free(opt_servers);
 
@@ -115,6 +141,11 @@ void options_parse(int argc, char *argv[])
   int option_index= 0;
   int option_rv;
 
+  memcached_programs_help_st help_options[]=
+  {
+    {0},
+  };
+
   static struct option long_options[]=
     {
       {"version", no_argument, NULL, OPT_VERSION},
@@ -147,16 +178,16 @@ void options_parse(int argc, char *argv[])
       opt_verbose = OPT_DEBUG;
       break;
     case OPT_VERSION: /* --version or -V */
-      printf("memcache tools, memcp, v1.0\n");
-      exit(0);
+      version_command(PROGRAM_NAME);
+      break;
     case OPT_HELP: /* --help or -h */
-      printf("useful help messages go here\n");
-      exit(0);
+      help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
+      break;
     case OPT_SERVERS: /* --servers or -s */
       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);