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 #pragma ident "@(#) libf/pxf/pxfgetenv.c 92.1 06/29/99 11:36:06" 00038 00039 /* 00040 * PXFGETENV -- return value for environment name 00041 * 00042 * Call from Fortran: 00043 * 00044 * CHARACTER*n NAME, VALUE 00045 * INTEGER LENNAME, LENVAL, IERROR 00046 * CALL PXFGETENV( NAME, LENNAME, VALUE, LENVAL, IERROR ) 00047 * 00048 * where 00049 * 00050 * NAME is an input character variable or array element 00051 * containing the name of an environment variable. 00052 * GETENV searches for NAME in the environment. 00053 * 00054 * LENNAME is an input integer variable containing the length 00055 * of NAME in characters. If LENNAME is zero, strip 00056 * trailing blanks. Change the length of the input 00057 * NAME to remove the number of blanks stripped. If 00058 * LENNAME is zero and NAME is all blanks, this is a 00059 * NULL string. 00060 * 00061 * VALUE is an output character variable or array element 00062 * containing the value of the environment variable 00063 * NAME. 00064 * 00065 * LENVAL is an output integer variable containing the length 00066 * of VALUE in characters. If NAME is found but has 00067 * no value, LENVAL is zero and VALUE contains all 00068 * blanks to indicate a NULL string. 00069 * 00070 * IERROR is output integer variable containing the status: 00071 * Zero is returned if GETENV is successful, i.e., 00072 * NAME is found. 00073 * EINVAL is returned if NAME is not in the 00074 * environment list. 00075 * ETRUNC is returned if the declared length of VALUE 00076 * is insufficient to contain the string to be 00077 * returned, the value of NAME shall be truncated to 00078 * fit in VALUE, and LENVAL will contain the original 00079 * length of the value of NAME before truncation. 00080 * 00081 */ 00082 #if !defined(_ABSOFT) 00083 #include <malloc.h> 00084 #endif 00085 #include <fortran.h> 00086 #include <errno.h> 00087 #include <liberrno.h> 00088 #include <stdlib.h> 00089 #include <string.h> 00090 00091 extern char *_fc_acopy(_fcd f); 00092 00093 #ifndef _UNICOS 00094 #include <stddef.h> 00095 00096 #if defined(BUILD_OS_DARWIN) 00097 extern char **environ; 00098 #else /* defined(BUILD_OS_DARWIN) */ 00099 extern char **_environ; 00100 #endif /* defined(BUILD_OS_DARWIN) */ 00101 00102 char * 00103 _GETENV(char *name) 00104 { 00105 char *s1, *s2; 00106 #if defined(BUILD_OS_DARWIN) 00107 char **p = environ; 00108 #else /* defined(BUILD_OS_DARWIN) */ 00109 char **p = _environ; 00110 #endif /* defined(BUILD_OS_DARWIN) */ 00111 char *ret = NULL; 00112 if (p == NULL) 00113 return(ret); 00114 while (*p != NULL) { 00115 s1 = name; 00116 s2 = *p++; 00117 while (*s1 == *s2++) 00118 if (*s1++ == '=') 00119 return(s2); 00120 if (*s1 == '\0' && *(s2-1) == '=') 00121 return(s2); 00122 } 00123 return(ret); 00124 } 00125 #endif 00126 00127 #ifdef _UNICOS 00128 void 00129 PXFGETENV( 00130 #else 00131 void 00132 _PXFGETENV( 00133 #endif 00134 _fcd name, 00135 _f_int *lenname, 00136 _fcd value, 00137 _f_int *lenval, 00138 _f_int *ierror) 00139 { 00140 char *buf, *cp, *v; 00141 int i, lensrc, lenin, lentarg, lenv; 00142 00143 lenin = *lenname; 00144 lensrc = _fcdlen(name); 00145 lentarg = _fcdlen(value); 00146 *ierror = 0; 00147 00148 /* check if the length of the lenname input argument is valid. */ 00149 if (lenin < 0 || lenin > lensrc) { 00150 *ierror = EINVAL; 00151 *lenval = 0; 00152 return; 00153 } 00154 00155 if (lensrc != 0) { 00156 00157 /* Copy input name. If lenname = 0, the trailing blanks 00158 * must be stripped and the string may be null when the 00159 * trailing blanks are stripped. 00160 */ 00161 00162 if ((buf = _fc_acopy(name)) == NULL) { 00163 00164 *ierror = ENOMEM; 00165 *lenval = 0; 00166 return; 00167 } 00168 00169 /* check for all blank input string */ 00170 if (strlen(buf) == 0) { 00171 *lenval = 0; 00172 cp = _fcdtocp(value); 00173 (void) memset (cp, (int) ' ', lentarg); 00174 return; 00175 } 00176 00177 /* get value of environment variable name */ 00178 #ifdef _UNICOS 00179 if ((v = getenv (buf)) == NULL) { 00180 #else 00181 if ((v = _GETENV(buf)) == NULL) { 00182 #endif 00183 00184 /* name not found, return without 00185 * setting other values. 00186 */ 00187 *ierror = EINVAL; 00188 *lenval = 0; 00189 return; 00190 } 00191 00192 free(buf); 00193 00194 lenv = strlen(v); 00195 *lenval = lenv; 00196 /* 00197 * return ETRUNC when string length greater than size of VALUE 00198 * but copy the string up to the size of VALUE 00199 */ 00200 if(lenv > lentarg) 00201 *ierror = ETRUNC; 00202 00203 /* destination is a character variable */ 00204 cp = _fcdtocp(value); 00205 for (i = 0; i < lentarg && *v != '\0'; i++){ 00206 *cp++=*v++; 00207 } 00208 } else { 00209 /* zero-length FCD NAME, return null pointer */ 00210 *lenval = 0; 00211 i=0; 00212 cp = _fcdtocp(value); 00213 } 00214 00215 /* blank fill if necessary */ 00216 for (;i<lentarg;i++){ 00217 *cp++=' '; 00218 } 00219 return; 00220 } 00221 00222 #ifndef _UNICOS 00223 00224 void 00225 pxfgetenv_( 00226 char *NAME, 00227 _f_int *LENNAME, 00228 char *VALUE, 00229 _f_int *LENVAL, 00230 _f_int *IERROR, 00231 _f_int namelen, 00232 _f_int valuelen) 00233 { 00234 _PXFGETENV( _cptofcd(NAME, namelen), LENNAME, 00235 _cptofcd(VALUE, valuelen), LENVAL, IERROR); 00236 return; 00237 } 00238 00239 #endif
1.5.6