Made sure memory was freed after calling memcached_get.
[awesomized/libmemcached] / clients / memcat.c
1 #include "libmemcached/common.h"
2 #include <stdio.h>
3 #include <inttypes.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <getopt.h>
7 #include <libmemcached/memcached.h>
8
9 #include "utilities.h"
10
11 #define PROGRAM_NAME "memcat"
12 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
13
14
15 /* Prototypes */
16 void options_parse(int argc, char *argv[]);
17
18 static int opt_binary= 0;
19 static int opt_verbose= 0;
20 static int opt_displayflag= 0;
21 static char *opt_servers= NULL;
22 static char *opt_hash= NULL;
23
24 int main(int argc, char *argv[])
25 {
26 memcached_st *memc;
27 char *string;
28 size_t string_length;
29 uint32_t flags;
30 memcached_return rc;
31 memcached_server_st *servers;
32
33 options_parse(argc, argv);
34
35 if (!opt_servers)
36 {
37 char *temp;
38
39 if ((temp= getenv("MEMCACHED_SERVERS")))
40 opt_servers= strdup(temp);
41 else
42 {
43 fprintf(stderr, "No Servers provided\n");
44 exit(1);
45 }
46 }
47
48 memc= memcached_create(NULL);
49 process_hash_option(memc, opt_hash);
50
51 servers= memcached_servers_parse(opt_servers);
52
53 memcached_server_push(memc, servers);
54 memcached_server_list_free(servers);
55 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
56 (uint64_t)opt_binary);
57
58 while (optind < argc)
59 {
60 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
61 &string_length, &flags, &rc);
62 if (rc == MEMCACHED_SUCCESS)
63 {
64 if (opt_displayflag)
65 {
66 if (opt_verbose)
67 printf("key: %s\nflags: ", argv[optind]);
68 printf("%x\n", flags);
69 }
70 else
71 {
72 if (opt_verbose)
73 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
74 argv[optind], flags, string_length);
75 printf("%.*s\n", (int)string_length, string);
76 free(string);
77 }
78 }
79 else if (rc != MEMCACHED_NOTFOUND)
80 {
81 fprintf(stderr, "memcat: %s: memcache error %s",
82 argv[optind], memcached_strerror(memc, rc));
83 if (memc->cached_errno)
84 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
85 fprintf(stderr, "\n");
86 }
87 optind++;
88 }
89
90 memcached_free(memc);
91
92 if (opt_servers)
93 free(opt_servers);
94 if (opt_hash)
95 free(opt_hash);
96
97 return 0;
98 }
99
100
101 void options_parse(int argc, char *argv[])
102 {
103 int option_index= 0;
104 int option_rv;
105
106 memcached_programs_help_st help_options[]=
107 {
108 {0},
109 };
110
111 static struct option long_options[]=
112 {
113 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
114 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
115 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
116 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
117 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
118 {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
119 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
120 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
121 {0, 0, 0, 0},
122 };
123
124 while (1)
125 {
126 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
127 if (option_rv == -1) break;
128 switch (option_rv)
129 {
130 case 0:
131 break;
132 case OPT_BINARY:
133 opt_binary = 1;
134 break;
135 case OPT_VERBOSE: /* --verbose or -v */
136 opt_verbose = OPT_VERBOSE;
137 break;
138 case OPT_DEBUG: /* --debug or -d */
139 opt_verbose = OPT_DEBUG;
140 break;
141 case OPT_VERSION: /* --version or -V */
142 version_command(PROGRAM_NAME);
143 break;
144 case OPT_HELP: /* --help or -h */
145 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
146 break;
147 case OPT_SERVERS: /* --servers or -s */
148 opt_servers= strdup(optarg);
149 break;
150 case OPT_HASH:
151 opt_hash= strdup(optarg);
152 break;
153 case '?':
154 /* getopt_long already printed an error message. */
155 exit(1);
156 default:
157 abort();
158 }
159 }
160 }