Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / zotnet / mf / muinc.c
1 /* muinc.c - mmdf to uucp inc */
2 #ifndef lint
3 static char Id[] = "@(#)$Id: muinc.c,v 1.2 1993/08/25 17:30:36 jromine Exp $";
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 /* \f */
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 /* \f */
78
79 static int  mbx_open (file)
80 char   *file;
81 {
82     int     count,
83             fd;
84     extern int  errno;
85
86     for (count = 2; count > 0; count--)
87         if ((fd = lkopen (file, 1)) == NOTOK)
88             switch (errno) {
89                 case ENOENT: 
90                     mbx_create (file);
91                     break;
92                 case ETXTBSY: 
93                     sleep (5);
94                     break;
95                 default: 
96                     goto openerr;
97             }
98
99     if (fd == NOTOK) {
100 openerr: 
101         if (errno == ETXTBSY)
102             die ("your UUCP mailbox '%s' is busy", file);
103         else
104             die ("unable to open UUCP mailbox '%s'", file);
105     }
106
107     lseek (fd, (off_t)0, 2);
108
109     return fd;
110 }
111
112 /* \f */
113
114 static  mbx_create (file)
115 char   *file;
116 {
117     int     fd;
118
119     if ((fd = creat (file, MBXMODE)) == NOTOK)
120         die ("unable to create UUCP mailbox '%s'", file);
121
122     close (fd);
123 }
124
125 /* \f */
126
127 static  mbx_copy (in, out)
128 int     in,
129         out;
130 {
131     int     i;
132     char    buffer[BUFSIZ];
133
134     lseek (in, (off_t)0, 0);
135
136     while ((i = read (in, buffer, sizeof buffer)) > 0)
137         if (write (out, buffer, i) != i)
138             die ("error writing UUCP mailbox");
139     if (i < 0)
140         die ("error reading temporary file");
141 }
142
143 /* \f */
144
145 static int  tmp_open (mbx_fd)
146 int    *mbx_fd;
147 {
148     int     fd;
149     char    tmpfil[LINESIZ];
150
151     strcpy (tmpfil, "/tmp/muincXXXXXX");
152     unlink (mktemp (tmpfil));
153     if ((fd = creat (tmpfil, TMPMODE)) == NOTOK)
154         die ("unable to create temporary file '%s'", tmpfil);
155     close (fd);
156
157     if ((fd = open (tmpfil, 2)) == NOTOK)
158         die ("unable to create temporary file '%s'", tmpfil);
159     unlink (tmpfil);
160
161     if ((*mbx_fd = dup (fd)) == NOTOK)
162         die ("unable to duplicate fd for temporary file '%s'", tmpfil);
163
164     return fd;
165 }
166
167 /* \f */
168
169 static  die (fmt, a, b, c, d)
170 char   *fmt,
171        *a,
172        *b,
173        *c,
174        *d;
175 {
176     lkclose (mmdf, mmdfbox), mmdf = NOTOK;
177     lkclose (uucp, uucpbox), uucp = NOTOK;
178
179     fflush (stdout);
180     fprintf (stderr, fmt, a, b, c, d);
181     putc ('\n', stderr);
182
183     exit (1);
184 }