home *** CD-ROM | disk | FTP | other *** search
/ Mega CD-ROM 1 / megacd_rom_1.zip / megacd_rom_1 / FREEMACS / EMACS16A.ZIP / MOVEMAIL.C < prev    next >
C/C++ Source or Header  |  1989-07-25  |  2KB  |  113 lines

  1. /* movemail -- convert from Unix mailbox format to Freemacs RMAIL format. */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <dir.h>
  6. #include <stdarg.h>
  7. #include <string.h>
  8.  
  9. char mailbox[64];
  10.  
  11. /* convenience function to make up a filename */
  12. char *makefn(char *ext)
  13. {
  14.     static char fn[64];
  15.  
  16.     sprintf(fn, "%s.%s", mailbox, ext);
  17.     return fn;
  18. }
  19.  
  20. void error(char *format,...)
  21. {
  22.     va_list argptr;
  23.  
  24.     va_start(argptr, format);
  25.     vfprintf(stderr, format, argptr);
  26.     va_end(argptr);
  27.     exit(1);
  28. }
  29.  
  30.  
  31. void exit1(void)
  32. {
  33.     /* now delete our lock file */
  34.     rmdir(makefn("lck"));
  35. }
  36.  
  37.  
  38. main(int argc, char *argv[])
  39. {
  40.     int msgcount;
  41.     struct ffblk ffblk;
  42.     FILE *inf, *outf;
  43.  
  44.     if (argc < 2) error("usage: movemail mailbox");
  45.  
  46.     /* Prepare the mailbox name.  Strip off the extension so we have */
  47.     /*   a known state.  Make sure that the extension is really the */
  48.     /*   extension of the filename and not the extension of the subdir. */
  49.     strcpy(mailbox, argv[1]);
  50.     { char *cp = strrchr(mailbox,'.');
  51.         if (cp && !strchr(cp, '\\') && !strchr(cp, '/'))
  52.             *cp = '\0';
  53.     }
  54.  
  55.     /* Create our lock file.  Do a single retry after five seconds. */
  56.     if (mkdir(makefn("lck")) < 0) {
  57.         sleep(5);
  58.         if (mkdir(makefn("lck")) < 0) error("movemail: lock file in use");
  59.     }
  60.  
  61.     /* be sure to remove our lock file */
  62.     atexit(exit1);
  63.  
  64.     /* find the highest existing file number */
  65.     msgcount = 0;
  66.     if (!findfirst(makefn("*"),&ffblk,0)) do {
  67.         char *cp = strchr(ffblk.ff_name,'.');
  68.  
  69.         if (cp) {
  70.             int i = atoi(cp+1);
  71.  
  72.             msgcount = max(msgcount, i);
  73.         }
  74.     } while (!findnext(&ffblk));
  75.  
  76.     /* open our mailbox */
  77.     inf = fopen(makefn("txt"), "r");
  78.     if (inf) {
  79.  
  80.         /* start with no output file */
  81.         outf = NULL;
  82.  
  83.         /* for each message in the Unix mailbox, create an output file. */
  84.         for (;;) {
  85.             char inl[512];    /* our input buffer */
  86.             char path[64];    /* our output file name */
  87.  
  88.             if (!fgets(inl, sizeof(inl), inf))
  89.                 break;
  90.             if (!strncmp(inl, "From ", 5)) {
  91.                 if (outf && fclose(outf) < 0)
  92. diskfull:
  93.                     error("movemail: disk full in file %s", path);
  94.                 sprintf(path, "%s.%03u", mailbox, ++msgcount);
  95.                 outf = fopen(path, "w");
  96.                 if (!outf)
  97.                     goto diskfull;
  98.                 continue;
  99.             }
  100.             if (fputs(inl, outf)<0)
  101.                 goto diskfull;
  102.         }
  103.  
  104.         /* close the output file (if any) */
  105.         if (outf && fclose(outf) < 0)
  106.             goto diskfull;
  107.  
  108.         fclose(inf);
  109.     }
  110.     /* now set our mailbox to zero size */
  111.     fclose(fopen(makefn("txt"), "w"));
  112. }
  113.