ia64/xen-unstable

view tools/xenstore/xenstore_client.c @ 6793:f2450805063b

Fix the check that the argument count is even, for xenstore_write.
author emellor@ewan
date Tue Sep 13 15:46:20 2005 +0100 (2005-09-13)
parents b6c98fe62e1a
children 4d899a738d59 e7c7196fa329
line source
1 /*
2 * This file is subject to the terms and conditions of the GNU General
3 * Public License. See the file "COPYING" in the main directory of
4 * this archive for more details.
5 *
6 * Copyright (C) 2005 by Christian Limpach
7 *
8 */
10 #include <err.h>
11 #include <fcntl.h>
12 #include <getopt.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <xs.h>
18 static void
19 usage(const char *progname)
20 {
21 #if defined(CLIENT_read)
22 errx(1, "Usage: %s [-h] [-p] key [...]", progname);
23 #elif defined(CLIENT_write)
24 errx(1, "Usage: %s [-h] key value [...]", progname);
25 #elif defined(CLIENT_rm)
26 errx(1, "Usage: %s [-h] key [...]", progname);
27 #endif
28 }
30 int
31 main(int argc, char **argv)
32 {
33 struct xs_handle *xsh;
34 bool success;
35 int ret = 0;
36 #if defined(CLIENT_read)
37 char *val;
38 int prefix = 0;
39 #endif
41 xsh = xs_domain_open();
42 if (xsh == NULL)
43 err(1, "xs_domain_open");
45 while (1) {
46 int c, index = 0;
47 static struct option long_options[] = {
48 {"help", 0, 0, 'h'},
49 #if defined(CLIENT_read)
50 {"prefix", 0, 0, 'p'},
51 #endif
52 {0, 0, 0, 0}
53 };
55 c = getopt_long(argc, argv, "h"
56 #if defined(CLIENT_read)
57 "p"
58 #endif
59 , long_options, &index);
60 if (c == -1)
61 break;
63 switch (c) {
64 case 'h':
65 usage(argv[0]);
66 /* NOTREACHED */
67 #if defined(CLIENT_read)
68 case 'p':
69 prefix = 1;
70 break;
71 #endif
72 }
73 }
75 if (optind == argc) {
76 usage(argv[0]);
77 /* NOTREACHED */
78 }
79 #if defined(CLIENT_write)
80 if ((argc - optind) % 2 == 1) {
81 usage(argv[0]);
82 /* NOTREACHED */
83 }
84 #endif
86 /* XXX maybe find longest common prefix */
87 success = xs_transaction_start(xsh, "/");
88 if (!success)
89 errx(1, "couldn't start transaction");
91 while (optind < argc) {
92 #if defined(CLIENT_read)
93 val = xs_read(xsh, argv[optind], NULL);
94 if (val == NULL) {
95 warnx("couldn't read path %s", argv[optind]);
96 ret = 1;
97 goto out;
98 }
99 if (prefix)
100 printf("%s: ", argv[optind]);
101 printf("%s\n", val);
102 free(val);
103 optind++;
104 #elif defined(CLIENT_write)
105 success = xs_write(xsh, argv[optind], argv[optind + 1],
106 strlen(argv[optind + 1]), O_CREAT);
107 if (!success) {
108 warnx("could not write path %s", argv[optind]);
109 ret = 1;
110 goto out;
111 }
112 optind += 2;
113 #elif defined(CLIENT_rm)
114 success = xs_rm(xsh, argv[optind]);
115 if (!success) {
116 warnx("could not remove path %s", argv[optind]);
117 ret = 1;
118 goto out;
119 }
120 optind++;
121 #endif
122 }
124 out:
125 success = xs_transaction_end(xsh, ret ? true : false);
126 if (!success)
127 errx(1, "couldn't end transaction");
129 return ret;
130 }