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