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