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