add file header.

This commit is contained in:
Renaud 2013-05-19 17:36:24 +08:00
parent 93f2a6d691
commit 73c372fc7f
4 changed files with 449 additions and 436 deletions

View File

@ -28,7 +28,7 @@ OBJ=basic.o bind.o buffer.o crypt.o display.o eval.o exec.o \
termio.o window.o word.o names.o globals.o \ termio.o window.o word.o names.o globals.o \
wrapper.o utf8.o wrapper.o utf8.o
HDR=basic.h crypt.h display.h ebind.h edef.h efunc.h estruct.h fileio.h \ HDR=basic.h crypt.h display.h ebind.h edef.h efunc.h estruct.h file.h fileio.h \
input.h line.h main.h version.h window.h wrapper.h input.h line.h main.h version.h window.h wrapper.h
# DO NOT ADD OR MODIFY ANY LINES ABOVE THIS -- make source creates them # DO NOT ADD OR MODIFY ANY LINES ABOVE THIS -- make source creates them
@ -139,7 +139,7 @@ crypt.o: crypt.c crypt.h display.h estruct.h edef.h input.h
display.o: display.c display.h estruct.h edef.h utf8.h version.h window.h display.o: display.c display.h estruct.h edef.h utf8.h version.h window.h
eval.o: eval.c estruct.h edef.h version.h fileio.h eval.o: eval.c estruct.h edef.h version.h fileio.h
exec.o: exec.c estruct.h edef.h exec.o: exec.c estruct.h edef.h
file.o: file.c crypt.h estruct.h edef.h fileio.h file.o: file.c file.h crypt.h estruct.h edef.h fileio.h
fileio.o: fileio.c fileio.h crypt.h display.h estruct.h edef.h fileio.o: fileio.c fileio.h crypt.h display.h estruct.h edef.h
ibmpc.o: ibmpc.c estruct.h edef.h ibmpc.o: ibmpc.c estruct.h edef.h
input.o: input.c input.h estruct.h edef.h input.o: input.c input.h estruct.h edef.h

24
efunc.h
View File

@ -1,12 +1,12 @@
/* efunc.h /* efunc.h
* *
* Function declarations and names. * Function declarations and names.
* *
* This file list all the C code functions used and the names to use * This file list all the C code functions used and the names to use
* to bind keys to them. To add functions, declare it here in both the * to bind keys to them. To add functions, declare it here in both the
* extern function list and the name binding table. * extern function list and the name binding table.
* *
* modified by Petri Kutvonen * modified by Petri Kutvonen
*/ */
/* External function declarations. */ /* External function declarations. */
@ -129,19 +129,7 @@ extern int unmark(int f, int n);
extern struct buffer *bfind(char *bname, int cflag, int bflag); extern struct buffer *bfind(char *bname, int cflag, int bflag);
/* file.c */ /* file.c */
extern int fileread(int f, int n); #include "file.h"
extern int insfile(int f, int n);
extern int filefind(int f, int n);
extern int viewfile(int f, int n);
extern int getfile(char *fname, int lockfl);
extern int readin(char *fname, int lockfl);
extern void makename(char *bname, char *fname);
extern void unqname(char *name);
extern int filewrite(int f, int n);
extern int filesave(int f, int n);
extern int writeout(char *fn);
extern int filename(int f, int n);
extern int ifile(char *fname);
/* exec.c */ /* exec.c */
extern int namedcmd(int f, int n); extern int namedcmd(int f, int n);

839
file.c
View File

