Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / zotnet / mf / RCS / muinc.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.30.36;      author jromine; state Exp;
10 branches;
11 next    1.1;
12
13 1.1
14 date    93.08.25.17.30.10;      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 @/* muinc.c - mmdf to uucp 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 /* \f */
47
48 main (argc, argv)
49 int     argc;
50 char   *argv[];
51 {
52     int     fd,
53             tmp;
54     struct stat st1,
55                 st2;
56
57     mts_init (*argv);
58     sprintf (mmdfbox, "%s/%s", MAILDIR, MAILFIL);
59     if (stat (mmdfbox, &st1) == NOTOK || st1.st_size == 0L)
60         exit (0);
61     if ((mmdf = lkopen (mmdfbox, 0)) == NOTOK)
62         die ("unable to lock and open %s", mmdfbox);
63     tmp = tmp_open (&fd);
64
65     switch (fd = mmdf2uucp (mmdf, fd, FALSE)) {
66         case MFOK: 
67             break;
68
69         case MFPRM: 
70             die ("internal error while filtering MMDF mail");
71
72         case MFSIO: 
73             die ("no free file pointers -- you lose");
74
75         case MFERR: 
76             die ("i/o error while filtering MMDF mail");
77
78         case MFROM: 
79         case MFHDR: 
80         case MFTXT: 
81             fprintf (stderr, "MMDF mailbox in bad format, patched...\n");
82             break;
83     }
84
85     sprintf (uucpbox, "%s/%s", UUCPDIR, UUCPFIL);
86     uucp = mbx_open (uucpbox);
87     mbx_copy (tmp, uucp);
88     close (tmp);
89     lkclose (uucp, uucpbox), uucp = NOTOK;
90
91     if (stat (mmdfbox, &st2) != NOTOK && st1.st_mtime != st2.st_mtime)
92         fprintf (stderr, "MMDF mailbox has been updated... (%s)\n",
93                 "so it won't be zero'd");
94     else
95         if ((fd = creat (mmdfbox, st1.st_mode & ~S_IFMT)) != NOTOK)
96             close (fd);
97         else
98             fprintf (stderr, "unable to zero MMDF mailbox\n");
99     lkclose (mmdf, mmdfbox), mmdf = NOTOK;
100
101     exit (0);
102 }
103
104 /* \f */
105
106 static int  mbx_open (file)
107 char   *file;
108 {
109     int     count,
110             fd;
111     extern int  errno;
112
113     for (count = 2; count > 0; count--)
114         if ((fd = lkopen (file, 1)) == NOTOK)
115             switch (errno) {
116                 case ENOENT: 
117                     mbx_create (file);
118                     break;
119                 case ETXTBSY: 
120                     sleep (5);
121                     break;
122                 default: 
123                     goto openerr;
124             }
125
126     if (fd == NOTOK) {
127 openerr: 
128         if (errno == ETXTBSY)
129             die ("your UUCP mailbox '%s' is busy", file);
130         else
131             die ("unable to open UUCP mailbox '%s'", file);
132     }
133
134     lseek (fd, (off_t)0, 2);
135
136     return fd;
137 }
138
139 /* \f */
140
141 static  mbx_create (file)
142 char   *file;
143 {
144     int     fd;
145
146     if ((fd = creat (file, MBXMODE)) == NOTOK)
147         die ("unable to create UUCP mailbox '%s'", file);
148
149     close (fd);
150 }
151
152 /* \f */
153
154 static  mbx_copy (in, out)
155 int     in,
156         out;
157 {
158     int     i;
159     char    buffer[BUFSIZ];
160
161     lseek (in, (off_t)0, 0);
162
163     while ((i = read (in, buffer, sizeof buffer)) > 0)
164         if (write (out, buffer, i) != i)
165             die ("error writing UUCP mailbox");
166     if (i < 0)
167         die ("error reading temporary file");
168 }
169
170 /* \f */
171
172 static int  tmp_open (mbx_fd)
173 int    *mbx_fd;
174 {
175     int     fd;
176     char    tmpfil[LINESIZ];
177
178     strcpy (tmpfil, "/tmp/muincXXXXXX");
179     unlink (mktemp (tmpfil));
180     if ((fd = creat (tmpfil, TMPMODE)) == NOTOK)
181         die ("unable to create temporary file '%s'", tmpfil);
182     close (fd);
183
184     if ((fd = open (tmpfil, 2)) == NOTOK)
185         die ("unable to create temporary file '%s'", tmpfil);
186     unlink (tmpfil);
187
188     if ((*mbx_fd = dup (fd)) == NOTOK)
189         die ("unable to duplicate fd for temporary file '%s'", tmpfil);
190
191     return fd;
192 }
193
194 /* \f */
195
196 static  die (fmt, a, b, c, d)
197 char   *fmt,
198        *a,
199        *b,
200        *c,
201        *d;
202 {
203     lkclose (mmdf, mmdfbox), mmdf = NOTOK;
204     lkclose (uucp, uucpbox), uucp = NOTOK;
205
206     fflush (stdout);
207     fprintf (stderr, fmt, a, b, c, d);
208     putc ('\n', stderr);
209
210     exit (1);
211 }
212 @
213
214
215 1.1
216 log
217 @Initial revision
218 @
219 text
220 @d2 3
221 d107 1
222 a107 1
223     lseek (fd, 0L, 2);
224 d134 1
225 a134 1
226     lseek (in, 0L, 0);
227 @