0
|
1 /* muinc.c - mmdf to uucp 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
|
|
21 main (argc, argv)
|
|
22 int argc;
|
|
23 char *argv[];
|
|
24 {
|
|
25 int fd,
|
|
26 tmp;
|
|
27 struct stat st1,
|
|
28 st2;
|
|
29
|
|
30 mts_init (*argv);
|
|
31 sprintf (mmdfbox, "%s/%s", MAILDIR, MAILFIL);
|
|
32 if (stat (mmdfbox, &st1) == NOTOK || st1.st_size == 0L)
|
|
33 exit (0);
|
|
34 if ((mmdf = lkopen (mmdfbox, 0)) == NOTOK)
|
|
35 die ("unable to lock and open %s", mmdfbox);
|
|
36 tmp = tmp_open (&fd);
|
|
37
|
|
38 switch (fd = mmdf2uucp (mmdf, fd, FALSE)) {
|
|
39 case MFOK:
|
|
40 break;
|
|
41
|
|
42 case MFPRM:
|
|
43 die ("internal error while filtering MMDF mail");
|
|
44
|
|
45 case MFSIO:
|
|
46 die ("no free file pointers -- you lose");
|
|
47
|
|
48 case MFERR:
|
|
49 die ("i/o error while filtering MMDF mail");
|
|
50
|
|
51 case MFROM:
|
|
52 case MFHDR:
|
|
53 case MFTXT:
|
|
54 fprintf (stderr, "MMDF mailbox in bad format, patched...\n");
|
|
55 break;
|
|
56 }
|
|
57
|
|
58 sprintf (uucpbox, "%s/%s", UUCPDIR, UUCPFIL);
|
|
59 uucp = mbx_open (uucpbox);
|
|
60 mbx_copy (tmp, uucp);
|
|
61 close (tmp);
|
|
62 lkclose (uucp, uucpbox), uucp = NOTOK;
|
|
63
|
|
64 if (stat (mmdfbox, &st2) != NOTOK && st1.st_mtime != st2.st_mtime)
|
|
65 fprintf (stderr, "MMDF mailbox has been updated... (%s)\n",
|
|
66 "so it won't be zero'd");
|
|
67 else
|
|
68 if ((fd = creat (mmdfbox, st1.st_mode & ~S_IFMT)) != NOTOK)
|
|
69 close (fd);
|
|
70 else
|
|
71 fprintf (stderr, "unable to zero MMDF mailbox\n");
|
|
72 lkclose (mmdf, mmdfbox), mmdf = NOTOK;
|
|
73
|
|
74 exit (0);
|
|
75 }
|
|
76
|
|
77 /* */
|
|
78
|
|
79 static int mbx_open (file)
|
|
80 char *file;
|
|
81 {
|
|
82 int count,
|
|
83 fd;
|
|
84
|
|
85 for (count = 2; count > 0; count--)
|
|
86 if ((fd = lkopen (file, 1)) == NOTOK)
|
|
87 switch (errno) {
|
|
88 case ENOENT:
|
|
89 mbx_create (file);
|
|
90 break;
|
|
91 case ETXTBSY:
|
|
92 sleep (5);
|
|
93 break;
|
|
94 default:
|
|
95 goto openerr;
|
|
96 }
|
|
97
|
|
98 if (fd == NOTOK) {
|
|
99 openerr:
|
|
100 if (errno == ETXTBSY)
|
|
101 die ("your UUCP mailbox '%s' is busy", file);
|
|
102 else
|
|
103 die ("unable to open UUCP mailbox '%s'", file);
|
|
104 }
|
|
105
|
|
106 lseek (fd, (off_t)0, 2);
|
|
107
|
|
108 return fd;
|
|
109 }
|
|
110
|
|
111 /* */
|
|
112
|
|
113 static mbx_create (file)
|
|
114 char *file;
|
|
115 {
|
|
116 int fd;
|
|
117
|
|
118 if ((fd = creat (file, MBXMODE)) == NOTOK)
|
|
119 die ("unable to create UUCP mailbox '%s'", file);
|
|
120
|
|
121 close (fd);
|
|
122 }
|
|
123
|
|
124 /* */
|
|
125
|
|
126 static mbx_copy (in, out)
|
|
127 int in,
|
|
128 out;
|
|
129 {
|
|
130 int i;
|
|
131 char buffer[BUFSIZ];
|
|
132
|
|
133 lseek (in, (off_t)0, 0);
|
|
134
|
|
135 while ((i = read (in, buffer, sizeof buffer)) > 0)
|
|
136 if (write (out, buffer, i) != i)
|
|
137 die ("error writing UUCP mailbox");
|
|
138 if (i < 0)
|
|
139 die ("error reading temporary file");
|
|
140 }
|
|
141
|
|
142 /* */
|
|
143
|
|
144 static int tmp_open (mbx_fd)
|
|
145 int *mbx_fd;
|
|
146 {
|
|
147 int fd;
|
|
148 char tmpfil[LINESIZ];
|
|
149
|
|
150 strcpy (tmpfil, "/tmp/muincXXXXXX");
|
|
151 unlink (mktemp (tmpfil));
|
|
152 if ((fd = creat (tmpfil, TMPMODE)) == NOTOK)
|
|
153 die ("unable to create temporary file '%s'", tmpfil);
|
|
154 close (fd);
|
|
155
|
|
156 if ((fd = open (tmpfil, 2)) == NOTOK)
|
|
157 die ("unable to create temporary file '%s'", tmpfil);
|
|
158 unlink (tmpfil);
|
|
159
|
|
160 if ((*mbx_fd = dup (fd)) == NOTOK)
|
|
161 die ("unable to duplicate fd for temporary file '%s'", tmpfil);
|
|
162
|
|
163 return fd;
|
|
164 }
|
|
165
|
|
166 /* */
|
|
167
|
|
168 static die (fmt, a, b, c, d)
|
|
169 char *fmt,
|
|
170 *a,
|
|
171 *b,
|
|
172 *c,
|
|
173 *d;
|
|
174 {
|
|
175 lkclose (mmdf, mmdfbox), mmdf = NOTOK;
|
|
176 lkclose (uucp, uucpbox), uucp = NOTOK;
|
|
177
|
|
178 fflush (stdout);
|
|
179 fprintf (stderr, fmt, a, b, c, d);
|
|
180 putc ('\n', stderr);
|
|
181
|
|
182 exit (1);
|
|
183 }
|