2013-05-17 10:58:27 -04:00
|
|
|
/* fileio.c -- implements fileio.h */
|
|
|
|
|
|
|
|
#include "fileio.h"
|
|
|
|
|
2013-10-11 02:09:29 -04:00
|
|
|
#ifdef CTRLZ
|
|
|
|
#undef CTRLZ
|
|
|
|
#endif
|
|
|
|
#define CTRLZ 0 /* add a ^Z at end of files under MSDOS only */
|
|
|
|
|
2013-06-08 05:32:48 -04:00
|
|
|
/* FILEIO.C
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
|
|
|
* The routines in this file read and write ASCII files from the disk. All of
|
|
|
|
* the knowledge about files are here.
|
|
|
|
*
|
2013-06-08 05:32:48 -04:00
|
|
|
* modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2013-05-18 01:40:11 -04:00
|
|
|
#include <stdio.h>
|
2013-06-10 01:30:06 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "defines.h"
|
2013-10-01 06:27:57 -04:00
|
|
|
#include "retcode.h"
|
2013-06-10 01:30:06 -04:00
|
|
|
|
2013-09-20 06:10:30 -04:00
|
|
|
#if CRYPT
|
2013-06-10 01:30:06 -04:00
|
|
|
boolean is_crypted ; /* currently encrypting? */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *fline = NULL ; /* dynamic return line */
|
2013-06-10 02:39:19 -04:00
|
|
|
int flen = 0 ; /* current allocated length of fline */
|
2013-06-10 01:30:06 -04:00
|
|
|
int ftype ;
|
2013-06-10 02:39:19 -04:00
|
|
|
int fpayload ; /* actual length of fline content */
|
2013-06-10 01:30:06 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-10 01:30:06 -04:00
|
|
|
static FILE *ffp ; /* File pointer, all functions. */
|
|
|
|
static boolean eofflag ; /* end-of-file flag */
|
2013-06-08 05:32:48 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a file for reading.
|
|
|
|
*/
|
2013-06-09 05:31:28 -04:00
|
|
|
fio_code ffropen( const char *fn)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2013-06-08 05:32:48 -04:00
|
|
|
if ((ffp = fopen(fn, "r")) == NULL)
|
|
|
|
return FIOFNF;
|
|
|
|
eofflag = FALSE;
|
|
|
|
ftype = FTYPE_NONE ;
|
|
|
|
return FIOSUC;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a file for writing. Return TRUE if all is well, and FALSE on error
|
|
|
|
* (cannot create).
|
|
|
|
*/
|
2013-06-09 05:31:28 -04:00
|
|
|
fio_code ffwopen( const char *fn)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
#if VMS
|
2013-06-08 05:32:48 -04:00
|
|
|
int fd;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-08 05:32:48 -04:00
|
|
|
if ((fd = creat(fn, 0666, "rfm=var", "rat=cr")) < 0
|
2013-06-10 23:53:08 -04:00
|
|
|
|| (ffp = fdopen(fd, "w")) == NULL)
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2013-06-10 23:53:08 -04:00
|
|
|
if ((ffp = fopen(fn, "w")) == NULL)
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2013-06-08 05:32:48 -04:00
|
|
|
return FIOERR;
|
2013-06-10 23:53:08 -04:00
|
|
|
|
2013-06-08 05:32:48 -04:00
|
|
|
return FIOSUC;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close a file. Should look at the status in all systems.
|
|
|
|
*/
|
2013-06-09 05:31:28 -04:00
|
|
|
fio_code ffclose(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2013-06-08 05:32:48 -04:00
|
|
|
/* free this since we do not need it anymore */
|
|
|
|
if (fline) {
|
|
|
|
free(fline);
|
|
|
|
fline = NULL;
|
|
|
|
}
|
|
|
|
eofflag = FALSE;
|
|
|
|
ftype = FTYPE_NONE ;
|
|
|
|
|
|
|
|
#if MSDOS & CTRLZ
|
|
|
|
fputc(26, ffp); /* add a ^Z at the end of the file */
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
#if V7 | USG | BSD | (MSDOS & (MSC | TURBO))
|
2013-06-10 23:53:08 -04:00
|
|
|
if (fclose(ffp) != FALSE)
|
2013-06-08 05:32:48 -04:00
|
|
|
return FIOERR;
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2013-06-08 05:32:48 -04:00
|
|
|
fclose(ffp);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2013-09-19 23:44:18 -04:00
|
|
|
return FIOSUC;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a line to the already opened file. The "buf" points to the buffer,
|
|
|
|
* and the "nbuf" is its length, less the free newline. Return the status.
|
|
|
|
* Check only at the newline.
|
|
|
|
*/
|
2013-06-10 21:51:29 -04:00
|
|
|
fio_code ffputline( char *buf, int nbuf, int dosflag) {
|
2013-06-08 05:32:48 -04:00
|
|
|
#if CRYPT
|
2013-06-10 01:30:06 -04:00
|
|
|
if( is_crypted) {
|
2013-06-10 00:09:39 -04:00
|
|
|
int i ;
|
|
|
|
|
|
|
|
for( i = 0 ; i < nbuf ; i++) {
|
2013-06-10 21:51:29 -04:00
|
|
|
char c ;
|
2013-06-10 00:09:39 -04:00
|
|
|
|
|
|
|
c = buf[ i] ;
|
|
|
|
myencrypt( &c, 1) ;
|
|
|
|
fputc( c, ffp) ;
|
|
|
|
}
|
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
|
2013-06-10 00:09:39 -04:00
|
|
|
fwrite( buf, 1, nbuf, ffp) ;
|
|
|
|
|
|
|
|
if( dosflag)
|
|
|
|
fputc( '\r', ffp) ;
|
2013-06-08 23:21:35 -04:00
|
|
|
|
2013-06-10 00:09:39 -04:00
|
|
|
fputc( '\n', ffp) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-10 23:53:08 -04:00
|
|
|
if( ferror( ffp))
|
2013-06-10 00:09:39 -04:00
|
|
|
return FIOERR ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-10 00:09:39 -04:00
|
|
|
return FIOSUC ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a line from a file, and store the bytes in the supplied buffer. The
|
|
|
|
* "nbuf" is the length of the buffer. Complain about long lines and lines
|
|
|
|
* at the end of the file that don't have a newline present. Check for I/O
|
|
|
|
* errors too. Return status.
|
|
|
|
*/
|
2013-06-09 05:31:28 -04:00
|
|
|
fio_code ffgetline(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2013-06-08 05:32:48 -04:00
|
|
|
int c; /* current character read */
|
|
|
|
int i; /* current index into fline */
|
|
|
|
|
|
|
|
/* if we are at the end...return it */
|
|
|
|
if (eofflag)
|
|
|
|
return FIOEOF;
|
|
|
|
|
|
|
|
/* dump fline if it ended up too big */
|
|
|
|
if (flen > NSTRING) {
|
|
|
|
free(fline);
|
|
|
|
fline = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we don't have an fline, allocate one */
|
|
|
|
if (fline == NULL)
|
|
|
|
if ((fline = malloc(flen = NSTRING)) == NULL)
|
|
|
|
return FIOMEM;
|
|
|
|
|
|
|
|
/* read the line in */
|
|
|
|
i = 0;
|
|
|
|
while ((c = fgetc(ffp)) != EOF && c != '\r' && c != '\n') {
|
2013-06-10 02:39:19 -04:00
|
|
|
fline[i++] = c;
|
|
|
|
/* if it's longer, get more room */
|
|
|
|
if (i >= flen) {
|
|
|
|
char *tmpline; /* temp storage for expanding line */
|
|
|
|
|
|
|
|
fpayload = i ;
|
|
|
|
tmpline = malloc(flen + NSTRING) ;
|
|
|
|
if( tmpline == NULL)
|
|
|
|
return FIOMEM ;
|
|
|
|
|
|
|
|
memcpy( tmpline, fline, flen) ;
|
|
|
|
flen += NSTRING;
|
|
|
|
free(fline);
|
|
|
|
fline = tmpline;
|
2013-06-08 05:32:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 02:39:19 -04:00
|
|
|
fpayload = i ;
|
|
|
|
|
2013-06-08 05:32:48 -04:00
|
|
|
/* test for any errors that may have occured */
|
|
|
|
if (c == EOF) {
|
2013-06-10 23:53:08 -04:00
|
|
|
if( ferror( ffp))
|
|
|
|
return FIOERR ;
|
2013-06-08 05:32:48 -04:00
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
eofflag = TRUE;
|
|
|
|
else
|
|
|
|
return FIOEOF;
|
|
|
|
} else if( c == '\r') {
|
|
|
|
c = fgetc( ffp) ;
|
|
|
|
if( c != '\n') {
|
|
|
|
ftype |= FTYPE_MAC ;
|
|
|
|
ungetc( c, ffp) ;
|
|
|
|
} else
|
|
|
|
ftype |= FTYPE_DOS ;
|
|
|
|
} else /* c == '\n' */
|
|
|
|
ftype |= FTYPE_UNIX ;
|
|
|
|
|
|
|
|
/* terminate and decrypt the string */
|
|
|
|
fline[i] = 0;
|
|
|
|
#if CRYPT
|
2013-06-10 01:30:06 -04:00
|
|
|
if( is_crypted)
|
2013-06-10 02:39:19 -04:00
|
|
|
myencrypt( fline, fpayload);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2013-06-08 05:32:48 -04:00
|
|
|
return FIOSUC;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|