84db8b425d36c616800b384f019c8aca5495574b
[awesomized/libmemcached] / src / memflush.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <memcached.h>
5 #include "client_options.h"
6 #include "utilities.h"
7
8 static int opt_verbose= 0;
9 static time_t opt_expire= 0;
10 static char *opt_servers= NULL;
11
12 #define PROGRAM_NAME "memflush"
13 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
14
15 /* Prototypes */
16 void options_parse(int argc, char *argv[]);
17
18 int main(int argc, char *argv[])
19 {
20 memcached_st *memc;
21 memcached_return rc;
22 memcached_server_st *servers;
23
24 options_parse(argc, argv);
25
26 if (!opt_servers)
27 {
28 char *temp;
29
30 if ((temp= getenv("MEMCACHED_SERVERS")))
31 opt_servers= strdup(temp);
32 else
33 exit(1);
34 }
35
36 memc= memcached_create(NULL);
37
38 servers= memcached_servers_parse(opt_servers);
39 memcached_server_push(memc, servers);
40 memcached_server_list_free(servers);
41
42 rc = memcached_flush(memc, opt_expire);
43 if (rc != MEMCACHED_SUCCESS)
44 {
45 fprintf(stderr, "memflush: memcache error %s\n",
46 memcached_strerror(memc, rc));
47 }
48
49 memcached_free(memc);
50
51 free(opt_servers);
52
53 return 0;
54 }
55
56
57 void options_parse(int argc, char *argv[])
58 {
59 memcached_programs_help_st help_options[]=
60 {
61 {0},
62 };
63
64 static struct option long_options[]=
65 {
66 {"version", no_argument, NULL, OPT_VERSION},
67 {"help", no_argument, NULL, OPT_HELP},
68 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
69 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
70 {"servers", required_argument, NULL, OPT_SERVERS},
71 {"expire", required_argument, NULL, OPT_EXPIRE},
72 {0, 0, 0, 0},
73 };
74 int option_index= 0;
75 int option_rv;
76
77 while (1)
78 {
79 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
80 if (option_rv == -1) break;
81 switch (option_rv)
82 {
83 case 0:
84 break;
85 case OPT_VERBOSE: /* --verbose or -v */
86 opt_verbose = OPT_VERBOSE;
87 break;
88 case OPT_DEBUG: /* --debug or -d */
89 opt_verbose = OPT_DEBUG;
90 break;
91 case OPT_VERSION: /* --version or -V */
92 version_command(PROGRAM_NAME);
93 break;
94 case OPT_HELP: /* --help or -h */
95 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
96 break;
97 case OPT_SERVERS: /* --servers or -s */
98 opt_servers= strdup(optarg);
99 break;
100 case OPT_EXPIRE: /* --expire */
101 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
102 break;
103 case '?':
104 /* getopt_long already printed an error message. */
105 exit(1);
106 default:
107 abort();
108 }
109 }
110 }