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