(C_entries): Cast result of xrealloc.
[bpt/emacs.git] / lib-src / cvtmail.c
1 /* Copyright (C) 1985, 1994 Free Software Foundation
2 This file is part of GNU Emacs.
3
4 GNU Emacs is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 GNU Emacs is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Emacs; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 /* cvtmail:
19 * Program to convert oldstyle goslings emacs mail directories into
20 * gnu-rmail format. Program expects a directory called Messages to
21 * exist in your home directory, containing individual mail messages in
22 * separate files in the standard gosling emacs mail reader format.
23 *
24 * Program takes one argument: an output file. This file will contain
25 * all the messages in Messages directory, in berkeley mail format.
26 * If no output file is mentioned, messages are put in ~/OMAIL.
27 *
28 * In order to get rmail to read the messages, the resulting file must
29 * be mv'ed to ~/mbox, and then have rmail invoked on them.
30 *
31 * Author: Larry Kolodney, 1985
32 */
33
34
35 #include <stdio.h>
36
37 char *malloc ();
38 char *realloc ();
39 char *getenv ();
40
41 char *xmalloc ();
42 char *xrealloc ();
43 void skip_to_lf ();
44 void sysfail ();
45
46 int
47 main (argc, argv)
48 int argc;
49 char *argv[];
50 {
51 char *hd;
52 char *md;
53 char *mdd;
54 char *mfile;
55 char *cf;
56 int cflen;
57 FILE *mddf;
58 FILE *mfilef;
59 FILE *cff;
60 char pre[10];
61 char name[14];
62 int c;
63
64 hd = (char *) getenv ("HOME");
65
66 md = (char *) xmalloc (strlen (hd) + 10);
67 strcpy (md, hd);
68 strcat (md, "/Messages");
69
70 mdd = (char *) xmalloc (strlen (md) + 11);
71 strcpy (mdd, md);
72 strcat (mdd, "/Directory");
73
74 cflen = 100;
75 cf = (char *) xmalloc (cflen);
76
77 mddf = fopen (mdd, "r");
78 if (!mddf)
79 sysfail (mdd);
80 if (argc > 1)
81 mfile = argv[1];
82 else
83 {
84 mfile = (char *) xmalloc (strlen (hd) + 7);
85 strcpy (mfile, hd);
86 strcat (mfile, "/OMAIL");
87 }
88 mfilef = fopen (mfile, "w");
89 if (!mfilef)
90 sysfail (mfile);
91
92 skip_to_lf (mddf);
93 while (fscanf (mddf, "%4c%14[0123456789]", pre, name) != EOF)
94 {
95 if (cflen < strlen (md) + strlen (name) + 2)
96 {
97 cflen = strlen (md) + strlen (name) + 2;
98 cf = (char *) xrealloc (cf, cflen);
99 }
100 strcpy (cf, md);
101 strcat (cf,"/");
102 strcat (cf, name);
103 cff = fopen (cf, "r");
104 if (!cff)
105 perror (cf);
106 else
107 {
108 while ((c = getc(cff)) != EOF)
109 putc (c, mfilef);
110 putc ('\n', mfilef);
111 skip_to_lf (mddf);
112 fclose (cff);
113 }
114 }
115 fclose (mddf);
116 fclose (mfilef);
117 return 0;
118 }
119
120 void
121 skip_to_lf (stream)
122 FILE *stream;
123 {
124 register int c;
125 while ((c = getc(stream)) != EOF && c != '\n')
126 ;
127 }
128
129
130 void
131 error (s1, s2)
132 char *s1, *s2;
133 {
134 fprintf (stderr, "cvtmail: ");
135 fprintf (stderr, s1, s2);
136 fprintf (stderr, "\n");
137 }
138
139 /* Print error message and exit. */
140
141 void
142 fatal (s1, s2)
143 char *s1, *s2;
144 {
145 error (s1, s2);
146 exit (1);
147 }
148
149 void
150 sysfail (s)
151 char *s;
152 {
153 fprintf (stderr, "cvtmail: ");
154 perror (s);
155 exit (1);
156 }
157
158 char *
159 xmalloc (size)
160 unsigned size;
161 {
162 char *result = malloc (size);
163 if (!result)
164 fatal ("virtual memory exhausted", 0);
165 return result;
166 }
167
168 char *
169 xrealloc (ptr, size)
170 char *ptr;
171 unsigned size;
172 {
173 char *result = realloc (ptr, size);
174 if (!result)
175 fatal ("virtual memory exhausted");
176 return result;
177 }