home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / libcs / getoct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  3.5 KB  |  110 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. /*  getoct --  prompt user for octal integer
  29.  *
  30.  *  Usage:  i = getoct (prompt,min,max,defalt)
  31.  *    unsigned int i,min,max,defalt;
  32.  *    char *prompt;
  33.  *
  34.  *  Getoct prints the message:  prompt  (min to max, octal)  [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 octal integer (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 getoct.
  43.  *  On error or EOF in the standard input, the default is returned.
  44.  *
  45.  *  HISTORY
  46.  * $Log:    getoct.c,v $
  47.  * Revision 1.2  90/12/11  17:55:03  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 error or EOF in standard input.
  55.  *
  56.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  57.  *    Rewritten for VAX.  Now uses all unsigned ints, to avoid
  58.  *    sign problems we had on PDP-11.
  59.  *
  60.  */
  61.  
  62. #include <stdio.h>
  63. #include <ctype.h>
  64.  
  65. unsigned int atoo();
  66.  
  67. unsigned int getoct (prompt,min,max,defalt)
  68. unsigned int min,max,defalt;
  69. char *prompt;
  70. {
  71.     char input [200];
  72.     register char *p;
  73.     register unsigned int i;
  74.     register int err;
  75.  
  76.     fflush (stdout);
  77.     do {
  78.  
  79.         fprintf (stderr,"%s  (%s%o to %s%o, octal)  [%s%o]  ",
  80.             prompt,(min ? "0" : ""),min,
  81.             (max ? "0" : ""),max,(defalt ? "0" : ""),defalt);
  82.         fflush (stderr);
  83.  
  84.         if (gets (input) == NULL) {
  85.             i = defalt;
  86.             err = (i < min || max < i);
  87.         }
  88.         else {
  89.             err = 0;
  90.             for (p=input; *p && (*p >= '0' && *p <= '7'); p++) ;
  91.             if (*p) {        /* non-numeric */
  92.                 err = 1;
  93.             } 
  94.             else {
  95.                 if (*input)    i = atoo (input);
  96.                 else        i = defalt;
  97.                 err = (i < min || max < i);
  98.             }
  99.         }
  100.  
  101.         if (err) {
  102.             fprintf (stderr,"Must be an unsigned octal number between %s%o and %s%o\n",
  103.             (min ? "0" : ""),min,(max ? "0" : ""),max);
  104.         }
  105.     } 
  106.     while (err);
  107.  
  108.     return (i);
  109. }
  110.