Rmail updates.
[emacs.git] / lib-src / cvtmail.c
blob1c262c99aef09b12963340427251943782f81dcf
1 /* cvtmail.c --- convert Gosling Emacs mail directories into RMAIL format
3 Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008, 2009 Free Software Foundation, Inc.
6 Author: Larry Kolodney
7 Created: 1985
9 This file is part of GNU Emacs.
11 GNU Emacs is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 GNU Emacs is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 /* Commentary:
27 Program to convert oldstyle goslings emacs mail directories into
28 gnu-rmail format. Program expects a directory called Messages to
29 exist in your home directory, containing individual mail messages in
30 separate files in the standard gosling emacs mail reader format.
32 Program takes one argument: an output file. This file will contain
33 all the messages in Messages directory, in berkeley mail format.
34 If no output file is mentioned, messages are put in ~/OMAIL.
36 In order to get rmail to read the messages, the resulting file must
37 be mv'ed to ~/mbox, and then have rmail invoked on them. */
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
43 #include <stdio.h>
45 #ifndef HAVE_STDLIB_H
46 char *getenv ();
47 #endif
49 char *xmalloc __P ((unsigned));
50 char *xrealloc __P ((char *, unsigned));
51 void skip_to_lf __P ((FILE *));
52 void sysfail __P ((char *));
54 int
55 main (argc, argv)
56 int argc;
57 char *argv[];
59 char *hd;
60 char *md;
61 char *mdd;
62 char *mfile;
63 char *cf;
64 int cflen;
65 FILE *mddf;
66 FILE *mfilef;
67 FILE *cff;
68 char pre[10];
69 char name[14];
70 int c;
72 hd = (char *) getenv ("HOME");
74 md = (char *) xmalloc (strlen (hd) + 10);
75 strcpy (md, hd);
76 strcat (md, "/Messages");
78 mdd = (char *) xmalloc (strlen (md) + 11);
79 strcpy (mdd, md);
80 strcat (mdd, "/Directory");
82 cflen = 100;
83 cf = (char *) xmalloc (cflen);
85 mddf = fopen (mdd, "r");
86 if (!mddf)
87 sysfail (mdd);
88 if (argc > 1)
89 mfile = argv[1];
90 else
92 mfile = (char *) xmalloc (strlen (hd) + 7);
93 strcpy (mfile, hd);
94 strcat (mfile, "/OMAIL");
96 mfilef = fopen (mfile, "w");
97 if (!mfilef)
98 sysfail (mfile);
100 skip_to_lf (mddf);
101 while (fscanf (mddf, "%4c%14[0123456789]", pre, name) != EOF)
103 if (cflen < strlen (md) + strlen (name) + 2)
105 cflen = strlen (md) + strlen (name) + 2;
106 cf = (char *) xrealloc (cf, cflen);
108 strcpy (cf, md);
109 strcat (cf,"/");
110 strcat (cf, name);
111 cff = fopen (cf, "r");
112 if (!cff)
113 perror (cf);
114 else
116 while ((c = getc(cff)) != EOF)
117 putc (c, mfilef);
118 putc ('\n', mfilef);
119 skip_to_lf (mddf);
120 fclose (cff);
123 fclose (mddf);
124 fclose (mfilef);
125 return EXIT_SUCCESS;
128 void
129 skip_to_lf (stream)
130 FILE *stream;
132 register int c;
133 while ((c = getc(stream)) != EOF && c != '\n')
138 void
139 error (s1, s2)
140 char *s1, *s2;
142 fprintf (stderr, "cvtmail: ");
143 fprintf (stderr, s1, s2);
144 fprintf (stderr, "\n");
147 /* Print error message and exit. */
149 void
150 fatal (s1, s2)
151 char *s1, *s2;
153 error (s1, s2);
154 exit (EXIT_FAILURE);
157 void
158 sysfail (s)
159 char *s;
161 fprintf (stderr, "cvtmail: ");
162 perror (s);
163 exit (EXIT_FAILURE);
166 char *
167 xmalloc (size)
168 unsigned size;
170 char *result = (char *) malloc (size);
171 if (!result)
172 fatal ("virtual memory exhausted", 0);
173 return result;
176 char *
177 xrealloc (ptr, size)
178 char *ptr;
179 unsigned size;
181 char *result = (char *) realloc (ptr, size);
182 if (!result)
183 fatal ("virtual memory exhausted", 0);
184 return result;
187 /* arch-tag: b93c25a9-9012-44f1-b78b-9cc7aed44a7a
188 (do not change this comment) */
190 /* cvtmail.c ends here */