Commit | Line | Data |
---|---|---|
00345809 | 1 | /* Copyright (C) 1985, 1994 Free Software Foundation |
518dd722 JB |
2 | This file is part of GNU Emacs. |
3 | ||
4 | GNU Emacs is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
00345809 | 6 | the Free Software Foundation; either version 2, or (at your option) |
518dd722 JB |
7 | any later version. |
8 | ||
9 | GNU Emacs is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with GNU Emacs; see the file COPYING. If not, write to | |
16 | the 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 |
37 | char *malloc (); |
38 | char *realloc (); | |
340ff9de DM |
39 | char *getenv (); |
40 | ||
00345809 DM |
41 | char *xmalloc (); |
42 | char *xrealloc (); | |
340ff9de | 43 | void skip_to_lf (); |
231c740a | 44 | void sysfail (); |
518dd722 | 45 | |
340ff9de | 46 | int |
518dd722 JB |
47 | main (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 | 120 | void |
518dd722 JB |
121 | skip_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 | |
130 | void | |
131 | error (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 | ||
141 | void | |
142 | fatal (s1, s2) | |
143 | char *s1, *s2; | |
144 | { | |
145 | error (s1, s2); | |
146 | exit (1); | |
147 | } | |
148 | ||
231c740a KH |
149 | void |
150 | sysfail (s) | |
151 | char *s; | |
152 | { | |
153 | fprintf (stderr, "cvtmail: "); | |
154 | perror (s); | |
155 | exit (1); | |
156 | } | |
157 | ||
00345809 | 158 | char * |
518dd722 | 159 | xmalloc (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 | 168 | char * |
518dd722 JB |
169 | xrealloc (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 | } |