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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  QUERY.C - Timed query with default for batch files
  5. **
  6. **  Usage: QUERY [Prompt_string] [X] [n]
  7. **         where: X = Default answer, `Y' or `N'
  8. **                n = Seconds to wait before using default answer
  9. **
  10. **  Note:  If any option is used, those preceding it must be specified.
  11. **         For example, to use the time out feature, both the prompt
  12. **         string and default answer must have previously been specified.
  13. **
  14. **  public domain by Bob Stout
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <time.h>
  19. #include <ctype.h>
  20. #include <stdlib.h>
  21. #include <conio.h>
  22.  
  23. main(int argc, char *argv[])
  24. {
  25.       int ch = '\0', def_ch = '\0';
  26.       char *prompt = "(y/n) ";
  27.       clock_t start, limit = (clock_t)0;
  28.  
  29.       if (1 < argc)
  30.       {
  31.             fputs(argv[1], stderr);
  32.             fputc(' ', stderr);
  33.       }
  34.       if (2 < argc)
  35.       {
  36.             def_ch = toupper(*argv[2]);
  37.             if ('Y' == def_ch)
  38.                   prompt[1] = def_ch;
  39.             else if ('N' == def_ch)
  40.                   prompt[3] = def_ch;
  41.             else  def_ch = '\0';
  42.       }
  43.       fputs(prompt, stderr);
  44.       if (3 < argc)
  45.       {
  46.             start = clock();
  47.             limit = (clock_t)(CLK_TCK * atoi(argv[3]));
  48.       }
  49.       while ('Y' != ch && 'N' != ch)
  50.       {
  51.             while (!kbhit())
  52.             {
  53.                   if (limit && (limit <= (clock() - start)))
  54.                   {
  55.                         ch = def_ch;
  56.                         goto BYE;
  57.                   }
  58.             }
  59.             ch = toupper(getch());
  60.             if ('Y' != ch && 'N' != ch && (1 < argc))
  61.                   ch = def_ch;
  62.       };
  63. BYE:  fputc(ch, stderr);
  64.       fputc('\n', stderr);
  65.       return ('Y' == ch);
  66. }
  67.