home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / pine / pico / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  4.1 KB  |  144 lines

  1. #if    !defined(lint) && !defined(DOS)
  2. static char rcsid[] = "$Id: fileio.c,v 4.4 1993/11/17 01:03:50 mikes Exp $";
  3. #endif
  4. /*
  5.  * Program:    ASCII file reading routines
  6.  *
  7.  *
  8.  * Michael Seibel
  9.  * Networks and Distributed Computing
  10.  * Computing and Communications
  11.  * University of Washington
  12.  * Administration Builiding, AG-44
  13.  * Seattle, Washington, 98195, USA
  14.  * Internet: mikes@cac.washington.edu
  15.  *
  16.  * Please address all bugs and comments to "pine-bugs@cac.washington.edu"
  17.  *
  18.  * Copyright 1991-1993  University of Washington
  19.  *
  20.  *  Permission to use, copy, modify, and distribute this software and its
  21.  * documentation for any purpose and without fee to the University of
  22.  * Washington is hereby granted, provided that the above copyright notice
  23.  * appears in all copies and that both the above copyright notice and this
  24.  * permission notice appear in supporting documentation, and that the name
  25.  * of the University of Washington not be used in advertising or publicity
  26.  * pertaining to distribution of the software without specific, written
  27.  * prior permission.  This software is made available "as is", and
  28.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  29.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  30.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  31.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  32.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  33.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  34.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  35.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  36.  *
  37.  * Pine and Pico are trademarks of the University of Washington.
  38.  * No commercial use of these trademarks may be made without prior
  39.  * written permission of the University of Washington.
  40.  *
  41.  */
  42. /*
  43.  * The routines in this file read and write ASCII files from the disk. All of
  44.  * the knowledge about files are here. A better message writing scheme should
  45.  * be used.
  46.  */
  47. #include        <stdio.h>
  48. #include    "estruct.h"
  49. #include        "edef.h"
  50.  
  51.  
  52. FILE    *ffp;                           /* File pointer, all functions. */
  53.  
  54. /*
  55.  * Open a file for reading.
  56.  */
  57. ffropen(fn)
  58.   char    *fn;
  59. {
  60.     if ((ffp=fopen(fn, "r")) == NULL)
  61.       return (FIOFNF);
  62.  
  63.     return (FIOSUC);
  64. }
  65.  
  66.  
  67. /*
  68.  * Write a line to the already opened file. The "buf" points to the buffer,
  69.  * and the "nbuf" is its length, less the free newline. Return the status.
  70.  * Check only at the newline.
  71.  */
  72. ffputline(buf, nbuf)
  73.     CELL  buf[];
  74. {
  75.     register int    i;
  76.  
  77.     for (i = 0; i < nbuf; ++i)
  78.        fputc(buf[i].c&0xFF, ffp);
  79.  
  80.     fputc('\n', ffp);
  81.  
  82.     if (ferror(ffp)) {
  83.         emlwrite("Write I/O error");
  84.         return (FIOERR);
  85.     }
  86.  
  87.     return (FIOSUC);
  88. }
  89.  
  90.  
  91.  
  92. /*
  93.  * Read a line from a file, and store the bytes in the supplied buffer. The
  94.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  95.  * at the end of the file that don't have a newline present. Check for I/O
  96.  * errors too. Return status.
  97.  */
  98. ffgetline(buf, nbuf)
  99.   register char   buf[];
  100. {
  101.     register int    c;
  102.     register int    i;
  103.  
  104.     i = 0;
  105.  
  106.     while ((c = fgetc(ffp)) != EOF && c != '\n') {
  107.     /*
  108.      * Don't blat the CR should the newline be CRLF and we're
  109.      * running on a unix system.  NOTE: this takes care of itself
  110.      * under DOS since the non-binary open turns newlines into '\n'.
  111.      */
  112.     if(c == '\r'){
  113.         if((c = fgetc(ffp)) == EOF || c == '\n')
  114.           break;
  115.  
  116.         if (i < nbuf-2)        /* Bare CR. Insert it and go on... */
  117.           buf[i++] = '\r';        /* else, we're up a creek */
  118.     }
  119.  
  120.         if (i >= nbuf-2) {
  121.         buf[nbuf - 2] = c;    /* store last char read */
  122.         buf[nbuf - 1] = 0;    /* and terminate it */
  123.             emlwrite("File has long line");
  124.             return (FIOLNG);
  125.         }
  126.         buf[i++] = c;
  127.     }
  128.  
  129.     if (c == EOF) {
  130.         if (ferror(ffp)) {
  131.             emlwrite("File read error");
  132.             return (FIOERR);
  133.         }
  134.  
  135.         if (i != 0)
  136.       emlwrite("File doesn't end with newline.  Adding one.", NULL);
  137.     else
  138.       return (FIOEOF);
  139.     }
  140.  
  141.     buf[i] = 0;
  142.     return (FIOSUC);
  143. }
  144.