#include <windows.h>
#include <commctrl.h>
#include <stdio.h>

void xenum(HKEY key2, char *data)
{
   int size2 = 10000 ;
   char buf2[10000] ;
   if (RegQueryValueEx(key2,"PATH",0,NULL,buf2,&size2) == ERROR_SUCCESS)
      printf("%s\n",buf2) ;
}
void xadd(HKEY key2, char *data)
{
   int size2 = 10000, l= strlen(data) ;
   char buf2[10000] ;
   if (RegQueryValueEx(key2,"PATH",0,NULL,buf2,&size2) == ERROR_SUCCESS) {
      if (!strstr(buf2,data)) {
         memmove(buf2+l+1,buf2,strlen(buf2)+1) ;
         memcpy(buf2,data,l) ;
         buf2[l]= ';' ;
         RegSetValueEx(key2,"PATH",0,REG_EXPAND_SZ,buf2,strlen(buf2)) ;
      }
   }
   
}
void xsub(HKEY key2, char *data)
{
   int size2 = 10000, l= strlen(data) ;
   char buf2[10000],*p ;
   if (RegQueryValueEx(key2,"PATH",0,NULL,buf2,&size2) == ERROR_SUCCESS) {
      if (p = strstr(buf2,data)) {
         memcpy(p,p+l+1,strlen(p)+1) ;
         RegSetValueEx(key2,"PATH",0,REG_SZ,buf2,strlen(buf2)) ;
      }
   }
   
}
int AddPath(void (*func)(HKEY key, char *buf), char *buf)
{
   HKEY key ;
   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,"System",
         0,KEY_READ,&key) == ERROR_SUCCESS) {
      int index = 0 ;
      char name[1024] ;
      char buf2[1024] ;
      int size = 256 ;
      FILETIME t ;
      int rv ;
      do {
         memset(name,0,1024) ;
         size = 256 ;
         rv = RegEnumKeyEx(key,index++,name,&size,0,NULL,NULL,&t) ;
         if (rv == ERROR_SUCCESS || rv == ERROR_MORE_DATA) {
            HKEY key2 ;
            int size2=256 ;
            strcpy(name+size,"\\control\\SessionManager\\Environment") ;
            if (RegOpenKeyEx(key,name,0,KEY_READ, &key2) == ERROR_SUCCESS) {
               func(key2,buf) ;
               RegCloseKey(key2) ;
            }
            size2 = 256 ;
            strcpy(name+size,"\\control\\Session Manager\\Environment") ;
            if (RegOpenKeyEx(key,name,0,KEY_READ|KEY_WRITE, &key2) == ERROR_SUCCESS) {
               func(key2,buf) ;
               RegCloseKey(key2) ;
            }
         }
      } while (rv == ERROR_SUCCESS || rv == ERROR_MORE_DATA) ;
      RegCloseKey(key) ;
   }
   return FALSE ;
}
void AddToPath(char *buf)
{
   char ibuf[256] ;
   sprintf(ibuf,"%sbin",buf) ;
   AddPath(xadd,ibuf) ;
}
void RemoveFromPath(char *buf)
{
   char ibuf[256] ;
   sprintf(ibuf,"%sbin",buf) ;
   AddPath(xsub,ibuf) ;
}
#ifdef XXXXX

main()
{
//   AddPath(xenum,"") ;
//   AddPath(xadd,"C:\\cc386\\bin") ;
//   AddPath(xenum,"") ;
   AddPath(xsub,"C:\\cc386\\bin") ;
//   AddPath(xenum,"") ;
}
#endif