(C_entries): Cast result of xrealloc.
[bpt/emacs.git] / lib-src / cvtmail.c
CommitLineData
00345809 1/* Copyright (C) 1985, 1994 Free Software Foundation
518dd722
JB
2This file is part of GNU Emacs.
3
4GNU Emacs is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
00345809 6the Free Software Foundation; either version 2, or (at your option)
518dd722
JB
7any later version.
8
9GNU Emacs is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with GNU Emacs; see the file COPYING. If not, write to
16the 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 *
231c740a 24 * Program takes one argument: an output file. This file will contain
518dd722
JB
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
518dd722
JB
32 */
33
34
35#include <stdio.h>
36
00345809
DM
37char *malloc ();
38char *realloc ();
340ff9de
DM
39char *getenv ();
40
00345809
DM
41char *xmalloc ();
42char *xrealloc ();
340ff9de 43void skip_to_lf ();
231c740a 44void sysfail ();
518dd722 45
340ff9de 46int
518dd722
JB
47main (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;
340ff9de 60 char pre[10];
518dd722
JB
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");
231c740a
KH
78 if (!mddf)
79 sysfail (mdd);
518dd722 80 if (argc > 1)
231c740a 81 mfile = argv[1];
518dd722
JB
82 else
83 {
84 mfile = (char *) xmalloc (strlen (hd) + 7);
85 strcpy (mfile, hd);
86 strcat (mfile, "/OMAIL");
518dd722 87 }
231c740a
KH
88 mfilef = fopen (mfile, "w");
89 if (!mfilef)
90 sysfail (mfile);
91
518dd722
JB
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");
231c740a
KH
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 }
518dd722
JB
114 }
115 fclose (mddf);
116 fclose (mfilef);
117 return 0;
118}
119
340ff9de 120void
518dd722
JB
121skip_to_lf (stream)
122 FILE *stream;
123{
124 register int c;
231c740a 125 while ((c = getc(stream)) != EOF && c != '\n')
518dd722
JB
126 ;
127}
128
340ff9de
DM
129
130void
131error (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
141void
142fatal (s1, s2)
143 char *s1, *s2;
144{
145 error (s1, s2);
146 exit (1);
147}
148
231c740a
KH
149void
150sysfail (s)
151 char *s;
152{
153 fprintf (stderr, "cvtmail: ");
154 perror (s);
155 exit (1);
156}
157
00345809 158char *
518dd722 159xmalloc (size)
00345809 160 unsigned size;
518dd722 161{
00345809 162 char *result = malloc (size);
518dd722
JB
163 if (!result)
164 fatal ("virtual memory exhausted", 0);
165 return result;
166}
167
00345809 168char *
518dd722
JB
169xrealloc (ptr, size)
170 char *ptr;
00345809 171 unsigned size;
518dd722 172{
00345809 173 char *result = realloc (ptr, size);
518dd722
JB
174 if (!result)
175 fatal ("virtual memory exhausted");
176 return result;
177}