Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
util.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/util.c     92.2    08/02/99 10:38:48"
00039 
00040 #include <ctype.h>
00041 #include <ffio.h>
00042 #include <memory.h>
00043 #include <string.h>
00044 #include <unistd.h>
00045 #include <sys/types.h>
00046 #include <sys/stat.h>
00047 #include "fio.h"
00048   
00049 /*
00050  *      _copy_n_trim
00051  *              copies "alen" characters from "a" into string
00052  *              "b", which gets null-terminated.  Trailing blanks
00053  *              are also removed.
00054  */
00055 void
00056 _copy_n_trim(char *a, ftnlen alen, char *b)
00057 {
00058         register char   *p;
00059   
00060         *(b + alen)     = '\0';
00061   
00062         /* Copy alen characters or up to null byte */
00063   
00064         (void) strncpy(b, a, alen);     /* Copy a to b */
00065   
00066         /* Delete any trailing blanks */
00067   
00068         for (p = b + strlen(b) - 1; p >= b && *p == ' '; p--)
00069                 *p      = '\0';
00070   
00071         return;
00072 }
00073   
00074 
00075 /* 
00076  *      _b_char copies null-terminated string "a" into a blank-padded
00077  *              buffer "b" of length "blen".
00078  */
00079 void
00080 _b_char(char *a, char *b, ftnlen blen)
00081 {
00082         register int    i;
00083   
00084         i       = strlen(a);                    /* Find end of a */
00085         i       = MIN(i, blen);                 /* Use shorter of the two */
00086   
00087         (void) strncpy(b, a, i);                /* Copy a to b */
00088   
00089         b       = b + i;
00090   
00091         (void) memset(b, (int) ' ', blen - i);  /* Blank rest of b */
00092   
00093         return;
00094 }
00095 
00096 /* 
00097  *      _is_file_name   
00098  *
00099  *              Determines whether an integer is an 8-byte hollerith file
00100  *              name.   Returns nonzero if it is and 0 if it is not.
00101  */
00102 
00103 int
00104 _is_file_name(long n)
00105 {
00106 #ifndef _UNICOS
00107         return(0);
00108 #else
00109         register short  i;
00110         long            rbn;
00111         char            *cp;
00112 /*
00113  *      Convert trailing blanks to nulls.
00114  */
00115         rbn     = _RBN(&n);
00116         cp      = (char *)&rbn;
00117 
00118         for (i = (sizeof(long) - 1); i >= 0; i--) {
00119                 if (cp[i] != '\0')
00120                         break;
00121         }
00122 
00123         if (i < 0)
00124                 return(0);
00125 
00126 /*
00127  *      Check that all characters prior to the trailing nulls are printable 
00128  *      and therefore are valid file name characters.
00129  */
00130         while (i >= 0) {
00131                 if (! isprint(cp[i]))
00132                         return(0);
00133                 i--;
00134         }
00135 
00136         return(1);                      /* Yes!  It's a file name. */
00137 #endif
00138 }
00139 
00140 /*
00141  * Removes trailing blanks from a character string.
00142  */
00143 
00144 static void
00145 g_skip(char *a, int alen)
00146 {
00147         char    *p;
00148 
00149         for (p = a + alen - 1; p >= a && *p == ' '; p--)
00150                 *p      = '\0';
00151 
00152         return;
00153 }
00154 
00155 /* 
00156  *      _fortname       
00157  *
00158  *              Makes a standard fortran file name (currently of the
00159  *              form "fort.n") in buffer "buf" using unit number "n".
00160  *              Buffer b must be of sufficient length to receive
00161  *              the name (MXUNITSZ).
00162  *
00163  *              "n" can be hollerith data.  The name generated is the 
00164  *              hollerith name trimmed of any trailing blanks.  This is
00165  *              a CRAY extension only.
00166  *
00167  *              Returns the number of characters in the name (excluding
00168  *              the \0) or a negative value if an error occurred.
00169  */
00170 
00171 int
00172 _fortname(
00173         char    *buf,   /* buffer to receive file name */
00174         unum_t  n)      /* unit number */
00175 {
00176 #ifdef  _UNICOS
00177         register short  lim;    /* Maximum size of a hollerith file name */
00178 
00179         lim     = sizeof(long);
00180 
00181         if (_is_file_name((long) n)) {
00182                 buf[lim]        = '\0';
00183                 (void) strncpy(buf, (char *)&n, lim);
00184                 g_skip(buf, strlen(buf));  /* Delete trailing blanks */
00185                 return(strlen(buf));
00186         }
00187         else
00188 #endif
00189                 return(sprintf(buf, "fort.%lld", n));
00190 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines