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