home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / getlong.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  3KB  |  103 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. /*  getlong --  prompt user for long
  29.  *
  30.  *  Usage:  i = getlong (prompt,min,max,defalt)
  31.  *    long i,min,max,defalt;
  32.  *    char *prompt;
  33.  *
  34.  *  Getlong prints the message:  prompt  (min to max)  [defalt]
  35.  *  and accepts a line of input from the user.  If the input
  36.  *  is not null or numeric, an error message is printed; otherwise,
  37.  *  the value is converted to an long (or the value "defalt" is
  38.  *  substituted if the input is null).  Then, the value is
  39.  *  checked to ensure that is lies within the range "min" to "max".
  40.  *  If it does not, an error message is printed.  As long as
  41.  *  errors occur, the cycle is repeated; when a legal value is
  42.  *  entered, this value is returned by getlong.
  43.  *  On error or EOF in the standard input, the default is returned.
  44.  *
  45.  *  HISTORY
  46.  * $Log:    getlong.c,v $
  47.  * Revision 1.2  90/12/11  17:54:48  mja
  48.  *     Add copyright/disclaimer for distribution.
  49.  * 
  50.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  51.  *    Modified for 4.2 BSD.  Now uses stderr for output.
  52.  *
  53.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  54.  *    Added code to return default on EOF or error in standard input.
  55.  *
  56.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  57.  *    Rewritten for VAX.
  58.  *
  59.  */
  60.  
  61. #include <stdio.h>
  62. #include <ctype.h>
  63.  
  64. long getlong (prompt,min,max,defalt)
  65. long min,max,defalt;
  66. char *prompt;
  67. {
  68.     char input [200];
  69.     register char *p;
  70.     register long i,err;
  71.  
  72.     fflush (stdout);
  73.     do {
  74.  
  75.         fprintf (stderr,"%s  (%D to %D)  [%D]  ",prompt,min,max,defalt);
  76.         fflush (stderr);
  77.  
  78.         if (gets (input) == NULL) {
  79.             i = defalt;
  80.             err = (i < min || max < i);
  81.         }
  82.         else {
  83.             err = 0;
  84.             for (p=input; *p && (isdigit(*p) || *p == '-' || *p == '+'); p++) ;
  85.     
  86.             if (*p) {        /* non-numeric */
  87.                 err = 1;
  88.             } 
  89.             else {
  90.                 if (*input)    i = atol (input);
  91.                 else        i = defalt;
  92.                 err = (i < min || max < i);
  93.             }
  94.         }
  95.  
  96.         if (err) fprintf (stderr,"Must be a number between %D and %D\n",
  97.         min,max);
  98.     } 
  99.     while (err);
  100.  
  101.     return (i);
  102. }
  103.