char *pStVarTwo = NULL;
int lenStVarTwo = 0;
+ int iflag = 0;
+ int m_out = 1, n_out = 1;
+
Rhs = Max(Rhs,0);
CheckRhs(1,2);
return 0;
}
- #ifdef _MSC_VER
- length_env = _MAX_ENV;
- #else
- length_env = bsiz;
- #endif
-
- default_env_value = pStVarTwo;
- env_name = pStVarOne;
-
- env_value = (char*)MALLOC( (length_env + 1) *sizeof(char) );
+ default_env_value = pStVarTwo;
+ env_name = pStVarOne;
- if (env_value == NULL)
- {
- if (default_env_value) {FREE(default_env_value); default_env_value = NULL;}
- if (env_name) {FREE(env_name); env_name = NULL;}
-
- Scierror(999,_("%s: No more memory.\n"), fname);
- return 0;
- }
- else
- {
- int m_out = 1, n_out = 1;
- int iflag = 0;
+ C2F(getenvc)(&ierr, env_name, NULL, &length_env, &iflag);
- C2F(getenvc)(&ierr, env_name, env_value, &length_env, &iflag);
+ if(ierr == 0)
+ {
+ env_value = (char*)MALLOC( (length_env + 1) * sizeof(char) );
+ if (env_value == NULL)
+ {
+ Scierror(999,_("%s: No more memory.\n"), fname);
+ }
+ else
+ {
+ C2F(getenvc)(&ierr, env_name, env_value, &length_env, &iflag);
- if (ierr == 0)
- {
- sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m_out, n_out, &env_value);
+ //create variable on stack and return it.
+ sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m_out, n_out, (const char**)&env_value);
if(sciErr.iErr)
{
printError(&sciErr, 0);
LhsVar(1) = Rhs + 1;
PutLhsVar();
- }
- else
+ }
+ }
+ else //ierr == 1 -> env var does not exist.
+ {
+ if (default_env_value)
{
- if (default_env_value)
- {
- sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m_out, n_out, &default_env_value);
- if(sciErr.iErr)
- {
- printError(&sciErr, 0);
- Scierror(999,_("%s: Memory allocation error.\n"), fname);
- return 0;
- }
-
- LhsVar(1) = Rhs + 1;
- PutLhsVar();
- }
- else
+ sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m_out, n_out, &default_env_value);
+ if(sciErr.iErr)
{
- Scierror(999,_("%s: Undefined environment variable %s.\n"), fname, env_name);
+ printError(&sciErr, 0);
+ Scierror(999,_("%s: Memory allocation error.\n"), fname);
}
+ else
+ {
+ LhsVar(1) = Rhs + 1;
+ PutLhsVar();
+ }
+ }
+ else
+ {
+ Scierror(999,_("%s: Undefined environment variable %s.\n"), fname, env_name);
}
+ }
- if (default_env_value) {FREE(default_env_value); default_env_value = NULL;}
- if (env_name) {FREE(env_name); env_name = NULL;}
- if (env_value) {FREE(env_value); env_value = NULL;}
- }
+
+ if (default_env_value)
+ {
+ FREE(default_env_value);
+ default_env_value = NULL;
+ }
+
+ if (env_name)
+ {
+ FREE(env_name);
+ env_name = NULL;
+ }
+
+ if (env_value)
+ {
+ FREE(env_value);
+ env_value = NULL;
+ }
+
return 0;
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
#ifndef _MSC_VER
static void searchenv_others(const char *filename, const char *varname,
- char *pathname);
+ char *pathname);
#endif
/*--------------------------------------------------------------------------*/
-void C2F(getenvc)(int *ierr,char *var,char *buf,int *buflen,int *iflag)
+void C2F(getenvc)(int *ierr, char *var, char *buf, int *buflen, int *iflag)
{
#ifdef _MSC_VER
- BOOL bMalloc = FALSE;
- wchar_t *wvar = to_wide_string(var);
- wchar_t *wbuf = _wgetenv(wvar);
-
- *ierr = 0;
- if(wbuf == NULL)
- {
- bMalloc = TRUE;
- wbuf = (wchar_t*)MALLOC(sizeof(wchar_t) * *buflen);
- if (GetEnvironmentVariableW(wvar, wbuf,(DWORD)*buflen) == 0)
- {
- if( *iflag == 1 )
- {
- sciprint(_("Undefined environment variable %s.\n"),var);
- }
- *ierr=1;
- }
- }
-
- if(*ierr != 1)
- {
- char* temp = wide_string_to_UTF8(wbuf);
- strcpy(buf, temp);
- *buflen = (int)strlen(buf);
- *ierr=0;
- }
-
- if(bMalloc)
- {
- FREE(wbuf);
- }
+ BOOL bMalloc = FALSE;
+ wchar_t *wvar = to_wide_string(var);
+ wchar_t *wbuf = _wgetenv(wvar);
+
+ *ierr = 0;
+ if (wbuf == NULL)
+ {
+ DWORD iLen = GetEnvironmentVariableW(wvar, NULL, 0);
+ bMalloc = TRUE;
+ if (iLen == 0)
+ {
+ if ( *iflag == 1 )
+ {
+ sciprint(_("Undefined environment variable %s.\n"), var);
+ }
+ *ierr = 1;
+ }
+ else
+ {
+ *buflen = iLen;
+ wbuf = (wchar_t*)MALLOC(sizeof(wchar_t) * *buflen);
+ if (GetEnvironmentVariableW(wvar, wbuf, (DWORD)*buflen) == 0)
+ {
+ if ( *iflag == 1 )
+ {
+ sciprint(_("Undefined environment variable %s.\n"), var);
+ }
+ *ierr = 1;
+ }
+ }
+ }
+
+ if (*ierr != 1)
+ {
+ *buflen = (int)wcslen(wbuf);
+ if (buf)
+ {
+ char* temp = wide_string_to_UTF8(wbuf);
+ strcpy(buf, temp);
+ *ierr = 0;
+ }
+ }
+
+ if (bMalloc)
+ {
+ FREE(wbuf);
+ }
#else
- char *locale = NULL;
- locale=getenv(var);
- if ( locale == NULL )
- {
- if ( *iflag == 1 ) sciprint(_("Undefined environment variable %s.\n"),var);
- *ierr=1;
- }
- else
- {
- *buflen = (int)strlen(locale);
- strcpy(buf,locale);
- *ierr=0;
- }
+ char *locale = NULL;
+ locale = getenv(var);
+ if ( locale == NULL )
+ {
+ if ( *iflag == 1 )
+ {
+ sciprint(_("Undefined environment variable %s.\n"), var);
+ }
+ *ierr = 1;
+ }
+ else
+ {
+ // updating the size of char array "buf"
+ *buflen = (int)strlen(locale);
+ if (buf)
+ {
+ // to avoid buffer overflow, we check the size of the source buffer
+ // and the size of the destination buffer
+ if ((int) strlen(locale) <= *buflen)
+ {
+ // "locale" can be copied entirely to "buf"
+ strcpy(buf, locale);
+ *ierr = 0;
+ }
+ else
+ {
+ *ierr = 2;
+ }
+ }
+ }
#endif
}
/*--------------------------------------------------------------------------*/
#ifndef _MSC_VER
static void searchenv_others(const char *filename,
- const char *varname,
- char *pathname)
+ const char *varname,
+ char *pathname)
{
- char *cp = NULL;
-
- *pathname = '\0';
-
- if( filename[0] == DIR_SEPARATOR[0])
- {
- strcpy(pathname, filename);
- return;
- }
-
- cp = getenv(varname);
- if(cp == NULL)
- {
- /* environment Variable not defined. */
- return;
- }
-
- while(*cp)
- {
- char *concat = NULL;
- *pathname = '\0';
- concat = pathname;
- /* skip PATH_SEPARATOR[0] and empty entries */
- while( (*cp) && (*cp == PATH_SEPARATOR[0]) )
- {
- cp++;
- }
-
- /* copy path */
- while( (*cp) && (*cp != PATH_SEPARATOR[0]) )
- {
- *concat = *cp;
- cp++;
- concat++;
- }
-
- if ( concat == pathname )
- {
- /* filename not found */
- *pathname = '\0';
- return;
- }
-
- if( *(concat-1) != DIR_SEPARATOR[0] )
- {
- /* add directory separator */
- *concat = DIR_SEPARATOR[0];
- concat++;
- }
-
- /* concatate path & filename */
- strcpy(concat, filename);
-
- /* file exists ? */
- if(FileExist(pathname))
- {
- // file found
- return;
- }
- }
-
- /* file not found */
- *pathname = '\0';
+ char *cp = NULL;
+
+ *pathname = '\0';
+
+ if ( filename[0] == DIR_SEPARATOR[0])
+ {
+ strcpy(pathname, filename);
+ return;
+ }
+
+ cp = getenv(varname);
+ if (cp == NULL)
+ {
+ /* environment Variable not defined. */
+ return;
+ }
+
+ while (*cp)
+ {
+ char *concat = NULL;
+ *pathname = '\0';
+ concat = pathname;
+ /* skip PATH_SEPARATOR[0] and empty entries */
+ while ( (*cp) && (*cp == PATH_SEPARATOR[0]) )
+ {
+ cp++;
+ }
+
+ /* copy path */
+ while ( (*cp) && (*cp != PATH_SEPARATOR[0]) )
+ {
+ *concat = *cp;
+ cp++;
+ concat++;
+ }
+
+ if ( concat == pathname )
+ {
+ /* filename not found */
+ *pathname = '\0';
+ return;
+ }
+
+ if ( *(concat - 1) != DIR_SEPARATOR[0] )
+ {
+ /* add directory separator */
+ *concat = DIR_SEPARATOR[0];
+ concat++;
+ }
+
+ /* concatate path & filename */
+ strcpy(concat, filename);
+
+ /* file exists ? */
+ if (FileExist(pathname))
+ {
+ // file found
+ return;
+ }
+ }
+
+ /* file not found */
+ *pathname = '\0';
}
#endif
/*--------------------------------------------------------------------------*/
-char *searchEnv(const char *name,const char *env_var)
+char *searchEnv(const char *name, const char *env_var)
{
- char *buffer = NULL;
- char fullpath[PATH_MAX];
+ char *buffer = NULL;
+ char fullpath[PATH_MAX];
- strcpy(fullpath,"");
+ strcpy(fullpath, "");
#if _MSC_VER
- {
- wchar_t *wname = NULL;
- wchar_t *wenv_var = NULL;
- wchar_t wfullpath[PATH_MAX];
+ {
+ wchar_t *wname = NULL;
+ wchar_t *wenv_var = NULL;
+ wchar_t wfullpath[PATH_MAX];
- wname = to_wide_string((char*)name);
- wenv_var = to_wide_string((char*)env_var);
+ wname = to_wide_string((char*)name);
+ wenv_var = to_wide_string((char*)env_var);
- wcscpy(wfullpath,L"");
+ wcscpy(wfullpath, L"");
- _wsearchenv(wname, wenv_var, wfullpath);
+ _wsearchenv(wname, wenv_var, wfullpath);
- if (wcslen(wfullpath) > 0)
- {
- buffer = wide_string_to_UTF8(wfullpath);
- }
+ if (wcslen(wfullpath) > 0)
+ {
+ buffer = wide_string_to_UTF8(wfullpath);
+ }
- FREE(wname);
- FREE(wenv_var);
- }
+ FREE(wname);
+ FREE(wenv_var);
+ }
#else
- searchenv_others(name, env_var,fullpath);
- if (strlen(fullpath) > 0)
- {
- buffer = strdup(fullpath);
- }
+ searchenv_others(name, env_var, fullpath);
+ if (strlen(fullpath) > 0)
+ {
+ buffer = strdup(fullpath);
+ }
#endif
- return buffer;
+ return buffer;
}
/*--------------------------------------------------------------------------*/