00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 const char *XrdOucEnvCVSID = "$Id: XrdOucEnv.cc 38011 2011-02-08 18:35:57Z ganis $";
00014
00015 #include "string.h"
00016 #include "stdio.h"
00017 #include <stdlib.h>
00018
00019 #include "XrdOuc/XrdOucEnv.hh"
00020
00021
00022
00023
00024
00025 XrdOucEnv::XrdOucEnv(const char *vardata, int varlen,
00026 const XrdSecEntity *secent)
00027 : env_Hash(8,13), secEntity(secent)
00028 {
00029 char *vdp, varsave, *varname, *varvalu;
00030
00031 if (!vardata) {global_env = 0; global_len = 0; return;}
00032
00033
00034
00035 if (!varlen) varlen = strlen(vardata);
00036 global_env = (char *)malloc(varlen+2); global_len = varlen;
00037 if (*vardata == '&') vdp = global_env;
00038 else {*global_env = '&'; vdp = global_env+1;}
00039 memcpy((void *)vdp, (const void *)vardata, (size_t)varlen);
00040 *(vdp+varlen) = '\0';
00041 vdp = global_env;
00042
00043
00044
00045 if (vdp) while(*vdp)
00046 {if (*vdp != '&') {vdp++; continue;}
00047 varname = ++vdp;
00048
00049 while(*vdp && *vdp != '=') vdp++;
00050 if (!*vdp) break;
00051 *vdp = '\0';
00052 varvalu = ++vdp;
00053
00054 while(*vdp && *vdp != '&') vdp++;
00055 varsave = *vdp; *vdp = '\0';
00056
00057 if (*varname && *varvalu)
00058 env_Hash.Rep(varname, strdup(varvalu), 0, Hash_dofree);
00059
00060 *vdp = varsave; *(--varvalu) = '=';
00061 }
00062 return;
00063 }
00064
00065
00066
00067
00068
00069 char *XrdOucEnv::Delimit(char *value)
00070 {
00071 while(*value) if (*value == ',') {*value = '\0'; return ++value;}
00072 else value++;
00073 return (char *)0;
00074 }
00075
00076
00077
00078
00079
00080 int XrdOucEnv::Export(const char *Var, const char *Val)
00081 {
00082 int vLen = strlen(Var);
00083 char *eBuff;
00084
00085
00086
00087 if (!Val) Val = "";
00088
00089
00090
00091 eBuff = (char *)malloc(vLen+strlen(Val)+2);
00092
00093
00094
00095 strcpy(eBuff, Var);
00096 *(eBuff+vLen) = '=';
00097 strcpy(eBuff+vLen+1, Val);
00098 return putenv(eBuff);
00099 }
00100
00101
00102
00103 int XrdOucEnv::Export(const char *Var, int Val)
00104 {
00105 char buff[32];
00106 sprintf(buff, "%d", Val);
00107 return Export(Var, buff);
00108 }
00109
00110
00111
00112
00113
00114 bool XrdOucEnv::Import( const char *var, char *&val )
00115 {
00116 char *value = getenv( var );
00117 if( !value || !*value )
00118 return false;
00119
00120 val = value;
00121 return true;
00122 }
00123
00124
00125
00126
00127 bool XrdOucEnv::Import( const char *var, long &val )
00128 {
00129 char *value;
00130 if( !Import( var, value ) )
00131 return false;
00132
00133 char *status;
00134 val = strtol( value, &status, 0 );
00135
00136 if( *status != 0 )
00137 return false;
00138 return true;
00139 }
00140
00141
00142
00143
00144
00145 long XrdOucEnv::GetInt(const char *varname)
00146 {
00147
00148
00149
00150 if (env_Hash.Find(varname) == NULL) {
00151 return -999999999;
00152 } else {
00153 return atol(env_Hash.Find(varname));
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162 void XrdOucEnv::PutInt(const char *varname, long value)
00163 {
00164
00165
00166 char stringValue[24];
00167 sprintf(stringValue, "%ld", value);
00168 env_Hash.Rep(varname, strdup(stringValue), 0, Hash_dofree);
00169 }