5 #include <libmemcached/memcached.h>
6 #include "client_options.h"
9 static int opt_binary
= 0;
10 static int opt_verbose
= 0;
11 static time_t opt_expire
= 0;
12 static char *opt_servers
= NULL
;
14 #define PROGRAM_NAME "memflush"
15 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
18 void options_parse(int argc
, char *argv
[]);
20 int main(int argc
, char *argv
[])
24 memcached_server_st
*servers
;
26 options_parse(argc
, argv
);
32 if ((temp
= getenv("MEMCACHED_SERVERS")))
33 opt_servers
= strdup(temp
);
36 fprintf(stderr
, "No Servers provided\n");
41 memc
= memcached_create(NULL
);
43 servers
= memcached_servers_parse(opt_servers
);
44 memcached_server_push(memc
, servers
);
45 memcached_server_list_free(servers
);
46 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
48 rc
= memcached_flush(memc
, opt_expire
);
49 if (rc
!= MEMCACHED_SUCCESS
)
51 fprintf(stderr
, "memflush: memcache error %s",
52 memcached_strerror(memc
, rc
));
53 if (memc
->cached_errno
)
54 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
55 fprintf(stderr
, "\n");
66 void options_parse(int argc
, char *argv
[])
68 memcached_programs_help_st help_options
[]=
73 static struct option long_options
[]=
75 {"version", no_argument
, NULL
, OPT_VERSION
},
76 {"help", no_argument
, NULL
, OPT_HELP
},
77 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
78 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
79 {"servers", required_argument
, NULL
, OPT_SERVERS
},
80 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
81 {"binary", no_argument
, NULL
, OPT_BINARY
},
89 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
90 if (option_rv
== -1) break;
98 case OPT_VERBOSE
: /* --verbose or -v */
99 opt_verbose
= OPT_VERBOSE
;
101 case OPT_DEBUG
: /* --debug or -d */
102 opt_verbose
= OPT_DEBUG
;
104 case OPT_VERSION
: /* --version or -V */
105 version_command(PROGRAM_NAME
);
107 case OPT_HELP
: /* --help or -h */
108 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
110 case OPT_SERVERS
: /* --servers or -s */
111 opt_servers
= strdup(optarg
);
113 case OPT_EXPIRE
: /* --expire */
114 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
117 /* getopt_long already printed an error message. */