home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / libcs / getstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  3.2 KB  |  88 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. /*  getstr --  prompt user for a string
  29.  *
  30.  *  Usage:  p = getstr (prompt,defalt,answer);
  31.  *    char *p,*prompt,*defalt,*answer;
  32.  *
  33.  *  Getstr prints this message:  prompt  [defalt]
  34.  *  and accepts a line of input from the user.  This line is
  35.  *  entered into "answer", which must be a big char array;
  36.  *  if the user types just carriage return, then the string
  37.  *  "defalt" is copied into answer.
  38.  *  Value returned by getstr is just the same as answer,
  39.  *  i.e. pointer to result string.
  40.  *  The default value is used on error or EOF in the standard input.
  41.  *
  42.  *  HISTORY
  43.  * $Log:    getstr.c,v $
  44.  * Revision 1.2  90/12/11  17:56:00  mja
  45.  *     Add copyright/disclaimer for distribution.
  46.  * 
  47.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  48.  *    Modified for 4.2 BSD.  Now uses stderr for output.
  49.  *
  50.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  51.  *    Added code to copy default to answer (in addition to Fil's code to
  52.  *    return NULL) on error or EOF in the standard input.
  53.  *
  54.  * 21-Oct-80  Fil Alleva (faa) at Carnegie-Mellon University
  55.  *    Getstr() now percuolates any errors from gets() up to the calling
  56.  *    routine.
  57.  *
  58.  * 19-May-80  Steven Shafer (sas) at Carnegie-Mellon University
  59.  *    Increased buffer size to 4000 characters.  Why not?
  60.  *
  61.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  62.  *    Rewritten for VAX.  Mike thinks a 0 pointer for the default should
  63.  *    print no default (i.e. not even braces); I'm not sure I like the idea
  64.  *    of a routine that doesn't explicitly tell you what happens if you
  65.  *    just hit Carriage Return.
  66.  *
  67.  */
  68.  
  69. #include <stdio.h>
  70.  
  71. char *getstr (prompt,defalt,answer)
  72. char *prompt,*defalt,*answer;
  73. {
  74.     char defbuf[4000];
  75.     register char *retval;
  76.  
  77.     fflush (stdout);
  78.     fprintf (stderr,"%s  [%s]  ",prompt,defalt);
  79.     fflush (stderr);
  80.     strcpy (defbuf,defalt);
  81.     retval = (char *) gets (answer);
  82.     if (retval == NULL || *answer == '\0')  strcpy (answer,defbuf);
  83.     if (retval == NULL)
  84.         return (retval);
  85.     else
  86.         return (answer);
  87. }
  88.