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