Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
dsetpos.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/dsetpos.c  92.1    06/18/99 10:21:14"
00039 
00040 #include <foreign.h>
00041 #include <errno.h>
00042 #include <liberrno.h>
00043 #include "fio.h"
00044 
00045 /*
00046  *      internal setpos routine
00047  */
00048 int
00049 _setpos(
00050 FIOSPTR css,            /* statement state */
00051 unit    *cup,           /* unit pointer */
00052 int     *pa,            /* original user's Position Array. maybe tape... */
00053 int     len)            /* length of pa */
00054 {
00055         int     ret;
00056         int     pos;
00057         long    offset;
00058         int     whence;
00059         int     retstat;
00060         struct fdinfo *fio;
00061         int     fp_parm[2];
00062 
00063         retstat = 0;
00064 
00065 /*
00066  *      Wait for completion of a preceding asynchronous BUFFER IN/OUT.
00067  */
00068         WAITIO(cup, return(cup->uffsw.sw_error));
00069 
00070         cup->urecpos = 0;
00071         if (cup->uwrt) {
00072                 if (cup->utrunc) {
00073                         ret = _unit_trunc(cup);
00074                         if (ret != 0)
00075                                 return(ret);
00076                 }
00077                 cup->uwrt = 0;
00078         }
00079 /*
00080  *      According to the file structure make the appropriate call to set
00081  *      the file position.  Positioning routines are file structure dependent.
00082  */
00083         pos = pa[0];
00084         switch( cup->ufs ) {
00085 
00086         case  FS_TEXT:
00087         case  STD:
00088                 if ( pos == -1 || pos == -2 ) {
00089                         offset = 0;
00090                         whence = 2;
00091                 }
00092                 else {
00093                         offset = pos; 
00094                         whence = 0;
00095                 }
00096 /*
00097  *              If unformatted file, then positioning is done to a word
00098  *              boundary.  Converto to bytes
00099  */
00100                 if ( cup->ufmt == NO )
00101                         offset = _dshiftl(offset, offset, 3);
00102 
00103                 ret = fseek(cup->ufp.std, offset, whence);
00104                 if (ret != 0)
00105                         return(errno);
00106 
00107                 break;
00108 
00109         case  FS_FDC:
00110                 fio = cup->ufp.fdc;
00111                 
00112                 if ((cup->uflagword & FFC_SEEKA) ||
00113                     ((pos == -1 || pos == -2) && (cup->uflagword & FFC_SEEKE))){
00114 
00115                         if ( pos == -1 || pos == -2 ) {
00116 
00117                                 if ( XRCALL(fio, seekrtn) fio, 0, 2,
00118                                                            &cup->uffsw) < 0)
00119                                         return(cup->uffsw.sw_error);
00120 
00121                         }
00122                         else {
00123 #if     defined(__mips) || defined(_LITTLE_ENDIAN)
00124         /* A couple of problems will need to be solved before we can */
00125         /* use the ffio posrtn on MIPS. That routine is currently */
00126         /* documented as expecting longs in the 3rd parameter for */
00127         /* FP_BSEEK . But more importantly, FP_BSEEK isn't supported */
00128         /* yet for MIPS, because  longs are not sufficiently large */
00129         /* to contain all possible file positions. */
00130                                 return(FDC_ERR_NOSUP);
00131 #else
00132 /*
00133  *                              If formatted file, then positioning is done
00134  *                              to a byte boundary.  If unformatted, input pos
00135  *                              is in words.  Convert to bits.
00136  */
00137                                 if ( cup->ufmt == YES )
00138                                         fp_parm[0] = _dshiftl(pos, pos, 3);
00139                                 else
00140                                         fp_parm[0] = _dshiftl(pos, pos, 6);
00141 /*
00142  *                              Check for Invalid argument.  If shifted result
00143  *                              is negative, we have a problem.
00144  */
00145                                 if ( fp_parm[0] < 0 )
00146                                         return(FEBIOISP);
00147                                         
00148                                 fp_parm[1] = 0;
00149                                 ret = XRCALL(fio, posrtn) fio, 
00150                                         FP_BSEEK, fp_parm, 2, &cup->uffsw);
00151 
00152                                 if (ret < 0)
00153                                         return(cup->uffsw.sw_error);
00154 #endif
00155                         }
00156                 }
00157                 else {
00158 /*
00159  *                      Assume that positioning is done via a 'magic cookie'.
00160  *                      Just pass it through.
00161  */
00162                         if( XRCALL(fio, posrtn)
00163                                 fio, FP_SETPOS, pa, len, &cup->uffsw) < 0)
00164                                 return(cup->uffsw.sw_error);
00165                 }
00166 
00167                 break;
00168 
00169         case FS_AUX:
00170                 return(FEMIXAUX);
00171         default:
00172                 return(FEINTFST);
00173         }
00174 /*
00175  *      Set the end flag if going to EOD, else clear it.
00176  */
00177         if (pos == -1 && len == 1) {
00178                 if (cup->ufs != FS_FDC)
00179                         cup->uend = LOGICAL_ENDFILE;
00180                 else {
00181                         /*
00182                          * We are after the terminal endfile record of the file,
00183                          * but we must decide if it's a logical or physical 
00184                          * endfile record.
00185                          */
00186                         if ((cup->uflagword & FFC_WEOF) == 0)
00187                                 cup->uend = LOGICAL_ENDFILE;
00188                         else {
00189                                 off_t fbytepos; /* byte offset within file */
00190 
00191                                 switch (fio->class) {
00192                                 case CLASS_COS:
00193                                         /*
00194                                          * Since seekrtn for the cos layer
00195                                          * when called with 0,2 does NOT return
00196                                          * the resulting byte offset, we
00197                                          * must query the position with this
00198                                          * seekrtn call.  The only case
00199                                          * where 0 might be returned is when                                             * the file is empty.
00200                                          */
00201                                         fbytepos = XRCALL(fio, seekrtn) fio, 0,
00202                                                 1, &cup->uffsw);
00203                                         if (fbytepos > 0)
00204                                                 cup->uend = PHYSICAL_ENDFILE;
00205                                         else
00206                                                 cup->uend = LOGICAL_ENDFILE;
00207                                         break;
00208                                 default:
00209                                         return(FEBIOSNT);
00210                                 }
00211                         }
00212                 }
00213         }
00214         else
00215                 /* 
00216                  * Sadly, we may be positioned after an endfile record, 
00217                  * and not know it.   This should be fixed. 
00218                  */
00219                 cup->uend = BEFORE_ENDFILE;
00220         return(OK);
00221 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines