memcap works, though it does nothing useful at the moment :)
[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 /* Prototypes */
13 void options_parse(int argc, char *argv[]);
14
15 int main(int argc, char *argv[])
16 {
17 memcached_st *memc;
18 memcached_return rc;
19 memcached_server_st *servers;
20
21 options_parse(argc, argv);
22
23 if (!opt_servers)
24 return 0;
25
26 memc= memcached_init(NULL);
27
28 servers= parse_opt_servers(opt_servers);
29 memcached_server_push(memc, servers);
30 memcached_server_list_free(servers);
31
32 rc = memcached_flush(memc, opt_expire);
33 if (rc != MEMCACHED_SUCCESS)
34 {
35 fprintf(stderr, "memflush: memcache error %s\n",
36 memcached_strerror(memc, rc));
37 }
38
39 memcached_deinit(memc);
40
41 free(opt_servers);
42
43 return 0;
44 }
45
46
47 void options_parse(int argc, char *argv[])
48 {
49 static struct option long_options[]=
50 {
51 {"version", no_argument, NULL, OPT_VERSION},
52 {"help", no_argument, NULL, OPT_HELP},
53 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
54 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
55 {"servers", required_argument, NULL, OPT_SERVERS},
56 {"expire", required_argument, NULL, OPT_EXPIRE},
57 {0, 0, 0, 0},
58 };
59 int option_index= 0;
60 int option_rv;
61
62 while (1)
63 {
64 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
65 if (option_rv == -1) break;
66 switch (option_rv)
67 {
68 case 0:
69 break;
70 case OPT_VERBOSE: /* --verbose or -v */
71 opt_verbose = OPT_VERBOSE;
72 break;
73 case OPT_DEBUG: /* --debug or -d */
74 opt_verbose = OPT_DEBUG;
75 break;
76 case OPT_VERSION: /* --version or -V */
77 printf("memcache tools, memflush, v1.0\n");
78 exit(0);
79 break;
80 case OPT_HELP: /* --help or -h */
81 printf("useful help messages go here\n");
82 exit(0);
83 break;
84 case OPT_SERVERS: /* --servers or -s */
85 opt_servers= strdup(optarg);
86 break;
87 case OPT_EXPIRE: /* --expire */
88 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
89 break;
90 case '?':
91 /* getopt_long already printed an error message. */
92 exit(1);
93 default:
94 abort();
95 }
96 }
97 }