X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fmemcp.c;h=bf3828ae544ba64476a36887c101c9f8fa41873d;hb=de46e62fe493ce152e1804feec0d19d5d0cfaa9f;hp=617057b32d4f54dfb836154498051ce7d6278459;hpb=98c99981e0ebe7f716bea2e4d95434d159103fcd;p=awesomized%2Flibmemcached diff --git a/clients/memcp.c b/clients/memcp.c index 617057b3..bf3828ae 100644 --- a/clients/memcp.c +++ b/clients/memcp.c @@ -9,7 +9,8 @@ * */ -#include "libmemcached/common.h" +#include "config.h" + #include #include #include @@ -22,6 +23,10 @@ #include #include #include +#include +#include +#include + #include @@ -41,6 +46,35 @@ static char *opt_hash= NULL; static int opt_method= OPT_SET; static uint32_t opt_flags= 0; static time_t opt_expires= 0; +static char *opt_username; +static char *opt_passwd; + +static long strtol_wrapper(const char *nptr, int base, bool *error) +{ + long val; + char *endptr; + + errno= 0; /* To distinguish success/failure after call */ + val= strtol(nptr, &endptr, base); + + /* Check for various possible errors */ + + if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) + || (errno != 0 && val == 0)) + { + *error= true; + return EXIT_SUCCESS; + } + + if (endptr == nptr) + { + *error= true; + return EXIT_SUCCESS; + } + + *error= false; + return val; +} int main(int argc, char *argv[]) { @@ -48,7 +82,10 @@ int main(int argc, char *argv[]) memcached_return_t rc; memcached_server_st *servers; + int return_code= 0; + options_parse(argc, argv); + initialize_sockets(); memc= memcached_create(NULL); process_hash_option(memc, opt_hash); @@ -58,7 +95,9 @@ int main(int argc, char *argv[]) char *temp; if ((temp= getenv("MEMCACHED_SERVERS"))) + { opt_servers= strdup(temp); + } else { fprintf(stderr, "No Servers provided\n"); @@ -75,6 +114,11 @@ int main(int argc, char *argv[]) memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t)opt_binary); + if (!initialize_sasl(memc, opt_username, opt_passwd)) + { + memcached_free(memc); + return EXIT_FAILURE; + } while (optind < argc) { @@ -144,9 +188,11 @@ int main(int argc, char *argv[]) { 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)); + if (memcached_last_error_errno(memc)) + fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc))); fprintf(stderr, "\n"); + + return_code= -1; } free(file_buffer_ptr); @@ -160,8 +206,9 @@ int main(int argc, char *argv[]) free(opt_servers); if (opt_hash) free(opt_hash); + shutdown_sasl(); - return 0; + return return_code; } static void options_parse(int argc, char *argv[]) @@ -188,6 +235,8 @@ static void options_parse(int argc, char *argv[]) {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE}, {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH}, {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, {0, 0, 0, 0}, }; @@ -220,11 +269,26 @@ static void options_parse(int argc, char *argv[]) opt_servers= strdup(optarg); break; case OPT_FLAG: /* --flag */ - opt_flags= (uint32_t)strtol(optarg, (char **)NULL, 16); - break; + { + bool strtol_error; + opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error); + if (strtol_error == true) + { + fprintf(stderr, "Bad value passed via --flag\n"); + exit(1); + } + break; + } case OPT_EXPIRE: /* --expire */ - opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10); - break; + { + bool strtol_error; + opt_expires= (time_t)strtol_wrapper(optarg, 16, &strtol_error); + if (strtol_error == true) + { + fprintf(stderr, "Bad value passed via --flag\n"); + exit(1); + } + } case OPT_SET: opt_method= OPT_SET; break; @@ -237,7 +301,13 @@ static void options_parse(int argc, char *argv[]) case OPT_HASH: opt_hash= strdup(optarg); break; - case '?': + case OPT_USERNAME: + opt_username= optarg; + break; + case OPT_PASSWD: + opt_passwd= optarg; + break; + case '?': /* getopt_long already printed an error message. */ exit(1); default: