Guide to the inpfuncs.c file

The inpfuncs.c file contains the getans() and more_to_do() functions.

Because these functions are used in several Informix® ESQL/C demonstration programs, they are in a separate file and included in the appropriate demonstration source files.

=======================================================================
1. /* The inpfuncs.c file contains functions useful in character-based
2.    input for a C program.
3.  */
4. #include <ctype.h>
5. #ifndef LCASE
6. #define LCASE(c) (isupper(c) ? tolower(c) : (c))
7. #endif
8. /* 
9.     Accepts user input, up to 'len' number of characters and returns 
10     it in 'ans'
11.  */
12. #define BUFSIZE 512
13. getans(ans, len)
14. char *ans;
15. mint len;
16. {
17.       char buf[BUFSIZE + 1];
18.       mint c, n = 0;
19.       while((c = getchar()) != ';' && n < BUFSIZE)
20.          buf[n++] = c;
21.      buf[n] = '\0';
22.      if(n > 1 && n >= len)
23.          {
24.          printf("Input exceeds maximum length");
25.          return 0;
26.          }
27.       if(len <= 1)
28.          *ans = buf[0];
29.       else
30.          strnpy(ans, buf, len);
31.       return 1;
32. }
=======================================================================

Lines 1 - 7

Line 4 includes the UNIX ctype.h header file. This header file provides the definitions of the islower() and tolower() macros used in the definition of the LCASE() macro (defined on line 6). The program only defines the LCASE macro if it has not yet been defined in the program.

Lines 8 - 32

The BUFSIZE constant (line 12) defines the size of the character buffer used in the getans() function. Lines 13 - 32 constitute the getans() function. The getans() function uses the getchar() standard library function to accept input from the user. Lines 14 and 15 define the arguments for getans(), the address of the buffer (ans) where it copies the input, and the maximum number of characters (len) that the calling function expects. Line 17 defines buf[], an input buffer array. The int variable c (line 18) receives the character that getchar() returned. The second integer defined on line 18, n, is used to subscript the buf[] input buffer.

Line 19 calls getchar() to receive input from the user until a \n newline character is encountered or until the maximum input is received; that is, n is not less than BUFFSZ. Line 20 moves the input character c into the current position in buf[]. Line 21 places a null terminator at the end of the input, buf[n].

Lines 22 - 26 check whether the number of characters received, n, is less than the number of characters expected, len. If not, line 24 displays a message to the user and line 25 returns 0 to the calling function to indicate that an error occurred. Line 27 checks whether one or more characters were entered. If the expected number of characters, len, is less than or equal to 1, line 28 moves only a single character to the address that the ans calling function gives. If only one character is expected, getans() does not append a null terminator to the input. If the length of the input is greater than 1, line 30 copies the input of the user to the address that the calling function (ans) supplies. Line 31 returns 1 to the calling function to indicate successful completion.

=======================================================================
33. /*
34.  *  Ask user if there is more to do
35.  */
36. more_to_do()
37. {
38.    char ans;
39.       do
40.          {
41.          printf("\n**** More? (y/n) ");
42.          getans(&ans, 1);
43.          } while((ans = LCASE(ans)) != 'y' && ans != 'n');
44.       return (ans == 'n') ? 0 : 1;
45. }
=======================================================================

Lines 33 - 45

The more_to_do() function displays "More? (y/n)..." to ask whether the user wants to continue program execution. The more_to_do() function does not have any input arguments. Line 38 defines a one-character field, ans, to receive the response from the user. The condition expressed on line 43 causes the question to be displayed again until the user answers y (yes) or n (no). The LCASE macro converts the answer of the user to lowercase letters for the comparison. Line 42 calls getans() to accept the input from the user. After the user answers yes or no, control passes to line 44, which returns 1 for yes and 0 for no to the calling function.