Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
rewind.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/rewind.c   92.1    06/21/99 10:37:55"
00039 
00040 #include <errno.h>
00041 #include <foreign.h>
00042 #include <liberrno.h>
00043 #include "fio.h"
00044 
00045 /*
00046  *      _REWF   Fortran rewind
00047  */
00048 
00049 #ifdef  _UNICOS
00050 #pragma _CRI duplicate _REWF as $REWF
00051 #endif
00052 
00053 int
00054 _REWF(
00055         _f_int  *unump,                 /* Fortran unit number */
00056         _f_int  *iostat,                /* IOSTAT= variable address, or NULL */
00057         int     errf)                   /* 1 if ERR= specifier is present */
00058 {
00059         register int    errn;           /* Error status */
00060         register unum_t unum;
00061         unit            *cup;
00062         struct ffsw     fst;
00063         struct fiostate cfs;
00064 
00065         errn    = 0; 
00066         unum    = *unump; 
00067 
00068         STMT_BEGIN(unum, 0, T_REWIND, NULL, &cfs, cup); /* lock the unit */
00069 
00070         if (!GOOD_UNUM(unum)) {
00071                 errn    = FEIVUNIT;     /* Invalid unit number */
00072                 goto rewind_done;
00073         }
00074 
00075 /*
00076  *      REWIND on unopened unit is OK, and does nothing.
00077  */
00078         if (cup == NULL) 
00079                 goto rewind_done;
00080 
00081         if (cup->useq == 0) {    /* If file opened for direct access */
00082                 errn    = FERWNDIV;     /* REWIND invalid on dir. acc.*/
00083                 goto rewind_done;
00084         }
00085 
00086 /*
00087  *      Wait for completion of a preceding asynchronous BUFFER IN/OUT.
00088  */
00089         WAITIO(cup, {errn = cup->uffsw.sw_error;});
00090 
00091         if (errn != 0) 
00092                 goto rewind_done;
00093 
00094         if (cup->pnonadv) {             /* There is a current record */
00095                 if (cup->uwrt) {
00096 
00097                         errn    = _nonadv_endrec(&cfs, cup);
00098 
00099                         if (errn != 0)
00100                                 goto rewind_done;
00101                 }
00102                 cup->pnonadv    = 0;    /* Flag no current record */
00103         }
00104 
00105         cup->uend       = BEFORE_ENDFILE;
00106         cup->ulastyp    = DT_NONE;
00107         cup->urecpos    = 0;
00108 
00109         if ( cup->uwrt ) {
00110                 if (cup->utrunc) {
00111                         /*
00112                          * Truncate file after last sequential write before 
00113                          * this REWIND.
00114                          */
00115                         errn    = _unit_trunc(cup);
00116 
00117                         if (errn != 0)
00118                                 goto rewind_done;
00119                 }
00120                 cup->uwrt       = 0;
00121         }
00122 
00123 /*
00124  *      Switch on file structure
00125  */
00126         switch (cup->ufs) {
00127 
00128         case FS_FDC:
00129                 errn    = XRCALL(cup->ufp.fdc, seekrtn) cup->ufp.fdc, 0, 0, &fst);
00130                 if (errn < 0)
00131                         errn    = fst.sw_error;
00132                 break;
00133 
00134         case FS_TEXT:
00135         case STD:
00136                 /*
00137                  * Can't rewind pipes, sockets, ttys.
00138                  */
00139                 if (!cup->useek) {
00140                         errn    = FENORWPI;     /* can't rewind pipes */ 
00141                         goto rewind_done;
00142                 }
00143 
00144                 errn    = fseek(cup->ufp.std, 0, 0);
00145 
00146                 if (errn != 0)
00147                         errn    = errno;
00148                 break;
00149 
00150         case FS_BIN:
00151                 cup->uwaddr     = 1;
00152                 break;
00153 
00154         case FS_AUX:
00155                 errn    = FEMIXAUX;     /* REWIND not allowed on a WAIO, 
00156                                          * MSIO, DRIO, or AQIO file. */
00157                 break;
00158 
00159         default:
00160                 errn    = FEINTFST;
00161                 break;
00162         }
00163 
00164 rewind_done:
00165         if (iostat != NULL)
00166                 *iostat = errn;
00167         else
00168                 if (errn != 0 && (errf == 0))
00169                         _ferr(&cfs, errn, unum);/* Pass unum to _ferr *
00170                                                  * in case of FEIVUNIT error */
00171 
00172         STMT_END(cup, T_REWIND, NULL, &cfs);    /* unlock the unit */
00173 
00174         errn    = (errn != 0) ? IO_ERR : IO_OKAY; /* Set error status */
00175 
00176         return(CFT77_RETVAL(errn));
00177 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines