FORM  4.2
unixfile.c
Go to the documentation of this file.
1 
5 /* #[ License : */
6 /*
7  * Copyright (C) 1984-2017 J.A.M. Vermaseren
8  * When using this file you are requested to refer to the publication
9  * J.A.M.Vermaseren "New features of FORM" math-ph/0010025
10  * This is considered a matter of courtesy as the development was paid
11  * for by FOM the Dutch physics granting agency and we would like to
12  * be able to track its scientific use to convince FOM of its value
13  * for the community.
14  *
15  * This file is part of FORM.
16  *
17  * FORM is free software: you can redistribute it and/or modify it under the
18  * terms of the GNU General Public License as published by the Free Software
19  * Foundation, either version 3 of the License, or (at your option) any later
20  * version.
21  *
22  * FORM is distributed in the hope that it will be useful, but WITHOUT ANY
23  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25  * details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with FORM. If not, see <http://www.gnu.org/licenses/>.
29  */
30 /* #] License : */
31 /*
32  #[ Includes :
33 
34  File with direct interface to the UNIX functions.
35  This makes a big difference in speed!
36 */
37 #include "form3.h"
38 
39 
40 /*[13jul2005 mt]:*/
41 #ifdef SAFESIGNAL
42 /*[15oct2004 mt]:*/
43 /*To access errno variable and EINTR constant:*/
44 #include <errno.h>
45 /*:[15oct2004 mt]*/
46 #endif
47 /*:[13jul2005 mt]*/
48 
49 #ifdef UFILES
50 
51 FILES TheStdout = { 1 };
52 FILES *Ustdout = &TheStdout;
53 
54 #ifdef GPP
55 extern "C" open();
56 extern "C" close();
57 extern "C" read();
58 extern "C" write();
59 extern "C" lseek();
60 #endif
61 
62 /*
63  #] Includes :
64  #[ Uopen :
65 */
66 
67 FILES *Uopen(char *filename, char *mode)
68 {
69  FILES *f = (FILES *)Malloc1(sizeof(FILES),"Uopen");
70  int flags = 0, rights = 0644;
71  while ( *mode ) {
72  if ( *mode == 'r' ) { flags |= O_RDONLY; }
73  else if ( *mode == 'w' ) { flags |= O_CREAT | O_TRUNC; }
74  else if ( *mode == 'a' ) { flags |= O_APPEND; }
75  else if ( *mode == 'b' ) { }
76  else if ( *mode == '+' ) { flags |= O_RDWR; }
77  mode++;
78  }
79  f->descriptor = open(filename,flags,rights);
80  if ( f->descriptor >= 0 ) return(f);
81  if ( ( flags & O_APPEND ) != 0 ) {
82  flags |= O_CREAT;
83  f->descriptor = open(filename,flags,rights);
84  if ( f->descriptor >= 0 ) return(f);
85  }
86  M_free(f,"Uopen");
87  return(0);
88 }
89 
90 /*
91  #] Uopen :
92  #[ Uclose :
93 */
94 
95 int Uclose(FILES *f)
96 {
97  int retval;
98  if ( f ) {
99  retval = close(f->descriptor);
100  M_free(f,"Uclose");
101  return(retval);
102  }
103  return(EOF);
104 }
105 
106 /*
107  #] Uclose :
108  #[ Uread :
109 */
110 
111 
112 size_t Uread(char *ptr, size_t size, size_t nobj, FILES *f)
113 {
114 /*[13jul2005 mt]:*/
115 #ifdef SAFESIGNAL
116 
117 /*[15oct2004 mt]:*/
118 /*Operation read() can be interrupted by a signal. Note, this is rather unlikely,
119 so we do not save size*nobj for future attempts*/
120 size_t ret;
121  /*If the syscall is interrupted by a signal before it
122  succeeded in getting any progress, it must be repeated:*/
123  while( ( (ret=read(f->descriptor,ptr,size*nobj))<1)&&(errno == EINTR) );
124  return(ret);
125 #else
126 #ifdef DEEPDEBUG
127  {
128  POSITION pos;
129  SETBASEPOSITION(pos,lseek(f->descriptor,0L,SEEK_CUR));
130  MesPrint("handle %d: reading %ld bytes from position %p\n",f->descriptor,size*nobj,pos);
131  }
132 #endif
133 
134  return(read(f->descriptor,ptr,size*nobj));
135 
136 #endif
137 /*:[15oct2004 mt]*/
138 /*:[13jul2005 mt]*/
139 }
140 
141 /*
142  #] Uread :
143  #[ Uwrite :
144 */
145 
146 size_t Uwrite(char *ptr, size_t size, size_t nobj, FILES *f)
147 {
148 /*[13jul2005 mt]:*/
149 #ifdef SAFESIGNAL
150 /*[15oct2004 mt]:*/
151 /*Operation write() can be interrupted by a signal. */
152 size_t ret;
153 size_t thesize=size*nobj;
154  /*If the syscall is interrupted by a signal before it
155  succeeded in getting any progress, it must be repeated:*/
156  while( ( (ret=write(f->descriptor,ptr,thesize))<1 )&&(errno == EINTR) );
157 
158  return(ret);
159 #else
160 #ifdef DEEPDEBUG
161  {
162  POSITION pos;
163  SETBASEPOSITION(pos,lseek(f->descriptor,0L,SEEK_CUR));
164  MesPrint("handle %d: writing %ld bytes to position %p\n",f->descriptor,size*nobj,pos);
165  }
166 #endif
167  return(write(f->descriptor,ptr,size*nobj));
168 
169 /*:[15oct2004 mt]*/
170 #endif
171 /*:[13jul2005 mt]*/
172 }
173 
174 /*
175  #] Uwrite :
176  #[ Useek :
177 */
178 
179 int Useek(FILES *f, off_t offset, int origin)
180 {
181  if ( f && ( lseek(f->descriptor,offset,origin) >= 0 ) ) return(0);
182  return(-1);
183 }
184 
185 /*
186  #] Useek :
187  #[ Utell :
188 */
189 
190 off_t Utell(FILES *f)
191 {
192  if ( f ) return((off_t)lseek(f->descriptor,0L,SEEK_CUR));
193  else return(-1);
194 }
195 
196 /*
197  #] Utell :
198  #[ Uflush :
199 */
200 
201 void Uflush(FILES *f) { DUMMYUSE(f); }
202 
203 /*
204  #] Uflush :
205  #[ Ugetpos :
206 */
207 
208 int Ugetpos(FILES *f, fpos_t *ptr)
209 {
210  DUMMYUSE(f); DUMMYUSE(ptr);
211  return(-1);
212 }
213 
214 /*
215  #] Ugetpos :
216  #[ Usetpos :
217 */
218 
219 int Usetpos(FILES *f,fpos_t *ptr)
220 {
221  DUMMYUSE(f); DUMMYUSE(ptr);
222  return(-1);
223 }
224 
225 /*
226  #] Usetpos :
227  #[ Usetbuf :
228 */
229 
230 void Usetbuf(FILES *f, char *ptr) { DUMMYUSE(f); DUMMYUSE(ptr); }
231 
232 /*
233  #] Usetbuf :
234 */
235 #endif