11 #include <memcached.h>
12 #include "client_options.h"
15 void options_parse(int argc
, char *argv
[]);
17 static int opt_verbose
;
18 static char *opt_servers
;
19 static int opt_replace
;
20 uint16_t opt_flags
= 0;
21 time_t opt_expires
= 0;
23 int main(int argc
, char *argv
[])
30 options_parse(argc
, argv
);
32 memc
= memcached_init(NULL
);
33 parse_opt_servers(memc
, opt_servers
);
35 while (optind
<= argc
)
42 fd
= open(argv
[optind
], O_RDONLY
);
44 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
49 (void)fstat(fd
, &sbuf
);
50 mptr
= mmap(NULL
, sbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
51 if (mptr
== MAP_FAILED
) {
52 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
58 ptr
= rindex(argv
[optind
], '/');
65 static char *opstr
[] = { "set", "add", "replace" };
66 printf("op: %s\nsource file: %s\nlength: %d\n"
67 "key: %s\nflags: %d\n expires: %ld\n",
68 opstr
[opt_replace
], argv
[optind
], sbuf
.st_size
,
69 ptr
, opt_flags
, opt_expires
);
73 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
75 opt_expires
, opt_flags
);
76 else if (opt_replace
== 1)
77 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
79 opt_expires
, opt_flags
);
80 else if (opt_replace
== 2)
81 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
83 opt_expires
, opt_flags
);
87 if (rc
!= MEMCACHED_SUCCESS
) {
88 fprintf(stderr
, "memcp: %s: memcache error %s\n",
89 ptr
, memcached_strerror(memc
, rc
));
92 munmap(mptr
, sbuf
.st_size
);
97 memcached_deinit(memc
);
102 void options_parse(int argc
, char *argv
[])
107 static struct option long_options
[] =
109 {"version", no_argument
, NULL
, OPT_VERSION
},
110 {"help", no_argument
, NULL
, OPT_HELP
},
111 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
112 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
113 {"servers", required_argument
, NULL
, OPT_SERVERS
},
114 {"flag", required_argument
, NULL
, OPT_FLAG
},
115 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
116 {"set", no_argument
, &opt_replace
, OPT_SET
},
117 {"add", no_argument
, &opt_replace
, OPT_ADD
},
118 {"replace", no_argument
, &opt_replace
, OPT_REPLACE
},
124 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
126 if (option_rv
== -1) break;
131 case OPT_VERSION
: /* --version or -V */
132 printf("memcache tools, memcp, v1.0\n");
134 case OPT_HELP
: /* --help or -h */
135 printf("useful help messages go here\n");
137 case OPT_SERVERS
: /* --servers or -s */
140 case OPT_FLAG
: /* --flag */
141 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 10);
143 case OPT_EXPIRE
: /* --expire */
144 opt_expires
= (time_t)strtol(optarg
, (char **)NULL
, 10);
147 /* getopt_long already printed an error message. */