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/pxfsetenv.c 92.1 06/29/99 11:36:06" 00038 00039 00040 00041 #include <malloc.h> 00042 #include <fortran.h> 00043 #include <errno.h> 00044 #include <liberrno.h> 00045 #include <stdlib.h> 00046 #include <string.h> 00047 00048 #ifndef _UNICOS 00049 #include <stddef.h> 00050 #endif 00051 00052 extern char *_fc_acopy(_fcd f); 00053 00054 /* 00055 * PXFSETENV -- set environment variable pair 00056 * (section 4.6.1 of POSIX 1003.9-1992) 00057 * 00058 * Call from Fortran: 00059 * 00060 * SUBROUTINE PXFSETENV(NAME, LENNAME, NEW, LENNEW, IOVERWRITE, IERROR) 00061 * CHARACTER*(*) NAME, NEW 00062 * INTEGER LENNAME, LENNEW, IOVERWRITE, IERROR 00063 * 00064 * Where: 00065 * 00066 * NAME is an input character variable or array element 00067 * containing the environment name value to be set. 00068 * 00069 * LENNAME is an input integer variable containing the 00070 * character length of NAME. If LENNAME is zero, 00071 * trailing blanks are removed. 00072 * 00073 * NEW is an input character variable or array element 00074 * containing the new environment value for the 00075 * name=value environment pair. 00076 * 00077 * LENNEW is an input integer variable containing the 00078 * character length of NEW. If LENNEW is zero, 00079 * trailing blanks are removed. 00080 * 00081 * IOVERWRITE is an input integer variable containing a zero 00082 * or nonzero number. When the value is zero, 00083 * a name=value pair with the name value matching 00084 * NAME will not be replaced with a new NAME=NEW 00085 * pair. A nonzero IOVERWRITE value will replace 00086 * the matching name=value pair with NAME=NEW pair. 00087 * 00088 * IERROR is an output integer varible that will contain 00089 * the status: 00090 * 00091 * zero - environment varible was changed. 00092 * 00093 * nonzero - PXFSETENV was not successful. 00094 * 00095 * PXFSETENV may return any of the following error 00096 * values: 00097 * 00098 * EINVAL If LENNAME < 0 or LENNAME > LEN(NAME) or 00099 * LENNEW < 0 or LENNEW > LEN(NEW) 00100 * 00101 * ENOMEM If PSFSETENV is unable to obtain memory 00102 * to copy NAME and NEW to a new name=value 00103 * string. 00104 * 00105 * 00106 */ 00107 00108 #ifdef _UNICOS 00109 void 00110 PXFSETENV( 00111 #else 00112 void 00113 _PXFSETENV( 00114 #endif 00115 _fcd NAME, 00116 _f_int *LENNAME, 00117 _fcd NEW, 00118 _f_int *LENNEW, 00119 _f_int *IOTHERWISE, 00120 _f_int *IERROR) 00121 { 00122 int ilenname, slenname, ilennew, slennew; 00123 char *buf, *cname, *cnew; 00124 00125 ilenname = *LENNAME; 00126 slenname = _fcdlen(NAME); 00127 ilennew = *LENNEW; 00128 slennew = _fcdlen(NEW); 00129 *IERROR = 0; 00130 00131 /* check if the length of the lenname input argument is valid. */ 00132 if (ilenname < 0 || ilenname > slenname || 00133 ilennew < 0 || ilennew > slennew) 00134 *IERROR = EINVAL; 00135 else { 00136 if (ilenname == 0) { 00137 /* 00138 * If length is zero, user wants trailing blanks stripped. 00139 * Otherwise, malloc memory and copy the string adding a 00140 * NULL terminator. 00141 */ 00142 00143 cname = _fc_acopy(NAME); 00144 ilenname = slenname; 00145 } else { 00146 00147 cname = (char *) malloc (ilenname + 1); 00148 if (cname != NULL) { 00149 memcpy(cname, _fcdtocp(NAME), ilenname); 00150 cname[ilenname] ='\0'; 00151 } else { 00152 *IERROR = ENOMEM; 00153 return; 00154 } 00155 00156 } 00157 00158 /* check if NAME already exists in the envrion variable when 00159 IOTHERWISE is zero. */ 00160 if (*IOTHERWISE == 0 && getenv(cname) != NULL) 00161 return; 00162 00163 if (ilennew == 0) { 00164 00165 /* 00166 * If length is zero, user wants trailing blanks stripped. 00167 * Otherwise, malloc memory and copy the string adding a 00168 * NULL terminator. 00169 */ 00170 00171 cnew = _fc_acopy(NEW); 00172 ilennew = slennew; 00173 } else { 00174 00175 cnew = (char *) malloc(ilennew + 1); 00176 if (cnew != NULL) { 00177 memcpy(cnew, _fcdtocp(NEW), ilennew); 00178 cnew[ilennew] ='\0'; 00179 } else { 00180 *IERROR = ENOMEM; 00181 return; 00182 } 00183 00184 } 00185 00186 if (cname == NULL || cnew == NULL) 00187 *IERROR = ENOMEM; 00188 else { 00189 00190 /* concatenate strings to make cname=cnew pair */ 00191 if ((buf = (char *) malloc(ilenname + ilennew + 2)) == NULL) { 00192 *IERROR = ENOMEM; 00193 return; 00194 } else { 00195 00196 /* create 'cname=cnew' pair */ 00197 strcpy(buf, cname); 00198 strcat(buf, "=\0"); 00199 strcat(buf, cnew); 00200 free(cname); 00201 free(cnew); 00202 } 00203 00204 if (putenv(buf) != 0) 00205 *IERROR = ENOMEM; 00206 } 00207 } 00208 return; 00209 } 00210 00211 #ifndef _UNICOS 00212 00213 void 00214 pxfsetenv_( 00215 char *NAME, 00216 _f_int *LENNAME, 00217 char *VALUE, 00218 _f_int *LENVAL, 00219 _f_int *IOTHERWISE, 00220 _f_int *IERROR, 00221 _f_int namelen, 00222 _f_int valuelen) 00223 { 00224 _PXFSETENV( _cptofcd(NAME, namelen), LENNAME, 00225 _cptofcd(VALUE, valuelen), LENVAL, 00226 IOTHERWISE, IERROR); 00227 return; 00228 } 00229 00230 #endif 00231 00232 00233
1.5.6