Update for release
[m6w6/libmemcached] / clients / memrm.cc
index d4d93c2e9c2621e80b3bc56613889f715802f231..73a29de96d9fbd9e646180f8c3f35edcce0deab2 100644 (file)
  */
 #include "config.h"
 
-#include <stdio.h>
-#include <unistd.h>
+#include <cstdio>
+#include <cstring>
 #include <getopt.h>
+#include <iostream>
+#include <unistd.h>
+
 #include <libmemcached/memcached.h>
-#include <string.h>
 #include "client_options.h"
 #include "utilities.h"
 
@@ -43,7 +45,7 @@ int main(int argc, char *argv[])
   options_parse(argc, argv);
   initialize_sockets();
 
-  if (!opt_servers)
+  if (opt_servers == 0)
   {
     char *temp;
 
@@ -51,8 +53,8 @@ int main(int argc, char *argv[])
       opt_servers= strdup(temp);
     else
     {
-      fprintf(stderr, "No Servers provided\n");
-      exit(1);
+      std::cerr << "No Servers provided" << std::endl;
+      return EXIT_FAILURE;
     }
   }
 
@@ -65,25 +67,42 @@ int main(int argc, char *argv[])
   memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
                          (uint64_t) opt_binary);
 
-  if (!initialize_sasl(memc, opt_username, opt_passwd))
+  if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
   {
     memcached_free(memc);
+    std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
     return EXIT_FAILURE;
   }
 
+  if (opt_username)
+  {
+    memcached_return_t ret;
+    if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+    {
+      std::cerr << memcached_last_error_message(memc) << std::endl;
+      memcached_free(memc);
+      return EXIT_FAILURE;
+    }
+  }
+
   while (optind < argc)
   {
     if (opt_verbose)
-      printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
-    rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
+    {
+      std::cout << "key: " << argv[optind] << std::endl;
+      std::cout << "expires: " << opt_expire << std::endl;
+    }
+    rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
 
-    if (rc != MEMCACHED_SUCCESS)
+    if (memcached_failed(rc))
     {
-      fprintf(stderr, "memrm: %s: memcache error %s",
-             argv[optind], memcached_strerror(memc, rc));
+      std::cerr << PROGRAM_NAME << ": " << argv[optind] << ": error " << memcached_strerror(memc, rc) << std::endl; 
+
       if (memcached_last_error_errno(memc))
-       fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
-      fprintf(stderr, "\n");
+      {
+        std::cerr << " system error " << strerror(memcached_last_error_errno(memc));
+      }
+      std::cerr << std::endl;
 
       return_code= -1;
     }
@@ -99,8 +118,6 @@ int main(int argc, char *argv[])
   if (opt_hash)
     free(opt_hash);
 
-  shutdown_sasl();
-
   return return_code;
 }