/*
Copyright (C) Magna Carta Software, Inc. 1990. All Rights Reserved.
INITPORT.C -- Code to simplify COMM_PORT structure initialization.
*/
#define CCT_DEVELOPMENT
#if (defined(CCTW) || defined(_WINDOWS))
#include <windows.h>
#endif
#include <string.h>
#include <comm.h>
#include <stdlib.h>
#include <kermit.h>
#include <modem.h>
#if defined(__TURBOC__)
#include <alloc.h>
#elif !defined(__NDPC__)
#include <malloc.h>
#endif
/* ARRAY OF FAR POINTERS TO COMM_PORT STRUCTURES */
COMM_PORT FAR_ * mcport[MAX_PORTS] = {NULL};
/*
K_DEINIT -- Free the memory allocated in the course of Kermit transfers.
Return value:
0 -- Success;
EOF -- Kermit structure is NULL;
*/
short k_deinit(KERMIT_PARMS *k)
{
if (k == NULL) return (EOF);
if (k->txbuf != NULL) memfree(k->txbuf);
if (k->rxbuf != NULL) memfree(k->rxbuf);
if (k->x->k != NULL) k->x->k = NULL;
if (isdynamic(k)) memfree(k);
else memset(k, 0X0, sizeof(KERMIT_PARMS));
return (0);
}
/*
DEINIT_PORT -- Deinitialize a COMM_PORT. Close it, restore interrupts,
and free the memory associated with it.
Return Value:
*/
short deinit_port(COMM_PORT *p)
{
short ret = 0;
/* IF A MODEM HAS BEEN DYNAMICALLY ALLOCATED -- FREE IT */
if (((MODEM *)p->modem) != NULL && isdynamic(((MODEM *)p->modem))) {
memfree(p->modem);
p->modem = NULL;
}
/* IF A KERMIT_PARMS HAS BEEN DYNAMICALLY ALLOCATED -- FREE IT */
if (p->x != NULL && ((XFER *)p->x)->k != NULL && isdynamic((KERMIT_PARMS *)(((XFER *)p->x)->k)))
k_deinit(((XFER *)p->x)->k);
ret = c_close_(p);
if (p->pkt_fn != NULL) (*p->pkt_fn)(p->ppb);
return (ret);
}
/*
DEINIT_ALL_PORTS -- Check all ports for initialization, and deinitialize
if necessary. This is installed as an atexit() fn.
*/
#if defined(_MSC_VER)
void CDECL_ deinit_all_ports(void)
#else
void deinit_all_ports(void)
#endif
{
short i;
for (i=0; i < MAX_PORTS; i++) if (mcport[i] != NULL) deinit_port((COMM_PORT *) mcport[i]);
}