Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
endfile.c
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001, Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2.1 of the GNU Lesser General Public License 
00007   as published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU Lesser General Public 
00021   License along with this program; if not, write the Free Software 
00022   Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 
00023   USA.
00024 
00025   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00026   Mountain View, CA 94043, or:
00027 
00028   http://www.sgi.com
00029 
00030   For further information regarding this notice, see:
00031 
00032   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00033 
00034 */
00035 
00036 
00037 
00038 #pragma ident "@(#) libf/fio/endfile.c  92.2    06/18/99 18:41:40"
00039 
00040 #include <errno.h>
00041 #include <foreign.h>
00042 #include <unistd.h>
00043 #ifdef  _ABSOFT
00044 #include "ac_sysdep.h"
00045 #else
00046 #include <sys/unistd.h>
00047 #endif
00048 #include "fio.h"
00049   
00050 /*
00051  *      _EOFW   Fortran endfile
00052  */
00053 #ifdef  _UNICOS
00054 #pragma _CRI duplicate _EOFW as $EOFW
00055 #endif
00056 
00057 int
00058 _EOFW(
00059         _f_int  *unump,         /* Fortran unit number */
00060         _f_int  *iostat,        /* IOSTAT= variable address, or NULL */
00061         int     errf)           /* 1 if ERR= specifier is present */
00062 {
00063         register unum_t unum;
00064         register int    errn;           /* nonzero when error is encountered */
00065         unit            *cup;
00066         struct  ffsw    fst;
00067         struct fiostate cfs;
00068   
00069 #if     defined(_ABSOFT) && (defined(TARGET_MAC_OS) || defined(TARGET_NT))
00070         mrwe_check();
00071 #endif
00072 
00073         errn    = 0;
00074         unum    = *unump;
00075 
00076         STMT_BEGIN(unum, 0, T_ENDFILE, NULL, &cfs, cup); /* lock the unit */
00077 
00078         if (!GOOD_UNUM(unum)) {
00079                 errn    = FEIVUNIT;     /* Invalid unit number */
00080                 goto endfile_done;
00081         }
00082   
00083 /*
00084  *      ENDFILE on unopened unit is OK, and does nothing.
00085  */
00086         if (cup == NULL)
00087                 goto endfile_done;
00088 
00089         if (cup->pnonadv) {             /* There is a current record */
00090                 if (cup->uwrt) {
00091                         errn    = _nonadv_endrec(&cfs, cup);
00092                         if (errn != 0)
00093                                 goto endfile_done;
00094                 }
00095                 cup->pnonadv    = 0;    /* Flag no current record */
00096         }
00097 
00098         cup->urecpos    = 0;
00099 
00100         if ((cup->uaction & OS_WRITE) == 0) {
00101                 errn    = FENOWRIT;     /* No write permission */
00102                 goto endfile_done;
00103         }
00104 
00105         if (cup->useq == 0) {   /* If file opened for direct access */
00106                 errn    = FEENDFIV;     /* ENDFILE not allowed on dir. acc. */
00107                 goto endfile_done;
00108         }
00109 
00110         cup->uwrt       = 1;
00111 
00112         if (cup->uend && !cup->umultfil && !cup->uspcproc) {
00113                 errn    = FEENAFEN;     /* ENDFILE after EOF */
00114                 goto endfile_done;
00115         }
00116   
00117         switch (cup->ufs) {
00118   
00119         case FS_FDC:
00120 /*
00121  *              See if the layer we are talking to can represent an
00122  *              EOF.  If not, write an EOD.  If EOFs are not permitted, then
00123  *              multi-files (muliple EOFs) are not allowed in the file.
00124  */
00125                 if (cup->umultfil) {
00126 
00127                         /*
00128                          * Replace a previously encountered logical endfile
00129                          * record with a physical endfile record.
00130                          */
00131                         if ((cup->uend == LOGICAL_ENDFILE) && !(cup->uspcproc)){
00132                                 if (XRCALL(cup->ufp.fdc, weofrtn)cup->ufp.fdc,
00133                                         &fst) < 0) {
00134                                         errn    = fst.sw_error;
00135                                         goto endfile_done;
00136                                 }
00137                                 cup->uend       = PHYSICAL_ENDFILE;
00138                         }
00139 
00140                         if (XRCALL(cup->ufp.fdc, weofrtn)cup->ufp.fdc,
00141                                 &fst) < 0) {
00142                                 errn    = fst.sw_error;
00143                                 goto endfile_done;
00144                         }
00145 
00146                         cup->uend       = PHYSICAL_ENDFILE;
00147                 }
00148                 else {
00149                         if (XRCALL(cup->ufp.fdc, weodrtn)cup->ufp.fdc, &fst) < 0)
00150                                 errn    = fst.sw_error;
00151 
00152                         cup->uend       = LOGICAL_ENDFILE;
00153                 }
00154                 break;
00155  
00156         case FS_TEXT:
00157         case STD:               
00158                 cup->uend       = LOGICAL_ENDFILE;
00159                 break;
00160 
00161         default:
00162                 errn    = FEINTFST;     /* Unknown file structure */
00163         }
00164   
00165 endfile_done:
00166         if (iostat != NULL)
00167                 *iostat = errn;
00168         else
00169                 if (errn != 0 && (errf == 0))
00170                         _ferr(&cfs, errn, unum);/* Pass unum to _ferr
00171                                                  * in case of FEIVUNIT error */
00172 
00173         STMT_END(cup, T_ENDFILE, NULL, &cfs);   /* unlock the unit */
00174 
00175         errn    = (errn != 0) ? IO_ERR : IO_OKAY;/* 1 if error; 0 if no error */
00176 
00177         return(CFT77_RETVAL(errn));
00178 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines