/* Copyright © 1995, 2001, Oracle Corporation. All Rights Reserved. */ /*********************************************************** * * * Below are five functions used in the sample C program * * SAMPLE.RC * * * * 'pad_string' copies a null terminated string to a * * specified target, with space padding or truncation. * * It is used by the 'read_string' function. * * * * 'read_int' reads decimal integer from standard input. * * * * 'read_float' reads a floating-point (real) number from * * standard input. * * * * 'read_string' reads a string from standard input, and * * returns a padded or truncated result. * * * * 'check_response' compares the user's input (source * * with a program literal (target). Returns 1 if * * source < target, 0 if source == target and 1 if * * source > target. * ***********************************************************/ #include #ifndef NULL #define NULL ((char*) 0) #endif #ifndef TRUE #define TRUE (1 == 1) #define FALSE (1 != 1) #endif #ifndef EOS #define EOS '\0' /* EOS is the null terminator */ #endif /******************************************************************* * pad_string: * * * * Takes a null-terminated string and either pads * * it out with spaces, or truncates it down to a specified * * size. It also makes sure that the resulting string is * * null-terminated. * * Function assumes that the target string is of * * length (size+1), because it places an EOS into the * * character following the padded result. * * * *******************************************************************/ void pad_string(source, target, size) char *source, *target; int size; { /* Copy no more than 'size' chars to the target string. */ strncpy(target, source, size); /* Make sure target is null terminated */ target[size] = EOS; /* If there was any padding at the end, change to spaces. */ for (source = &target[size-1]; *source == EOS; *source-- = ' '); } /******************************************************************* * * * read_int: * * * * Reads a decimal integer from stdin; * * keeps prompting until receives valid integer input. * * * *******************************************************************/ int read_int(prompt) char *prompt; { int len; int i; int matches; len = strlen(prompt); /* Extract the length once. */ while (TRUE) { /* If prompt specified, output it. */ if (len != 0) fputs(prompt, stdout); /* Get a decimal integer from stdin, converted */ /* (Note that any whitespace will terminate it) */ matches = scanf("%d", &i); /* Flush extraneous input */ while (getchar() != '\n'); if (matches == 1) /* If we matched on scanf, we got one. */ break; /* invalid, so print error message -- try again. */ fprintf(stderr, "Invalid input -- try again\n"); } return i; } /******************************************************************* * * * read_string: * * * * Reads a string from stdin; truncates or pads the string * * to size. * * * * The size specified includes space for the terminating * * null character. * * * * If valid string input, function returns zero. * * If EOF, function returns EOF. * * * *******************************************************************/ int read_string(prompt, target, size) char *prompt; char *target; int size; { static char buffer[255]; /* input buffer */ /* If prompt specified, output it. */ if (strlen(prompt) != 0) fputs(prompt, stdout); /* Get an input line. */ if (gets(buffer) == NULL) return EOF; else pad_string(buffer, target, size); return 0; /* Success */ } /******************************************************************* * check_response: * * * * Compares the user's input (source) with a program literal * * (target). * * * * Returns 1 if st * * * *******************************************************************/ check_response (source, target) char *source, *target; { int i; char *ptemp; int slen, tlen; slen = strlen (source); tlen = strlen (target); i = 1; /* Remove padding by adjusting slen. */ for (ptemp = &source[slen-1]; *ptemp == ' '; *ptemp--, slen--); /* Compare source and target strings. */ while (toupper (*source++) == toupper (*target++)) { if ((i == slen) && (i == tlen)) return(0); i++; } return (1); }