1 #include "libmemcached/common.h"
6 #include <libmemcached/memcached.h>
7 #include "client_options.h"
10 static int opt_binary
= 0;
11 static int opt_verbose
= 0;
12 static time_t opt_expire
= 0;
13 static char *opt_servers
= NULL
;
15 #define PROGRAM_NAME "memflush"
16 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
19 void options_parse(int argc
, char *argv
[]);
21 int main(int argc
, char *argv
[])
25 memcached_server_st
*servers
;
27 options_parse(argc
, argv
);
33 if ((temp
= getenv("MEMCACHED_SERVERS")))
34 opt_servers
= strdup(temp
);
37 fprintf(stderr
, "No Servers provided\n");
42 memc
= memcached_create(NULL
);
44 servers
= memcached_servers_parse(opt_servers
);
45 memcached_server_push(memc
, servers
);
46 memcached_server_list_free(servers
);
47 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
48 (uint64_t) opt_binary
);
50 rc
= memcached_flush(memc
, opt_expire
);
51 if (rc
!= MEMCACHED_SUCCESS
)
53 fprintf(stderr
, "memflush: memcache error %s",
54 memcached_strerror(memc
, rc
));
55 if (memc
->cached_errno
)
56 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
57 fprintf(stderr
, "\n");
68 void options_parse(int argc
, char *argv
[])
70 memcached_programs_help_st help_options
[]=
75 static struct option long_options
[]=
77 {"version", no_argument
, NULL
, OPT_VERSION
},
78 {"help", no_argument
, NULL
, OPT_HELP
},
79 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
80 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
81 {"servers", required_argument
, NULL
, OPT_SERVERS
},
82 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
83 {"binary", no_argument
, NULL
, OPT_BINARY
},
91 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
92 if (option_rv
== -1) break;
100 case OPT_VERBOSE
: /* --verbose or -v */
101 opt_verbose
= OPT_VERBOSE
;
103 case OPT_DEBUG
: /* --debug or -d */
104 opt_verbose
= OPT_DEBUG
;
106 case OPT_VERSION
: /* --version or -V */
107 version_command(PROGRAM_NAME
);
109 case OPT_HELP
: /* --help or -h */
110 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
112 case OPT_SERVERS
: /* --servers or -s */
113 opt_servers
= strdup(optarg
);
115 case OPT_EXPIRE
: /* --expire */
116 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
119 /* getopt_long already printed an error message. */