bugfixes and formating fixes
[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 #include <errno.h>
10
11 #include <memcached.h>
12 #include "client_options.h"
13
14 /* Prototypes */
15 void options_parse(int argc, char *argv[]);
16
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;
22
23 int main(int argc, char *argv[])
24 {
25 memcached_st *memc;
26 char *string;
27 size_t string_length;
28 memcached_return rc;
29
30 options_parse(argc, argv);
31
32 memc= memcached_init(NULL);
33 parse_opt_servers(memc, opt_servers);
34
35 while (optind <= argc)
36 {
37 char *mptr;
38 struct stat sbuf;
39 int fd;
40 char *ptr;
41
42 fd= open(argv[optind], O_RDONLY);
43 if (fd < 0)
44 {
45 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
46 optind++;
47 continue;
48 }
49
50 (void)fstat(fd, &sbuf);
51 mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
52 if (mptr == MAP_FAILED)
53 {
54 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
55 close(fd);
56 optind++;
57 continue;
58 }
59
60 ptr= rindex(argv[optind], '/');
61 if (ptr)
62 ptr++;
63 else
64 ptr= argv[optind];
65
66 if (opt_verbose)
67 {
68 static char *opstr[]= { "set", "add", "replace" };
69 printf("op: %s\nsource file: %s\nlength: %zu\n"
70 "key: %s\nflags: %x\n expires: %ld\n",
71 opstr[opt_replace], argv[optind], sbuf.st_size,
72 ptr, opt_flags, opt_expires);
73 }
74
75 if (opt_replace == 0)
76 rc= memcached_set(memc, ptr, strlen(ptr),
77 mptr, sbuf.st_size,
78 opt_expires, opt_flags);
79 else if (opt_replace == 1)
80 rc= memcached_add(memc, ptr, strlen(ptr),
81 mptr, sbuf.st_size,
82 opt_expires, opt_flags);
83 else if (opt_replace == 2)
84 rc= memcached_replace(memc, ptr, strlen(ptr),
85 mptr, sbuf.st_size,
86 opt_expires, opt_flags);
87 else
88 abort();
89
90 if (rc != MEMCACHED_SUCCESS)
91 {
92 fprintf(stderr, "memcp: %s: memcache error %s\n",
93 ptr, memcached_strerror(memc, rc));
94 }
95
96 munmap(mptr, sbuf.st_size);
97 close(fd);
98 optind++;
99 }
100
101 memcached_deinit(memc);
102
103 return 0;
104 }
105
106 void options_parse(int argc, char *argv[])
107 {
108 int option_index= 0;
109 int option_rv;
110
111 static struct option long_options[]=
112 {
113 {"version", no_argument, NULL, OPT_VERSION},
114 {"help", no_argument, NULL, OPT_HELP},
115 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
116 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
117 {"servers", required_argument, NULL, OPT_SERVERS},
118 {"flag", required_argument, NULL, OPT_FLAG},
119 {"expire", required_argument, NULL, OPT_EXPIRE},
120 {"set", no_argument, &opt_replace, OPT_SET},
121 {"add", no_argument, &opt_replace, OPT_ADD},
122 {"replace", no_argument, &opt_replace, OPT_REPLACE},
123 {0, 0, 0, 0},
124 };
125
126 while (1)
127 {
128 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
129
130 if (option_rv == -1) break;
131
132 switch (option_rv)
133 {
134 case 0:
135 break;
136 case OPT_VERSION: /* --version or -V */
137 printf("memcache tools, memcp, v1.0\n");
138 exit(0);
139 case OPT_HELP: /* --help or -h */
140 printf("useful help messages go here\n");
141 exit(0);
142 case OPT_SERVERS: /* --servers or -s */
143 opt_servers= optarg;
144 break;
145 case OPT_FLAG: /* --flag */
146 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16);
147 break;
148 case OPT_EXPIRE: /* --expire */
149 opt_expires= (time_t)strtol(optarg, (char **)NULL, 10);
150 break;
151 case '?':
152 /* getopt_long already printed an error message. */
153 exit(1);
154 default:
155 abort();
156 }
157 }
158 }