Commit | Line | Data |
---|---|---|
b3d90e46 | 1 | /* Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, |
a5b68355 | 2 | 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
3b7ad313 | 3 | |
518dd722 JB |
4 | This file is part of GNU Emacs. |
5 | ||
294981c7 | 6 | GNU Emacs is free software: you can redistribute it and/or modify |
518dd722 | 7 | it under the terms of the GNU General Public License as published by |
294981c7 GM |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) any later version. | |
518dd722 JB |
10 | |
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
294981c7 GM |
17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
18 | ||
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 |
340ff9de | 43 | char *getenv (); |
0b3b8286 DL |
44 | #endif |
45 | ||
46 | char *xmalloc __P ((unsigned)); | |
47 | char *xrealloc __P ((char *, unsigned)); | |
48 | void skip_to_lf __P ((FILE *)); | |
49 | void sysfail __P ((char *)); | |
518dd722 | 50 | |
340ff9de | 51 | int |
518dd722 JB |
52 | main (argc, argv) |
53 | int argc; | |
54 | char *argv[]; | |
55 | { | |
56 | char *hd; | |
57 | char *md; | |
58 | char *mdd; | |
59 | char *mfile; | |
60 | char *cf; | |
61 | int cflen; | |
62 | FILE *mddf; | |
63 | FILE *mfilef; | |
64 | FILE *cff; | |
340ff9de | 65 | char pre[10]; |
518dd722 JB |
66 | char name[14]; |
67 | int c; | |
68 | ||
69 | hd = (char *) getenv ("HOME"); | |
70 | ||
71 | md = (char *) xmalloc (strlen (hd) + 10); | |
72 | strcpy (md, hd); | |
73 | strcat (md, "/Messages"); | |
74 | ||
75 | mdd = (char *) xmalloc (strlen (md) + 11); | |
76 | strcpy (mdd, md); | |
77 | strcat (mdd, "/Directory"); | |
78 | ||
79 | cflen = 100; | |
80 | cf = (char *) xmalloc (cflen); | |
81 | ||
82 | mddf = fopen (mdd, "r"); | |
231c740a KH |
83 | if (!mddf) |
84 | sysfail (mdd); | |
518dd722 | 85 | if (argc > 1) |
231c740a | 86 | mfile = argv[1]; |
518dd722 JB |
87 | else |
88 | { | |
89 | mfile = (char *) xmalloc (strlen (hd) + 7); | |
90 | strcpy (mfile, hd); | |
91 | strcat (mfile, "/OMAIL"); | |
518dd722 | 92 | } |
231c740a KH |
93 | mfilef = fopen (mfile, "w"); |
94 | if (!mfilef) | |
95 | sysfail (mfile); | |
96 | ||
518dd722 JB |
97 | skip_to_lf (mddf); |
98 | while (fscanf (mddf, "%4c%14[0123456789]", pre, name) != EOF) | |
99 | { | |
100 | if (cflen < strlen (md) + strlen (name) + 2) | |
101 | { | |
102 | cflen = strlen (md) + strlen (name) + 2; | |
103 | cf = (char *) xrealloc (cf, cflen); | |
104 | } | |
105 | strcpy (cf, md); | |
106 | strcat (cf,"/"); | |
107 | strcat (cf, name); | |
108 | cff = fopen (cf, "r"); | |
231c740a KH |
109 | if (!cff) |
110 | perror (cf); | |
111 | else | |
112 | { | |
113 | while ((c = getc(cff)) != EOF) | |
114 | putc (c, mfilef); | |
115 | putc ('\n', mfilef); | |
116 | skip_to_lf (mddf); | |
117 | fclose (cff); | |
118 | } | |
518dd722 JB |
119 | } |
120 | fclose (mddf); | |
2f8fe2f4 | 121 | fclose (mfilef); |
65396510 | 122 | return EXIT_SUCCESS; |
518dd722 JB |
123 | } |
124 | ||
340ff9de | 125 | void |
518dd722 JB |
126 | skip_to_lf (stream) |
127 | FILE *stream; | |
128 | { | |
129 | register int c; | |
231c740a | 130 | while ((c = getc(stream)) != EOF && c != '\n') |
518dd722 JB |
131 | ; |
132 | } | |
133 | ||
340ff9de DM |
134 | |
135 | void | |
136 | error (s1, s2) | |
137 | char *s1, *s2; | |
138 | { | |
139 | fprintf (stderr, "cvtmail: "); | |
140 | fprintf (stderr, s1, s2); | |
141 | fprintf (stderr, "\n"); | |
142 | } | |
143 | ||
144 | /* Print error message and exit. */ | |
145 | ||
146 | void | |
147 | fatal (s1, s2) | |
148 | char *s1, *s2; | |
149 | { | |
150 | error (s1, s2); | |
65396510 | 151 | exit (EXIT_FAILURE); |
340ff9de DM |
152 | } |
153 | ||
231c740a KH |
154 | void |
155 | sysfail (s) | |
156 | char *s; | |
157 | { | |
158 | fprintf (stderr, "cvtmail: "); | |
159 | perror (s); | |
65396510 | 160 | exit (EXIT_FAILURE); |
231c740a KH |
161 | } |
162 | ||
00345809 | 163 | char * |
518dd722 | 164 | xmalloc (size) |
00345809 | 165 | unsigned size; |
518dd722 | 166 | { |
2a61b9d2 | 167 | char *result = (char *) malloc (size); |
518dd722 JB |
168 | if (!result) |
169 | fatal ("virtual memory exhausted", 0); | |
170 | return result; | |
171 | } | |
172 | ||
00345809 | 173 | char * |
518dd722 JB |
174 | xrealloc (ptr, size) |
175 | char *ptr; | |
00345809 | 176 | unsigned size; |
518dd722 | 177 | { |
2a61b9d2 | 178 | char *result = (char *) realloc (ptr, size); |
518dd722 | 179 | if (!result) |
6d8f7d5d | 180 | fatal ("virtual memory exhausted", 0); |
518dd722 JB |
181 | return result; |
182 | } | |
ab5796a9 MB |
183 | |
184 | /* arch-tag: b93c25a9-9012-44f1-b78b-9cc7aed44a7a | |
185 | (do not change this comment) */ | |
65396510 TTN |
186 | |
187 | /* cvtmail.c ends here */ |