home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / libcs / wantread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  3.9 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. /*  wantread  --  attempt to open file for input
  29.  *
  30.  *  Usage:  i = wantread (path,file,fullname,prompt);
  31.  *    int i;
  32.  *    char *path,*file,*fullname,*prompt;
  33.  *
  34.  *  Wantread will search through the specified path for the
  35.  *  specified file, attempting to open it for input if it is
  36.  *  found.  If no such file is found, the user is given
  37.  *  an opportunity to enter a new file name (after the prompt is
  38.  *  printed).  The new file will then be sought using the same
  39.  *  path.
  40.  *  If the path is the null string, the file will be searched for
  41.  *  with no prefix.  If the file name is null, the user will be
  42.  *  prompted for a file name immediately.  The user can always
  43.  *  abort wantread by typing just a carriage return to the prompt.
  44.  *  Wantread will put the name of the successfully opened file into
  45.  *  the "fullname" string (which therefore must be long enough to hold a
  46.  *  complete file name), and return its file descriptor; if no file
  47.  *  is successfully found, -1 is returned.
  48.  *
  49.  *  HISTORY
  50.  * $Log:    wantread.c,v $
  51.  * Revision 1.2  90/12/11  18:00:52  mja
  52.  *     Add copyright/disclaimer for distribution.
  53.  * 
  54.  * 30-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  55.  *    Adapted for 4.2 BSD UNIX:  New openp routine to conform to new open call.
  56.  *
  57.  * 21-Oct-81  Fil Alleva (faa) at Carnegie-Mellon University
  58.  *    Fixed bug which caused an infinite loop when getstr() got
  59.  *    an EOT error and returned NULL. The error return was ignored
  60.  *    and the value of "answer" was not changed which caused the loop.
  61.  *
  62.  * 28-Aug-80  Mike Accetta (mja) at Carnegie-Mellon University
  63.  *    Fixed bug which used the "file" string to hold name typed at terminal
  64.  *    even though "file" may have been passed as a constant.
  65.  *
  66.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  67.  *    Rewritten for VAX.
  68.  *
  69.  */
  70.  
  71. #include <stdio.h>
  72. #include <sys/file.h>
  73.  
  74. int strcmp(),openp();
  75. char *getstr();
  76.  
  77. int wantread (path,file,fullname,prompt)
  78. char *path,*file,*fullname,*prompt;
  79. {
  80.     register int value;
  81.     char myfile[2000], *retval;
  82.  
  83.     fflush (stdout);
  84.     if (*file == '\0') {
  85.         getstr (prompt,"no file",myfile);
  86.         if (strcmp(myfile,"no file") == 0)  return (-1);
  87.     }
  88.     else
  89.         strcpy(myfile, file);
  90.  
  91.     do {
  92.         value = openp (path,myfile,fullname,O_RDONLY,0);
  93.         if (value < 0) {
  94.             if (*path && (*myfile != '/')) {
  95.                 sprintf (fullname,"Want to read %s in path \"%s\"",myfile,path);
  96.             }
  97.             else {
  98.                 sprintf (fullname,"Want to read %s",myfile);
  99.             }
  100.             perror (fullname);
  101.             retval = getstr (prompt,"no file",myfile);
  102.             if ((strcmp(myfile,"no file") == 0) || retval == NULL)
  103.                 return (-1);
  104.         }
  105.     } 
  106.     while (value < 0);
  107.  
  108.     return (value);
  109. }
  110.