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