Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
getpos.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/getpos.c   92.1    06/21/99 10:37:21"
00039 
00040 #include <errno.h>
00041 #include <foreign.h>
00042 #include <liberrno.h>
00043 #include "fio.h"
00044 #include "fstats.h"
00045  
00046 #define ERET(val) {                     \
00047         errn    = val;                  \
00048         ret     = -1;                   \
00049         goto getpos_done;               \
00050 }
00051 
00052 /*
00053  *      GETPOS 
00054  *
00055  *              Get the current postion of the file.  The position is the
00056  *              word offset within the file.
00057  *
00058  *                      bits 0-2        - byte offset within the word.
00059  *                      bits 3-63       - word offset within the file.
00060  *
00061  *      Can be called as:
00062  *
00063  *                      IPOS = GETPOS(IUN)
00064  *                      IPOS = GETPOS(IUN, PA)
00065  *                      IPOS = GETPOS(IUN, LEN, PA [,ISTAT] )
00066  *
00067  *                      CALL   GETPOS(IUN, PA)
00068  *                      CALL   GETPOS(IUN, LEN, PA [,ISTAT] )
00069  *
00070  *      Duplicate entry points
00071  *
00072  *              _GETPOS_ - if user declares it INTRINSIC with CF90 
00073  *              GETPOS   - if user calls it with > 1 argument
00074  *              $GETPOS  - if user declares it INTRINSIC on with CF77
00075  *              _GETPOS  - if user declares it INTRINSIC with CF77 6.0.0.3 or
00076  *                         previous on the T3D (obsolete)
00077  */
00078 #pragma _CRI duplicate _GETPOS_ as GETPOS
00079 #pragma _CRI duplicate _GETPOS_ as $GETPOS
00080 #ifdef _CRAYMPP
00081 #pragma _CRI duplicate _GETPOS_ as _GETPOS
00082 #endif
00083 long
00084 _GETPOS_(
00085         _f_int  *unump,
00086         _f_int  *arg2,
00087         _f_int  *arg3,
00088         _f_int  *arg4)
00089 {
00090         register int    errn;
00091         register int    narg;
00092         long            fp_arg[32];
00093         long            len;
00094         long            pos;
00095         long            ret;            /* return value */
00096         _f_int          *pa;
00097         _f_int          *stat = NULL;
00098         register unum_t unum;
00099         unit            *cup;
00100         struct fiostate cfs;
00101 
00102         unum    = *unump;
00103 
00104         STMT_BEGIN(unum, 0, T_GETPOS, NULL, &cfs, cup);  /* lock the unit */
00105 /*
00106  *      If not connected, do an implicit open.  Abort if the open fails.
00107  */
00108         if (cup == NULL)
00109                 cup     = _imp_open(&cfs, SEQ, UNF, unum, 0, NULL);
00110 
00111         if (cup->useq == 0)     /* If direct access file */
00112                 _ferr(&cfs, FEBIONDA, "GETPOS");
00113 
00114         narg    = _numargs();
00115 
00116         switch (narg) {
00117 
00118         case 1:                 /* IPOS = GETPOS(IUN) */
00119                 len     = 0;
00120                 pa      = NULL;
00121                 break;
00122 
00123         case 2:                 /* CALL GETPOS(IUN, PA) */
00124                 len     = 1;
00125                 pa      = arg2;
00126                 break;
00127 
00128         case 3:                 /* CALL GETPOS(IUN, LEN, PA) */
00129                 len     = *arg2;
00130                 pa      = arg3;
00131                 break;
00132 
00133         case 4:                 /* CALL GETPOS(IUN, LEN, PA, ISTAT) */
00134                 len     = *arg2;
00135                 pa      = arg3;
00136                 stat    = arg4;
00137                 break;
00138 
00139         default:
00140                 _ferr(&cfs, FEARGCNT, "GETPOS", narg, "1, 2, 3 or 4");
00141         }
00142 
00143 /*
00144  *      According to the file structure make the appropriate call to get
00145  *      the current file position.  Postion routines are file structure
00146  *      dependent.
00147  */
00148         errn    = 0;
00149 
00150         if (narg != 1 && len <= 0) {
00151                 ERET(FEBIOSNT);
00152         }
00153 
00154         switch ( cup->ufs ) {
00155 
00156         case  FS_TEXT:
00157         case  STD:
00158                 pos     = ftell(cup->ufp.std);
00159 /*
00160  *              If unformatted file, then positioning is done
00161  *              on a word boundry.  Therefore we need to calculate a word
00162  *              offset before returning to the caller.  For formatted files
00163  *              positioning must be done on a byte boundry.
00164  */
00165                 if ( cup->ufmt == NO )  /* if unformatted */
00166                         pos     = pos >> 3;
00167 
00168                 if ( pa != NULL )
00169                         *pa     = pos;
00170                 ret     = pos;
00171                 break;
00172 
00173         case FS_FDC:
00174 /*
00175  *              If this is async I/O, wait for any outstanding requests.
00176  */
00177                 WAITIO(cup, ERET(cup->uffsw.sw_error));
00178 
00179 
00180 /*
00181  *              If we can seek, use the seek interface
00182  */
00183                 if ((cup->uflagword & FFC_SEEKA) != 0)
00184                         {
00185                         fp_arg[0]       = 0;
00186                         fp_arg[1]       = 1;
00187                         ret     = XRCALL(cup->ufp.fdc, posrtn)
00188                                 cup->ufp.fdc, FP_BSEEK, fp_arg, 2, &cup->uffsw);
00189                         if (ret < 0)
00190                                 ERET(cup->uffsw.sw_error);
00191 /*
00192  *                      If unformatted file, then positioning is done
00193  *                      on a word boundry.  Therefore we need to calculate
00194  *                      a word offset before returning to the caller.  For
00195  *                      formatted files positioning must be done on a byte
00196  *                      boundry.
00197  */
00198                         if ( cup->ufmt == NO )
00199                                 ret     = _dshiftr(ret, ret, 6); /* to words (unfmtd) */
00200                         else
00201                                 ret     = _dshiftr(ret, ret, 3); /* to bytes (fmtd) */
00202                         if ( pa != NULL && len > 0)
00203                                 pa[0]   = ret;
00204                         }
00205                 else /* can't seek */
00206                         {
00207                         ret     = XRCALL(cup->ufp.fdc, posrtn)
00208                                 cup->ufp.fdc, FP_GETPOS, pa, len, &cup->uffsw);
00209                         if (ret < 0)
00210                                 ERET(cup->uffsw.sw_error);
00211                         }
00212 
00213                 break;
00214 
00215         case FS_AUX:
00216                 _ferr(&cfs, FEMIXAUX);
00217                 break;
00218         default:
00219                 _ferr(&cfs, FEINTFST);
00220 
00221         }
00222 
00223 getpos_done: 
00224         if (stat != NULL)
00225                 *stat   = errn;
00226         else if (errn != 0)
00227                 _ferr(&cfs, errn);
00228 
00229         STMT_END(cup, T_GETPOS, NULL, &cfs);
00230 
00231         return(ret);
00232 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines