[Coco] getenv() bug in cgfx7!
    Willard Goosey 
    goosey at virgo.sdc.org
       
    Fri Dec 11 23:45:06 EST 2009
    
    
  
This has not been my day for having computers cooperate with me...
Anyway, I found a bug in getenv() in Mike Sweet's cgfx7 library.
Here's the corrected source for the function:
------cut here-----------------------------------------------
/*
 "getenv.c" - environment variable reading/setting functions.
 updated 8/6/90 by Mike Sweet
 updated 12/11/2k9 by Willard Goosey
 ---compare with the length of the string were looking for
 ---not the string from _ENVFILE
*/
/*#define DEBUG 1*/
char *_ENVFILE=0; /* this global pointer holds the current ENV.FILE contents. */
static char envstr[81];
static char *readenv()
{
 register int path,len;
 if (!(_ENVFILE=malloc(1024)))
  return(0);
 if ((path=open("/dd/sys/env.file",1))==-1)
  {
   free(_ENVFILE);
   _ENVFILE=0;
   return(0);
  };
 if ((len=read(path,_ENVFILE,1023))<0)
  {
   close(path);
   free(_ENVFILE);
   _ENVFILE=0;
   return(0);
  };
 _ENVFILE[len]=0;
 close(path);
 return(_ENVFILE);
}
static char *findenv(s)
char *s;
{
 char *temp;
 register int len;
 if (!_ENVFILE)
  readenv();
 temp=_ENVFILE;
 while (temp)
  {
   if (temp[0]!='*') /* ignore commented lines */
    {
     len=0;
     while ((temp[len]!=' ') && (temp[len]!='='))
      len++;
/*do this compare with len(s) chars, not len(temp) chars*/
     if (strnucmp(temp,s,strlen(s))==0)
      {
#ifdef DEBUG
      printf("matched %s with %s",s,temp);
#endif
       temp+=len;
       while (*temp && *temp!='=')
        temp++;
       temp++;
       while (*temp && *temp==' ')
        temp++;
       return(temp);
      };
    };
   if (temp=strchr(temp,13))
    temp++;
  };
 return(0);
}
char *getenv(s)
char *s;
{
 register char *temp;
 temp=findenv(s);
 strncpy(envstr,temp,(char *)strchr(temp,13)-temp);
 return(envstr);
} 
putenv(v,s)
char *v,*s;
{
 register char *temp;
 int len,slen,path;
 slen=strlen(s);
 if (temp=findenv(v))
  {
   len=(char *)strchr(temp,13)-temp;
   movemem(temp+slen,temp+len,strlen(temp+len)+1);
   movemem(temp,s,slen);
  }
 else if (_ENVFILE)
  {
   if ((strlen(v)+strlen(_ENVFILE)+slen+2)>1023)
    return;
   strcat(_ENVFILE,v);
   strcat(_ENVFILE,"=");
   strcat(_ENVFILE,s);
   strcat(_ENVFILE,"\n");
  }
 else
  return;
 if ((path=open("/dd/sys/env.file",2))==-1)
  return;
 write(path,_ENVFILE,strlen(_ENVFILE));
 close(path);
 return;
}
----------------------------cut here-------------------------
I don't know what else to do with this.  RTSI seems to have quit
updating thier ftp site.
I recommomend compiling this, then do: merge getenv.r cgfx.l > newcgfx.l
I tried to use lb to split the library up and replace the rof file,
but that didn't work because there are 3 modules that are called
mouse.r in the library, and lb overwrites the first 2!  And right now
I'm too cranky to attempt recompiling all of cgfx7.
now maybe I can get back to the bugs in MY code. :-(
Willard
-- 
Willard Goosey  goosey at sdc.org
Socorro, New Mexico, USA
I search my heart and find Cimmeria, land of Darkness and the Night.
  -- R.E. Howard
    
    
More information about the Coco
mailing list