X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fmemcp.c;h=1dc30631900eed71032fa4424c6201eceed8b9b0;hb=d227f2a06b136be62333f220b80226c0ec1a472f;hp=bdab6b096692e4a00d9197764273a531fc5f0b84;hpb=b4de8d3fd063b9017797dd9809ab3acb8a537606;p=m6w6%2Flibmemcached diff --git a/clients/memcp.c b/clients/memcp.c index bdab6b09..1dc30631 100644 --- a/clients/memcp.c +++ b/clients/memcp.c @@ -1,3 +1,14 @@ +/* LibMemcached + * Copyright (C) 2006-2009 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + * + * Summary: + * + */ + #include "libmemcached/common.h" #include #include @@ -21,7 +32,7 @@ #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster." /* Prototypes */ -void options_parse(int argc, char *argv[]); +static void options_parse(int argc, char *argv[]); static int opt_binary=0; static int opt_verbose= 0; @@ -30,6 +41,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 0; + } + + if (endptr == nptr) + { + *error= true; + return 0; + } + + *error= false; + return val; +} int main(int argc, char *argv[]) { @@ -37,6 +77,8 @@ int main(int argc, char *argv[]) memcached_return_t rc; memcached_server_st *servers; + int return_code= 0; + options_parse(argc, argv); memc= memcached_create(NULL); @@ -47,7 +89,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"); @@ -64,8 +108,13 @@ 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 1; + } - while (optind < argc) + while (optind < argc) { struct stat sbuf; int fd; @@ -89,7 +138,7 @@ int main(int argc, char *argv[]) else ptr= argv[optind]; - if (opt_verbose) + if (opt_verbose) { static const char *opstr[] = { "set", "add", "replace" }; printf("op: %s\nsource file: %s\nlength: %zu\n" @@ -100,13 +149,13 @@ int main(int argc, char *argv[]) if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL) { - fprintf(stderr, "malloc: %s\n", strerror(errno)); + fprintf(stderr, "malloc: %s\n", strerror(errno)); exit(1); } if ((read_length= read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1) { - fprintf(stderr, "read: %s\n", strerror(errno)); + fprintf(stderr, "read: %s\n", strerror(errno)); exit(1); } @@ -131,11 +180,13 @@ int main(int argc, char *argv[]) if (rc != MEMCACHED_SUCCESS) { - fprintf(stderr, "memcp: %s: memcache error %s", + 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"); + + return_code= -1; } free(file_buffer_ptr); @@ -149,11 +200,12 @@ int main(int argc, char *argv[]) free(opt_servers); if (opt_hash) free(opt_hash); + shutdown_sasl(); - return 0; + return return_code; } -void options_parse(int argc, char *argv[]) +static void options_parse(int argc, char *argv[]) { int option_index= 0; int option_rv; @@ -177,10 +229,12 @@ 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}, }; - while (1) + while (1) { option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); @@ -209,11 +263,26 @@ 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; @@ -226,7 +295,13 @@ 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: