
PID No. 18413B
2 of 3
#include <stdio.h>
#include <hif.h>
#include <stdlib.h>
showmen() {
/**********************************************************************************/
/* The following subroutine will display a menu to standard output. The _write() */
/* HIF service was used rather than printf for the following reasons: */
/* - To show the usage of the _write() HIF service */
/* - When characters are sent to standard output using printf, the output is */
/* buffered and will not be displayed until a '\n' is used. Therefore, the */
/* "Enter your selection ->" line would not appear on standard output if */
/* printf were used because there is no '\n' character at the end of the */
/* string. */
/**********************************************************************************/
_write(1,"\nMenu:\n",7);
_write(1,"\t1) Selection #1\n",17);
_write(1,"\t2) Selection #2\n",17);
_write(1,"\t3) Exit\n",9);
_write(1,"\nEnter your selection -> ",25);
}
void main() {
/* Declare necessary variables */
int terminate=0, proc=0;
/* Create a 1-character buffer to hold input */
char *input=(char *) malloc(sizeof(char));
/* Set standard input to NBLOCK mode using the _ioctl() HIF service */
_ioctl(0,0x0010);
showmen();
/********************************************************************************/
/* Following is the main loop where processing occurs. The exit condition is */
/* set by selecting the appropriate menu item. */
/********************************************************************************/
while(!terminate) {
/******************************************************************************/
/* Because standard input has been set to NBLOCK mode using the _ioctl() HIF */
/* service, the _read() HIF service will return a negative number if there is */
/* no input available. If there is input available, _read() will return a non-*/
/* negative number and the character read will be contained in the input */
/* buffer. */
/******************************************************************************/
if(_read(0,input,1)>=0) {
/* React to the input received */
switch(input[0]) {
case '1':
printf("\n\nYou selected choice #1.\n");
showmen();
break;
case '2':
printf("\n\nYou selected choice #2.\n");
showmen();
break;
case '3':
/* Choice #3 is to exit, so set a flag to end the main loop */
terminate++;
break;
default:
printf("\n\nInvalid selection.\n");
showmen();
break;
}
}