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