#ifndef	lint
static char sccsid[] = "@(#)channel.c 1.4 88/12/21";
#endif

#include "cbratp.h"

#ifndef	WIRETAP
/*
** new_channel()
**
** Scans the channel array and returns the index of the first available slot
** Returns -1 if no slot found.
*/
new_channel()
{
    register int i;

    for (i = 0; i < NCHANNELS; i++)
	if (chan[i] == NULL)
	    return i;
    return -1;
}
#endif	/*WIRETAP*/

/*
** alloc_channel(c, type, arg)
**
** Initialises channel 'c'. Sets the type to be 'type' and its arguement
** to be 'arg'. If space for the channel has not been allocated it is
** malloc'd
*/
alloc_channel(c, type, arg)
    u_char	c;
    u_char	type;
    u_char	arg;
{
    if (chan[c] == NULL)
	chan[c] = (struct channel *)malloc(sizeof(struct channel));
    chan[c]->type = type;
    chan[c]->arg = arg;
    chan[c]->len = 0;
}

/*
** free_channel(c)
**
** Frees all memory associated with channel 'c' and resets the channel
** reference
*/
free_channel(c)
    u_char	c;
{
    if (chan[c]) {
	if (chan[c]->len > 0)
	    free((char *) chan[c]->buf);
	free((char *) chan[c]);
	chan[c] = NULL;
    }
}
