forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunixfile.c
235 lines (202 loc) · 4.82 KB
/
unixfile.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
/** @file unixfile.c
*
* The interface to a fast variety of file routines in the unix system.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2023 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ Includes :
File with direct interface to the UNIX functions.
This makes a big difference in speed!
*/
#include "form3.h"
/*[13jul2005 mt]:*/
#ifdef SAFESIGNAL
/*[15oct2004 mt]:*/
/*To access errno variable and EINTR constant:*/
#include <errno.h>
/*:[15oct2004 mt]*/
#endif
/*:[13jul2005 mt]*/
#ifdef UFILES
FILES TheStdout = { 1 };
FILES *Ustdout = &TheStdout;
#ifdef GPP
extern "C" open();
extern "C" close();
extern "C" read();
extern "C" write();
extern "C" lseek();
#endif
/*
#] Includes :
#[ Uopen :
*/
FILES *Uopen(char *filename, char *mode)
{
FILES *f = (FILES *)Malloc1(sizeof(FILES),"Uopen");
int flags = 0, rights = 0644;
while ( *mode ) {
if ( *mode == 'r' ) { flags |= O_RDONLY; }
else if ( *mode == 'w' ) { flags |= O_CREAT | O_TRUNC; }
else if ( *mode == 'a' ) { flags |= O_APPEND; }
else if ( *mode == 'b' ) { }
else if ( *mode == '+' ) { flags |= O_RDWR; }
mode++;
}
f->descriptor = open(filename,flags,rights);
if ( f->descriptor >= 0 ) return(f);
if ( ( flags & O_APPEND ) != 0 ) {
flags |= O_CREAT;
f->descriptor = open(filename,flags,rights);
if ( f->descriptor >= 0 ) return(f);
}
M_free(f,"Uopen");
return(0);
}
/*
#] Uopen :
#[ Uclose :
*/
int Uclose(FILES *f)
{
int retval;
if ( f ) {
retval = close(f->descriptor);
M_free(f,"Uclose");
return(retval);
}
return(EOF);
}
/*
#] Uclose :
#[ Uread :
*/
size_t Uread(char *ptr, size_t size, size_t nobj, FILES *f)
{
/*[13jul2005 mt]:*/
#ifdef SAFESIGNAL
/*[15oct2004 mt]:*/
/*Operation read() can be interrupted by a signal. Note, this is rather unlikely,
so we do not save size*nobj for future attempts*/
size_t ret;
/*If the syscall is interrupted by a signal before it
succeeded in getting any progress, it must be repeated:*/
while( ( (ret=read(f->descriptor,ptr,size*nobj))<1)&&(errno == EINTR) );
return(ret);
#else
#ifdef DEEPDEBUG
{
POSITION pos;
SETBASEPOSITION(pos,lseek(f->descriptor,0L,SEEK_CUR));
MesPrint("handle %d: reading %ld bytes from position %p\n",f->descriptor,size*nobj,pos);
}
#endif
return(read(f->descriptor,ptr,size*nobj));
#endif
/*:[15oct2004 mt]*/
/*:[13jul2005 mt]*/
}
/*
#] Uread :
#[ Uwrite :
*/
size_t Uwrite(char *ptr, size_t size, size_t nobj, FILES *f)
{
/*[13jul2005 mt]:*/
#ifdef SAFESIGNAL
/*[15oct2004 mt]:*/
/*Operation write() can be interrupted by a signal. */
size_t ret;
size_t thesize=size*nobj;
/*If the syscall is interrupted by a signal before it
succeeded in getting any progress, it must be repeated:*/
while( ( (ret=write(f->descriptor,ptr,thesize))<1 )&&(errno == EINTR) );
return(ret);
#else
#ifdef DEEPDEBUG
{
POSITION pos;
SETBASEPOSITION(pos,lseek(f->descriptor,0L,SEEK_CUR));
MesPrint("handle %d: writing %ld bytes to position %p\n",f->descriptor,size*nobj,pos);
}
#endif
return(write(f->descriptor,ptr,size*nobj));
/*:[15oct2004 mt]*/
#endif
/*:[13jul2005 mt]*/
}
/*
#] Uwrite :
#[ Useek :
*/
int Useek(FILES *f, off_t offset, int origin)
{
if ( f && ( lseek(f->descriptor,offset,origin) >= 0 ) ) return(0);
return(-1);
}
/*
#] Useek :
#[ Utell :
*/
off_t Utell(FILES *f)
{
if ( f ) return((off_t)lseek(f->descriptor,0L,SEEK_CUR));
else return(-1);
}
/*
#] Utell :
#[ Uflush :
*/
void Uflush(FILES *f) { DUMMYUSE(f); }
/*
#] Uflush :
#[ Ugetpos :
*/
int Ugetpos(FILES *f, fpos_t *ptr)
{
DUMMYUSE(f); DUMMYUSE(ptr);
return(-1);
}
/*
#] Ugetpos :
#[ Usetpos :
*/
int Usetpos(FILES *f,fpos_t *ptr)
{
DUMMYUSE(f); DUMMYUSE(ptr);
return(-1);
}
/*
#] Usetpos :
#[ Usetbuf :
*/
void Usetbuf(FILES *f, char *ptr) { DUMMYUSE(f); DUMMYUSE(ptr); }
/*
#] Usetbuf :
*/
#endif