| 1 |
C Copyright(C) 1999-2020 National Technology & Engineering Solutions |
| 2 |
C of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
C NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
C |
| 5 |
C See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
c ====================================================================== |
| 8 |
c ====================================================================== |
| 9 |
c ====================================================================== |
| 10 |
c ====================================================================== |
| 11 |
|
| 12 |
c ROUTINE: filhnd |
| 13 |
|
| 14 |
c DESCRIPTION: Opens and closes files. |
| 15 |
|
| 16 |
c AUTHOR: John H. Glick |
| 17 |
c Sandia National Laboratories |
| 18 |
c Division 1511 |
| 19 |
|
| 20 |
c DATE: December 20, 1988 |
| 21 |
|
| 22 |
c TYPE OF SUBPROGRAM: subroutine |
| 23 |
|
| 24 |
c USAGE: call filhnd (unit , fil1, ecodei, ecodeo, |
| 25 |
c type, fform, facces, frecl, *) |
| 26 |
|
| 27 |
c PARAMETERS: |
| 28 |
|
| 29 |
c integer unit -- (INPUT) |
| 30 |
c If > 0, specifies the logical unit to be |
| 31 |
c opened. |
| 32 |
c If < 0, -unit specifies the logical unit to |
| 33 |
c close. |
| 34 |
c If = 0, all open logical units are to be |
| 35 |
c closed. |
| 36 |
|
| 37 |
c character type -- (INPUT) |
| 38 |
c 'I' if input file (status = 'old') |
| 39 |
c 'O' if output file (status = 'new') |
| 40 |
c 'U' if unknown file type (status = 'unknown') |
| 41 |
c 'S' if scratch file (status = 'scratch') |
| 42 |
|
| 43 |
c character fform -- (INPUT) |
| 44 |
c 'F' if formatted file |
| 45 |
c 'U' if unformatted file |
| 46 |
|
| 47 |
c CALLS: |
| 48 |
|
| 49 |
c prterr (BLOT) -- Prints an error message if one occurred |
| 50 |
c during the execution of filhnd. |
| 51 |
c exname (SUPES) -- Gets the filename associated with a unit |
| 52 |
c number. |
| 53 |
c lenstr (strlib) -- Gets the length of a string (excluding |
| 54 |
c trailing blanks). |
| 55 |
|
| 56 |
c GLOBAL VARIABLES REFERENCED: |
| 57 |
|
| 58 |
c CALLING ROUTINE(S): getins (BLOT) |
| 59 |
|
| 60 |
c SYSTEM DEPENDENCIES: none |
| 61 |
|
| 62 |
c ====================================================================== |
| 63 |
c ====================================================================== |
| 64 |
|
| 65 |
subroutine filhnd (unit, filn, ecodei, ecodeo, type, fform, |
| 66 |
& facces, frecl, *) |
| 67 |
|
| 68 |
c parameters |
| 69 |
|
| 70 |
integer unit |
| 71 |
c if > 0, the logical unit of the file to open. |
| 72 |
c if < 0, the logical unit of the file to close. |
| 73 |
c if = 0, close all files |
| 74 |
logical ecodei |
| 75 |
c On input, .TRUE., if program should abort |
| 76 |
c if an error on opening the file occurred. |
| 77 |
c .FALSE. otherwise. |
| 78 |
logical ecodeo |
| 79 |
c On output, .TRUE. if the file opening occurred successfully. |
| 80 |
c .FALSE. otherwise. |
| 81 |
integer frecl |
| 82 |
c If a file is to be opened and it is a direct access file, |
| 83 |
c the file's record length. |
| 84 |
c If a single file ( not all files ) is to be closed, |
| 85 |
c this parameter = 1 to delete the file on closing. |
| 86 |
c Otherwise, the default status parameter is used on the close |
| 87 |
c call. |
| 88 |
character type |
| 89 |
c 'o' if an old file, |
| 90 |
c 'n' if a new file, |
| 91 |
c 's' if a scratch file |
| 92 |
c 'u' if unknown |
| 93 |
character fform |
| 94 |
c 'u' if unformatted |
| 95 |
c 'f' if formatted |
| 96 |
character facces |
| 97 |
c 'd' if direct |
| 98 |
c s' if sequential |
| 99 |
character * (*) filn |
| 100 |
c Name of the file to open. If ! = ' ', then filhnd calls |
| 101 |
c the SUPES routine EXNAME to get the filename associated |
| 102 |
c with the specified unit number. |
| 103 |
|
| 104 |
c if unit <= 0, then all other parameters are ignored. |
| 105 |
|
| 106 |
c declarations |
| 107 |
|
| 108 |
character*2048 filnam |
| 109 |
c filename associated with unit |
| 110 |
integer lname |
| 111 |
c length of string filnam (less trailing blanks) |
| 112 |
character*11 form |
| 113 |
c value of form keyword in open statement |
| 114 |
character*10 access |
| 115 |
c value of access keyword in open statement |
| 116 |
character*7 status |
| 117 |
c value of status keyword in open statement |
| 118 |
integer lform,lstat |
| 119 |
c length of form and status keywords (less trailing blanks) |
| 120 |
integer lacces |
| 121 |
c length of access keyword (less trailing blanks) |
| 122 |
integer ios |
| 123 |
c integer indicating error status of the open call. |
| 124 |
c = 0 no error, |
| 125 |
c = error code otherwise. |
| 126 |
character*1 cparm |
| 127 |
c dummy argument for call to exparm |
| 128 |
character tform, ttype, tacces |
| 129 |
c Temporary variables for storing modified values of fform, |
| 130 |
c type, and facces |
| 131 |
|
| 132 |
c ***************************************************************** |
| 133 |
c ***************************************************************** |
| 134 |
|
| 135 |
c static declarations |
| 136 |
|
| 137 |
logical first |
| 138 |
save first |
| 139 |
|
| 140 |
integer maxunits |
| 141 |
parameter (maxunits=25) |
| 142 |
c maximum number of units that may be open at once. |
| 143 |
integer numopn |
| 144 |
c number of currently open files |
| 145 |
integer opnlst(maxunits) |
| 146 |
c array of currently open unit |
| 147 |
c numbers |
| 148 |
|
| 149 |
save numopn, opnlst |
| 150 |
|
| 151 |
data first / .TRUE. / |
| 152 |
|
| 153 |
c ********************************************************************* |
| 154 |
c ********************************************************************* |
| 155 |
|
| 156 |
if (first) then |
| 157 |
numopn = 0 |
| 158 |
first = .FALSE. |
| 159 |
end if |
| 160 |
|
| 161 |
c print *, 'numopen = ',numopn |
| 162 |
c if ( numopn .gt. 0 ) |
| 163 |
c & print *, 'list is ',(opnlst(i),i=1,numopn) |
| 164 |
|
| 165 |
if ( unit .gt. 0 ) then |
| 166 |
|
| 167 |
c open file associated with unit |
| 168 |
|
| 169 |
c set open keywords |
| 170 |
|
| 171 |
cparm = fform |
| 172 |
call upcase_bl ( cparm ) |
| 173 |
tform = cparm(1:1) |
| 174 |
if ( tform .eq. 'U' ) then |
| 175 |
form = 'unformatted' |
| 176 |
else if (tform .eq. 'F' ) then |
| 177 |
form = 'formatted' |
| 178 |
else |
| 179 |
call prterr ('PROGRAM', |
| 180 |
& 'Bad value for fform parameter in filhnd routine.') |
| 181 |
return 1 |
| 182 |
endif |
| 183 |
lform = lenstr ( form ) |
| 184 |
|
| 185 |
cparm = type |
| 186 |
call upcase_bl ( cparm ) |
| 187 |
ttype = cparm(1:1) |
| 188 |
if ( ttype .eq. 'O' ) then |
| 189 |
status = 'old' |
| 190 |
else if ( ttype .eq. 'N' ) then |
| 191 |
status = 'new' |
| 192 |
else if ( ttype .eq. 'U' ) then |
| 193 |
status = 'unknown' |
| 194 |
else if ( ttype .eq. 'S' ) then |
| 195 |
status = 'scratch' |
| 196 |
else |
| 197 |
call prterr ('PROGRAM', |
| 198 |
& 'Bad value for type parameter in filhnd.') |
| 199 |
return 1 |
| 200 |
endif |
| 201 |
lstat = lenstr ( status ) |
| 202 |
|
| 203 |
cparm = facces |
| 204 |
call upcase_bl ( cparm ) |
| 205 |
tacces = cparm(1:1) |
| 206 |
if ( tacces .eq. 'D' ) then |
| 207 |
access = 'direct' |
| 208 |
else if ( tacces .eq. 'S' ) then |
| 209 |
access = 'sequential' |
| 210 |
else |
| 211 |
call prterr ('PROGRAM', |
| 212 |
& 'Bad value for access parameter in filhnd.') |
| 213 |
return 1 |
| 214 |
endif |
| 215 |
lacces = lenstr ( access ) |
| 216 |
|
| 217 |
c open file |
| 218 |
|
| 219 |
if ( status .ne. 'scratch' ) then |
| 220 |
|
| 221 |
c get file associated with unit |
| 222 |
|
| 223 |
filnam = filn |
| 224 |
call pack ( filnam, lname ) |
| 225 |
if ( lname .eq. 0 ) then |
| 226 |
call exname ( unit, filnam, lname ) |
| 227 |
endif |
| 228 |
|
| 229 |
if ( access .eq. 'direct' ) then |
| 230 |
open ( unit=unit, file=filnam(:lname), |
| 231 |
& form=form(:lform), |
| 232 |
& status=status(:lstat), access=access(:lacces), |
| 233 |
& recl=frecl, iostat=ios ) |
| 234 |
else |
| 235 |
c print *,'filename=',filnam(:lname),'=' |
| 236 |
c print *,'form=',form(:lform),'=' |
| 237 |
c print *,'status=',status(:lstat),'=' |
| 238 |
open ( unit=unit, file=filnam(:lname), |
| 239 |
& form=form(:lform), |
| 240 |
& status=status(:lstat), iostat=ios) |
| 241 |
endif |
| 242 |
|
| 243 |
if ( ios .ne. 0 ) then |
| 244 |
if ( ecodei ) then |
| 245 |
call prterr ('FATAL', |
| 246 |
& 'Error opening file in filhnd') |
| 247 |
return 1 |
| 248 |
else |
| 249 |
call prterr ('ERROR', |
| 250 |
& 'Error opening file in filhnd') |
| 251 |
ecodeo = .FALSE. |
| 252 |
endif |
| 253 |
else |
| 254 |
ecodeo = .TRUE. |
| 255 |
endif |
| 256 |
|
| 257 |
else |
| 258 |
|
| 259 |
if ( access .eq. 'direct' ) then |
| 260 |
open ( unit=unit, form=form(:lform), |
| 261 |
& status=status(:lstat), access=access(:lacces), |
| 262 |
& recl=frecl, iostat=ios ) |
| 263 |
else |
| 264 |
open ( unit=unit, form=form(:lform), |
| 265 |
& status=status(:lstat), iostat=ios) |
| 266 |
endif |
| 267 |
|
| 268 |
if ( ios .ne. 0 ) then |
| 269 |
if ( ecodei ) then |
| 270 |
call prterr ('FATAL', |
| 271 |
& 'Error opening file in filhnd') |
| 272 |
return 1 |
| 273 |
else |
| 274 |
call prterr ('ERROR', |
| 275 |
& 'Error opening file in filhnd') |
| 276 |
ecodeo = .FALSE. |
| 277 |
endif |
| 278 |
else |
| 279 |
ecodeo = .TRUE. |
| 280 |
endif |
| 281 |
|
| 282 |
endif |
| 283 |
|
| 284 |
c update list of open files |
| 285 |
|
| 286 |
if ( ecodeo ) then |
| 287 |
numopn = numopn + 1 |
| 288 |
opnlst(numopn) = unit |
| 289 |
endif |
| 290 |
|
| 291 |
else if ( unit .lt. 0 ) then |
| 292 |
|
| 293 |
c close file |
| 294 |
|
| 295 |
unit = -unit |
| 296 |
if ( frecl .eq. 1 ) then |
| 297 |
close ( unit=unit, status='delete', iostat=ios ) |
| 298 |
else |
| 299 |
close ( unit=unit, iostat=ios ) |
| 300 |
endif |
| 301 |
if ( ios .ne. 0 ) then |
| 302 |
call exname ( unit, filnam, lname ) |
| 303 |
if ( ecodei ) then |
| 304 |
call prterr ('PROGRAM', |
| 305 |
& 'Error closing file in filhnd') |
| 306 |
return 1 |
| 307 |
else |
| 308 |
call prterr ('PROGRAM', |
| 309 |
& 'Error closing file in filhnd') |
| 310 |
ecodeo = .FALSE. |
| 311 |
endif |
| 312 |
else |
| 313 |
ecodeo = .TRUE. |
| 314 |
endif |
| 315 |
|
| 316 |
c update list of open files |
| 317 |
|
| 318 |
if ( ecodeo ) then |
| 319 |
i = 1 |
| 320 |
100 continue |
| 321 |
if ( (i .le. numopn) .and. (unit .ne. opnlst(i)) ) then |
| 322 |
i = i + 1 |
| 323 |
go to 100 |
| 324 |
endif |
| 325 |
|
| 326 |
if ( i .gt. numopn ) then |
| 327 |
call prterr ('PROGRAM', |
| 328 |
&'Closed a file in filhnd that was not on the list of open files') |
| 329 |
return 1 |
| 330 |
else |
| 331 |
numopn = numopn - 1 |
| 332 |
do 110 j = i, numopn |
| 333 |
opnlst(j) = opnlst(j+1) |
| 334 |
110 continue |
| 335 |
endif |
| 336 |
|
| 337 |
endif |
| 338 |
else |
| 339 |
|
| 340 |
c close all open files |
| 341 |
|
| 342 |
ecodeo = .TRUE. |
| 343 |
do 120 i = 1, numopn |
| 344 |
|
| 345 |
close ( unit=opnlst(i), iostat=ios ) |
| 346 |
if ( ios .ne. 0 ) then |
| 347 |
call exname ( opnlst(i), filnam, lname ) |
| 348 |
if ( ecodei ) then |
| 349 |
call prterr ('PROGRAM', |
| 350 |
& 'Error closing file in filhnd') |
| 351 |
return 1 |
| 352 |
else |
| 353 |
call prterr ('PROGRAM', |
| 354 |
& 'Error closing file in filhnd') |
| 355 |
ecodeo = .FALSE. |
| 356 |
endif |
| 357 |
endif |
| 358 |
120 continue |
| 359 |
|
| 360 |
endif |
| 361 |
|
| 362 |
c print *, 'about to exit filhnd' |
| 363 |
c print *, 'numopen = ',numopn |
| 364 |
c if ( numopn .gt. 0 ) |
| 365 |
c & print *, 'list is ',(opnlst(i),i=1,numopn) |
| 366 |
|
| 367 |
return |
| 368 |
end |