0
|
1 /* uminc.c - uucp to mmdf inc */
|
|
2 #ifndef lint
|
|
3 static char Id[] = "@(#)$Id$";
|
|
4 #endif
|
|
5
|
|
6 #include "mf.h"
|
|
7 #include <stdio.h>
|
|
8 #include "../mts/mts.h"
|
|
9 #include <errno.h>
|
|
10 #include <sys/types.h>
|
|
11 #include <sys/stat.h>
|
|
12
|
|
13
|
|
14 static int mmdf = NOTOK;
|
|
15 static int uucp = NOTOK;
|
|
16 static char mmdfbox[LINESIZ];
|
|
17 static char uucpbox[LINESIZ];
|
|
18
|
|
19
|
|
20 off_t lseek ();
|
|
21
|
|
22 /* */
|
|
23
|
|
24 main (argc, argv)
|
|
25 int argc;
|
|
26 char *argv[];
|
|
27 {
|
|
28 int fd,
|
|
29 tmp;
|
|
30 struct stat st1,
|
|
31 st2;
|
|
32
|
|
33 mts_init (*argv);
|
|
34 sprintf (uucpbox, "%s/%s", UUCPDIR, UUCPFIL);
|
|
35 if (stat (uucpbox, &st1) == NOTOK || st1.st_size == 0L)
|
|
36 exit (0);
|
|
37 if ((uucp = lkopen (uucpbox, 0)) == NOTOK)
|
|
38 die ("unable to lock and open %s", uucpbox);
|
|
39 tmp = tmp_open (&fd);
|
|
40
|
|
41 switch (fd = uucp2mmdf (uucp, fd, FALSE)) {
|
|
42 case MFOK:
|
|
43 break;
|
|
44
|
|
45 case MFPRM:
|
|
46 die ("internal error while filtering UUCP mail");
|
|
47
|
|
48 case MFSIO:
|
|
49 die ("no free file pointers -- you lose");
|
|
50
|
|
51 case MFERR:
|
|
52 die ("i/o error while filtering UUCP mail");
|
|
53
|
|
54 case MFROM:
|
|
55 case MFHDR:
|
|
56 case MFTXT:
|
|
57 fprintf (stderr, "UUCP mailbox in bad format, patched...\n");
|
|
58 break;
|
|
59 }
|
|
60
|
|
61 sprintf (mmdfbox, "%s/%s", MAILDIR, MAILFIL);
|
|
62 mmdf = mbx_open (mmdfbox);
|
|
63 mbx_copy (tmp, mmdf);
|
|
64 close (tmp);
|
|
65 lkclose (mmdf, mmdfbox), mmdf = NOTOK;
|
|
66
|
|
67 if (stat (uucpbox, &st2) != NOTOK && st1.st_mtime != st2.st_mtime)
|
|
68 fprintf (stderr, "UUCP mailbox has been updated... (%s)\n",
|
|
69 "so it won't be removed");
|
|
70 else
|
|
71 if (unlink (uucpbox) == NOTOK)
|
|
72 if ((fd = creat (uucpbox, st1.st_mode & ~S_IFMT)) != NOTOK)
|
|
73 close (fd);
|
|
74 else
|
|
75 fprintf (stderr, "unable to remove or zero UUCP mailbox\n");
|
|
76 lkclose (uucp, uucpbox), uucp = NOTOK;
|
|
77
|
|
78 exit (0);
|
|
79 }
|
|
80
|
|
81 /* */
|
|
82
|
|
83 static int mbx_open (file)
|
|
84 char *file;
|
|
85 {
|
|
86 int clear,
|
|
87 count,
|
|
88 fd;
|
|
89 struct stat stbuf;
|
|
90
|
|
91 for (clear = FALSE, count = 2; count > 0; count--)
|
|
92 if ((fd = lkopen (file, 6)) == NOTOK)
|
|
93 switch (errno) {
|
|
94 case ENOENT:
|
|
95 mbx_create (file);
|
|
96 clear++;
|
|
97 break;
|
|
98
|
|
99 case ETXTBSY:
|
|
100 sleep (5);
|
|
101 break;
|
|
102
|
|
103 default:
|
|
104 goto openerr;
|
|
105 }
|
|
106 else {
|
|
107 if (fstat (fd, &stbuf) == NOTOK)
|
|
108 die ("unable to stat MMDF mailbox '%s'", file);
|
|
109 clear = stbuf.st_size == 0L;
|
|
110 break;
|
|
111 }
|
|
112
|
|
113 if (fd == NOTOK) {
|
|
114 openerr:
|
|
115 if (errno == ETXTBSY)
|
|
116 die ("your MMDF mailbox '%s' is busy", file);
|
|
117 else
|
|
118 die ("unable to open MMDF mailbox '%s'", file);
|
|
119 }
|
|
120 if (!clear)
|
|
121 mbx_chk (fd, file);
|
|
122
|
|
123 return fd;
|
|
124 }
|
|
125
|
|
126 /* */
|
|
127
|
|
128 static mbx_create (file)
|
|
129 char *file;
|
|
130 {
|
|
131 int fd;
|
|
132
|
|
133 if ((fd = creat (file, MBXMODE)) == NOTOK)
|
|
134 die ("unable to create MMDF mailbox '%s'", file);
|
|
135
|
|
136 close (fd);
|
|
137 }
|
|
138
|
|
139
|
|
140 static mbx_chk (fd, file)
|
|
141 int fd;
|
|
142 char *file;
|
|
143 {
|
|
144 int count;
|
|
145 char ldelim[20];
|
|
146
|
|
147 count = strlen (mmdlm2);
|
|
148
|
|
149 if (lseek (fd, (off_t) - count, 2) == (off_t) NOTOK
|
|
150 || read (fd, ldelim, count) != count)
|
|
151 die ("error reading MMDF mailbox '%s'", file);
|
|
152 ldelim[count] = NULL;
|
|
153
|
|
154 if (strcmp (ldelim, mmdlm2)) {
|
|
155 fprintf (stderr,
|
|
156 "MMDF mailbox '%s' has bad delimiter, patching...\n",
|
|
157 file);
|
|
158 if (write (fd, mmdlm2, count) != count)
|
|
159 die ("error writing MMDF mailbox '%s'", file);
|
|
160 }
|
|
161 }
|
|
162
|
|
163 /* */
|
|
164
|
|
165 static mbx_copy (in, out)
|
|
166 int in,
|
|
167 out;
|
|
168 {
|
|
169 int i;
|
|
170 char buffer[BUFSIZ];
|
|
171
|
|
172 lseek (in, (off_t)0, 0);
|
|
173
|
|
174 while ((i = read (in, buffer, sizeof buffer)) > 0)
|
|
175 if (write (out, buffer, i) != i)
|
|
176 die ("error writing MMDF mailbox");
|
|
177 if (i < 0)
|
|
178 die ("error reading temporary file");
|
|
179
|
|
180 close (in);
|
|
181 close (out);
|
|
182 }
|
|
183
|
|
184 /* */
|
|
185
|
|
186 static int tmp_open (mbx_fd)
|
|
187 int *mbx_fd;
|
|
188 {
|
|
189 int fd;
|
|
190 char tmpfil[LINESIZ];
|
|
191
|
|
192 strcpy (tmpfil, "/tmp/umincXXXXXX");
|
|
193 unlink (mktemp (tmpfil));
|
|
194 if ((fd = creat (tmpfil, TMPMODE)) == NOTOK)
|
|
195 die ("unable to create temporary file '%s'", tmpfil);
|
|
196 close (fd);
|
|
197
|
|
198 if ((fd = open (tmpfil, 2)) == NOTOK)
|
|
199 die ("unable to create temporary file '%s'", tmpfil);
|
|
200 unlink (tmpfil);
|
|
201
|
|
202 if ((*mbx_fd = dup (fd)) == NOTOK)
|
|
203 die ("unable to duplicate fd for temporary file '%s'", tmpfil);
|
|
204
|
|
205 return fd;
|
|
206 }
|
|
207
|
|
208 /* */
|
|
209
|
|
210 static die (fmt, a, b, c, d)
|
|
211 char *fmt,
|
|
212 *a,
|
|
213 *b,
|
|
214 *c,
|
|
215 *d;
|
|
216 {
|
|
217 lkclose (mmdf, mmdfbox), mmdf = NOTOK;
|
|
218 lkclose (uucp, uucpbox), uucp = NOTOK;
|
|
219
|
|
220 fflush (stdout);
|
|
221 fprintf (stderr, fmt, a, b, c, d);
|
|
222 putc ('\n', stderr);
|
|
223
|
|
224 exit (1);
|
|
225 }
|