| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1995-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.NAME osa |
| 31 |
.LANGUAGE C |
| 32 |
.AUTHOR IPG-ESO Garching |
| 33 |
.CATEGORY Host operating system interfaces. Files in ASCII (text files) |
| 34 |
.COMMENTS |
| 35 |
Handling of ascii files (using buffered I/O). These |
| 36 |
routines convert text data from the host format into |
| 37 |
internal format and viceversa. |
| 38 |
Text files are accessed sequentially in a synchronous form. |
| 39 |
Text files are referenced by the physical name in the |
| 40 |
open function. A file identifier is returned by this |
| 41 |
function to be used in later i/o operations. |
| 42 |
Associated to each file there is a pointer to locate the |
| 43 |
current file position (hidden in the file descriptor). |
| 44 |
The routines return always |
| 45 |
a non-negative integer number on successful return. |
| 46 |
Otherwise, a value of -1 is set to indicate an error |
| 47 |
condition and the variable ``oserror'' contains the |
| 48 |
symbolic error code. |
| 49 |
Symbolic error codes for each function to be defined. |
| 50 |
|
| 51 |
.VERSION 0.0 25-Aug-1986 Definition J. D. Ponz |
| 52 |
.VERSION 1.0 04-Nov-1986 Programmation B. Pirenne |
| 53 |
.VERSION 1.1 14-Apr-1987 Improve performance B. Pirenne |
| 54 |
.VERSION 1.5 20-Jul-1987 ost* -> osa* ; use "fileno()" to get the file |
| 55 |
#in osaopen() ; upgrade osaread(). B. Pirenne. |
| 56 |
.VERSION 1.6 31-May-1988 Cosmetic changes. C. Guirao |
| 57 |
.VERSION 1.7 04-Oct-1988 osaread modified: ensure the rest of a too long |
| 58 |
record is ignored (F. Ochsenbein) |
| 59 |
.VERSION 1.8 26-Jun-1989 Modified osaread (was detroying last byte) |
| 60 |
.VERSION 1.9 07-Nov-1989 Simplified osaopen |
| 61 |
.VERSION 1.10 08-Feb-1990 Corrected bug in osaread (error reported) |
| 62 |
.VERSION 1.11 04-May-1990 Be sure that stdin (0), stdout(1) ansd stderr(2) |
| 63 |
are understood... |
| 64 |
.VERSION 1.12 19-Dec-1990 Not checking FNAME_LEN any more CG. |
| 65 |
|
| 66 |
090324 last modif |
| 67 |
------------------------------------------------------------*/ |
| 68 |
|
| 69 |
#include <fcntl.h> |
| 70 |
#include <stdio.h> |
| 71 |
#include <errno.h> |
| 72 |
#include <osparms.h> |
| 73 |
#include <proto_os.h> /* ANSI-C prototyping */ |
| 74 |
|
| 75 |
/* array to contain up to _NFILE_ file pointers. |
| 76 |
* This will be used to allow a the use of a transparent and |
| 77 |
* integer file identifier |
| 78 |
*/ |
| 79 |
static FILE *fptr[_NFILE_]; |
| 80 |
|
| 81 |
#ifdef __STDC__ |
| 82 |
static FILE *get(int fid) |
| 83 |
#else |
| 84 |
static FILE *get(fid) |
| 85 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 86 |
.PURPOSE Convert fileno into file pointer. |
| 87 |
.RETURNS File pointer. NULL if error. |
| 88 |
.REMARKS Ensures a correct check. |
| 89 |
------------------------------------------------------------*/ |
| 90 |
int fid; /* IN : file identifier */ |
| 91 |
#endif |
| 92 |
{ |
| 93 |
FILE *fd; |
| 94 |
|
| 95 |
fd = NULL; |
| 96 |
|
| 97 |
oserror = 0; |
| 98 |
|
| 99 |
if (fid >= _NFILE_) oserror = EINVAL; |
| 100 |
else { |
| 101 |
switch(fid){ |
| 102 |
case 0: |
| 103 |
fd = stdin; |
| 104 |
break; |
| 105 |
case 1: |
| 106 |
fd = stdout; |
| 107 |
break; |
| 108 |
case 2: |
| 109 |
fd = stderr; |
| 110 |
break; |
| 111 |
default: |
| 112 |
fd = fptr[fid]; |
| 113 |
} |
| 114 |
if (fd == NULL) oserror = EINVAL; |
| 115 |
} |
| 116 |
|
| 117 |
return(fd); |
| 118 |
} |
| 119 |
|
| 120 |
int osaopen(phname, mode) |
| 121 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 122 |
.PURPOSE Opens text file for read or write. |
| 123 |
The variable mode |
| 124 |
defines the way of opening the file as READ, |
| 125 |
WRITE, READ_WRITE and APPEND. (see midas/osparms.h). |
| 126 |
The file pointer is set to the beginning of the file. |
| 127 |
.RETURNS Upon successful completion a file descriptor is returned. -1 is |
| 128 |
returned on error. |
| 129 |
.REMARKS System dependencies: |
| 130 |
-- UNIX: fopen(3S), fileno(macro in stdio.h) |
| 131 |
------------------------------------------------------------*/ |
| 132 |
char *phname; /* IN : physical filename */ |
| 133 |
int mode; /* IN : open mode */ |
| 134 |
{ |
| 135 |
FILE *fid, *fopen(); |
| 136 |
static char *fmode[4] = { "r", "w", "r+", "a" }; |
| 137 |
char *t; |
| 138 |
int ret; |
| 139 |
|
| 140 |
|
| 141 |
#if 0 /* Old code */ |
| 142 |
switch (mode) { |
| 143 |
case READ: /* open for read only */ |
| 144 |
t = fmode[0]; |
| 145 |
break; |
| 146 |
case WRITE: /* open for write only */ |
| 147 |
t = fmode[1]; |
| 148 |
break; |
| 149 |
case READ_WRITE: /* open for read-write */ |
| 150 |
t = fmode[2]; |
| 151 |
break; |
| 152 |
case APPEND: /* open for append */ |
| 153 |
t = fmode[3]; |
| 154 |
break; |
| 155 |
default: |
| 156 |
oserror = EINVAL; |
| 157 |
return(-1); |
| 158 |
} |
| 159 |
#else /* New Code */ |
| 160 |
t = fmode[mode&3]; |
| 161 |
#endif |
| 162 |
|
| 163 |
if ((fid = fopen(phname,t)) == NULL) { |
| 164 |
oserror = errno; |
| 165 |
return(-1); |
| 166 |
} |
| 167 |
ret = fileno(fid); /* indice of the file table is */ |
| 168 |
if (ret >= _NFILE_) { |
| 169 |
oserror = EINVAL; |
| 170 |
return(-2); |
| 171 |
} |
| 172 |
fptr[ret] = fid; /* the _file of struct FILE and */ |
| 173 |
/* this number becomes the file ptr */ |
| 174 |
return(ret); |
| 175 |
} |
| 176 |
|
| 177 |
int osaclose(fid) |
| 178 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 179 |
.PURPOSE Closes a text file. |
| 180 |
The argument fid is the file identification obtained from osaopen. |
| 181 |
.RETURNS Value 0 on normal return, -1 on error. |
| 182 |
.REMARKS System dependencies: |
| 183 |
-- VAX VMS: |
| 184 |
-- UNIX: fclose(3S) |
| 185 |
-- MIDAS VMS Equivalence: Not available. |
| 186 |
------------------------------------------------------------*/ |
| 187 |
int fid; /* IN : file identifier */ |
| 188 |
{ |
| 189 |
FILE *fd; |
| 190 |
int ret; |
| 191 |
|
| 192 |
if ( (fd = get(fid)) == NULL) return(-1); |
| 193 |
|
| 194 |
if ((ret = fclose(fd)) < 0) oserror = errno; |
| 195 |
fptr[fid] = NULL; /* File Closed: reset File Pointer */ |
| 196 |
return(ret); |
| 197 |
} |
| 198 |
|
| 199 |
int osaread(fid, pbuf, nochar) |
| 200 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 201 |
.PURPOSE Synchronous read from text file. |
| 202 |
The argument fid is the file identification obtained from osaopen. |
| 203 |
The routine reads 1 record from the current file |
| 204 |
position and stores up to nochar characters into the buffer |
| 205 |
pointed by pbuf. It the record is larger than the buffer size, |
| 206 |
the remainder is ignored. |
| 207 |
The carriage return character is never returned. The buffer returned |
| 208 |
is always null terminated. |
| 209 |
.RETURNS Number of characters of the record. (may be less than nochar |
| 210 |
if the EOF or a carriage return is encountered, larger if record |
| 211 |
truncated). |
| 212 |
Returns 0 if the record only contained the line feed char. |
| 213 |
Returns -1 if an error is encountered or if the end of file is |
| 214 |
reached. (In these last cases, oserror is set to the relevant error |
| 215 |
number). |
| 216 |
.REMARKS System dependencies: fgets / fgetc |
| 217 |
------------------------------------------------------------*/ |
| 218 |
int fid; /* IN : file identifier */ |
| 219 |
char *pbuf; /* IN : pointer to i/o buffer */ |
| 220 |
int nochar; /* IN : number of i/o characters */ |
| 221 |
{ |
| 222 |
FILE *fd; |
| 223 |
char *pc, *pe; |
| 224 |
int c; |
| 225 |
|
| 226 |
if ( (fd = get(fid)) == NULL) return(-1); |
| 227 |
|
| 228 |
errno = 0; /* MUST BE INITIALIZED... Feb-1990 */ |
| 229 |
if (fgets(pbuf, nochar, fd) == NULL) { |
| 230 |
oserror = errno; |
| 231 |
return(-1); |
| 232 |
} |
| 233 |
|
| 234 |
pe = pbuf + nochar - 1; /* Last Available Byte */ |
| 235 |
|
| 236 |
/* Continue to read if the end-of-record not found */ |
| 237 |
|
| 238 |
for (pc = pbuf; (pc <= pe) && (*pc != '\n') && (*pc != '\0'); pc++ ) ; |
| 239 |
|
| 240 |
if (pc > pe) /* Not Found */ |
| 241 |
*pe = '\0', pc = pe; |
| 242 |
|
| 243 |
if (*pc == '\n') /* Replace the newline + carriage-return */ |
| 244 |
{ |
| 245 |
while ( (--pc >= pbuf) && (*pc == '\r')) ; |
| 246 |
*++pc = '\0'; |
| 247 |
} |
| 248 |
else for (c = 0, --pc; (c != EOF) && (c != '\n'); pc++) |
| 249 |
c = getc(fd); |
| 250 |
|
| 251 |
return((pc - pbuf)); |
| 252 |
} |
| 253 |
|
| 254 |
int osawrite(fid, pbuf, nochar) |
| 255 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 256 |
.PURPOSE Synchronous RECORD write into a text file. |
| 257 |
The argument fid is the file identification obtained from osaopen. |
| 258 |
The routine writes nochar characters from the buffer pointed |
| 259 |
by pbuf into the current file position. |
| 260 |
Upon return the file pointer is incremented by the number of |
| 261 |
characters actually written. |
| 262 |
.RETURNS Number of characters actually written. (possibly 0 on error) |
| 263 |
.REMARKS System dependencies: |
| 264 |
-- UNIX: fwrite(3S) |
| 265 |
------------------------------------------------------------*/ |
| 266 |
int fid; /* IN : file identifier */ |
| 267 |
char *pbuf; /* IN : pointer to i/o buffer */ |
| 268 |
int nochar; /* IN : number of i/o characters */ |
| 269 |
{ |
| 270 |
FILE *fd; |
| 271 |
int ret, add_newline; |
| 272 |
|
| 273 |
if ( (fd = get(fid)) == NULL) return(-1); |
| 274 |
|
| 275 |
add_newline = 0; |
| 276 |
ret = 0; |
| 277 |
|
| 278 |
if (nochar) /* There are bytes to write... */ |
| 279 |
{ |
| 280 |
ret = fwrite(pbuf, sizeof(char), nochar, fd); |
| 281 |
if (ret == 0) ; /* Error */ |
| 282 |
else if (*(pbuf+nochar-1) != '\n') add_newline = 1; |
| 283 |
} |
| 284 |
else add_newline = 1; |
| 285 |
|
| 286 |
if (add_newline) ret += fwrite("\n", 1, 1, fd); |
| 287 |
|
| 288 |
if (ret == 0) oserror = errno; |
| 289 |
|
| 290 |
return(ret); |
| 291 |
} |
| 292 |
|
| 293 |
long osaseek(fid, address, mode) |
| 294 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 295 |
.PURPOSE moves read/write pointer to text file. The address defines |
| 296 |
the pointer position in characters relative to: |
| 297 |
start of the file, current position or end of the file as |
| 298 |
defined by the variable mode (FILE_START, FILE_CURRENT, FILE_END). |
| 299 |
The argument fid is the file identification obtained from osaopen. |
| 300 |
.RETURNS Upon successful completion, the resulting pointer position |
| 301 |
.RETURNS measured in characters is returned or -1 on error. |
| 302 |
.REMARKS System dependencies: |
| 303 |
-- UNIX: fseek(3S), ftell(3S) |
| 304 |
------------------------------------------------------------*/ |
| 305 |
int fid; /* IN : file identifier */ |
| 306 |
long address; /* IN : file pointer */ |
| 307 |
int mode; /* IN : mode of addressing */ |
| 308 |
{ |
| 309 |
long ftell(); |
| 310 |
FILE *fd; |
| 311 |
|
| 312 |
if ( (fd = get(fid)) == NULL) return(-1L); |
| 313 |
|
| 314 |
if (fseek(fd, address, mode) < 0) { |
| 315 |
oserror = errno; |
| 316 |
return(-1L); |
| 317 |
} |
| 318 |
return(ftell(fd)); |
| 319 |
} |
| 320 |
|
| 321 |
int osawait(fid) |
| 322 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 323 |
.PURPOSE Flushes the input/output streams. |
| 324 |
.RETURNS The function returns 0 for success, and -1 if any error was detected |
| 325 |
(such as trying to write to a file that has not been opened for |
| 326 |
writing) was detected. |
| 327 |
.REMARKS System dependencies: |
| 328 |
--UNIX: fflush(3) |
| 329 |
------------------------------------------------------------*/ |
| 330 |
int fid; /* file identification */ |
| 331 |
{ |
| 332 |
FILE *fd; |
| 333 |
|
| 334 |
if ( (fd = get(fid)) == NULL) return(-1); |
| 335 |
|
| 336 |
return(fflush(fd)); |
| 337 |
} |
| 338 |
|
| 339 |
long osasize(fid) |
| 340 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 341 |
.PURPOSE Retrieve the current size of an opened file. |
| 342 |
.RETURNS The size of the file, -1 in case of error (oserror explains) |
| 343 |
.REMARKS |
| 344 |
------------------------------------------------------------*/ |
| 345 |
int fid; /* IN: file identification */ |
| 346 |
{ |
| 347 |
FILE *fd; |
| 348 |
long ret, pos, ftell(); |
| 349 |
|
| 350 |
if ( (fd = get(fid)) == NULL) return(-1L); |
| 351 |
|
| 352 |
|
| 353 |
pos = ftell(fd); /* Current position */ |
| 354 |
ret = -1; |
| 355 |
|
| 356 |
if (pos != -1) |
| 357 |
{ (void)fseek(fd, 0L, 2); /* end of file */ |
| 358 |
ret = ftell(fd); |
| 359 |
(void)fseek(fd, pos, 0); |
| 360 |
} |
| 361 |
else oserror = errno; |
| 362 |
|
| 363 |
return(ret); |
| 364 |
} |
| 365 |
|
| 366 |
int osaunix(/*fid*/) |
| 367 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 368 |
.PURPOSE Tells if a file is Unix-like |
| 369 |
.RETURNS Always 1 |
| 370 |
.REMARKS For VMS compatibility |
| 371 |
------------------------------------------------------------*/ |
| 372 |
/*int fid;*/ /* IN: file identification */ |
| 373 |
{ |
| 374 |
return(1); |
| 375 |
} |
| 376 |
|