home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************/
- /*** ***/
- /*** oMMM - The Outbound Matrix Message Masher ***/
- /*** Copyright 1989 BS Software ***/
- /*** ***/
- /*** FILENAME: MISC.C ***/
- /*** ***/
- /*** Miscellaneous routines ***/
- /*** ***/
- /*** Based on the original oMMM, a portion of ***/
- /*** the Opus Computer-Based Conversation System ***/
- /*** Copyright 1986, Wynn Wagner III ***/
- /*** ***/
- /***************************************************************************/
- /*** ***/
- /*** Tabs set at every 4th column ***/
- /*** ***/
- /***************************************************************************/
-
- /*
- Polytron Version Control System Comments:
-
- The revision of this file is *** $Revision: 1.40 $ ***
-
- History of changes from 1.30 release version
-
- $Log: C:/OMMM/PROJFILE/MISC.C_V $
- *
- * Rev 1.40BP 2 June 1989 23:18:00 Bill Andrus
- * Public Release Version 1.40BP (OS/2 Protect Mode, Bound)
- *
- * Rev 1.40 12 Feb 1989 4:55:24 Marshall Presnell
- * Public Release Version 1.40
- *
- * Rev 1.35 01 Feb 1989 15:20:10 Marshall Presnell
- * Solves (hopefully) the year and misformatting problems permanently!
- *
- * Rev 1.34 31 Jan 1989 0:57:34 Marshall Presnell
- * oMMM 1.35 Beta Release Version
- *
- * Rev 1.33 30 Jan 1989 21:40:30 Marshall Presnell
- * Month and year in packet header now to FTSC specification
- *
- * Rev 1.32 23 Jan 1989 18:00:06 Marshall Presnell
- * Net number now being inserted into header
- *
- * Rev 1.31 23 Jan 1989 17:57:56 Marshall Presnell
- * Output line while scanning messages cleared of debris
- *
- * Rev 1.30 23 Jan 1989 17:53:50 Marshall Presnell
- * Public Source Code Release - Version 1.30
-
- */
-
- /*--------------------------------------------------------------------------*/
- /* Include files */
- /*--------------------------------------------------------------------------*/
-
- #include "ommm.h"
- #include <errno.h>
-
- #ifdef MSC
- # include <dos.h>
- # include <stdlib.h>
- # include <sys\types.h>
- #endif
-
- #ifdef IBMC
- #define INCL_DOSPROCESS
- # include <os2.h>
- #endif
-
- #include <sys\stat.h>
- #include <ctype.h>
- #include <string.h>
- #include <io.h>
- #include <fcntl.h>
- #include <time.h>
-
- #ifdef TURBO_C
- # include <dir.h>
- #endif
-
- #ifdef ZTC
- # include <dos.h>
- # include <stdlib.h>
- #endif
-
- /*--------------------------------------------------------------------------*/
- /* Static function declarations */
- /*--------------------------------------------------------------------------*/
-
- /* ... NONE ... */
-
- /*--------------------------------------------------------------------------*/
- /* Static variable definitions */
- /*--------------------------------------------------------------------------*/
-
- static struct _pkthdr packet_header;
- static struct _FILEFINDBUF InfoBuf;
-
- /*--------------------------------------------------------------------------*/
- /* External variable declarations */
- /*--------------------------------------------------------------------------*/
-
- extern NETADDRESS ctlnet[];
- extern PW_PTR pw[];
- extern int num_pw;
-
- #ifdef ZTC
- extern volatile int errno;
- #else
- extern int errno;
- #endif
-
- /*--------------------------------------------------------------------------*/
- /* Locally defined globals */
- /*--------------------------------------------------------------------------*/
-
- int num_addrs;
- HDIR hDir;
- USHORT cSearch;
- USHORT usAttrib;
-
- /*--------------------------------------------------------------------------*/
- /* Local constants */
- /*--------------------------------------------------------------------------*/
-
- #ifdef IBMC
- # define FILENAMELEN 13
- #endif
- #ifdef ZTC
- int access(char *fn, int mode);
- # define O_BINARY 0
- # define ENMFILE 18
- #endif
-
- /****************************************************************************/
-
-
- /*--------------------------------------------------------------------------*/
- /* OPEN OUTFILE */
- /*--------------------------------------------------------------------------*/
-
- int
- open_outfile(int flagchar, unsigned int tonet, unsigned int tonode, unsigned int tozone, unsigned int verbose)
- {
- int i;
-
- /*--------------------------------------------------------------*/
- /* Open an OUT file */
- /*--------------------------------------------------------------*/
-
- if (outfile)
- fclose(outfile);
-
- if (verbose) {
- printf(" ... To: %d:%d/%d",tozone,tonet,tonode);
- switch (flagchar) {
- case 'O':
- printf(" (NORM)");
- break;
- case 'H':
- printf(" (HOLD)");
- break;
- case 'C':
- printf(" (CM)");
- break;
- }
- }
-
- sprintf(template, "%s%04x%04x.%cUT", adjust_packet_path(tozone), tonet, tonode, flagchar);
-
- if (access(template,0) == 0) {
- outfile = fopen(template,"r+b");
- fseek(outfile, -2L, SEEK_END);
- if (verbose)
- printf(" (APPEND)");
- return (0);
- }
-
- if ((outfile = fopen(template,"w+b")) == NULL) {
- printf("\nCan't create OUT file\n");
- exit(1);
- }
-
- printf(" ");
-
- packet_header.orig_net = OURNET;
- packet_header.orig_node = OURNODE;
- packet_header.dest_node = tonode;
- packet_header.dest_net = tonet;
- dosdate(&packet_header.month, &packet_header.day, &packet_header.year, &i);
- packet_header.year -= 1900;
- dostime(&packet_header.hour, &packet_header.minute, &packet_header.second);
- packet_header.rate = 0;
- packet_header.ver = 2;
- packet_header.product = isOPUS;
-
- for (i = 0; i < num_pw; i++) {
- /* Find out if we have a password for this guy */
- if ((pw[i]->net == tonet) && (pw[i]->node == tonode) && (pw[i]->zone == tozone)) {
- strncpy(packet_header.password, pw[i]->password, 8);
- break;
- }
- }
-
- fwrite((char *) &packet_header, sizeof(struct _pkthdr), 1, outfile);
- return (1);
- }
-
- /*--------------------------------------------------------------------------*/
- /* FANCY STR */
- /*--------------------------------------------------------------------------*/
-
- char *
- fancy_str(char * value)
- {
- register char * sptr;
- char lower = 0;
-
- sptr = value;
-
- if (sptr) { /* don't touch any NULL pointers */
- while (*sptr) {
- if (lower)
- *sptr = tolower(*sptr);
- else
- *sptr = toupper(*sptr);
- lower = (isalnum(*sptr++));
- }
- }
- return (value);
- }
-
- /*--------------------------------------------------------------------------*/
- /* ADD BACKSLASH */
- /*--------------------------------------------------------------------------*/
-
- void
- add_backslash(char * path)
- {
- register int i;
-
- i = strlen(path) - 1;
- if ((path[0] > ' ') && (path[i] != '\\'))
- path[++i] = '\\';
- path[i + 1] = '\0';
-
- fancy_str(path);
- }
-
- /*--------------------------------------------------------------------------*/
- /* CONVERT DATE */
- /*--------------------------------------------------------------------------*/
-
- void
- convert_date(char *str)
- {
- int mo;
- int d;
- int y;
- int h;
- int m;
- int s;
-
- if (((str[19] & (char) 0xff) == (char) 0xff) && (sscanf(str, "%d-%d-%d %d:%d:%d", &mo, &d, &y, &h, &m, &s) == 6)) {
- sprintf(str, "%02d %s %02d %02d:%02d:%02d", d, _months[mo - 1], y, h, m, s);
- }
- }
-
- /*--------------------------------------------------------------------------*/
- /* EXTRACT PARTS */
- /*--------------------------------------------------------------------------*/
-
- void
- extract_parts(char * file_path, char * file_name, char * flist, char * file_attached)
- {
- char * p;
- char * s;
- char * t;
-
- strcpy(file_path, file_attached);
- p = strrchr(file_path, '\\');
- if (p == NULL)
- p = strrchr(file_path, ':');
- if (p == NULL) {
- strcat(flist, file_attached);
- strcat(flist, " ");
- file_path[0] = 0;
- strcpy(file_name, file_attached);
- return;
- };
- p++;
- s = p;
- t = file_name;
-
- while (*s) {
- if (isspace(*s))
- break;
- *t++ = *s++;
- }
- *t = 0;
-
- strcat(flist, p);
- strcat(flist, " ");
- *p = 0;
- }
-
- /*--------------------------------------------------------------------------*/
- /* STRMFE */
- /*--------------------------------------------------------------------------*/
-
- int
- strmfe(char * s1, char * s2, char * s3)
- {
- char *q;
-
- strcpy(s1, s2);
- q = s1 + strlen(s1);
- while ((q >= s1) && (*q != '.') && (*q != '/') && (*q != '\\') && (*q != ':'))
- --q;
- if (q < s1)
- q = s1;
- if (*q != '.')
- q = s1 + strlen(s1);
- *q++ = '.';
- strcpy(q, s3);
- return (0);
- }
-
- /*--------------------------------------------------------------------------*/
- /* STPBLK */
- /*--------------------------------------------------------------------------*/
-
- char *
- stpblk(char * s)
- {
- while ((*s) && isspace(*s))
- ++s;
- return (s);
- }
-
- /*--------------------------------------------------------------------------*/
- /* GETNET */
- /*--------------------------------------------------------------------------*/
-
- void
- getnet(char * prm_name)
- {
- FILE * fh;
- int i;
- int ver;
- struct {
- int node;
- int net;
- } part_addr;
-
- struct {
- int zone;
- int net;
- int node;
- int point;
- } full_addr;
-
-
- errno = 0;
- if ((fh = fopen(prm_name, "rb")) == NULL) {
- printf("Could not open '%s' - exiting\n", prm_name);
- exit(1);
- }
-
- ver = fgetc(fh);
-
- if (ver == EOF) {
- printf("Could not read from '%s' - exiting\n", prm_name);
- exit(1);
- }
-
- if (ver < 14) {
- /* Could be wrong control file version number. */
- /* Less than 14 won't work. Over 14 MAY work. */
- printf("Wrong control file version found in '%s' - exiting\n", prm_name);
- exit(1);
- }
-
- if (ver < 16) {
- fseek(fh,301l,SEEK_SET);
- for (i = 0; i < ALIAS_CNT; i++) {
- fread(&part_addr,sizeof(part_addr),1,fh);
- ctlnet[i].zone = our_zone;
- ctlnet[i].net = part_addr.net;
- ctlnet[i].node = part_addr.node;
- if (ctlnet[i].net == 0)
- break;
- }
- } else {
- fgetc(fh);
- for (i = 0; i < ALIAS_CNT; i++) {
- fread(&full_addr,sizeof(full_addr),1,fh);
- ctlnet[i].zone = full_addr.zone;
- ctlnet[i].net = full_addr.net;
- ctlnet[i].node = full_addr.node;
- if (ctlnet[i].net == 0)
- break;
- }
- }
-
- num_addrs = i;
- fclose(fh);
- }
-
- /*--------------------------------------------------------------------------*/
- /* DOSTIME */
- /*--------------------------------------------------------------------------*/
-
- void
- dostime(int * hr, int * min, int * sec)
- {
- time_t timer;
- struct tm *tms;
-
- time(&timer);
- tms = localtime(&timer);
-
- *hr = tms->tm_hour;
- *min = tms->tm_min;
- *sec = tms->tm_sec;
- }
-
- /*--------------------------------------------------------------------------*/
- /* DOSDATE */
- /*--------------------------------------------------------------------------*/
-
- void
- dosdate(int * month, int * mday, int * year, int * wday)
- {
- time_t timer;
- struct tm *tms;
-
- time(&timer);
- tms = localtime(&timer);
-
- *month = tms->tm_mon;
- *mday = tms->tm_mday;
- *year = tms->tm_year + 1900;
- *wday = tms->tm_wday;
- }
-
- /*--------------------------------------------------------------------------*/
- /* DIR_FINDFIRST FOR PORTABILITY */
- /*--------------------------------------------------------------------------*/
-
- int
- dir_findfirst(char * filename, int attribute, struct _dta * dta)
- {
- # ifdef IBMC
- hDir = 0xffff;
- usAttrib = 0;
- cSearch = 1;
-
- if (DosFindFirst( filename
- , &hDir
- , usAttrib
- , &InfoBuf
- , (USHORT)( sizeof(InfoBuf) * cSearch )
- , &cSearch
- , (ULONG)NULL ) != 0 )
- {
- DosFindClose( hDir );
- errno = ENOENT;
- return (-1);
- } else {
- dta->size = InfoBuf.cbFile;
- strcpy( dta->name, InfoBuf.achName);
- errno = 0;
- return (0);
- }
- # else
- union REGS ir;
- union REGS or;
-
- ir.h.ah = 0x1a;
- ir.x.dx = (unsigned int) dta;
- intdos(&ir,&or);
- ir.h.ah = 0x4e;
- ir.x.cx = (unsigned int) attribute;
- ir.x.dx = (unsigned int) filename;
- intdos(&ir,&or);
- if (or.x.cflag) {
- errno = ENOENT;
- return (-1);
- } else {
- errno = 0;
- return (0);
- }
- # endif
- }
-
- /*--------------------------------------------------------------------------*/
- /* DIR_FINDNEXT FOR PORTABILITY */
- /*--------------------------------------------------------------------------*/
-
- int
- dir_findnext(struct _dta * dta)
- {
- # ifdef IBMC
-
- if ((DosFindNext( hDir
- , &InfoBuf
- , (USHORT)(FILENAMELEN + 23)
- , &cSearch)
- ) || (cSearch != 1))
- {
- DosFindClose( hDir );
- errno = ENOENT;
- return (-1);
- } else {
- dta->size = InfoBuf.cbFile;
- strcpy( dta->name, InfoBuf.achName);
- errno = 0;
- return (0);
- }
- # else
- union REGS ir, or;
-
- ir.h.ah = 0x1a;
- ir.x.dx = (unsigned int) dta;
- intdos(&ir,&or);
-
- ir.h.ah = 0x4f;
- intdos(&ir,&or);
-
- if (or.x.cflag) {
- errno = ENOENT;
- return (-1);
- } else {
- errno = 0;
- return (0);
- }
- # endif
- }
-
- /*--------------------------------------------------------------------------*/
- /* GET PACKET NAME */
- /*--------------------------------------------------------------------------*/
-
- char *
- get_packet_name(int zone, int net, int node)
- {
- static char name[80];
- static long int timer = 0;
-
- if (timer == 0)
- timer = (time(NULL) << 5);
- else
- timer++;
-
- memset(name, 0, sizeof(name));
- sprintf(name, "%s%08lx.pkt", adjust_packet_path(zone), timer);
-
- return (name);
- }
-
- /*--------------------------------------------------------------------------*/
- /* END OF FILE */
- /*--------------------------------------------------------------------------*/