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