@ -1,25 +1,32 @@
/* file.c /* file.c -- implements file.h */
#include "file.h"
/* file.c
* *
* The routines in this file handle the reading, writing * The routines in this file handle the reading, writing
* and lookup of disk files. All of details about the * and lookup of disk files. All of details about the
* reading and writing of the disk are in "fileio.c". * reading and writing of the disk are in "fileio.c".
* *
* modified by Petri Kutvonen * modified by Petri Kutvonen
*/ */
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "crypt.h" #include "crypt.h"
#include "display.h"
#include "estruct.h" #include "estruct.h"
#include "edef.h" #include "edef.h"
#include "efunc.h" #include "efunc.h"
#include "fileio.h" #include "fileio.h"
#include "input.h"
#include "line.h" #include "line.h"
#include "main.h"
#if defined(PKCODE) #if defined(PKCODE)
/* Max number of lines from one file. */ /* Max number of lines from one file. */
#define MAXNLINE 10000000 #define MAXNLINE 10000000
#endif #endif
/* /*
@ -31,14 +38,14 @@
*/ */
int fileread(int f, int n) int fileread(int f, int n)
{ {
int s; int s;
char fname[NFILEN]; char fname[NFILEN];
if (restflag) /* don't allow this command if restricted */ if (restflag) /* don't allow this command if restricted */
return resterr(); return resterr();
if ((s = mlreply("Read file: ", fname, NFILEN)) != TRUE) if ((s = mlreply("Read file: ", fname, NFILEN)) != TRUE)
return s; return s;
return readin(fname, TRUE); return readin(fname, TRUE);
} }
/* /*
@ -50,18 +57,18 @@ int fileread(int f, int n)
*/ */
int insfile(int f, int n) int insfile(int f, int n)
{ {
int s; int s;
char fname[NFILEN]; char fname[NFILEN];
if (restflag) /* don't allow this command if restricted */ if (restflag) /* don't allow this command if restricted */
return resterr(); return resterr();
if (curbp->b_mode & MDVIEW) /* don't allow this command if */ if (curbp->b_mode & MDVIEW) /* don't allow this command if */
return rdonly(); /* we are in read only mode */ return rdonly(); /* we are in read only mode */
if ((s = mlreply("Insert file: ", fname, NFILEN)) != TRUE) if ((s = mlreply("Insert file: ", fname, NFILEN)) != TRUE)
return s; return s;
if ((s = ifile(fname)) != TRUE) if ((s = ifile(fname)) != TRUE)
return s; return s;
return reposition(TRUE, -1); return reposition(TRUE, -1);
} }
/* /*
@ -75,132 +82,132 @@ int insfile(int f, int n)
*/ */
int filefind(int f, int n) int filefind(int f, int n)
{ {
char fname[NFILEN]; /* file user wishes to find */ char fname[NFILEN]; /* file user wishes to find */
int s; /* status return */ int s; /* status return */
if (restflag) /* don't allow this command if restricted */ if (restflag) /* don't allow this command if restricted */
return resterr(); return resterr();
if ((s = mlreply("Find file: ", fname, NFILEN)) != TRUE) if ((s = mlreply("Find file: ", fname, NFILEN)) != TRUE)
return s; return s;
return getfile(fname, TRUE); return getfile(fname, TRUE);
} }
int viewfile(int f, int n) int viewfile(int f, int n)
{ /* visit a file in VIEW mode */ { /* visit a file in VIEW mode */
char fname[NFILEN]; /* file user wishes to find */ char fname[NFILEN]; /* file user wishes to find */
int s; /* status return */ int s; /* status return */
struct window *wp; /* scan for windows that need updating */ struct window *wp; /* scan for windows that need updating */
if (restflag) /* don't allow this command if restricted */ if (restflag) /* don't allow this command if restricted */
return resterr(); return resterr();
if ((s = mlreply("View file: ", fname, NFILEN)) != TRUE) if ((s = mlreply("View file: ", fname, NFILEN)) != TRUE)
return s; return s;
s = getfile(fname, FALSE); s = getfile(fname, FALSE);
if (s) { /* if we succeed, put it in view mode */ if (s) { /* if we succeed, put it in view mode */
curwp->w_bufp->b_mode |= MDVIEW; curwp->w_bufp->b_mode |= MDVIEW;
/* scan through and update mode lines of all windows */ /* scan through and update mode lines of all windows */
wp = wheadp; wp = wheadp;
while (wp != NULL) { while (wp != NULL) {
wp->w_flag |= WFMODE; wp->w_flag |= WFMODE;
wp = wp->w_wndp; wp = wp->w_wndp;
} }
} }
return s; return s;
} }
#if CRYPT #if CRYPT
static int resetkey(void) static int resetkey(void)
{ /* reset the encryption key if needed */ { /* reset the encryption key if needed */
int s; /* return status */ int s; /* return status */
/* turn off the encryption flag */ /* turn off the encryption flag */
cryptflag = FALSE; cryptflag = FALSE;
/* if we are in crypt mode */ /* if we are in crypt mode */
if (curbp->b_mode & MDCRYPT) { if (curbp->b_mode & MDCRYPT) {
if (curbp->b_key[0] == 0) { if (curbp->b_key[0] == 0) {
s = set_encryption_key(FALSE, 0); s = set_encryption_key(FALSE, 0);
if (s != TRUE) if (s != TRUE)
return s; return s;
} }
/* let others know... */ /* let others know... */
cryptflag = TRUE; cryptflag = TRUE;
/* and set up the key to be used! */ /* and set up the key to be used! */
/* de-encrypt it */ /* de-encrypt it */
myencrypt((char *) NULL, 0); myencrypt((char *) NULL, 0);
myencrypt(curbp->b_key, strlen(curbp->b_key)); myencrypt(curbp->b_key, strlen(curbp->b_key));
/* re-encrypt it...seeding it to start */ /* re-encrypt it...seeding it to start */
myencrypt((char *) NULL, 0); myencrypt((char *) NULL, 0);
myencrypt(curbp->b_key, strlen(curbp->b_key)); myencrypt(curbp->b_key, strlen(curbp->b_key));
} }
return TRUE; return TRUE;
} }
#endif #endif
/* /*
* getfile() * getfile()
* *
* char fname[]; file name to find * char fname[]; file name to find
* int lockfl; check the file for locks? * int lockfl; check the file for locks?
*/ */
int getfile(char *fname, int lockfl) int getfile(char *fname, int lockfl)
{ {
struct buffer *bp; struct buffer *bp;
struct line *lp; struct line *lp;
int i; int i;
int s; int s;
char bname[NBUFN]; /* buffer name to put file */ char bname[NBUFN]; /* buffer name to put file */
#if MSDOS #if MSDOS
mklower(fname); /* msdos isn't case sensitive */ mklower(fname); /* msdos isn't case sensitive */
#endif #endif
for (bp = bheadp; bp != NULL; bp = bp->b_bufp) { for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
if ((bp->b_flag & BFINVS) == 0 if ((bp->b_flag & BFINVS) == 0
&& strcmp(bp->b_fname, fname) == 0) { && strcmp(bp->b_fname, fname) == 0) {
swbuffer(bp); swbuffer(bp);
lp = curwp->w_dotp; lp = curwp->w_dotp;
i = curwp->w_ntrows / 2; i = curwp->w_ntrows / 2;
while (i-- && lback(lp) != curbp->b_linep) while (i-- && lback(lp) != curbp->b_linep)
lp = lback(lp); lp = lback(lp);
curwp->w_linep = lp; curwp->w_linep = lp;
curwp->w_flag |= WFMODE | WFHARD; curwp->w_flag |= WFMODE | WFHARD;
cknewwindow(); cknewwindow();
mlwrite("(Old buffer)"); mlwrite("(Old buffer)");
return TRUE; return TRUE;
} }
} }
makename(bname, fname); /* New buffer name. */ makename(bname, fname); /* New buffer name. */
while ((bp = bfind(bname, FALSE, 0)) != NULL) { while ((bp = bfind(bname, FALSE, 0)) != NULL) {
/* old buffer name conflict code */ /* old buffer name conflict code */
s = mlreply("Buffer name: ", bname, NBUFN); s = mlreply("Buffer name: ", bname, NBUFN);
if (s == ABORT) /* ^G to just quit */ if (s == ABORT) /* ^G to just quit */
return s; return s;
if (s == FALSE) { /* CR to clobber it */ if (s == FALSE) { /* CR to clobber it */
makename(bname, fname); makename(bname, fname);
break; break;
} }
} }
if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) { if (bp == NULL && (bp = bfind(bname, TRUE, 0)) == NULL) {
mlwrite("Cannot create buffer"); mlwrite("Cannot create buffer");
return FALSE; return FALSE;
} }
if (--curbp->b_nwnd == 0) { /* Undisplay. */ if (--curbp->b_nwnd == 0) { /* Undisplay. */
curbp->b_dotp = curwp->w_dotp; curbp->b_dotp = curwp->w_dotp;
curbp->b_doto = curwp->w_doto; curbp->b_doto = curwp->w_doto;
curbp->b_markp = curwp->w_markp; curbp->b_markp = curwp->w_markp;
curbp->b_marko = curwp->w_marko; curbp->b_marko = curwp->w_marko;
} }
curbp = bp; /* Switch to it. */ curbp = bp; /* Switch to it. */
curwp->w_bufp = bp; curwp->w_bufp = bp;
curbp->b_nwnd++; curbp->b_nwnd++;
s = readin(fname, lockfl); /* Read it in. */ s = readin(fname, lockfl); /* Read it in. */
cknewwindow(); cknewwindow();
return s; return s;
} }
/* /*
@ -211,110 +218,110 @@ int getfile(char *fname, int lockfl)
* The command bound to M-FNR is called after the buffer is set up * The command bound to M-FNR is called after the buffer is set up
* and before it is read. * and before it is read.
* *
* char fname[]; name of file to read * char fname[]; name of file to read
* int lockfl; check for file locks? * int lockfl; check for file locks?
*/ */
int readin(char *fname, int lockfl) int readin(char *fname, int lockfl)
{ {
struct line *lp1; struct line *lp1;
struct line *lp2; struct line *lp2;
int i; int i;
struct window *wp; struct window *wp;
struct buffer *bp; struct buffer *bp;
int s; int s;
int nbytes; int nbytes;
int nline; int nline;
char mesg[NSTRING]; char mesg[NSTRING];
#if (FILOCK && BSD) || SVR4 #if (FILOCK && BSD) || SVR4
if (lockfl && lockchk(fname) == ABORT) if (lockfl && lockchk(fname) == ABORT)
#if PKCODE #if PKCODE
{ {
s = FIOFNF; s = FIOFNF;
bp = curbp; bp = curbp;
strcpy(bp->b_fname, ""); strcpy(bp->b_fname, "");
goto out; goto out;
} }
#else #else
return ABORT; return ABORT;
#endif #endif
#endif #endif
#if CRYPT #if CRYPT
s = resetkey(); s = resetkey();
if (s != TRUE) if (s != TRUE)
return s; return s;
#endif #endif
bp = curbp; /* Cheap. */ bp = curbp; /* Cheap. */
if ((s = bclear(bp)) != TRUE) /* Might be old. */ if ((s = bclear(bp)) != TRUE) /* Might be old. */
return s; return s;
bp->b_flag &= ~(BFINVS | BFCHG); bp->b_flag &= ~(BFINVS | BFCHG);
strcpy(bp->b_fname, fname); strcpy(bp->b_fname, fname);
/* let a user macro get hold of things...if he wants */ /* let a user macro get hold of things...if he wants */
execute(META | SPEC | 'R', FALSE, 1); execute(META | SPEC | 'R', FALSE, 1);
if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */ if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */
goto out; goto out;
if (s == FIOFNF) { /* File not found. */ if (s == FIOFNF) { /* File not found. */
mlwrite("(New file)"); mlwrite("(New file)");
goto out; goto out;
} }
/* read the file in */ /* read the file in */
mlwrite("(Reading file)"); mlwrite("(Reading file)");
nline = 0; nline = 0;
while ((s = ffgetline()) == FIOSUC) { while ((s = ffgetline()) == FIOSUC) {
nbytes = strlen(fline); nbytes = strlen(fline);
if ((lp1 = lalloc(nbytes)) == NULL) { if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM; /* Keep message on the */ s = FIOMEM; /* Keep message on the */
break; /* display. */ break; /* display. */
} }
#if PKCODE #if PKCODE
if (nline > MAXNLINE) { if (nline > MAXNLINE) {
s = FIOMEM; s = FIOMEM;
break; break;
} }
#endif #endif
lp2 = lback(curbp->b_linep); lp2 = lback(curbp->b_linep);
lp2->l_fp = lp1; lp2->l_fp = lp1;
lp1->l_fp = curbp->b_linep; lp1->l_fp = curbp->b_linep;
lp1->l_bp = lp2; lp1->l_bp = lp2;
curbp->b_linep->l_bp = lp1; curbp->b_linep->l_bp = lp1;
for (i = 0; i < nbytes; ++i) for (i = 0; i < nbytes; ++i)
lputc(lp1, i, fline[i]); lputc(lp1, i, fline[i]);
++nline; ++nline;
} }
ffclose(); /* Ignore errors. */ ffclose(); /* Ignore errors. */
strcpy(mesg, "("); strcpy(mesg, "(");
if (s == FIOERR) { if (s == FIOERR) {
strcat(mesg, "I/O ERROR, "); strcat(mesg, "I/O ERROR, ");
curbp->b_flag |= BFTRUNC; curbp->b_flag |= BFTRUNC;
} }
if (s == FIOMEM) { if (s == FIOMEM) {
strcat(mesg, "OUT OF MEMORY, "); strcat(mesg, "OUT OF MEMORY, ");
curbp->b_flag |= BFTRUNC; curbp->b_flag |= BFTRUNC;
} }
sprintf(&mesg[strlen(mesg)], "Read %d line", nline); sprintf(&mesg[strlen(mesg)], "Read %d line", nline);
if (nline != 1) if (nline != 1)
strcat(mesg, "s"); strcat(mesg, "s");
strcat(mesg, ")"); strcat(mesg, ")");
mlwrite(mesg); mlwrite(mesg);
out: out:
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) { for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) { if (wp->w_bufp == curbp) {
wp->w_linep = lforw(curbp->b_linep); wp->w_linep = lforw(curbp->b_linep);
wp->w_dotp = lforw(curbp->b_linep); wp->w_dotp = lforw(curbp->b_linep);
wp->w_doto = 0; wp->w_doto = 0;
wp->w_markp = NULL; wp->w_markp = NULL;
wp->w_marko = 0; wp->w_marko = 0;
wp->w_flag |= WFMODE | WFHARD; wp->w_flag |= WFMODE | WFHARD;
} }
} }
if (s == FIOERR || s == FIOFNF) /* False if error. */ if (s == FIOERR || s == FIOFNF) /* False if error. */
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
/* /*
@ -326,59 +333,59 @@ int readin(char *fname, int lockfl)
*/ */
void makename(char *bname, char *fname) void makename(char *bname, char *fname)
{ {
char *cp1; char *cp1;
char *cp2; char *cp2;
cp1 = &fname[0]; cp1 = &fname[0];
while (*cp1 != 0) while (*cp1 != 0)
++cp1; ++cp1;
#if VMS #if VMS
#if PKCODE #if PKCODE
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != ']' while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != ']'
&& cp1[-1] != '>') && cp1[-1] != '>')
#else #else
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != ']') while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != ']')
#endif #endif
--cp1; --cp1;
#endif #endif
#if MSDOS #if MSDOS
while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != '\\' while (cp1 != &fname[0] && cp1[-1] != ':' && cp1[-1] != '\\'
&& cp1[-1] != '/') && cp1[-1] != '/')
--cp1; --cp1;
#endif #endif
#if V7 | USG | BSD #if V7 | USG | BSD
while (cp1 != &fname[0] && cp1[-1] != '/') while (cp1 != &fname[0] && cp1[-1] != '/')
--cp1; --cp1;
#endif #endif
cp2 = &bname[0]; cp2 = &bname[0];
while (cp2 != &bname[NBUFN - 1] && *cp1 != 0 && *cp1 != ';') while (cp2 != &bname[NBUFN - 1] && *cp1 != 0 && *cp1 != ';')
*cp2++ = *cp1++; *cp2++ = *cp1++;
*cp2 = 0; *cp2 = 0;
} }
/* /*
* make sure a buffer name is unique * make sure a buffer name is unique
* *
* char *name; name to check on * char *name; name to check on
*/ */
void unqname(char *name) void unqname(char *name)
{ {
char *sp; char *sp;
/* check to see if it is in the buffer list */ /* check to see if it is in the buffer list */
while (bfind(name, 0, FALSE) != NULL) { while (bfind(name, 0, FALSE) != NULL) {
/* go to the end of the name */ /* go to the end of the name */
sp = name; sp = name;
while (*sp) while (*sp)
++sp; ++sp;
if (sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) { if (sp == name || (*(sp - 1) < '0' || *(sp - 1) > '8')) {
*sp++ = '0'; *sp++ = '0';
*sp = 0; *sp = 0;
} else } else
*(--sp) += 1; *(--sp) += 1;
} }
} }
/* /*
@ -392,25 +399,25 @@ void unqname(char *name)
*/ */
int filewrite(int f, int n) int filewrite(int f, int n)
{ {
struct window *wp; struct window *wp;
int s; int s;
char fname[NFILEN]; char fname[NFILEN];
if (restflag) /* don't allow this command if restricted */ if (restflag) /* don't allow this command if restricted */
return resterr(); return resterr();
if ((s = mlreply("Write file: ", fname, NFILEN)) != TRUE) if ((s = mlreply("Write file: ", fname, NFILEN)) != TRUE)
return s; return s;
if ((s = writeout(fname)) == TRUE) { if ((s = writeout(fname)) == TRUE) {
strcpy(curbp->b_fname, fname); strcpy(curbp->b_fname, fname);
curbp->b_flag &= ~BFCHG; curbp->b_flag &= ~BFCHG;
wp = wheadp; /* Update mode lines. */ wp = wheadp; /* Update mode lines. */
while (wp != NULL) { while (wp != NULL) {
if (wp->w_bufp == curbp) if (wp->w_bufp == curbp)
wp->w_flag |= WFMODE; wp->w_flag |= WFMODE;
wp = wp->w_wndp; wp = wp->w_wndp;
} }
} }
return s; return s;
} }
/* /*
@ -423,36 +430,36 @@ int filewrite(int f, int n)
*/ */
int filesave(int f, int n) int filesave(int f, int n)
{ {
struct window *wp; struct window *wp;
int s; int s;
if (curbp->b_mode & MDVIEW) /* don't allow this command if */ if (curbp->b_mode & MDVIEW) /* don't allow this command if */
return rdonly(); /* we are in read only mode */ return rdonly(); /* we are in read only mode */
if ((curbp->b_flag & BFCHG) == 0) /* Return, no changes. */ if ((curbp->b_flag & BFCHG) == 0) /* Return, no changes. */
return TRUE; return TRUE;
if (curbp->b_fname[0] == 0) { /* Must have a name. */ if (curbp->b_fname[0] == 0) { /* Must have a name. */
mlwrite("No file name"); mlwrite("No file name");
return FALSE; return FALSE;
} }
/* complain about truncated files */ /* complain about truncated files */
if ((curbp->b_flag & BFTRUNC) != 0) { if ((curbp->b_flag & BFTRUNC) != 0) {
if (mlyesno("Truncated file ... write it out") == FALSE) { if (mlyesno("Truncated file ... write it out") == FALSE) {
mlwrite("(Aborted)"); mlwrite("(Aborted)");
return FALSE; return FALSE;
} }
} }
if ((s = writeout(curbp->b_fname)) == TRUE) { if ((s = writeout(curbp->b_fname)) == TRUE) {
curbp->b_flag &= ~BFCHG; curbp->b_flag &= ~BFCHG;
wp = wheadp; /* Update mode lines. */ wp = wheadp; /* Update mode lines. */
while (wp != NULL) { while (wp != NULL) {
if (wp->w_bufp == curbp) if (wp->w_bufp == curbp)
wp->w_flag |= WFMODE; wp->w_flag |= WFMODE;
wp = wp->w_wndp; wp = wp->w_wndp;
} }
} }
return s; return s;
} }
/* /*
@ -465,41 +472,41 @@ int filesave(int f, int n)
*/ */
int writeout(char *fn) int writeout(char *fn)
{ {
int s; int s;
struct line *lp; struct line *lp;
int nline; int nline;
#if CRYPT #if CRYPT
s = resetkey(); s = resetkey();
if (s != TRUE) if (s != TRUE)
return s; return s;
#endif #endif
if ((s = ffwopen(fn)) != FIOSUC) { /* Open writes message. */ if ((s = ffwopen(fn)) != FIOSUC) { /* Open writes message. */
return FALSE; return FALSE;
} }
mlwrite("(Writing...)"); /* tell us were writing */ mlwrite("(Writing...)"); /* tell us were writing */
lp = lforw(curbp->b_linep); /* First line. */ lp = lforw(curbp->b_linep); /* First line. */
nline = 0; /* Number of lines. */ nline = 0; /* Number of lines. */
while (lp != curbp->b_linep) { while (lp != curbp->b_linep) {
if ((s = ffputline(&lp->l_text[0], llength(lp))) != FIOSUC) if ((s = ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
break; break;
++nline; ++nline;
lp = lforw(lp); lp = lforw(lp);
} }
if (s == FIOSUC) { /* No write error. */ if (s == FIOSUC) { /* No write error. */
s = ffclose(); s = ffclose();
if (s == FIOSUC) { /* No close error. */ if (s == FIOSUC) { /* No close error. */
if (nline == 1) if (nline == 1)
mlwrite("(Wrote 1 line)"); mlwrite("(Wrote 1 line)");
else else
mlwrite("(Wrote %d lines)", nline); mlwrite("(Wrote %d lines)", nline);
} }
} else /* Ignore close error */ } else /* Ignore close error */
ffclose(); /* if a write error. */ ffclose(); /* if a write error. */
if (s != FIOSUC) /* Some sort of error. */ if (s != FIOSUC) /* Some sort of error. */
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
/* /*
@ -513,26 +520,26 @@ int writeout(char *fn)
*/ */
int filename(int f, int n) int filename(int f, int n)
{ {
struct window *wp; struct window *wp;
int s; int s;
char fname[NFILEN]; char fname[NFILEN];
if (restflag) /* don't allow this command if restricted */ if (restflag) /* don't allow this command if restricted */
return resterr(); return resterr();
if ((s = mlreply("Name: ", fname, NFILEN)) == ABORT) if ((s = mlreply("Name: ", fname, NFILEN)) == ABORT)
return s; return s;
if (s == FALSE) if (s == FALSE)
strcpy(curbp->b_fname, ""); strcpy(curbp->b_fname, "");
else else
strcpy(curbp->b_fname, fname); strcpy(curbp->b_fname, fname);
wp = wheadp; /* Update mode lines. */ wp = wheadp; /* Update mode lines. */
while (wp != NULL) { while (wp != NULL) {
if (wp->w_bufp == curbp) if (wp->w_bufp == curbp)
wp->w_flag |= WFMODE; wp->w_flag |= WFMODE;
wp = wp->w_wndp; wp = wp->w_wndp;
} }
curbp->b_mode &= ~MDVIEW; /* no longer read only mode */ curbp->b_mode &= ~MDVIEW; /* no longer read only mode */
return TRUE; return TRUE;
} }
/* /*
@ -542,89 +549,89 @@ int filename(int f, int n)
*/ */
int ifile(char *fname) int ifile(char *fname)
{ {
struct line *lp0; struct line *lp0;
struct line *lp1; struct line *lp1;
struct line *lp2; struct line *lp2;
int i; int i;
struct buffer *bp; struct buffer *bp;
int s; int s;
int nbytes; int nbytes;
int nline; int nline;
char mesg[NSTRING]; char mesg[NSTRING];
bp = curbp; /* Cheap. */ bp = curbp; /* Cheap. */
bp->b_flag |= BFCHG; /* we have changed */ bp->b_flag |= BFCHG; /* we have changed */
bp->b_flag &= ~BFINVS; /* and are not temporary */ bp->b_flag &= ~BFINVS; /* and are not temporary */
if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */ if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */
goto out; goto out;
if (s == FIOFNF) { /* File not found. */ if (s == FIOFNF) { /* File not found. */
mlwrite("(No such file)"); mlwrite("(No such file)");
return FALSE; return FALSE;
} }
mlwrite("(Inserting file)"); mlwrite("(Inserting file)");
#if CRYPT #if CRYPT
s = resetkey(); s = resetkey();
if (s != TRUE) if (s != TRUE)
return s; return s;
#endif #endif
/* back up a line and save the mark here */ /* back up a line and save the mark here */
curwp->w_dotp = lback(curwp->w_dotp); curwp->w_dotp = lback(curwp->w_dotp);
curwp->w_doto = 0; curwp->w_doto = 0;
curwp->w_markp = curwp->w_dotp; curwp->w_markp = curwp->w_dotp;
curwp->w_marko = 0; curwp->w_marko = 0;
nline = 0; nline = 0;
while ((s = ffgetline()) == FIOSUC) { while ((s = ffgetline()) == FIOSUC) {
nbytes = strlen(fline); nbytes = strlen(fline);
if ((lp1 = lalloc(nbytes)) == NULL) { if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM; /* Keep message on the */ s = FIOMEM; /* Keep message on the */
break; /* display. */ break; /* display. */
} }
lp0 = curwp->w_dotp; /* line previous to insert */ lp0 = curwp->w_dotp; /* line previous to insert */
lp2 = lp0->l_fp; /* line after insert */ lp2 = lp0->l_fp; /* line after insert */
/* re-link new line between lp0 and lp2 */ /* re-link new line between lp0 and lp2 */
lp2->l_bp = lp1; lp2->l_bp = lp1;
lp0->l_fp = lp1; lp0->l_fp = lp1;
lp1->l_bp = lp0; lp1->l_bp = lp0;
lp1->l_fp = lp2; lp1->l_fp = lp2;
/* and advance and write out the current line */ /* and advance and write out the current line */
curwp->w_dotp = lp1; curwp->w_dotp = lp1;
for (i = 0; i < nbytes; ++i) for (i = 0; i < nbytes; ++i)
lputc(lp1, i, fline[i]); lputc(lp1, i, fline[i]);
++nline; ++nline;
} }
ffclose(); /* Ignore errors. */ ffclose(); /* Ignore errors. */
curwp->w_markp = lforw(curwp->w_markp); curwp->w_markp = lforw(curwp->w_markp);
strcpy(mesg, "("); strcpy(mesg, "(");
if (s == FIOERR) { if (s == FIOERR) {
strcat(mesg, "I/O ERROR, "); strcat(mesg, "I/O ERROR, ");
curbp->b_flag |= BFTRUNC; curbp->b_flag |= BFTRUNC;
} }
if (s == FIOMEM) { if (s == FIOMEM) {
strcat(mesg, "OUT OF MEMORY, "); strcat(mesg, "OUT OF MEMORY, ");
curbp->b_flag |= BFTRUNC; curbp->b_flag |= BFTRUNC;
} }
sprintf(&mesg[strlen(mesg)], "Inserted %d line", nline); sprintf(&mesg[strlen(mesg)], "Inserted %d line", nline);
if (nline > 1) if (nline > 1)
strcat(mesg, "s"); strcat(mesg, "s");
strcat(mesg, ")"); strcat(mesg, ")");
mlwrite(mesg); mlwrite(mesg);
out: out:
/* advance to the next line and mark the window for changes */ /* advance to the next line and mark the window for changes */
curwp->w_dotp = lforw(curwp->w_dotp); curwp->w_dotp = lforw(curwp->w_dotp);
curwp->w_flag |= WFHARD | WFMODE; curwp->w_flag |= WFHARD | WFMODE;
/* copy window parameters back to the buffer structure */ /* copy window parameters back to the buffer structure */
curbp->b_dotp = curwp->w_dotp; curbp->b_dotp = curwp->w_dotp;
curbp->b_doto = curwp->w_doto; curbp->b_doto = curwp->w_doto;
curbp->b_markp = curwp->w_markp; curbp->b_markp = curwp->w_markp;
curbp->b_marko = curwp->w_marko; curbp->b_marko = curwp->w_marko;
if (s == FIOERR) /* False if error. */ if (s == FIOERR) /* False if error. */
return FALSE; return FALSE;
return TRUE; return TRUE;
} }

18
file.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _FILE_H_
#define _FILE_H_
int fileread( int f, int n) ;
int insfile( int f, int n) ;
int filefind( int f, int n) ;
int viewfile( int f, int n) ;
int getfile( char *fname, int lockfl) ;
int readin( char *fname, int lockfl) ;
void makename( char *bname, char *fname) ;
void unqname( char *name) ;
int filewrite( int f, int n) ;
int filesave( int f, int n) ;
int writeout( char *fn) ;
int filename( int f, int n) ;
int ifile( char *fname) ;
#endif