Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
openutil.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/openutil.c 92.2    06/21/99 16:28:21"
00039 
00040 #include <errno.h>
00041 #include <ffio.h>
00042 #include <memory.h>
00043 #include <string.h>
00044 #include <liberrno.h>
00045 #include <stdarg.h>
00046 #include <unistd.h>
00047 #include <cray/assign.h>
00048 #include <sys/types.h>
00049 #include <sys/stat.h>
00050 #include "fio.h"
00051 
00052 /*
00053  *      _deduce_fstruct
00054  *
00055  *      Return the effective file structure FS_xxx code for a file based
00056  *      on the ufs field from the unit table, the (optional) ffio fffd,
00057  *      and the (optional) form for the file.  This routine's purpose is
00058  *      to return the best description of the file structure based on the
00059  *      information available.  For example, STD is translated into
00060  *      FS_TEXT or FS_UNBLOCKED when possible.  FS_FDC is translated into
00061  *      FS_COS, FS_UNBLOCKED, or FS_TEXT when possible and appropriate.
00062  *
00063  *      If ufs is FS_FDC, the ffio spec (if present) is examined.  If ufs
00064  *      is STD, the formatted/unformatted parameter will distinguish
00065  *      between FS_TEXT and FS_PURE.
00066  *
00067  *      _deduce_fstruct is callable in any of the following ways:
00068  *
00069  *              _deduce_fstruct(ufs, NULL, form);
00070  *
00071  *      Parameters:
00072  *              ufs     the ufs field from the unit table.
00073  *              fffd    a pointer to the ffio spec list, or NULL.
00074  *              form    1 if formatted, 0 if unformatted, -1 if 
00075  *                      unknown.
00076  *
00077  *      Return values:
00078  *              0               if file structure is unknown
00079  *              FS_TEXT         if newline-delimited records
00080  *              FS_COS          if COS blocked
00081  *              FS_UNBLOCKED    if unblocked 
00082  *              FS_PURE         if pure data 
00083  *              FS_TAPE         if tape block format
00084  *              FS_FDC          if misc FFIO file
00085  *              STD             if unsure whether TEXT or UNBLOCKED
00086  */
00087 
00088 int
00089 _deduce_fstruct(
00090         int             ufs,
00091         struct fdinfo   *fffd,
00092         int             formatted)
00093 {
00094         switch (ufs) {
00095         case FS_TEXT:
00096         case FS_COS:
00097         case FS_UNBLOCKED:
00098         case FS_PURE:
00099         case FS_TAPE:
00100         case FS_AUX:
00101                 return(ufs);
00102         case FS_BIN:
00103         case FS_U:
00104                 return(FS_UNBLOCKED);
00105         case STD:
00106                 if (formatted == -1) 
00107                         return(STD);
00108                 else {          
00109                         switch (formatted) {
00110                         case 0:
00111                                 return(FS_UNBLOCKED);
00112                         case 1: 
00113                                 return(FS_TEXT);
00114                         default:
00115                                 return(STD);
00116                         }
00117                 }
00118         case FS_FDC:
00119                 if (fffd == NULL)
00120                         return(0);
00121                 else {
00122                         switch (fffd->class) {
00123                         case CLASS_COS:
00124                                 return(FS_COS);
00125                         case CLASS_TEXT:
00126                                 return(FS_TEXT);
00127 #ifndef _UNICOS
00128                         case CLASS_X:
00129                                 return(FS_F77); /* might be nosve on UNICOS */
00130 #endif
00131                         case CLASS_SYSTEM:
00132                         case CLASS_SDS:
00133                         case CLASS_MR:
00134                         case CLASS_CACHE:
00135                         case CLASS_CACHEA:
00136                                 return(FS_UNBLOCKED);
00137                         default:
00138                                 return(0);
00139                         }
00140                 }
00141         }
00142         return(0);              /* MIPS compiler requires a return here */
00143 }
00144 
00145 /*
00146  *      _setup_cvrt
00147  *
00148  *      Set up the character set to use for character conversion.  The
00149  *      specification of numeric convert implies character conversion
00150  *      unless otherwise specified.
00151  *
00152  *      Internally, the unumcvrt and ucharset variables always control
00153  *      the conversion for numbers and characters respectively.
00154  */
00155 void
00156 _setup_cvrt(unit *cup)
00157 {
00158         if (cup->ucharset == 0 && cup->unumcvrt != 0)
00159                 switch(cup->unumcvrt) {
00160                         case NCV_IBM:
00161                                 cup->ucharset   = CS_EBCDIC;
00162                                 break;
00163                         case NCV_CDC:
00164                                 cup->ucharset   = CS_CDC;
00165                                 break;
00166                         default:
00167                                 break;
00168                 } /* switch */
00169 
00170         /* Suppress conversion if type is native. */
00171 
00172         if (cup->unumcvrt == NCV_NATIVE)
00173                 cup->unumcvrt   = 0;
00174 
00175         if (cup->ucharset == CS_NATIVE)
00176                 cup->ucharset   = 0;
00177 
00178         return;
00179 }
00180 
00181 /*
00182  *      _set_device_and_inode
00183  *
00184  *      Get a device and inode number for a file descriptor.  If sysfd == -1,
00185  *      then this Fortran file does not correspond to one system file.
00186  */
00187 void
00188 _set_device_and_inode(
00189         int     sysfd,
00190         dev_t   *devp,
00191         ino_t   *inodep)
00192 {
00193         struct stat     statbuf;
00194  
00195         if (sysfd >= 0 && fstat(sysfd, &statbuf) == 0) {
00196                 *inodep = statbuf.st_ino;
00197                 *devp   = statbuf.st_dev;
00198         }
00199         else {
00200                 *inodep = -1;
00201                 *devp   = -1;
00202         }
00203 
00204         return;
00205 }
00206 
00207 /*
00208  *      _UNIQINOD
00209  *
00210  *      Check that another unit hasn't been opened to the same file
00211  *
00212  *      If the inode is -1, something funny is going on.
00213  *      Most likely, the file has been assigned to SDS or another
00214  *      device by FDC.  So skip the check.
00215  *
00216  *      Return value:
00217  *         0    If no other unit is connected to the file identified
00218  *              by the (inode, device number) pair.
00219  *      <> 0    If another unit is connected to the file.  The return
00220  *              value is FEMDUPOP or FEOPFNCN if this is true.
00221  *              
00222  */ 
00223 int
00224 _uniqinod(
00225         unit            *cup,           /* unit being checked */
00226         assign_info     *aip)           /* assign information, or NULL */
00227 {
00228         register int     multup;        /* 1 if "-m on" specified */
00229         unit            *uptr;
00230         ino_t           inode;
00231         dev_t           device; 
00232         struct stat     statbuf;
00233 
00234         inode   = cup->uinode;
00235         device  = cup->udevice;
00236         multup  = (aip != NULL && aip->m_multup_flg && aip->m_multup);
00237 
00238         if (RSVD_UNUM(cup->uid))
00239                 return(0);      /* always allow units 100, 101, 102 to connect*/
00240 /*
00241  *      Stdin, stdout, and stderr may actually be different file descriptors
00242  *      pointing to the same file.  So allow them to connect to multiple
00243  *      units.
00244  */
00245         if (STDIN_FILENO <= cup->usysfd && cup->usysfd <= STDERR_FILENO)
00246                 return(0);
00247 
00248 /*
00249  *      Allow mr.scr or sds.scr files to connect.
00250  */
00251         if (inode == -1)
00252                 return(0);
00253 
00254         if (fstat(cup->usysfd, &statbuf) != -1) {
00255                 /*
00256                  * If file exists, check for character special files, fifo 
00257                  * files, and stdin/stdout/stderr.
00258                  * Allow multiple units to be connected to these files.
00259                  */
00260                 if(S_ISCHR(statbuf.st_mode))
00261                         return(0);
00262                 if(S_ISFIFO(statbuf.st_mode))
00263                         return(0);
00264         }
00265 
00266 /*
00267  *      Loop on _get_next_unit() to check for another open unit connected
00268  *      to the same file.
00269  */
00270         uptr    = _get_next_unit(NULL, 0, 1);
00271 
00272         for ( ; uptr != NULL ; uptr = _get_next_unit(uptr, 0, 1)) {
00273 
00274                 if (uptr == cup)                continue;
00275                 if (RSVD_UNUM(uptr->uid))       continue;
00276                 if (multup && uptr->umultup)    continue;
00277 
00278                 if (inode == uptr->uinode && device == uptr->udevice) {
00279                         if (multup)
00280                                 return(FEMDUPOP);
00281                         else
00282                                 return(FEOPFNCN);
00283                 }
00284         }
00285 
00286         return(0);
00287 }
00288 
00289 /*
00290  *      _mixed_scope
00291  *
00292  *      Check that the same unit number is not open as both a private and
00293  *      a global unit.
00294  *
00295  *      Return value:
00296  *                 0    if ok
00297  *              <> 0    if the unit number is opened as both a global and a
00298  *                      private.  Return value is error number if this is
00299  *                      true.
00300  */ 
00301 int
00302 _mixed_scope(unit *cup)
00303 {
00304         unit            *uptr;
00305         ino_t           inode;
00306         dev_t           device; 
00307         struct stat     statbuf;
00308 
00309 /*
00310  *      Loop on _get_next_unit() to check for multiple units connected with
00311  *      the same unit number.
00312  */
00313         uptr    = _get_next_unit(NULL, 0, 0);
00314 
00315         for ( ; uptr != NULL ; uptr = _get_next_unit(uptr, 0, 0)) {
00316                 if (uptr->uid == cup->uid && uptr->private != cup->private)
00317                         return(FEMIXSCP);
00318         }
00319 
00320         return(0);
00321 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines