-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.c
310 lines (279 loc) · 7.51 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include "estruct.h"
#include "etype.h"
#include "edef.h"
#include "english.h"
/*
* Read file "fname" into the current buffer, blowing away any text found
* there. Called by both the read and find commands. Return the final
* status of the read. Also called by the mainline, to read in a file
* specified on the command line as an argument. The command in $readhook is
* called after the buffer is set up and before it is read.
*/
readin(char fname[])
{
register LINE *lp1;
register LINE *lp2;
register int i;
register WINDOW *wp;
register BUFFER *bp;
register int s;
register int nbytes;
register int nline;
register int cmark; /* current mark */
char mesg[NSTRING];
bp = curbp; /* Cheap. */
if ((s = bclear(bp)) != TRUE) /* Might be old. */
return (s);
bp->b_flag &= ~(BFINVS | BFCHG);
strcpy(bp->b_fname, fname);
/* turn off ALL keyboard translation in case we get a dos error */
TTkclose();
if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */
goto out;
if (s == FIOFNF)
{ /* File not found. */
mlwrite(TEXT138);
/* "[New file]" */
goto out;
}
/* read the file in */
mlwrite(TEXT139);
/* "[Reading file]" */
nline = 0;
while ((s = ffgetline()) == FIOSUC)
{
nbytes = strlen(fline);
if ((lp1 = lalloc(nbytes)) == (LINE *) NULL)
{
s = FIOMEM; /* Keep message on the */
break; /* display. */
}
lp2 = lback(curbp->b_linep);
lp2->l_fp = lp1;
lp1->l_fp = curbp->b_linep;
lp1->l_bp = lp2;
curbp->b_linep->l_bp = lp1;
for (i = 0; i < nbytes; ++i)
lputc(lp1, i, fline[i]);
++nline;
}
ffclose(); /* Ignore errors. */
strcpy(mesg, "[");
if (s == FIOERR)
{
strcat(mesg, TEXT141);
/* "I/O ERROR, " */
curbp->b_flag |= BFTRUNC;
}
if (s == FIOMEM)
{
strcat(mesg, TEXT142);
/* "OUT OF MEMORY, " */
curbp->b_flag |= BFTRUNC;
}
strcat(mesg, TEXT140);
/* "Read " */
strcat(mesg, int_asc(nline));
strcat(mesg, TEXT143);
/* " line" */
if (nline > 1)
strcat(mesg, "s");
strcat(mesg, "]");
mlwrite(mesg);
out:
TTkopen(); /* open the keyboard again */
for (wp = wheadp; wp != (WINDOW *) NULL; wp = wp->w_wndp)
{
if (wp->w_bufp == curbp)
{
wp->w_linep = lforw(curbp->b_linep);
wp->w_dotp = lforw(curbp->b_linep);
wp->w_doto = 0;
for (cmark = 0; cmark < NMARKS; cmark++)
{
wp->w_markp[cmark] = (LINE *) NULL;
wp->w_marko[cmark] = 0;
}
wp->w_flag |= WFMODE | WFHARD;
}
}
if (s == FIOERR || s == FIOFNF) /* False if error. */
return (FALSE);
return (TRUE);
}
/*
* Take a file name, and from it fabricate a buffer name. This routine knows
* about the syntax of file names on the target system. I suppose that this
* information could be put in a better place than a line of code. Returns a
* pointer into fname indicating the end of the file path; i.e., 1 character
* BEYOND the path name.
*/
char *makename(char bname[], char fname[])
{
register char *cp1;
register char *cp2;
register char *pathp;
cp1 = &fname[0];
while (*cp1 != 0)
++cp1;
while (cp1 != &fname[0] && cp1[-1] != '/')
--cp1;
/* cp1 is pointing to the first real filename char */
pathp = cp1;
cp2 = &bname[0];
while (cp2 != &bname[NBUFN - 1] && *cp1 != 0 && *cp1 != ';')
*cp2++ = *cp1++;
*cp2 = 0;
return (pathp);
}
/*
* Save the contents of the current buffer in its associatd file. Do nothing
* if nothing has changed (this may be a bug, not a feature). Error if there
* is no remembered file name for the buffer. Bound to "C-X C-S". May get
* called by "C-Z".
*/
filesave()
{
register int s;
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
return (rdonly()); /* we are in read only mode */
if ((curbp->b_flag & BFCHG) == 0) /* Return, no changes. */
return (TRUE);
if (curbp->b_fname[0] == 0)
{ /* Must have a name. */
mlwrite(TEXT145);
/* "No file name" */
return (FALSE);
}
/* complain about truncated files */
if ((curbp->b_flag & BFTRUNC) != 0)
{
if (mlyesno(TEXT146) == FALSE)
{
/* "Truncated file..write it out" */
mlwrite(TEXT8);
/* "[Aborted]" */
return (FALSE);
}
}
/* complain about narrowed buffers */
if ((curbp->b_flag & BFNAROW) != 0)
{
if (mlyesno(TEXT147) == FALSE)
{
/* "Narrowed Buffer..write it out" */
mlwrite(TEXT8);
/* "[Aborted]" */
return (FALSE);
}
}
if ((s = writeout(curbp->b_fname)) == TRUE)
{
curbp->b_flag &= ~BFCHG;
/* Update mode lines. */
upmode();
}
return (s);
}
/*
* This function performs the details of file writing. It uses the file
* management routines in the "fileio.c" package. The number of lines written
* is displayed. Several errors are posible, and cause writeout to return a
* FALSE result. When $ssave is TRUE, the buffer is written out to a
* temporary file, and then the old file is unlinked and the temporary
* renamed to the original name. Before the file is written, a user
* specifyable routine (in $writehook) can be run.
*/
writeout(char *fn)
{
register LINE *lp; /* line to scan while writing */
register char *sp; /* temporary string pointer */
register int nline; /* number of lines written */
int status; /* return status */
int sflag; /* are we safe saving? */
char tname[NSTRING]; /* temporary file name */
char buf[NSTRING]; /* message buffer */
/* determine if we will use the save method */
sflag = FALSE;
if (ssave && fexist(fn))
sflag = TRUE;
/* turn off ALL keyboard translation in case we get a dos error */
TTkclose();
/* Perform Safe Save..... */
if (sflag)
{
/* duplicate original file name, and find where to trunc it */
sp = tname + (makename(tname, fn) - fn) + 1;
strcpy(tname, fn);
/* create a unique name, using random numbers */
do
{
*sp = 0;
strcat(tname, int_asc(ernd()));
} while (fexist(tname));
/* open the temporary file */
status = ffwopen(tname);
}
else
status = ffwopen(fn);
/* if the open failed.. clean up and abort */
if (status != FIOSUC)
{
TTkopen();
return (FALSE);
}
/* write the current buffer's lines to the open disk file */
mlwrite(TEXT148); /* tell us that we're writing */
/* "[Writing...]" */
lp = lforw(curbp->b_linep); /* start at the first line. */
nline = 0; /* track the Number of lines */
while (lp != curbp->b_linep)
{
if ((status = ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
break;
++nline;
lp = lforw(lp);
}
/* report on status of file write */
*buf = 0;
status |= ffclose();
if (status == FIOSUC)
{
/* report on success (or lack therof) */
strcpy(buf, TEXT149);
/* "[Wrote " */
strcat(buf, int_asc(nline));
strcat(buf, TEXT143);
/* " line" */
if (nline > 1)
strcat(buf, "s");
if (sflag)
{
/* erase original file */
/* rename temporary file to original name */
if (unlink(fn) == 0 && rename1(tname, fn) == 0)
;
else
{
strcat(buf, TEXT150);
/* ", saved as " */
strcat(buf, tname);
status = FIODEL; /* failed */
}
}
strcat(buf, "]");
mlwrite(buf);
}
/* reopen the keyboard, and return our status */
TTkopen();
return (status == FIOSUC);
}
int fileread ()
{
char *fname; /* file name to read */
if ((fname = gtfilename (TEXT131)) == (char *) NULL)
/* "Read file" */
return (FALSE);
return (readin (fname));
}