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