X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fmemcat.cc;h=52b02aca3a427d6f648414cc0dc2caae0e8d9cca;hb=c8b9a1330aaf70eb9e820a3a48799c6baf606a4a;hp=ab482e5d6ad57549b17f06d0352b3b132562f1b4;hpb=956d15b5b1f3f3518eb374a3a9e0393e9dacd3b6;p=awesomized%2Flibmemcached diff --git a/clients/memcat.cc b/clients/memcat.cc index ab482e5d..52b02aca 100644 --- a/clients/memcat.cc +++ b/clients/memcat.cc @@ -1,4 +1,5 @@ /* LibMemcached + * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ * Copyright (C) 2006-2009 Brian Aker * All rights reserved. * @@ -9,14 +10,14 @@ * */ -#include +#include #include #include #include #include #include -#include +#include #include "utilities.h" @@ -38,35 +39,41 @@ static char *opt_file; int main(int argc, char *argv[]) { - memcached_st *memc; char *string; size_t string_length; uint32_t flags; memcached_return_t rc; - memcached_server_st *servers; - int return_code= 0; + int return_code= EXIT_SUCCESS; options_parse(argc, argv); initialize_sockets(); - if (!opt_servers) + if (opt_servers == NULL) { char *temp; if ((temp= getenv("MEMCACHED_SERVERS"))) + { opt_servers= strdup(temp); - else + } + + if (opt_servers == NULL) { - fprintf(stderr, "No Servers provided\n"); - exit(1); + std::cerr << "No servers provied" << std::endl; + exit(EXIT_FAILURE); } } - memc= memcached_create(NULL); - process_hash_option(memc, opt_hash); + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } - servers= memcached_servers_parse(opt_servers); + memcached_st* memc= memcached_create(NULL); + process_hash_option(memc, opt_hash); memcached_server_push(memc, servers); memcached_server_list_free(servers); @@ -100,69 +107,66 @@ int main(int argc, char *argv[]) if (opt_displayflag) { if (opt_verbose) - printf("key: %s\nflags: ", argv[optind]); - printf("%x\n", flags); + { + std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << std::endl; + } } else { if (opt_verbose) { - printf("key: %s\nflags: %x\nlength: %lu\nvalue: ", - argv[optind], flags, (unsigned long)string_length); + std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << "length: " << string_length << std::endl << "value: "; } if (opt_file) { - FILE *fp; - size_t written; - - fp= fopen(opt_file, "w"); - if (!fp) + FILE *fp= fopen(opt_file, "w"); + if (fp == NULL) { perror("fopen"); - return_code= -1; + return_code= EXIT_FAILURE; break; } - written= fwrite(string, 1, string_length, fp); + size_t written= fwrite(string, 1, string_length, fp); if (written != string_length) { - fprintf(stderr, "error writing file (written %lu, should be %lu)\n", (unsigned long)written, (unsigned long)string_length); - return_code= -1; + std::cerr << "error writing file to file " << opt_file << " wrote " << written << ", should have written" << string_length << std::endl; + return_code= EXIT_FAILURE; break; } if (fclose(fp)) { - fprintf(stderr, "error closing file\n"); - return_code= -1; + std::cerr << "error closing " << opt_file << std::endl; + return_code= EXIT_FAILURE; break; } } else { - printf("%.*s\n", (int)string_length, string); + std::cout.write(string, string_length); + std::cout << std::endl; } free(string); } } else if (rc != MEMCACHED_NOTFOUND) { - fprintf(stderr, "memcat: %s: memcache error %s", - argv[optind], memcached_strerror(memc, rc)); + std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(memc, rc) << ")"; if (memcached_last_error_errno(memc)) { - fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc))); + std::cerr << " system error (" << strerror(memcached_last_error_errno(memc)) << ")" << std::endl; } - fprintf(stderr, "\n"); + std::cerr << std::endl; - return_code= -1; + return_code= EXIT_FAILURE; break; } else // Unknown Issue { - fprintf(stderr, "memcat: %s not found\n", argv[optind]); - return_code= -1; + std::cerr << "error on " << argv[optind] << "("<< memcached_strerror(NULL, rc) << ")" << std::endl; + return_code= EXIT_FAILURE; } optind++; } @@ -170,9 +174,13 @@ int main(int argc, char *argv[]) memcached_free(memc); if (opt_servers) + { free(opt_servers); + } if (opt_hash) + { free(opt_hash); + } return return_code; } @@ -181,7 +189,6 @@ int main(int argc, char *argv[]) void options_parse(int argc, char *argv[]) { int option_index= 0; - int option_rv; memcached_programs_help_st help_options[]= { @@ -207,7 +214,7 @@ void options_parse(int argc, char *argv[]) while (1) { - option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); if (option_rv == -1) break; switch (option_rv) { @@ -250,7 +257,7 @@ void options_parse(int argc, char *argv[]) case '?': /* getopt_long already printed an error message. */ - exit(1); + exit(EXIT_FAILURE); default: abort(); }