10 #include <memcached.h>
12 static int opt_verbose
;
13 static char *opt_servers
;
14 static int opt_replace
;
16 struct memcached_st
*parse_opt_servers (struct memcached_st
*m
,
21 while (s
= strsep(&opt_servers
, ",")) {
22 hostname
= strsep(&s
, ":");
24 memcached_server_add(m
, hostname
, portnum
);
29 int main(int argc
, char *argv
[])
39 static struct option long_options
[] =
41 {"version", no_argument
, NULL
, OPT_VERSION
},
42 {"help", no_argument
, NULL
, OPT_HELP
},
43 {"verbose", no_argument
, &opt_verbose
, 1},
44 {"debug", no_argument
, &opt_verbose
, 2},
45 {"servers", required_argument
, NULL
, OPT_SERVERS
},
46 {"flag", required_argument
, NULL
, OPT_FLAG
},
47 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
48 {"set", no_argument
, &opt_replace
, 0},
49 {"add", no_argument
, &opt_replace
, 1},
50 {"replace", no_argument
, &opt_replace
, 2},
57 option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
58 if (option_rv
== -1) break;
61 if (long_options
[option_index
].name
)
63 case OPT_VERSION
: /* --version */
64 printf("memcache tools, memcp, v1.0\n");
67 case OPT_HELP
: /* --help */
68 printf("useful help messages go here\n");
71 case OPT_SERVERS
: /* --servers */
72 opt_servers
= strdup(optarg
);
74 case OPT_FLAG
: /* --flag */
75 flags
= (uint16_t) atoi(optarg
);
77 case OPT_EXPIRE
: /* --expire */
78 expires
= (time_t)atoi(optarg
);
81 /* getopt_long already printed an error message. */
88 memc
= memcached_init(NULL
);
89 memc
= parse_opt_servers(memc
, opt_servers
);
91 while (optind
< argc
) {
97 fd
= open(argv
[optind
], O_RDONLY
);
101 fprintf(stderr
, "Failed opening %s\n", argv
[optind
]);
105 (void)fstat(fd
, &sbuf
);
106 mptr
= mmap(NULL
, sbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
108 ptr
= rindex(argv
[optind
], '/');
118 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
122 munmap(mptr
, sbuf
.st_size
);
127 memcached_deinit(memc
);