home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / pine3.07 / pico / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-06  |  3.6 KB  |  122 lines

  1. /*
  2.  * Program:    ASCII file reading routines
  3.  *
  4.  * Modifier:    Michael Seibel
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: mikes@cac.washington.edu
  11.  *
  12.  * Date:    19 Jan 1991
  13.  * Last Edited:    6 Jan 1992
  14.  *
  15.  * Copyright 1991 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35. /*
  36.  * The routines in this file read and write ASCII files from the disk. All of
  37.  * the knowledge about files are here. A better message writing scheme should
  38.  * be used.
  39.  */
  40. #include        <stdio.h>
  41. #include    "estruct.h"
  42. #include        "edef.h"
  43.  
  44.  
  45. FILE    *ffp;                           /* File pointer, all functions. */
  46.  
  47. /*
  48.  * Open a file for reading.
  49.  */
  50. ffropen(fn)
  51. char    *fn;
  52. {
  53.         if ((ffp=fopen(fn, "r")) == NULL)
  54.                 return (FIOFNF);
  55.         return (FIOSUC);
  56. }
  57.  
  58.  
  59. /*
  60.  * Write a line to the already opened file. The "buf" points to the buffer,
  61.  * and the "nbuf" is its length, less the free newline. Return the status.
  62.  * Check only at the newline.
  63.  */
  64. ffputline(buf, nbuf)
  65. char    buf[];
  66. {
  67.         register int    i;
  68.  
  69.         for (i = 0; i < nbuf; ++i)
  70.                 fputc(buf[i]&0xFF, ffp);
  71.  
  72.         fputc('\n', ffp);
  73.  
  74.         if (ferror(ffp)) {
  75.                 emlwrite("Write I/O error");
  76.                 return (FIOERR);
  77.         }
  78.  
  79.         return (FIOSUC);
  80. }
  81.  
  82. /*
  83.  * Read a line from a file, and store the bytes in the supplied buffer. The
  84.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  85.  * at the end of the file that don't have a newline present. Check for I/O
  86.  * errors too. Return status.
  87.  */
  88. ffgetline(buf, nbuf)
  89. register char   buf[];
  90. {
  91.         register int    c;
  92.         register int    i;
  93.  
  94.         i = 0;
  95.  
  96.         while ((c = fgetc(ffp)) != EOF && c != '\n') {
  97.                 if (i >= nbuf-2) {
  98.             buf[nbuf - 2] = c;    /* store last char read */
  99.             buf[nbuf - 1] = 0;    /* and terminate it */
  100.                         emlwrite("File has long line");
  101.                         return (FIOLNG);
  102.                 }
  103.                 buf[i++] = c;
  104.         }
  105.  
  106.         if (c == EOF) {
  107.                 if (ferror(ffp)) {
  108.                         emlwrite("File read error");
  109.                         return (FIOERR);
  110.                 }
  111.  
  112.                 if (i != 0) {
  113.                         emlwrite("File has funny line at EOF");
  114.                         return (FIOERR);
  115.                 }
  116.                 return (FIOEOF);
  117.         }
  118.  
  119.         buf[i] = 0;
  120.         return (FIOSUC);
  121. }
  122.