size_t string_length;\r
uint16_t flags;\r
memcached_return rc;\r
- unsigned int x;\r
\r
static struct option long_options[] =\r
{\r
{"version", no_argument, NULL, OPT_VERSION},\r
{"help", no_argument, NULL, OPT_HELP},\r
- {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},\r
- {"debug", no_argument, &opt_verbose, OPT_DEBUG},\r
+ {"verbose", no_argument, &opt_verbose, 1},\r
+ {"debug", no_argument, &opt_verbose, 2},\r
{"servers", required_argument, NULL, OPT_SERVERS},\r
{"flag", no_argument, &opt_displayflag, OPT_FLAG},\r
{0, 0, 0, 0},\r
}\r
}\r
\r
- memc = malloc(sizeof(struct memcached_st));\r
- memcached_init(memc);\r
+ memc= memcached_init(NULL);\r
memc= parse_opt_servers(memc, opt_servers);\r
- memc= memcached_init(memc);\r
-\r
- for (x= 1; x < argc; x++)\r
- {\r
- string= memcached_get(memc, argv[x], strlen(argv[x]),\r
+ \r
+ while (optind < argc) {\r
+ string= memcached_get(memc, argv[optind], strlen(argv[optind]),\r
&string_length, &flags, &rc);\r
if (rc == MEMCACHED_SUCCESS) {\r
if (opt_displayflag) {\r
}\r
}\r
}\r
+\r
+ optind++;\r
}\r
\r
memcached_deinit(memc);\r
#include <stdio.h>\r
+#include <unistd.h>\r
+#include <getopt.h>\r
#include <sys/types.h>\r
#include <sys/stat.h>\r
#include <sys/types.h>\r
\r
#include <memcached.h>\r
\r
+static int opt_verbose;\r
+static char *opt_servers;\r
+static int opt_replace;\r
+\r
+struct memcached_st *parse_opt_servers (struct memcached_st *m,\r
+ char *opt_servers)\r
+{\r
+ char *s, *hostname;\r
+ unsigned int portnum;\r
+ while (s = strsep(&opt_servers, ",")) {\r
+ hostname = strsep(&s, ":");\r
+ portnum = atoi(s);\r
+ memcached_server_add(m, hostname, portnum);\r
+ }\r
+ return m;\r
+}\r
+\r
int main(int argc, char *argv[])\r
{\r
memcached_st *memc;\r
char *string;\r
unsigned int x;\r
size_t string_length;\r
- uint16_t flags;\r
+ uint16_t flags = 0;\r
+ time_t expires = 0;\r
memcached_return rc;\r
\r
- if (argc < 3)\r
- return 0;\r
-\r
- memc= memcached_init(NULL);\r
-\r
- /* Get the server name */\r
- {\r
- char *ptr;\r
- char *hostname;\r
- size_t hostname_length;\r
- unsigned int port;\r
-\r
- ptr= index(argv[argc-1], ':');\r
-\r
- if (ptr)\r
+ static struct option long_options[] =\r
{\r
- hostname_length= ptr - argv[argc-1];\r
- hostname= (char *)malloc(hostname_length+1);\r
- memset(hostname, 0, hostname_length+1);\r
- memcpy(hostname, argv[argc-1], hostname_length);\r
-\r
- ptr++;\r
-\r
- port= strtol(ptr, (char **)NULL, 10);\r
-\r
- memcached_server_add(memc, hostname, port);\r
- free(hostname);\r
- }\r
- else\r
- {\r
- memcached_server_add(memc, argv[argc -1], 0);\r
+ {"version", no_argument, NULL, OPT_VERSION},\r
+ {"help", no_argument, NULL, OPT_HELP},\r
+ {"verbose", no_argument, &opt_verbose, 1},\r
+ {"debug", no_argument, &opt_verbose, 2},\r
+ {"servers", required_argument, NULL, OPT_SERVERS},\r
+ {"flag", required_argument, NULL, OPT_FLAG},\r
+ {"expire", required_argument, NULL, OPT_EXPIRE},\r
+ {"set", no_argument, &opt_replace, 0},\r
+ {"add", no_argument, &opt_replace, 1},\r
+ {"replace", no_argument, &opt_replace, 2},\r
+ {0, 0, 0, 0},\r
+ };\r
+ int option_index = 0;\r
+ int option_rv;\r
+ while (1) \r
+ {\r
+ option_rv = getopt_long(argc, argv, "", long_options, &option_index);\r
+ if (option_rv == -1) break;\r
+ switch (option_rv) {\r
+ case 0:\r
+ if (long_options[option_index].name)\r
+ break;\r
+ case OPT_VERSION: /* --version */\r
+ printf("memcache tools, memcp, v1.0\n");\r
+ exit(0);\r
+ break;\r
+ case OPT_HELP: /* --help */\r
+ printf("useful help messages go here\n");\r
+ exit(0);\r
+ break;\r
+ case OPT_SERVERS: /* --servers */\r
+ opt_servers = strdup(optarg);\r
+ break;\r
+ case OPT_FLAG: /* --flag */\r
+ flags = (uint16_t) atoi(optarg);\r
+ break;\r
+ case OPT_EXPIRE: /* --expire */\r
+ expires = (time_t)atoi(optarg);\r
+ break;\r
+ case '?':\r
+ /* getopt_long already printed an error message. */\r
+ exit(1);\r
+ default:\r
+ abort();\r
}\r
}\r
\r
- for (x= 1; x < argc-1; x++)\r
- {\r
+ memc= memcached_init(NULL);\r
+ memc= parse_opt_servers(memc, opt_servers);\r
+\r
+ while (optind < argc) {\r
char *mptr;\r
struct stat sbuf;\r
int fd;\r
char *ptr;\r
\r
- fd= open(argv[x], O_RDONLY);\r
+ fd= open(argv[optind], O_RDONLY);\r
\r
if (fd == -1)\r
{\r
- fprintf(stderr, "Failed opening %s\n", argv[x]);\r
- exit(1);\r
+ fprintf(stderr, "Failed opening %s\n", argv[optind]);\r
+ continue;\r
}\r
\r
(void)fstat(fd, &sbuf);\r
mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);\r
\r
- ptr= rindex(argv[x], '/');\r
+ ptr= rindex(argv[optind], '/');\r
if (ptr)\r
{\r
ptr++;\r
}\r
else\r
{\r
- ptr= argv[x];\r
+ ptr= argv[optind];\r
}\r
\r
rc= memcached_set(memc, ptr, strlen(ptr),\r
mptr, sbuf.st_size,\r
- (time_t)0, (uint16_t)0);\r
+ expire, flags);\r
\r
munmap(mptr, sbuf.st_size);\r
close(fd);\r
+ optind++;\r
}\r
\r
memcached_deinit(memc);\r