#include <string.h>

/****************************************************************/
/* 								*/
/* Define global variables					*/
/* 								*/
/****************************************************************/
	
int max[2];
int high[2];
int low[2];

char valid_resp[] = "VALID";
char invalid_resp[] = "INVALID";


/****************************************************************/
/* testinit :							*/
/* 								*/
/* Initialize global variables					*/
/****************************************************************/

void testinit()
{
	max[0]  = 104; max[1]  = 84;
	high[0] = 100; high[1] = 80;
	low[0]  = 97;  low[1]  = 77;
}

/****************************************************************/
/* asst1 :							*/
/* 								*/
/* Accepts temperatures in Celsius from M,			*/
/* outputs to XCALL routine in Fahrenheit			*/
/*								*/
/****************************************************************/

int asst1(int celsius)
{
	int fahren;
	fahren = ((celsius * 9) / 5) + 32;
	return(fahren);
}


/****************************************************************/
/* func1 :							*/
/* 								*/
/* Input:							*/
/*    p1 - selector for temperature ranges			*/
/*    p2 - temperature in degrees Fahrenheit			*/
/*    p3 - length of output buffer				*/
/*    p4 - pointer to output buffer				*/
/* Output:							*/
/*    return value - pointer to string 'VALID' or 'INVALID'	*/
/*    p4 buffer - characterization of input temperature 	*/
/*                compared to the selected ranges		*/
/****************************************************************/

char *func1(short p1, int p2, int p3, char *p4)
{
	char *status;
	int len;

	if ((p1 < 1) || (p1 > 2)) {	/* validate range selector */
	    *p4 = '\0';			/* returned string must be valid */
	    return invalid_resp;	/* return error indication */
	}

	p1 -= 1;			/* adjust for subscript use */
	
	if (p2 > max[p1])		/* check input against range */
	    status = "CRITICAL";
	else if (p2 > high[p1])
	    status = "HIGH";
	else if (p2 < low[p1])
	    status = "LOW";
	else
	    status = "NORMAL";

	len = strlen(status);
	if ((len + 1) < p3)
	    strcpy(p4, status);		/* status fits in buffer */
	else {
	    strncpy(p4, status, p3-1);	/* truncate response */
	    *(p4 + p3 - 1) = '\0';	/* provide ending delimiter */
	}
	return valid_resp;		/* return successful indication */
}
