home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 4.ddi / C / APPROMPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-13  |  1.6 KB  |  67 lines

  1. /**
  2. *
  3. * Name        apprompt -- Get one-line response from standard input,
  4. *                displaying a prompt if appropriate.
  5. *
  6. * Synopsis    ercode = apprompt(pprompt,presp,resp_size);
  7. *
  8. *        int  ercode      0 if okay, 1 if end of file, 2 if error.
  9. *        char *pprompt      The prompt to be displayed if the
  10. *                  standard input channel is a console.
  11. *        char *presp      Pointer to character buffer in which
  12. *                  to put response.
  13. *        int  resp_size      Size of response buffer (including
  14. *                  newline ('\n'), if any, and trailing
  15. *                  NUL ('\0')).
  16. *
  17. * Description    This function returns one line of text from standard
  18. *        input.    If standard input is a console, APPROMPT first
  19. *        displays the prompt on the current console.
  20. *
  21. * Returns    ercode          0 if okay, 1 if end of file, 2 if error.
  22. *        *presp          Line from standard input, including
  23. *                  newline ('\n'), if any.
  24. *
  25. * Version    3.0 Copyright (C) Blaise Computing Inc. 1986
  26. *
  27. * Version    3.02 March 23, 1987
  28. *        Forced stdout and stderr to be flushed.
  29. *
  30. **/
  31.  
  32. #include <stdio.h>
  33.  
  34. #include <compiler.h>
  35. #include <bapplic.h>
  36.  
  37. #if MSC300
  38. #include <io.h>
  39. #else
  40. #include <stdlib.h>
  41. #endif
  42.  
  43. #define  OK        0
  44. #define  END_OF_FILE    1
  45. #define  ERROR        2
  46.  
  47. int apprompt(pprompt,presp,resp_size)
  48. char *pprompt,*presp;
  49. int  resp_size;
  50. {
  51.     if (isatty((int) fileno(stdin)))
  52.     {
  53.     fflush(stdout);
  54.     fputs(pprompt,stderr);          /* Display prompt.          */
  55.     fflush(stderr);
  56.     }
  57.  
  58.     if (fgets(presp,resp_size,stdin) == NULL)
  59.     {
  60.     if (feof(stdin))
  61.         return END_OF_FILE;
  62.     else
  63.         return ERROR;
  64.     }
  65.     return OK;
  66. }
  67.