10 #include <memcached.h>
11 #include "client_options.h"
13 static int opt_verbose
;
14 static char *opt_servers
;
15 static int opt_replace
;
17 struct memcached_st
*parse_opt_servers (struct memcached_st
*m
,
22 while (s
= strsep(&opt_servers
, ",")) {
23 hostname
= strsep(&s
, ":");
25 memcached_server_add(m
, hostname
, portnum
);
30 int main(int argc
, char *argv
[])
40 static struct option long_options
[] =
42 {"version", no_argument
, NULL
, OPT_VERSION
},
43 {"help", no_argument
, NULL
, OPT_HELP
},
44 {"verbose", no_argument
, &opt_verbose
, 1},
45 {"debug", no_argument
, &opt_verbose
, 2},
46 {"servers", required_argument
, NULL
, OPT_SERVERS
},
47 {"flag", required_argument
, NULL
, OPT_FLAG
},
48 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
49 {"set", no_argument
, &opt_replace
, 0},
50 {"add", no_argument
, &opt_replace
, 1},
51 {"replace", no_argument
, &opt_replace
, 2},
58 option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
59 if (option_rv
== -1) break;
62 if (long_options
[option_index
].name
)
64 case OPT_VERSION
: /* --version */
65 printf("memcache tools, memcp, v1.0\n");
68 case OPT_HELP
: /* --help */
69 printf("useful help messages go here\n");
72 case OPT_SERVERS
: /* --servers */
73 opt_servers
= strdup(optarg
);
75 case OPT_FLAG
: /* --flag */
76 flags
= (uint16_t) atoi(optarg
);
78 case OPT_EXPIRE
: /* --expire */
79 expires
= (time_t)atoi(optarg
);
82 /* getopt_long already printed an error message. */
89 memc
= memcached_init(NULL
);
90 memc
= parse_opt_servers(memc
, opt_servers
);
92 while (optind
<= argc
) {
98 fd
= open(argv
[optind
], O_RDONLY
);
102 fprintf(stderr
, "Failed opening %s\n", argv
[optind
]);
106 (void)fstat(fd
, &sbuf
);
107 mptr
= mmap(NULL
, sbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
109 ptr
= rindex(argv
[optind
], '/');
119 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
123 munmap(mptr
, sbuf
.st_size
);
128 memcached_deinit(memc
);