Code updates which makes things compile as C++. Mostly type casts were
[yaz-moved-to-github.git] / zoom / zoom-bug-641.c
1 /* $Id: zoom-bug-641.c,v 1.3 2007-05-06 20:12:21 adam Exp $  */
2
3 /** \file zoom-bug641.c
4     \brief Program to illustrate bug 641
5 */
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <yaz/zoom.h>
13 #include <yaz/xmalloc.h>
14
15 #ifdef WIN32
16 #error Unix only
17 #endif
18
19 int main(int argc, char **argv)
20 {
21     ZOOM_connection z;
22     int i, error;
23     const char *errmsg, *addinfo;
24
25     if (argc < 3) {
26         fprintf(stderr, "Usage:\n%s <target> <file> [<file> ...]\n", argv[0]);
27         fprintf(stderr, " eg.  bagel.indexdata.dk/gils foo.xml bar.xml\n");
28         return 1;
29     }
30
31     z = ZOOM_connection_create(0);
32
33     for (i = 2; i < argc; i++) {
34         char *buf, *fn = argv[i];
35         struct stat statbuf;
36         size_t size, offset = 0;
37         int fd, n;
38
39         ZOOM_connection_connect(z, argv[1], 0);
40         if ((error = ZOOM_connection_error(z, &errmsg, &addinfo))) {
41             fprintf(stderr, "Error: %s (%d) %s\n", errmsg, error, addinfo);
42             return 2;
43         }
44
45         if (stat(fn, &statbuf) < 0 ||
46             (fd = open(fn, O_RDONLY)) < 0) {
47             perror(fn);
48             return 3;
49         }
50         size = statbuf.st_size;
51         printf("size=%lu\n", (unsigned long) size);
52         buf = (char *) xmalloc(size+1);
53         while ((n = read(fd, &buf[offset], size)) < size) {
54             if (n < 0) {
55                 perror("read");
56                 return 4;
57             }
58             size -= n;
59             offset += n;
60         }
61         close(fd);
62         buf[size] = 0;
63
64         {
65             ZOOM_package pkg = ZOOM_connection_package(z, 0);
66             ZOOM_package_option_set(pkg, "action", "specialUpdate");
67             ZOOM_package_option_set(pkg, "record", buf);
68             ZOOM_package_send(pkg, "update");
69             if ((error = ZOOM_connection_error(z, &errmsg, &addinfo))) {
70                 printf("file '%s': error %d (%s) %s\n",
71                        fn, error, errmsg, addinfo);
72             } else {
73                 printf("file '%s': ok\n", fn);
74             }
75         }
76
77         xfree(buf);
78         if (i < argc-1) sleep(2);
79     }
80
81     ZOOM_connection_destroy(z);
82     return 0;
83 }
84 /*
85  * Local variables:
86  * c-basic-offset: 4
87  * indent-tabs-mode: nil
88  * End:
89  * vim: shiftwidth=4 tabstop=8 expandtab
90  */