af7ab35317c7ba4e178a86b024ccd201a9ccac7a
[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 return 0;
28
29 memc= memcached_create(NULL);
30
31 servers= parse_opt_servers(opt_servers);
32 memcached_server_push(memc, servers);
33 memcached_server_list_free(servers);
34
35 rc = memcached_flush(memc, opt_expire);
36 if (rc != MEMCACHED_SUCCESS)
37 {
38 fprintf(stderr, "memflush: memcache error %s\n",
39 memcached_strerror(memc, rc));
40 }
41
42 memcached_free(memc);
43
44 free(opt_servers);
45
46 return 0;
47 }
48
49
50 void options_parse(int argc, char *argv[])
51 {
52 memcached_programs_help_st help_options[]=
53 {
54 {0},
55 };
56
57 static struct option long_options[]=
58 {
59 {"version", no_argument, NULL, OPT_VERSION},
60 {"help", no_argument, NULL, OPT_HELP},
61 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
62 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
63 {"servers", required_argument, NULL, OPT_SERVERS},
64 {"expire", required_argument, NULL, OPT_EXPIRE},
65 {0, 0, 0, 0},
66 };
67 int option_index= 0;
68 int option_rv;
69
70 while (1)
71 {
72 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
73 if (option_rv == -1) break;
74 switch (option_rv)
75 {
76 case 0:
77 break;
78 case OPT_VERBOSE: /* --verbose or -v */
79 opt_verbose = OPT_VERBOSE;
80 break;
81 case OPT_DEBUG: /* --debug or -d */
82 opt_verbose = OPT_DEBUG;
83 break;
84 case OPT_VERSION: /* --version or -V */
85 version_command(PROGRAM_NAME);
86 break;
87 case OPT_HELP: /* --help or -h */
88 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
89 break;
90 case OPT_SERVERS: /* --servers or -s */
91 opt_servers= strdup(optarg);
92 break;
93 case OPT_EXPIRE: /* --expire */
94 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
95 break;
96 case '?':
97 /* getopt_long already printed an error message. */
98 exit(1);
99 default:
100 abort();
101 }
102 }
103 }