Fixed bug.
[yaz-moved-to-github.git] / util / marcdump.c
1 /*
2  * Copyright (c) 1995-2000, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: marcdump.c,v $
7  * Revision 1.11  2000-07-04 08:53:22  adam
8  * Fixed bug.
9  *
10  * Revision 1.10  2000/02/29 13:44:55  adam
11  * Check for config.h (currently not generated).
12  *
13  * Revision 1.9  1999/11/30 13:47:12  adam
14  * Improved installation. Moved header files to include/yaz.
15  *
16  * Revision 1.8  1999/05/26 07:49:35  adam
17  * C++ compilation.
18  *
19  * Revision 1.7  1998/02/11 11:53:36  adam
20  * Changed code so that it compiles as C++.
21  *
22  * Revision 1.6  1997/12/12 06:32:33  adam
23  * Added include of string.h.
24  *
25  * Revision 1.5  1997/09/24 13:29:40  adam
26  * Added verbose option -v to marcdump utility.
27  *
28  * Revision 1.4  1995/11/01 13:55:05  quinn
29  * Minor adjustments
30  *
31  * Revision 1.3  1995/05/16  08:51:12  quinn
32  * License, documentation, and memory fixes
33  *
34  * Revision 1.2  1995/05/15  11:56:56  quinn
35  * Debuggng & adjustments.
36  *
37  * Revision 1.1  1995/04/10  10:28:47  quinn
38  * Added copy of CCL and MARC display
39  *
40  */
41
42 #if HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <yaz/marcdisp.h>
51 #include <yaz/xmalloc.h>
52 #include <yaz/options.h>
53
54 #ifndef SEEK_SET
55 #define SEEK_SET 0
56 #endif
57 #ifndef SEEK_END
58 #define SEEK_END 2
59 #endif
60  
61 int main (int argc, char **argv)
62 {
63     int ret;
64     char *arg;
65     int verbose = 0;
66     FILE *inf;
67     long file_size;
68     char *buf, *p;
69     char *prog = *argv;
70     int count = 0;
71     int no = 0;
72
73     while ((ret = options("v", argv, argc, &arg)) != -2)
74     {
75         no++;
76         switch (ret)
77         {
78         case 0:
79             inf = fopen (arg, "r");
80             if (!inf)
81             {
82                 fprintf (stderr, "%s: cannot open %s:%s\n",
83                          prog, arg, strerror (errno));
84                 exit (1);
85             }
86             if (fseek (inf, 0L, SEEK_END))
87             {
88                 fprintf (stderr, "%s: cannot seek in %s:%s\n",
89                          prog, arg, strerror (errno));
90                 exit (1);
91             }
92             file_size = ftell (inf);    
93             if (fseek (inf, 0L, SEEK_SET))
94             {
95                 fprintf (stderr, "%s: cannot seek in %s:%s\n",
96                          prog, arg, strerror (errno));
97                 exit (1);
98             }
99             buf = (char *)xmalloc (file_size);
100             if (!buf)
101             {
102                 fprintf (stderr, "%s: cannot xmalloc: %s\n",
103                          prog, strerror (errno));
104                 exit (1);
105             }
106             if ((long) fread (buf, 1, file_size, inf) != file_size)
107             {
108                 fprintf (stderr, "%s: cannot read %s: %s\n",
109                          prog, arg, strerror (errno));
110                 exit (1);
111             }
112             for (p = buf; (ret = marc_display_ex (p, stdout, verbose)) > 0;)
113             {
114                 p += ret;
115                 count++;
116             }
117             fclose (inf);
118             xfree (buf);
119             break;
120         case 'v':
121             verbose++;
122             break;
123         default:
124             fprintf (stderr, "Usage: %s [-v] file...\n", prog);
125             exit (1);
126         }
127     }
128     if (!no)
129     {
130         fprintf (stderr, "Usage: %s [-v] file...\n", prog);
131         exit (1);
132     }
133     exit (0);
134 }