home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / GETYN.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  80 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /************************************************************************/
  4. /*                                                                      */
  5. /*  GETYN.C                                                             */
  6. /*                                                                      */
  7. /*  Original Copyright 1993-94 by Robert B. Stout as part of            */
  8. /*  the MicroFirm Function Library (MFL)                                */
  9. /*                                                                      */
  10. /*  The user is granted a free limited license to use this source file  */
  11. /*  to create royalty-free programs, subject to the terms of the        */
  12. /*  license restrictions specified in the LICENSE.MFL file.             */
  13. /*                                                                      */
  14. /************************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <time.h>
  18. #include <ctype.h>
  19. #include "sniptype.h"
  20. #include "snipkbio.h"
  21.  
  22. /************************************************************************/
  23. /*                                                                      */
  24. /*  getYN()                                                             */
  25. /*                                                                      */
  26. /*  Prompt a user for a Yes/No response with default and timeout        */
  27. /*  features.                                                           */
  28. /*                                                                      */
  29. /*  Parameters: 1 - Prompt string                                       */
  30. /*              2 - Default response, 'Y' or 'N', '\0' for none         */
  31. /*              3 - Timeout before default assumed - 0 for none         */
  32. /*                                                                      */
  33. /*  Returns: True_ or False_                                            */
  34. /*                                                                      */
  35. /*  NOTE: All output is via stderr                                      */
  36. /*                                                                      */
  37. /************************************************************************/
  38.  
  39. Boolean_T getYN(char *prompt, int def_ch, unsigned timeout)
  40. {
  41.       int ch;
  42.       char *yn = " (y/n) ";
  43.       clock_t start, limit = (clock_t)0;
  44.  
  45.       fputs(prompt, stderr);
  46.       
  47.       def_ch = toupper(def_ch);
  48.       if ('Y' == def_ch)
  49.             yn[2] = def_ch;
  50.       else if ('N' == def_ch)
  51.             yn[4] = def_ch;
  52.       else  def_ch = '\0';
  53.  
  54.       fputs(yn, stderr);
  55.  
  56.       if (0 != def_ch && 0 < timeout)
  57.       {
  58.             start = clock();
  59.             limit = (clock_t)(CLK_TCK * timeout);
  60.       }
  61.       while ('Y' != ch && 'N' != ch)
  62.       {
  63.             while (!kbhit())
  64.             {
  65.                   if (limit && (limit <= (clock() - start)))
  66.                   {
  67.                         ch = def_ch;
  68.                         goto BYE;
  69.                   }
  70.             }
  71.             ch = toupper(getch());
  72.             if (def_ch && 'Y' != ch && 'N' != ch)
  73.                   ch = def_ch;
  74.       }
  75.  
  76. BYE:  fputc(ch, stderr);
  77.       fputc('\n', stderr);
  78.       return ('Y' == ch);
  79. }
  80.