home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / pico / fileio.c < prev    next >
C/C++ Source or Header  |  1998-07-10  |  3KB  |  147 lines

  1. #if    !defined(lint) && !defined(DOS)
  2. static char rcsid[] = "$Id: fileio.c,v 4.16 1998/07/10 22:40:58 hubert 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.  *
  19.  * Pine and Pico are registered trademarks of the University of Washington.
  20.  * No commercial use of these trademarks may be made without prior written
  21.  * permission of the University of Washington.
  22.  * 
  23.  * Pine, Pico, and Pilot software and its included text are Copyright
  24.  * 1989-1998 by the University of Washington.
  25.  * 
  26.  * The full text of our legal notices is contained in the file called
  27.  * CPYRIGHT, included with this distribution.
  28.  *
  29.  */
  30. /*
  31.  * The routines in this file read and write ASCII files from the disk. All of
  32.  * the knowledge about files are here. A better message writing scheme should
  33.  * be used.
  34.  */
  35. #include    "headers.h"
  36.  
  37.  
  38. #if    defined(bsd) || defined(lnx)
  39. extern int errno;
  40. #endif
  41.  
  42.  
  43. FIOINFO    g_pico_fio;
  44.  
  45. /*
  46.  * Open a file for reading.
  47.  */
  48. ffropen(fn)
  49.   char    *fn;
  50. {
  51.     int status;
  52.  
  53.     if ((status = fexist(g_pico_fio.name = fn, "r", (off_t *)NULL)) == FIOSUC){
  54.     g_pico_fio.flags = FIOINFO_READ;
  55.     if((g_pico_fio.fp = fopen(g_pico_fio.name, "r")) == NULL)
  56.       status = FIOFNF;
  57.     }
  58.  
  59.     return (status);
  60. }
  61.  
  62.  
  63. /*
  64.  * Write a line to the already opened file. The "buf" points to the buffer,
  65.  * and the "nbuf" is its length, less the free newline. Return the status.
  66.  * Check only at the newline.
  67.  */
  68. ffputline(buf, nbuf)
  69.     CELL  buf[];
  70.     int      nbuf;
  71. {
  72.     register int    i;
  73.  
  74.     for (i = 0; i < nbuf; ++i)
  75.        if(fputc(buf[i].c&0xFF, g_pico_fio.fp) == EOF)
  76.      break;
  77.  
  78.    if(i == nbuf)
  79.       fputc('\n', g_pico_fio.fp);
  80.  
  81.     if (ferror(g_pico_fio.fp)) {
  82.         emlwrite("\007Write error: %s", errstr(errno));
  83.     sleep(5);
  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, msg)
  99.   register char   buf[];
  100.   int nbuf;
  101.   int msg;
  102. {
  103.     register int    c;
  104.     register int    i;
  105.  
  106.     i = 0;
  107.  
  108.     while ((c = fgetc(g_pico_fio.fp)) != EOF && c != '\n') {
  109.     /*
  110.      * Don't blat the CR should the newline be CRLF and we're
  111.      * running on a unix system.  NOTE: this takes care of itself
  112.      * under DOS since the non-binary open turns newlines into '\n'.
  113.      */
  114.     if(c == '\r'){
  115.         if((c = fgetc(g_pico_fio.fp)) == EOF || c == '\n')
  116.           break;
  117.  
  118.         if (i < nbuf-2)        /* Bare CR. Insert it and go on... */
  119.           buf[i++] = '\r';        /* else, we're up a creek */
  120.     }
  121.  
  122.         if (i >= nbuf-2) {
  123.         buf[nbuf - 2] = c;    /* store last char read */
  124.         buf[nbuf - 1] = 0;    /* and terminate it */
  125.         if (msg)
  126.           emlwrite("File has long line", NULL);
  127.             return (FIOLNG);
  128.         }
  129.         buf[i++] = c;
  130.     }
  131.  
  132.     if (c == EOF) {
  133.         if (ferror(g_pico_fio.fp)) {
  134.             emlwrite("File read error", NULL);
  135.             return (FIOERR);
  136.         }
  137.  
  138.         if (i != 0)
  139.       emlwrite("File doesn't end with newline.  Adding one.", NULL);
  140.     else
  141.       return (FIOEOF);
  142.     }
  143.  
  144.     buf[i] = 0;
  145.     return (FIOSUC);
  146. }
  147.