#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;
static uint32_t opt_flags= 0;
static time_t opt_expires= 0;
+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[])
{
memcached_st *memc;
char *temp;
if ((temp= getenv("MEMCACHED_SERVERS")))
+ {
opt_servers= strdup(temp);
+ }
else
{
fprintf(stderr, "No Servers provided\n");
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
(uint64_t)opt_binary);
- while (optind < argc)
+ while (optind < argc)
{
struct stat sbuf;
int fd;
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"
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);
}
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));
return 0;
}
-void options_parse(int argc, char *argv[])
+static void options_parse(int argc, char *argv[])
{
int option_index= 0;
int option_rv;
{0, 0, 0, 0},
};
- while (1)
+ while (1)
{
option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
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;