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