home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mh / mh-6.8 / zotnet / mf / uminc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-14  |  4.3 KB  |  224 lines

  1. /* uminc.c - uucp to mmdf 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. long    lseek ();
  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 (uucpbox, "%s/%s", UUCPDIR, UUCPFIL);
  32.     if (stat (uucpbox, &st1) == NOTOK || st1.st_size == 0L)
  33.     exit (0);
  34.     if ((uucp = lkopen (uucpbox, 0)) == NOTOK)
  35.     die ("unable to lock and open %s", uucpbox);
  36.     tmp = tmp_open (&fd);
  37.  
  38.     switch (fd = uucp2mmdf (uucp, fd, FALSE)) {
  39.     case MFOK: 
  40.         break;
  41.  
  42.     case MFPRM: 
  43.         die ("internal error while filtering UUCP mail");
  44.  
  45.     case MFSIO: 
  46.         die ("no free file pointers -- you lose");
  47.  
  48.     case MFERR: 
  49.         die ("i/o error while filtering UUCP mail");
  50.  
  51.     case MFROM: 
  52.     case MFHDR: 
  53.     case MFTXT: 
  54.         fprintf (stderr, "UUCP mailbox in bad format, patched...\n");
  55.         break;
  56.     }
  57.  
  58.     sprintf (mmdfbox, "%s/%s", MAILDIR, MAILFIL);
  59.     mmdf = mbx_open (mmdfbox);
  60.     mbx_copy (tmp, mmdf);
  61.     close (tmp);
  62.     lkclose (mmdf, mmdfbox), mmdf = NOTOK;
  63.  
  64.     if (stat (uucpbox, &st2) != NOTOK && st1.st_mtime != st2.st_mtime)
  65.     fprintf (stderr, "UUCP mailbox has been updated... (%s)\n",
  66.         "so it won't be removed");
  67.     else
  68.     if (unlink (uucpbox) == NOTOK)
  69.         if ((fd = creat (uucpbox, st1.st_mode & ~S_IFMT)) != NOTOK)
  70.         close (fd);
  71.         else
  72.         fprintf (stderr, "unable to remove or zero UUCP mailbox\n");
  73.     lkclose (uucp, uucpbox), uucp = NOTOK;
  74.  
  75.     exit (0);
  76. }
  77.  
  78. /*   */
  79.  
  80. static int  mbx_open (file)
  81. char   *file;
  82. {
  83.     int     clear,
  84.             count,
  85.             fd;
  86.     extern int  errno;
  87.     struct stat stbuf;
  88.  
  89.     for (clear = FALSE, count = 2; count > 0; count--)
  90.     if ((fd = lkopen (file, 6)) == NOTOK)
  91.         switch (errno) {
  92.         case ENOENT: 
  93.             mbx_create (file);
  94.             clear++;
  95.             break;
  96.  
  97.         case ETXTBSY: 
  98.             sleep (5);
  99.             break;
  100.  
  101.         default: 
  102.             goto openerr;
  103.         }
  104.     else {
  105.         if (fstat (fd, &stbuf) == NOTOK)
  106.         die ("unable to stat MMDF mailbox '%s'", file);
  107.         clear = stbuf.st_size == 0L;
  108.         break;
  109.     }
  110.  
  111.     if (fd == NOTOK) {
  112. openerr: 
  113.     if (errno == ETXTBSY)
  114.         die ("your MMDF mailbox '%s' is busy", file);
  115.     else
  116.         die ("unable to open MMDF mailbox '%s'", file);
  117.     }
  118.     if (!clear)
  119.     mbx_chk (fd, file);
  120.  
  121.     return fd;
  122. }
  123.  
  124. /*   */
  125.  
  126. static  mbx_create (file)
  127. char   *file;
  128. {
  129.     int     fd;
  130.  
  131.     if ((fd = creat (file, MBXMODE)) == NOTOK)
  132.     die ("unable to create MMDF mailbox '%s'", file);
  133.  
  134.     close (fd);
  135. }
  136.  
  137.  
  138. static  mbx_chk (fd, file)
  139. int     fd;
  140. char   *file;
  141. {
  142.     int     count;
  143.     char    ldelim[20];
  144.  
  145.     count = strlen (mmdlm2);
  146.  
  147.     if (lseek (fd, (long) - count, 2) == (long) NOTOK
  148.         || read (fd, ldelim, count) != count)
  149.     die ("error reading MMDF mailbox '%s'", file);
  150.     ldelim[count] = NULL;
  151.  
  152.     if (strcmp (ldelim, mmdlm2)) {
  153.     fprintf (stderr,
  154.         "MMDF mailbox '%s' has bad delimiter, patching...\n",
  155.         file);
  156.     if (write (fd, mmdlm2, count) != count)
  157.         die ("error writing MMDF mailbox '%s'", file);
  158.     }
  159. }
  160.  
  161. /*   */
  162.  
  163. static  mbx_copy (in, out)
  164. int     in,
  165.         out;
  166. {
  167.     int     i;
  168.     char    buffer[BUFSIZ];
  169.  
  170.     lseek (in, 0L, 0);
  171.  
  172.     while ((i = read (in, buffer, sizeof buffer)) > 0)
  173.     if (write (out, buffer, i) != i)
  174.         die ("error writing MMDF mailbox");
  175.     if (i < 0)
  176.     die ("error reading temporary file");
  177.  
  178.     close (in);
  179.     close (out);
  180. }
  181.  
  182. /*   */
  183.  
  184. static int  tmp_open (mbx_fd)
  185. int    *mbx_fd;
  186. {
  187.     int     fd;
  188.     char    tmpfil[LINESIZ];
  189.  
  190.     strcpy (tmpfil, "/tmp/umincXXXXXX");
  191.     unlink (mktemp (tmpfil));
  192.     if ((fd = creat (tmpfil, TMPMODE)) == NOTOK)
  193.     die ("unable to create temporary file '%s'", tmpfil);
  194.     close (fd);
  195.  
  196.     if ((fd = open (tmpfil, 2)) == NOTOK)
  197.     die ("unable to create temporary file '%s'", tmpfil);
  198.     unlink (tmpfil);
  199.  
  200.     if ((*mbx_fd = dup (fd)) == NOTOK)
  201.     die ("unable to duplicate fd for temporary file '%s'", tmpfil);
  202.  
  203.     return fd;
  204. }
  205.  
  206. /*   */
  207.  
  208. static  die (fmt, a, b, c, d)
  209. char   *fmt,
  210.        *a,
  211.        *b,
  212.        *c,
  213.        *d;
  214. {
  215.     lkclose (mmdf, mmdfbox), mmdf = NOTOK;
  216.     lkclose (uucp, uucpbox), uucp = NOTOK;
  217.  
  218.     fflush (stdout);
  219.     fprintf (stderr, fmt, a, b, c, d);
  220.     putc ('\n', stderr);
  221.  
  222.     exit (1);
  223. }
  224.