forked from th-otto/gulam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fio.c
154 lines (133 loc) · 3.33 KB
/
fio.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
/*
* $Header: f:/src/gulam\RCS\fio.c,v 1.1 1991/09/10 01:02:04 apratt Exp $ $Locker: $
* ======================================================================
* $Log: fio.c,v $
* Revision 1.1 1991/09/10 01:02:04 apratt
* First CI of AKP
*
* Revision: 1.3 89.06.16.17.23.26 apratt
* Header style change.
*
* Revision: 1.2 89.02.22.14.42.38 Author: apratt
* Changed userfeedback level of (Reading...) and (Read...) messages to 2.
* At verbosity 1, they won't appear (useful for mwc which starts gu which
* reads gulam.g each time).
*
* Revision: 1.1 88.06.03.15.38.26 Author: apratt
* Initial Revision
*
*/
/*
fio.c of Gulam/uE April 1986
copyright (c) 1987 [email protected]
The routines in this file read and write ASCII files from the disk.
All of the knowledge about files are here. A better message writing
scheme should be used. This is a complete rewrite, by [email protected], of
what was in microEmacs.
*/
#include "ue.h"
int lnn; /* line number */
int evalu;
long ntotal;
static int fd; /* File descriptor, all functions. */
static int rwflag; /* 0=read or 1=write flag */
static long x;
static long n;
static long lsz;
static uchar *txtp;
static uchar TWD[] = "that was a directory!";
/* Open a file for writing. Return FIOSUC if all is well, and FIOERR
on errors. */
int ffwopen(char *fn)
{
if (isdir(fn))
{
userfeedback(TWD, -1);
return FIOERR;
}
if ((fd = (int)gfcreate(fn, 0)) < 0)
{
userfeedback("Cannot open file for writing", -1);
return FIOERR;
}
/* we try to allocate a large buffer for read/write */
lsz = 0x0FFFFFFFL;
txtp = maxalloc(&lsz);
x = n = 0L;
rwflag = 1;
userfeedback(sprintp("(Writing %s ...)", fn), 1);
return FIOSUC;
}
static int twrite(void)
{
if (gfwrite(fd, txtp, x) == x)
{
x = 0;
return FIOSUC;
}
userfeedback("Write I/O error", -1);
return FIOERR;
}
/* Close a file. Should look at the status in all systems. */
int ffclose(void)
{
int r;
r = FIOSUC;
if ((rwflag == 1) && (x < lsz))
r = twrite();
gfclose(fd); /* check for errors ?? */
maxfree(txtp);
return r;
}
/* 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. */
int ffputline(char *buf, int nb)
{
int i;
char *p;
p = buf;
xxxx:
while (nb > 0)
{
if (x + nb > lsz && twrite() == FIOERR)
return FIOERR;
i = (int)(x + nb <= lsz ? nb : lsz - x);
cpymem(txtp + x, p, i);
p += i;
nb -= i;
x += i;
}
if (p != &DFLNSEP[LDFLNSEP])
{
p = DFLNSEP;
nb = LDFLNSEP;
goto xxxx;
}
return FIOSUC;
}
/* Open a file for reading, and apply func to each of its lines. If
file not found return. */
int frdapply(char *fnm, void (*fn)(uchar *q, int n))
{
if (isdir(fnm))
{
userfeedback(TWD, -1);
return FIOERR;
}
if ((fd = (int)gfopen(fnm, 0)) < 0)
return FIOFNF;
userfeedback(sprintp("(Reading %s ...)", fnm), 2);
lnn = evalu = 0;
ntotal = 0;
eachline(fd, fn); /* Fcloses(fd) also */
if (evalu < 0)
emsg = "File read error";
if (emsg)
{
mlmesg(emsg);
return FIOERR;
}
userfeedback(sprintp("(Read %s, %D bytes in %d lines)", fnm, ntotal, lnn), 2);
return FIOSUC;
}