2013-05-17 10:58:27 -04:00
|
|
|
/* fileio.c -- implements fileio.h */
|
|
|
|
|
|
|
|
#include "fileio.h"
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* FILEIO.C
|
|
|
|
*
|
|
|
|
* The routines in this file read and write ASCII files from the disk. All of
|
|
|
|
* the knowledge about files are here.
|
|
|
|
*
|
|
|
|
* modified by Petri Kutvonen
|
|
|
|
*/
|
|
|
|
|
2013-05-18 01:40:11 -04:00
|
|
|
#include <stdio.h>
|
2013-05-18 00:20:01 -04:00
|
|
|
#include "crypt.h"
|
2013-05-18 01:40:11 -04:00
|
|
|
#include "display.h"
|
|
|
|
#include "estruct.h"
|
|
|
|
#include "edef.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-01-29 19:17:02 -05:00
|
|
|
static FILE *ffp; /* File pointer, all functions. */
|
|
|
|
static int eofflag; /* end-of-file flag */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a file for reading.
|
|
|
|
*/
|
2013-05-17 04:54:02 -04:00
|
|
|
int ffropen( const char *fn)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2013-05-17 10:58:27 -04:00
|
|
|
if ((ffp = fopen(fn, "rt")) == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOFNF;
|
2005-05-31 11:50:56 -04:00
|
|
|
eofflag = FALSE;
|
2010-08-29 06:03:55 -04:00
|
|
|
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-05-17 10:58:27 -04:00
|
|
|
int ffwopen( const char *fn)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
#if VMS
|
2010-02-27 05:38:22 -05:00
|
|
|
int fd;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((fd = creat(fn, 0666, "rfm=var", "rat=cr")) < 0
|
|
|
|
|| (ffp = fdopen(fd, "w")) == NULL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((ffp = fopen(fn, "w")) == NULL) {
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
mlwrite("Cannot open file for writing");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOERR;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOSUC;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close a file. Should look at the status in all systems.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int ffclose(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
/* free this since we do not need it anymore */
|
|
|
|
if (fline) {
|
|
|
|
free(fline);
|
|
|
|
fline = NULL;
|
|
|
|
}
|
|
|
|
eofflag = FALSE;
|
|
|
|
|
|
|
|
#if MSDOS & CTRLZ
|
|
|
|
fputc(26, ffp); /* add a ^Z at the end of the file */
|
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
#if V7 | USG | BSD | (MSDOS & (MSC | TURBO))
|
2005-09-30 18:26:09 -04:00
|
|
|
if (fclose(ffp) != FALSE) {
|
|
|
|
mlwrite("Error closing file");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOERR;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOSUC;
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
fclose(ffp);
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOSUC;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int ffputline(char *buf, int nbuf)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int i;
|
2005-05-31 11:50:56 -04:00
|
|
|
#if CRYPT
|
2005-09-30 18:26:09 -04:00
|
|
|
char c; /* character to translate */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (cryptflag) {
|
2005-09-30 18:26:09 -04:00
|
|
|
for (i = 0; i < nbuf; ++i) {
|
2005-05-31 11:50:56 -04:00
|
|
|
c = buf[i] & 0xff;
|
2006-11-19 14:52:18 -05:00
|
|
|
myencrypt(&c, 1);
|
2005-05-31 11:50:56 -04:00
|
|
|
fputc(c, ffp);
|
|
|
|
}
|
|
|
|
} else
|
2005-09-30 18:26:09 -04:00
|
|
|
for (i = 0; i < nbuf; ++i)
|
|
|
|
fputc(buf[i] & 0xFF, ffp);
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
for (i = 0; i < nbuf; ++i)
|
|
|
|
fputc(buf[i] & 0xFF, ffp);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
fputc('\n', ffp);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (ferror(ffp)) {
|
|
|
|
mlwrite("Write I/O error");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOERR;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-08-29 06:03:55 -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.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int ffgetline(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int c; /* current character read */
|
|
|
|
int i; /* current index into fline */
|
|
|
|
char *tmpline; /* temp storage for expanding line */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if we are at the end...return it */
|
|
|
|
if (eofflag)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOEOF;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* 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)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOMEM;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* read the line in */
|
|
|
|
#if PKCODE
|
|
|
|
if (!nullflag) {
|
2005-09-30 18:26:09 -04:00
|
|
|
if (fgets(fline, NSTRING, ffp) == (char *) NULL) { /* EOF ? */
|
|
|
|
i = 0;
|
|
|
|
c = EOF;
|
|
|
|
} else {
|
|
|
|
i = strlen(fline);
|
|
|
|
c = 0;
|
|
|
|
if (i > 0) {
|
|
|
|
c = fline[i - 1];
|
|
|
|
i--;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
} else {
|
2005-05-31 11:50:56 -04:00
|
|
|
i = 0;
|
|
|
|
c = fgetc(ffp);
|
|
|
|
}
|
|
|
|
while (c != EOF && c != '\n') {
|
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
i = 0;
|
|
|
|
while ((c = fgetc(ffp)) != EOF && c != '\n') {
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
if (c) {
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
fline[i++] = c;
|
|
|
|
/* if it's longer, get more room */
|
|
|
|
if (i >= flen) {
|
|
|
|
if ((tmpline =
|
|
|
|
malloc(flen + NSTRING)) == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOMEM;
|
2005-09-30 18:26:09 -04:00
|
|
|
strncpy(tmpline, fline, flen);
|
|
|
|
flen += NSTRING;
|
|
|
|
free(fline);
|
|
|
|
fline = tmpline;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
c = fgetc(ffp);
|
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* test for any errors that may have occured */
|
2005-09-30 18:26:09 -04:00
|
|
|
if (c == EOF) {
|
|
|
|
if (ferror(ffp)) {
|
|
|
|
mlwrite("File read error");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOERR;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (i != 0)
|
2005-05-31 11:50:56 -04:00
|
|
|
eofflag = TRUE;
|
|
|
|
else
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOEOF;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* terminate and decrypt the string */
|
2005-09-30 18:26:09 -04:00
|
|
|
fline[i] = 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
#if CRYPT
|
|
|
|
if (cryptflag)
|
2006-11-19 14:52:18 -05:00
|
|
|
myencrypt(fline, strlen(fline));
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2010-08-29 06:03:55 -04:00
|
|
|
return FIOSUC;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* does <fname> exist on disk?
|
|
|
|
*
|
|
|
|
* char *fname; file to check for existance
|
|
|
|
*/
|
2013-05-17 10:58:27 -04:00
|
|
|
int fexist( const char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
/* try to open the file for reading */
|
|
|
|
fp = fopen(fname, "r");
|
|
|
|
|
|
|
|
/* if it fails, just return false! */
|
|
|
|
if (fp == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* otherwise, close it and report true */
|
|
|
|
fclose(fp);
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|