home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / OS2OMMM.SRC / ARCHIVE.C next >
Text File  |  1989-06-01  |  9KB  |  321 lines

  1. /***************************************************************************/
  2. /***                                                                     ***/
  3. /***               oMMM - The Outbound Matrix Message Masher             ***/
  4. /***                      Copyright 1989 BS Software                     ***/
  5. /***                                                                     ***/
  6. /***                         FILENAME: ARCHIVE.C                         ***/
  7. /***                                                                     ***/
  8. /***                      Handles ARCMail type files                     ***/
  9. /***                                                                     ***/
  10. /***                 Based on the original oMMM, a portion of             ***/
  11. /***               the Opus Computer-Based Conversation System             ***/
  12. /***                     Copyright 1986, Wynn Wagner III                 ***/
  13. /***                                                                     ***/
  14. /***************************************************************************/
  15. /***                                                                     ***/
  16. /***                    Tabs set at every 4th column                     ***/
  17. /***                                                                     ***/
  18. /***************************************************************************/
  19.  
  20. /*
  21.     Polytron Version Control System Comments:
  22.  
  23.     The revision of this file is *** $Revision:   1.40    $ ***
  24.  
  25.     History of changes from 1.30 release version
  26.  
  27.     $Log:    C:/OMMM/PROJFILE/ARCHIVE.C_V  $
  28.  *
  29.  *      Rev 1.40BP 2 June 1989 23:22:00    Bill Andrus
  30.  * Public Release Version 1.40BP (OS/2 Protect Mode, Bound)
  31.  *
  32.  *      Rev 1.40     12 Feb 1989  4:55:14    Marshall Presnell
  33.  * Public Release Version 1.40
  34.  *
  35.  *      Rev 1.31     31 Jan 1989  0:57:24    Marshall Presnell
  36.  * oMMM 1.35 Beta Release Version
  37.  *
  38.  *      Rev 1.30     23 Jan 1989 17:54:10    Marshall Presnell
  39.  * Public Source Code Release - Version 1.30
  40.  
  41. */
  42.  
  43. /*--------------------------------------------------------------------------*/
  44. /* Include files                                                            */
  45. /*--------------------------------------------------------------------------*/
  46.  
  47. #include    "ommm.h"
  48.  
  49. #ifdef      MSC
  50. #    include <sys\types.h>
  51. #    include <memory.h>
  52. #endif
  53.  
  54. #include    <sys\stat.h>
  55. #include    <string.h>
  56.  
  57. #ifdef      ZTC
  58. #    include <stdlib.h>
  59. #    include <process.h>
  60. #    include <io.h>
  61. #endif
  62.  
  63. /*--------------------------------------------------------------------------*/
  64. /* Static function declarations                                             */
  65. /*--------------------------------------------------------------------------*/
  66.  
  67. static void get_archivename(char *filename, char *arcname);
  68.  
  69. /*--------------------------------------------------------------------------*/
  70. /* Static variable definitions                                                */
  71. /*--------------------------------------------------------------------------*/
  72.  
  73. static char *    extchars[]    =    { "SU","MO","TU","WE","TH","FR","SA",NULL };
  74. static char *    arca        = "ARCA";
  75. static char *    pkarc1        = "PKARC";
  76. static char *    pkarc2        = "-OCT";
  77. static char *    pkarc3        = "M";
  78. static char     templ2[80];
  79.  
  80. /*--------------------------------------------------------------------------*/
  81. /* External variable declarations                                            */
  82. /*--------------------------------------------------------------------------*/
  83.  
  84. extern NETADDRESS        ctlnet[];
  85.  
  86. #ifdef ZTC
  87.     extern volatile int errno;
  88. #else
  89.     extern int            errno;
  90. #endif
  91.  
  92. /*--------------------------------------------------------------------------*/
  93. /* Local constants                                                            */
  94. /*--------------------------------------------------------------------------*/
  95.  
  96. #ifdef ZTC
  97. #    define    stricmp     strcmpl
  98. #    define    P_WAIT        0
  99. #endif
  100.  
  101. /****************************************************************************/
  102.  
  103. /*--------------------------------------------------------------------------*/
  104. /* GET ARCHIVENAME                                            LOCAL FUNCTION    */
  105. /*--------------------------------------------------------------------------*/
  106.  
  107.  static void
  108. get_archivename(char * filename, char * arcname)
  109. {
  110.     struct stat status;
  111.     int         i;
  112.     int         j;
  113.  
  114.     memset(filename, 0, 30);
  115.     printf("\nArchive is to %d:%d/%d\n", dest.zone,dest.net, dest.node);
  116.  
  117.     /*
  118.      * determine which archive name was used last and delete any
  119.      * zero length arcs that might be laying about
  120.      */
  121.  
  122.     for (j = 0; extchars[j] != NULL; j++) { /* days of the week */
  123.         for (i = '0'; i <= '9'; i++) {  /* trailing digit */
  124.             sprintf(filename, "%04x%04x.%s%c",
  125.                 ctlnet[0].net - dest.net, ctlnet[0].node - dest.node,
  126.                 extchars[j], i);
  127.             sprintf(arcname, "%s%s", adjust_packet_path(dest.zone), filename);
  128.  
  129.             if (!stat(arcname, &status)) {
  130.                 if (status.st_size == 0L) {
  131.  
  132.                     printf("Deleting old truncated `%s'\n", arcname);
  133.  
  134.                     errno = 0;
  135.                     unlink(arcname);
  136.                     if (errno)
  137.                         perror("Can't delete");
  138.  
  139.                     i = strlen(arcname) - 1;
  140.                     if (arcname[i] == '0') {
  141.                         if (oldstyle) {
  142.                             sprintf(filename, "%04x%04x.MO1",
  143.                                 ctlnet[0].net - dest.net,
  144.                                 ctlnet[0].node - dest.node);
  145.                         } else {
  146.                             sprintf(filename, "%04x%04x.%s1",
  147.                                 ctlnet[0].net - dest.net,
  148.                                 ctlnet[0].node - dest.node,
  149.                                 (extchars[j + 1] != NULL) ? extchars[j + 1] : "SU");
  150.                         }
  151.                         sprintf(arcname, "%s%s", adjust_packet_path(dest.zone), filename);
  152.                     } else {
  153.                         arcname[i]++;
  154.                         if (arcname[i] > '9') {
  155.                             arcname[i] = '0';
  156.                             filename[strlen(filename) - 1] = '0';
  157.                         }
  158.                     }
  159.                     return;
  160.  
  161.                 }
  162.                 printf("Adding bundle to `%s'\n", arcname);
  163.                 return;
  164.             }
  165.         }
  166.     }
  167.  
  168.     sprintf(filename, "%04x%04x.MO1", ctlnet[0].net - dest.net, ctlnet[0].node - dest.node);
  169.     sprintf(arcname, "%s%s", adjust_packet_path(dest.zone), filename);
  170.  
  171.     printf("New archive `%s'\n", arcname);
  172. }
  173.  
  174. /*--------------------------------------------------------------------------*/
  175. /* ARCHIVE                                                                    */
  176. /*--------------------------------------------------------------------------*/
  177.  
  178.  void
  179. archive(char * parm)
  180. {
  181.     register int    i;
  182.     char            filename[80];
  183.     char            tstring[80];
  184.     char            arcname[80];
  185.     FILE *            flowfile;
  186.     FILE *            flow1;
  187.     int             newone;
  188.     static char *    flow_ext = "FLO";
  189.     struct stat     StatBuf;
  190.  
  191.     if (*parm == '\0')
  192.         return;
  193.     get_archivename(filename, arcname);
  194.     sprintf(templ2, "%s%04x%04x.FLO", adjust_packet_path(dest.zone), dest.net, dest.node);
  195.  
  196.     /*
  197.      * Here's the theory on how to do ArcAddHold style verbs:  They
  198.      * use another global - AddFlag.  If AddFlag is 0, things work just as
  199.      * they do now.  However, if AddFlag is non-zero, then at this point,
  200.      * we test for the existance of the archive file.  Later on, at the
  201.      * flow file stuff, if the archive existed and AddFlag is set, we
  202.      * DO NOT mess around with the existing flow file.
  203.      *
  204.      * This works on the blatant assumption that zero length files do not
  205.      * exist
  206.      *
  207.      * I think that's a valid assumption, I hope someone will tell me if
  208.      * not.
  209.      *
  210.      * It's also DOS (probably msc) dependent.
  211.      * Oh, AddFlag is set and cleared in do_route()
  212.      * There will be problems if you are using old style naming (i.e.,TU).
  213.      *
  214.      * The "Add-Verbs" may be disabled by undefining ADD_STUFF in oMMM.H.
  215.      *
  216.      */
  217.  
  218. #ifdef ADD_STUFF
  219.     if (AddFlag)
  220.         AddFlag = !stat(arcname, &StatBuf);
  221. #endif
  222.  
  223.     switch ('z' == ArcType) {
  224.     case 0:
  225.         if (pkarc) {
  226.             i = spawnlp(P_WAIT, pkarc1, pkarc1, pkarc2, pkarc3, arcname, parm, NULL);
  227.         } else {
  228.             i = spawnlp(P_WAIT, arca, arca, arcname, parm, "/D", NULL);
  229.         }
  230.         break;
  231.     default:
  232.         i = spawnlp(P_WAIT, "zoo", "zoo", "-move", arcname, parm, NULL);
  233.         break;
  234.     }
  235.     if (i)
  236.         printf("\nARC err#%d\n", i);
  237.  
  238. #ifdef ADD_STUFF
  239.     switch (AddFlag) {
  240.     case 0:
  241. #endif
  242.         *flow_ext = (char) mode;
  243.         strncpy(tstring, parm, sizeof(tstring) - 1);
  244.         strmfe(parm, templ2, flow_ext);
  245.  
  246.         errno = 0;
  247.         flowfile = fopen(parm, "r+t");
  248.         newone = 0;
  249.         if (errno) {
  250.             errno = 0;
  251.             printf("New FLO file `%s' for %d:%d/%d\n", parm, dest.zone,dest.net, dest.node);
  252.             if ((flowfile = fopen(parm, "wt")) == NULL)
  253.                 perror("Can't create FLO file");
  254.             newone = 1;
  255.         }
  256.         errno = 0;
  257.         fseek(flowfile, 0L, SEEK_END);
  258.         if (errno)
  259.             perror("SEEKEND");
  260.  
  261.         strmfe(tstring, templ2, "FLO");
  262.  
  263.         errno = 0;
  264.         flow1 = fopen(tstring, "rt");
  265.  
  266.         if (flow1 != NULL) {
  267.             while (fgets(filename, sizeof filename, flow1))
  268.                 fputs(filename, flowfile);
  269.             fclose(flow1);
  270.             flow1 = NULL;
  271.             unlink(tstring);
  272.             newone = 0;
  273.         }
  274.  
  275.         fclose(flowfile);
  276.         flowfile = fopen(parm, "r+t");
  277.  
  278.         if (!newone) {
  279.             while (fgets(filename, sizeof filename, flowfile)) {
  280.                 for (i = 0; filename[i]; i++) {
  281.                     if (filename[i] <= ' ')
  282.                         filename[i] = 0;
  283.                 }
  284.                 if (!stricmp(filename + 1, arcname)) {
  285.                     printf("File already listed for transmission.  \n");
  286.                     fclose(flowfile);
  287.                     flowfile = NULL;
  288.                     parm[0] = 0;
  289.                     return;
  290.                 }
  291.             }
  292.         }
  293.  
  294.         errno = 0;
  295.         rewind(flowfile);
  296.         if (errno)
  297.             perror("REWIND");
  298.         fseek(flowfile, 0L, SEEK_END);
  299.         if (errno)
  300.             perror("SEEKEND");
  301.         fprintf(flowfile, "#%s\n", arcname);
  302.         if (errno)
  303.             perror("PRINT");
  304.         fclose(flowfile);
  305.         flowfile = NULL;
  306.         if (errno)
  307.             perror("CLOSE");
  308. #ifdef ADD_STUFF
  309.         break;
  310.     default:
  311.         printf("Add mode for %d:%d/%d\n", dest.zone,dest.net, dest.node);
  312.         break;
  313.     }
  314. #endif
  315.     parm[0] = 0;
  316. }
  317.  
  318. /*--------------------------------------------------------------------------*/
  319. /*                                  END OF FILE                                */
  320. /*--------------------------------------------------------------------------*/
  321.