fix to dont lose the last item on the command line
[awesomized/libmemcached] / src / memcp.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9
10 #include <memcached.h>
11 #include "client_options.h"
12
13 static int opt_verbose;
14 static char *opt_servers;
15 static int opt_replace;
16
17 struct memcached_st *parse_opt_servers (struct memcached_st *m,
18 char *opt_servers)
19 {
20 char *s, *hostname;
21 unsigned int portnum;
22 while (s = strsep(&opt_servers, ",")) {
23 hostname = strsep(&s, ":");
24 portnum = atoi(s);
25 memcached_server_add(m, hostname, portnum);
26 }
27 return m;
28 }
29
30 int main(int argc, char *argv[])
31 {
32 memcached_st *memc;
33 char *string;
34 unsigned int x;
35 size_t string_length;
36 uint16_t flags = 0;
37 time_t expires = 0;
38 memcached_return rc;
39
40 static struct option long_options[] =
41 {
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},
52 {0, 0, 0, 0},
53 };
54 int option_index = 0;
55 int option_rv;
56 while (1)
57 {
58 option_rv = getopt_long(argc, argv, "", long_options, &option_index);
59 if (option_rv == -1) break;
60 switch (option_rv) {
61 case 0:
62 if (long_options[option_index].name)
63 break;
64 case OPT_VERSION: /* --version */
65 printf("memcache tools, memcp, v1.0\n");
66 exit(0);
67 break;
68 case OPT_HELP: /* --help */
69 printf("useful help messages go here\n");
70 exit(0);
71 break;
72 case OPT_SERVERS: /* --servers */
73 opt_servers = strdup(optarg);
74 break;
75 case OPT_FLAG: /* --flag */
76 flags = (uint16_t) atoi(optarg);
77 break;
78 case OPT_EXPIRE: /* --expire */
79 expires = (time_t)atoi(optarg);
80 break;
81 case '?':
82 /* getopt_long already printed an error message. */
83 exit(1);
84 default:
85 abort();
86 }
87 }
88
89 memc= memcached_init(NULL);
90 memc= parse_opt_servers(memc, opt_servers);
91
92 while (optind <= argc) {
93 char *mptr;
94 struct stat sbuf;
95 int fd;
96 char *ptr;
97
98 fd= open(argv[optind], O_RDONLY);
99
100 if (fd == -1)
101 {
102 fprintf(stderr, "Failed opening %s\n", argv[optind]);
103 continue;
104 }
105
106 (void)fstat(fd, &sbuf);
107 mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
108
109 ptr= rindex(argv[optind], '/');
110 if (ptr)
111 {
112 ptr++;
113 }
114 else
115 {
116 ptr= argv[optind];
117 }
118
119 rc= memcached_set(memc, ptr, strlen(ptr),
120 mptr, sbuf.st_size,
121 expires, flags);
122
123 munmap(mptr, sbuf.st_size);
124 close(fd);
125 optind++;
126 }
127
128 memcached_deinit(memc);
129
130 return 0;
131 };