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. | |
32 | * | |
33 | * Author: Larry Kolodney, 1985 | |
518dd722 JB |
34 | */ |
35 | ||
36 | ||
37 | #include <stdio.h> | |
38 | ||
00345809 DM |
39 | char *malloc (); |
40 | char *realloc (); | |
340ff9de DM |
41 | char *getenv (); |
42 | ||
00345809 DM |
43 | char *xmalloc (); |
44 | char *xrealloc (); | |
340ff9de | 45 | void skip_to_lf (); |
231c740a | 46 | void sysfail (); |
518dd722 | 47 | |
340ff9de | 48 | int |
518dd722 JB |
49 | main (argc, argv) |
50 | int argc; | |
51 | char *argv[]; | |
52 | { | |
53 | char *hd; | |
54 | char *md; | |
55 | char *mdd; | |
56 | char *mfile; | |
57 | char *cf; | |
58 | int cflen; | |
59 | FILE *mddf; | |
60 | FILE *mfilef; | |
61 | FILE *cff; | |
340ff9de | 62 | char pre[10]; |
518dd722 JB |
63 | char name[14]; |
64 | int c; | |
65 | ||
66 | hd = (char *) getenv ("HOME"); | |
67 | ||
68 | md = (char *) xmalloc (strlen (hd) + 10); | |
69 | strcpy (md, hd); | |
70 | strcat (md, "/Messages"); | |
71 | ||
72 | mdd = (char *) xmalloc (strlen (md) + 11); | |
73 | strcpy (mdd, md); | |
74 | strcat (mdd, "/Directory"); | |
75 | ||
76 | cflen = 100; | |
77 | cf = (char *) xmalloc (cflen); | |
78 | ||
79 | mddf = fopen (mdd, "r"); | |
231c740a KH |
80 | if (!mddf) |
81 | sysfail (mdd); | |
518dd722 | 82 | if (argc > 1) |
231c740a | 83 | mfile = argv[1]; |
518dd722 JB |
84 | else |
85 | { | |
86 | mfile = (char *) xmalloc (strlen (hd) + 7); | |
87 | strcpy (mfile, hd); | |
88 | strcat (mfile, "/OMAIL"); | |
518dd722 | 89 | } |
231c740a KH |
90 | mfilef = fopen (mfile, "w"); |
91 | if (!mfilef) | |
92 | sysfail (mfile); | |
93 | ||
518dd722 JB |
94 | skip_to_lf (mddf); |
95 | while (fscanf (mddf, "%4c%14[0123456789]", pre, name) != EOF) | |
96 | { | |
97 | if (cflen < strlen (md) + strlen (name) + 2) | |
98 | { | |
99 | cflen = strlen (md) + strlen (name) + 2; | |
100 | cf = (char *) xrealloc (cf, cflen); | |
101 | } | |
102 | strcpy (cf, md); | |
103 | strcat (cf,"/"); | |
104 | strcat (cf, name); | |
105 | cff = fopen (cf, "r"); | |
231c740a KH |
106 | if (!cff) |
107 | perror (cf); | |
108 | else | |
109 | { | |
110 | while ((c = getc(cff)) != EOF) | |
111 | putc (c, mfilef); | |
112 | putc ('\n', mfilef); | |
113 | skip_to_lf (mddf); | |
114 | fclose (cff); | |
115 | } | |
518dd722 JB |
116 | } |
117 | fclose (mddf); | |
118 | fclose (mfilef); | |
119 | return 0; | |
120 | } | |
121 | ||
340ff9de | 122 | void |
518dd722 JB |
123 | skip_to_lf (stream) |
124 | FILE *stream; | |
125 | { | |
126 | register int c; | |
231c740a | 127 | while ((c = getc(stream)) != EOF && c != '\n') |
518dd722 JB |
128 | ; |
129 | } | |
130 | ||
340ff9de DM |
131 | |
132 | void | |
133 | error (s1, s2) | |
134 | char *s1, *s2; | |
135 | { | |
136 | fprintf (stderr, "cvtmail: "); | |
137 | fprintf (stderr, s1, s2); | |
138 | fprintf (stderr, "\n"); | |
139 | } | |
140 | ||
141 | /* Print error message and exit. */ | |
142 | ||
143 | void | |
144 | fatal (s1, s2) | |
145 | char *s1, *s2; | |
146 | { | |
147 | error (s1, s2); | |
148 | exit (1); | |
149 | } | |
150 | ||
231c740a KH |
151 | void |
152 | sysfail (s) | |
153 | char *s; | |
154 | { | |
155 | fprintf (stderr, "cvtmail: "); | |
156 | perror (s); | |
157 | exit (1); | |
158 | } | |
159 | ||
00345809 | 160 | char * |
518dd722 | 161 | xmalloc (size) |
00345809 | 162 | unsigned size; |
518dd722 | 163 | { |
00345809 | 164 | char *result = malloc (size); |
518dd722 JB |
165 | if (!result) |
166 | fatal ("virtual memory exhausted", 0); | |
167 | return result; | |
168 | } | |
169 | ||
00345809 | 170 | char * |
518dd722 JB |
171 | xrealloc (ptr, size) |
172 | char *ptr; | |
00345809 | 173 | unsigned size; |
518dd722 | 174 | { |
00345809 | 175 | char *result = realloc (ptr, size); |
518dd722 JB |
176 | if (!result) |
177 | fatal ("virtual memory exhausted"); | |
178 | return result; | |
179 | } |