home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / OS2OMMM.SRC / MISC.C < prev    next >
Text File  |  1989-06-01  |  14KB  |  568 lines

  1. /***************************************************************************/
  2. /***                                                                     ***/
  3. /***               oMMM - The Outbound Matrix Message Masher             ***/
  4. /***                      Copyright 1989 BS Software                     ***/
  5. /***                                                                     ***/
  6. /***                            FILENAME: MISC.C                         ***/
  7. /***                                                                     ***/
  8. /***                         Miscellaneous routines                      ***/
  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/MISC.C_V  $
  28.  *
  29.  *      Rev 1.40BP 2 June 1989 23:18:00    Bill Andrus
  30.  * Public Release Version 1.40BP (OS/2 Protect Mode, Bound)
  31.  *
  32.  *      Rev 1.40     12 Feb 1989  4:55:24    Marshall Presnell
  33.  * Public Release Version 1.40
  34.  *
  35.  *      Rev 1.35     01 Feb 1989 15:20:10    Marshall Presnell
  36.  * Solves (hopefully) the year and misformatting problems permanently!
  37.  *
  38.  *      Rev 1.34     31 Jan 1989  0:57:34    Marshall Presnell
  39.  * oMMM 1.35 Beta Release Version
  40.  *
  41.  *      Rev 1.33     30 Jan 1989 21:40:30    Marshall Presnell
  42.  * Month and year in packet header now to FTSC specification
  43.  *
  44.  *      Rev 1.32     23 Jan 1989 18:00:06    Marshall Presnell
  45.  * Net number now being inserted into header
  46.  *
  47.  *      Rev 1.31     23 Jan 1989 17:57:56    Marshall Presnell
  48.  * Output line while scanning messages cleared of debris
  49.  *
  50.  *      Rev 1.30     23 Jan 1989 17:53:50    Marshall Presnell
  51.  * Public Source Code Release - Version 1.30
  52.  
  53. */
  54.  
  55. /*--------------------------------------------------------------------------*/
  56. /* Include files                                                            */
  57. /*--------------------------------------------------------------------------*/
  58.  
  59. #include    "ommm.h"
  60. #include    <errno.h>
  61.  
  62. #ifdef    MSC
  63. #    include <dos.h>
  64. #    include <stdlib.h>
  65. #    include <sys\types.h>
  66. #endif
  67.  
  68. #ifdef    IBMC
  69. #define INCL_DOSPROCESS
  70. #    include <os2.h>
  71. #endif
  72.  
  73. #include    <sys\stat.h>
  74. #include    <ctype.h>
  75. #include    <string.h>
  76. #include    <io.h>
  77. #include    <fcntl.h>
  78. #include    <time.h>
  79.  
  80. #ifdef    TURBO_C
  81. #    include <dir.h>
  82. #endif
  83.  
  84. #ifdef    ZTC
  85. #    include <dos.h>
  86. #    include <stdlib.h>
  87. #endif
  88.  
  89. /*--------------------------------------------------------------------------*/
  90. /* Static function declarations                                             */
  91. /*--------------------------------------------------------------------------*/
  92.  
  93. /*    ... NONE ...  */
  94.  
  95. /*--------------------------------------------------------------------------*/
  96. /* Static variable definitions                                                */
  97. /*--------------------------------------------------------------------------*/
  98.  
  99. static struct _pkthdr    packet_header;
  100. static struct _FILEFINDBUF InfoBuf;
  101.  
  102. /*--------------------------------------------------------------------------*/
  103. /* External variable declarations                                            */
  104. /*--------------------------------------------------------------------------*/
  105.  
  106. extern NETADDRESS        ctlnet[];
  107. extern PW_PTR            pw[];
  108. extern int                num_pw;
  109.  
  110. #ifdef    ZTC
  111.     extern volatile int errno;
  112. #else
  113.     extern int            errno;
  114. #endif
  115.  
  116. /*--------------------------------------------------------------------------*/
  117. /* Locally defined globals                                                    */
  118. /*--------------------------------------------------------------------------*/
  119.  
  120. int                     num_addrs;
  121. HDIR                    hDir;
  122. USHORT                    cSearch;
  123. USHORT                    usAttrib;
  124.  
  125. /*--------------------------------------------------------------------------*/
  126. /* Local constants                                                            */
  127. /*--------------------------------------------------------------------------*/
  128.  
  129. #ifdef    IBMC
  130. #    define    FILENAMELEN 13
  131. #endif
  132. #ifdef    ZTC
  133.     int access(char *fn, int mode);
  134. #    define    O_BINARY  0
  135. #    define    ENMFILE   18
  136. #endif
  137.  
  138. /****************************************************************************/
  139.  
  140.  
  141. /*--------------------------------------------------------------------------*/
  142. /* OPEN OUTFILE                                                             */
  143. /*--------------------------------------------------------------------------*/
  144.  
  145.  int
  146. open_outfile(int flagchar, unsigned int tonet, unsigned int tonode, unsigned int tozone, unsigned int verbose)
  147. {
  148.     int     i;
  149.  
  150.     /*--------------------------------------------------------------*/
  151.     /* Open an OUT file                                             */
  152.     /*--------------------------------------------------------------*/
  153.  
  154.     if (outfile)
  155.         fclose(outfile);
  156.  
  157.     if (verbose) {
  158.         printf(" ... To: %d:%d/%d",tozone,tonet,tonode);
  159.         switch (flagchar) {
  160.         case 'O':
  161.             printf(" (NORM)");
  162.             break;
  163.         case 'H':
  164.             printf(" (HOLD)");
  165.             break;
  166.         case 'C':
  167.             printf(" (CM)");
  168.             break;
  169.         }
  170.     }
  171.  
  172.     sprintf(template, "%s%04x%04x.%cUT", adjust_packet_path(tozone), tonet, tonode, flagchar);
  173.  
  174.     if (access(template,0) == 0) {
  175.         outfile = fopen(template,"r+b");
  176.         fseek(outfile, -2L, SEEK_END);
  177.         if (verbose)
  178.             printf(" (APPEND)");
  179.         return (0);
  180.     }
  181.  
  182.     if ((outfile = fopen(template,"w+b")) == NULL) {
  183.         printf("\nCan't create OUT file\n");
  184.         exit(1);
  185.     }
  186.  
  187.     printf("          ");
  188.  
  189.     packet_header.orig_net = OURNET;
  190.     packet_header.orig_node = OURNODE;
  191.     packet_header.dest_node = tonode;
  192.     packet_header.dest_net = tonet;
  193.     dosdate(&packet_header.month, &packet_header.day, &packet_header.year, &i);
  194.     packet_header.year -= 1900;
  195.     dostime(&packet_header.hour, &packet_header.minute, &packet_header.second);
  196.     packet_header.rate = 0;
  197.     packet_header.ver = 2;
  198.     packet_header.product = isOPUS;
  199.  
  200.     for (i = 0; i < num_pw; i++) {
  201.         /* Find out if we have a password for this guy */
  202.         if ((pw[i]->net == tonet) && (pw[i]->node == tonode) && (pw[i]->zone == tozone)) {
  203.             strncpy(packet_header.password, pw[i]->password, 8);
  204.             break;
  205.         }
  206.     }
  207.  
  208.     fwrite((char *) &packet_header, sizeof(struct _pkthdr), 1, outfile);
  209.     return (1);
  210. }
  211.  
  212. /*--------------------------------------------------------------------------*/
  213. /* FANCY STR                                                                */
  214. /*--------------------------------------------------------------------------*/
  215.  
  216.  char *
  217. fancy_str(char * value)
  218. {
  219.     register char * sptr;
  220.     char            lower = 0;
  221.  
  222.     sptr = value;
  223.  
  224.     if (sptr) {     /* don't touch any NULL pointers */
  225.         while (*sptr) {
  226.             if (lower)
  227.                 *sptr = tolower(*sptr);
  228.             else
  229.                 *sptr = toupper(*sptr);
  230.             lower = (isalnum(*sptr++));
  231.         }
  232.     }
  233.     return (value);
  234. }
  235.  
  236. /*--------------------------------------------------------------------------*/
  237. /* ADD BACKSLASH                                                            */
  238. /*--------------------------------------------------------------------------*/
  239.  
  240.  void
  241. add_backslash(char * path)
  242. {
  243.     register int i;
  244.  
  245.     i = strlen(path) - 1;
  246.     if ((path[0] > ' ') && (path[i] != '\\'))
  247.         path[++i] = '\\';
  248.     path[i + 1] = '\0';
  249.  
  250.     fancy_str(path);
  251. }
  252.  
  253. /*--------------------------------------------------------------------------*/
  254. /* CONVERT DATE                                                             */
  255. /*--------------------------------------------------------------------------*/
  256.  
  257.  void
  258. convert_date(char *str)
  259. {
  260.     int     mo;
  261.     int     d;
  262.     int     y;
  263.     int     h;
  264.     int     m;
  265.     int     s;
  266.  
  267.     if (((str[19] & (char) 0xff) == (char) 0xff) && (sscanf(str, "%d-%d-%d %d:%d:%d", &mo, &d, &y, &h, &m, &s) == 6)) {
  268.         sprintf(str, "%02d %s %02d  %02d:%02d:%02d", d, _months[mo - 1], y, h, m, s);
  269.     }
  270. }
  271.  
  272. /*--------------------------------------------------------------------------*/
  273. /* EXTRACT PARTS                                                            */
  274. /*--------------------------------------------------------------------------*/
  275.  
  276.  void
  277. extract_parts(char * file_path, char * file_name, char * flist, char * file_attached)
  278. {
  279.     char *    p;
  280.     char *    s;
  281.     char *    t;
  282.  
  283.     strcpy(file_path, file_attached);
  284.     p = strrchr(file_path, '\\');
  285.     if (p == NULL)
  286.         p = strrchr(file_path, ':');
  287.     if (p == NULL) {
  288.         strcat(flist, file_attached);
  289.         strcat(flist, " ");
  290.         file_path[0] = 0;
  291.         strcpy(file_name, file_attached);
  292.         return;
  293.     };
  294.     p++;
  295.     s = p;
  296.     t = file_name;
  297.  
  298.     while (*s) {
  299.         if (isspace(*s))
  300.             break;
  301.         *t++ = *s++;
  302.     }
  303.     *t = 0;
  304.  
  305.     strcat(flist, p);
  306.     strcat(flist, " ");
  307.     *p = 0;
  308. }
  309.  
  310. /*--------------------------------------------------------------------------*/
  311. /* STRMFE                                                                    */
  312. /*--------------------------------------------------------------------------*/
  313.  
  314.  int
  315. strmfe(char * s1, char * s2, char * s3)
  316. {
  317.     char   *q;
  318.  
  319.     strcpy(s1, s2);
  320.     q = s1 + strlen(s1);
  321.     while ((q >= s1) && (*q != '.') && (*q != '/') && (*q != '\\') && (*q != ':'))
  322.         --q;
  323.     if (q < s1)
  324.         q = s1;
  325.     if (*q != '.')
  326.         q = s1 + strlen(s1);
  327.     *q++ = '.';
  328.     strcpy(q, s3);
  329.     return (0);
  330. }
  331.  
  332. /*--------------------------------------------------------------------------*/
  333. /* STPBLK                                                                    */
  334. /*--------------------------------------------------------------------------*/
  335.  
  336.  char *
  337. stpblk(char * s)
  338. {
  339.     while ((*s) && isspace(*s))
  340.         ++s;
  341.     return (s);
  342. }
  343.  
  344. /*--------------------------------------------------------------------------*/
  345. /* GETNET                                                                    */
  346. /*--------------------------------------------------------------------------*/
  347.  
  348.  void
  349. getnet(char * prm_name)
  350. {
  351.     FILE *    fh;
  352.     int     i;
  353.     int     ver;
  354.     struct {
  355.         int node;
  356.         int net;
  357.     }        part_addr;
  358.  
  359.     struct {
  360.         int zone;
  361.         int net;
  362.         int node;
  363.         int point;
  364.     }        full_addr;
  365.  
  366.  
  367.     errno = 0;
  368.     if ((fh = fopen(prm_name, "rb")) == NULL) {
  369.         printf("Could not open '%s' - exiting\n", prm_name);
  370.         exit(1);
  371.     }
  372.  
  373.     ver = fgetc(fh);
  374.  
  375.     if (ver == EOF) {
  376.         printf("Could not read from '%s' - exiting\n", prm_name);
  377.         exit(1);
  378.     }
  379.  
  380.     if (ver < 14) {
  381.         /* Could be wrong control file version number. */
  382.         /* Less than 14 won't work.  Over 14 MAY work. */
  383.         printf("Wrong control file version found in '%s' - exiting\n", prm_name);
  384.         exit(1);
  385.     }
  386.  
  387.     if (ver < 16) {
  388.         fseek(fh,301l,SEEK_SET);
  389.         for (i = 0; i < ALIAS_CNT; i++) {
  390.             fread(&part_addr,sizeof(part_addr),1,fh);
  391.             ctlnet[i].zone = our_zone;
  392.             ctlnet[i].net = part_addr.net;
  393.             ctlnet[i].node = part_addr.node;
  394.             if (ctlnet[i].net == 0)
  395.                 break;
  396.         }
  397.     } else {
  398.         fgetc(fh);
  399.         for (i = 0; i < ALIAS_CNT; i++) {
  400.             fread(&full_addr,sizeof(full_addr),1,fh);
  401.             ctlnet[i].zone = full_addr.zone;
  402.             ctlnet[i].net = full_addr.net;
  403.             ctlnet[i].node = full_addr.node;
  404.             if (ctlnet[i].net == 0)
  405.                 break;
  406.         }
  407.     }
  408.  
  409.     num_addrs = i;
  410.     fclose(fh);
  411. }
  412.  
  413. /*--------------------------------------------------------------------------*/
  414. /* DOSTIME                                                                    */
  415. /*--------------------------------------------------------------------------*/
  416.  
  417.  void
  418. dostime(int * hr, int * min, int * sec)
  419. {
  420.     time_t    timer;
  421.     struct tm *tms;
  422.  
  423.     time(&timer);
  424.     tms = localtime(&timer);
  425.  
  426.     *hr = tms->tm_hour;
  427.     *min = tms->tm_min;
  428.     *sec = tms->tm_sec;
  429. }
  430.  
  431. /*--------------------------------------------------------------------------*/
  432. /* DOSDATE                                                                    */
  433. /*--------------------------------------------------------------------------*/
  434.  
  435.  void
  436. dosdate(int * month, int * mday, int * year, int * wday)
  437. {
  438.     time_t    timer;
  439.     struct tm *tms;
  440.  
  441.     time(&timer);
  442.     tms = localtime(&timer);
  443.  
  444.     *month = tms->tm_mon;
  445.     *mday = tms->tm_mday;
  446.     *year = tms->tm_year + 1900;
  447.     *wday = tms->tm_wday;
  448. }
  449.  
  450. /*--------------------------------------------------------------------------*/
  451. /* DIR_FINDFIRST                                     FOR PORTABILITY        */
  452. /*--------------------------------------------------------------------------*/
  453.  
  454.  int
  455. dir_findfirst(char * filename, int attribute, struct _dta * dta)
  456. {
  457. #    ifdef    IBMC
  458.     hDir     = 0xffff;
  459.     usAttrib = 0;
  460.     cSearch  = 1;
  461.  
  462.     if (DosFindFirst( filename
  463.                     , &hDir
  464.                     , usAttrib
  465.                     , &InfoBuf
  466.                     , (USHORT)( sizeof(InfoBuf) * cSearch )
  467.                     , &cSearch
  468.                     , (ULONG)NULL ) != 0 )
  469.     {
  470.         DosFindClose( hDir );
  471.         errno = ENOENT;
  472.         return (-1);
  473.     } else {
  474.         dta->size       = InfoBuf.cbFile;
  475.         strcpy( dta->name, InfoBuf.achName);
  476.         errno = 0;
  477.         return (0);
  478.     }
  479. #    else
  480.     union REGS    ir;
  481.     union REGS    or;
  482.  
  483.     ir.h.ah = 0x1a;
  484.     ir.x.dx = (unsigned int) dta;
  485.     intdos(&ir,&or);
  486.     ir.h.ah = 0x4e;
  487.     ir.x.cx = (unsigned int) attribute;
  488.     ir.x.dx = (unsigned int) filename;
  489.     intdos(&ir,&or);
  490.     if (or.x.cflag) {
  491.         errno = ENOENT;
  492.         return (-1);
  493.     } else {
  494.         errno = 0;
  495.         return (0);
  496.     }
  497. #    endif
  498. }
  499.  
  500. /*--------------------------------------------------------------------------*/
  501. /* DIR_FINDNEXT                                      FOR PORTABILITY        */
  502. /*--------------------------------------------------------------------------*/
  503.  
  504.  int
  505. dir_findnext(struct _dta * dta)
  506. {
  507. #    ifdef    IBMC
  508.  
  509.     if ((DosFindNext( hDir
  510.                     , &InfoBuf
  511.                     , (USHORT)(FILENAMELEN + 23)
  512.                     , &cSearch)
  513.                     ) || (cSearch != 1))
  514.     {
  515.         DosFindClose( hDir );
  516.         errno = ENOENT;
  517.         return (-1);
  518.     } else {
  519.         dta->size       = InfoBuf.cbFile;
  520.         strcpy(  dta->name, InfoBuf.achName);
  521.         errno = 0;
  522.         return (0);
  523.     }
  524. #    else
  525.     union REGS ir, or;
  526.  
  527.     ir.h.ah = 0x1a;
  528.     ir.x.dx = (unsigned int) dta;
  529.     intdos(&ir,&or);
  530.  
  531.     ir.h.ah = 0x4f;
  532.     intdos(&ir,&or);
  533.  
  534.     if (or.x.cflag) {
  535.         errno = ENOENT;
  536.         return (-1);
  537.     } else {
  538.         errno = 0;
  539.         return (0);
  540.     }
  541. #    endif
  542. }
  543.  
  544. /*--------------------------------------------------------------------------*/
  545. /* GET PACKET NAME                                                            */
  546. /*--------------------------------------------------------------------------*/
  547.  
  548.  char *
  549. get_packet_name(int zone, int net, int node)
  550. {
  551.     static char     name[80];
  552.     static long int timer = 0;
  553.  
  554.     if (timer == 0)
  555.         timer = (time(NULL) << 5);
  556.     else
  557.         timer++;
  558.  
  559.     memset(name, 0, sizeof(name));
  560.     sprintf(name, "%s%08lx.pkt", adjust_packet_path(zone), timer);
  561.  
  562.     return (name);
  563. }
  564.  
  565. /*--------------------------------------------------------------------------*/
  566. /*                                  END OF FILE                                */
  567. /*--------------------------------------------------------------------------*/
  568.