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