home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / getbool.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  3KB  |  86 lines

  1. /*
  2.  * Copyright (c) 1990 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
  12.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
  14.  * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
  15.  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  17.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Users of this software agree to return to Carnegie Mellon any
  21.  * improvements or extensions that they make and grant Carnegie the
  22.  * rights to redistribute these changes.
  23.  *
  24.  * Export of this software is permitted only after complying with the
  25.  * regulations of the U.S. Deptartment of Commerce relating to the
  26.  * Export of Technical Data.
  27.  */
  28. /*  getbool -- ask user a yes/no question
  29.  *
  30.  *  Usage:  i = getbool (prompt, defalt);
  31.  *
  32.  *  Example:  do {...} while (getbool ("More?",1));
  33.  *
  34.  *  Prints prompt string, asks user for response.  Defalt is
  35.  *  0 (no) or 1 (yes), and is used if user types just carriage return,
  36.  *  or on end-of-file or error in the standard input.
  37.  *
  38.  *  HISTORY
  39.  * $Log:    getbool.c,v $
  40.  * Revision 1.2  90/12/11  17:53:39  mja
  41.  *     Add copyright/disclaimer for distribution.
  42.  * 
  43.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  44.  *    Modified for 4.2 BSD.  Now uses stderr for output.
  45.  *
  46.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  47.  *    Added code to return default if gets returns NULL.
  48.  *
  49.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  50.  *    Rewritten for VAX.  Possible changes for the future:  accept "t" (true)
  51.  *    and "f" (false), 0 and 1, etc.
  52.  *
  53.  */
  54.  
  55. #include <stdio.h>
  56. #include <c.h>
  57.  
  58. int getbool (prompt, defalt)
  59. char *prompt;
  60. int defalt;
  61. {
  62.     register int valu;
  63.     register char ch;
  64.     char input [100];
  65.  
  66.     fflush (stdout);
  67.     if (defalt != TRUE && defalt != FALSE)  defalt = TRUE;
  68.     valu = 2;                /* meaningless value */
  69.     do {
  70.         fprintf (stderr,"%s  [%s]  ",prompt,(defalt ? "yes" : "no"));
  71.         fflush (stderr);            /* in case it's buffered */
  72.         if (gets (input) == NULL) {
  73.             valu = defalt;
  74.         }
  75.         else {
  76.             ch = *input;            /* first char */
  77.             if (ch == 'y' || ch == 'Y')        valu = TRUE;
  78.             else if (ch == 'n' || ch == 'N')    valu = FALSE;
  79.             else if (ch == '\0')        valu = defalt;
  80.             else fprintf (stderr,"Must begin with 'y' (yes) or 'n' (no).\n");
  81.         }
  82.     } 
  83.     while (valu == 2);            /* until correct response */
  84.     return (valu);
  85. }
  86.