home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckoreg.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  25KB  |  870 lines

  1. /* C K O R E G  --  Kermit interface for MS Win32 Registry */
  2.  
  3. /*
  4.   Author: Jeffrey Altman (jaltman@secure-endpoints.com),
  5.             Secure Endpoints Inc., New York City.
  6.  
  7.   Copyright (C) 1985, 2004, Trustees of Columbia University in the City of New
  8.   York.
  9. */
  10.  
  11. /*
  12.  * =============================#includes=====================================
  13.  */
  14.  
  15. #include "ckcdeb.h"             /* Typedefs, debug formats, etc */
  16. #include "ckcker.h"             /* Kermit definitions */
  17. #include <string.h>
  18. #include <process.h>
  19. #ifdef NT
  20. #include <windows.h>
  21. #else /* NT */
  22. #define INCL_WINSHELLDATA
  23. #include <os2.h>
  24. #undef COMMENT
  25. #endif /* NT */
  26.  
  27. #define DIRSEP       '/'
  28. #define ISDIRSEP(c)  ((c)=='/'||(c)=='\\')
  29.  
  30. #ifdef NT
  31. static HINSTANCE hAdvApi32 = NULL;
  32. static LONG (WINAPI * p_RegOpenCurrentUser)(REGSAM samDesired, PHKEY phkResult)=NULL;
  33. #endif /* NT */
  34.  
  35. char *
  36. GetAppData( int common )
  37. {
  38. #ifdef NT
  39.     HKEY  hkCurrentUser=0;
  40.     HKEY  hkShellKey=0;
  41.     HKEY  hkSubKey=0;
  42.     static CHAR  lpszKeyValue[CKMAXPATH+1]="";
  43.     DWORD dwType=0;
  44.     DWORD dwSize=0;
  45.     CHAR *lpszValueName=NULL, *env;
  46.     int i;
  47.  
  48. #ifdef COMMENT
  49.     env = getenv(common ? "ALLUSERSPROFILE" : "APPDATA");
  50.     if (env) {
  51.         ckstrncpy(lpszKeyValue,env,CKMAXPATH);
  52.         if ( common )
  53.             ckstrncat(lpszKeyValue,"\\Application Data",sizeof(lpszKeyValue));
  54.         debug(F111,"GetAppData env",lpszKeyValue,common);
  55.     } else 
  56. #endif /* COMMENT */    
  57.     {
  58.         if ( !common && !isWin95() ) {
  59.             if ( !p_RegOpenCurrentUser ) {
  60.                 if ( !hAdvApi32 )
  61.                     hAdvApi32 = LoadLibrary("advapi32.dll");
  62.                 if (hAdvApi32)
  63.                     (FARPROC) p_RegOpenCurrentUser =
  64.                         GetProcAddress( hAdvApi32, "RegOpenCurrentUser");
  65.             }
  66.             if (p_RegOpenCurrentUser)
  67.                 p_RegOpenCurrentUser(KEY_READ,&hkCurrentUser);
  68.         }
  69.  
  70.         if ( RegOpenKeyEx(common ? HKEY_LOCAL_MACHINE : 
  71.                            (hkCurrentUser ? hkCurrentUser : HKEY_CURRENT_USER),
  72.                            "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", 
  73.                            0, KEY_READ, &hkShellKey) ) {
  74.             if ( !common ) {
  75.                 if (hkCurrentUser)
  76.                     RegCloseKey(hkCurrentUser);
  77.                 return NULL;
  78.             }
  79.  
  80.             if (RegCreateKeyEx(common ? HKEY_LOCAL_MACHINE : 
  81.                                 (hkCurrentUser ? hkCurrentUser : HKEY_CURRENT_USER),
  82.                            "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", 
  83.                            0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 
  84.                                 NULL, &hkShellKey, NULL)) 
  85.             {
  86.                 if (hkCurrentUser)
  87.                     RegCloseKey(hkCurrentUser);
  88.                 return NULL;            /* failed */
  89.             }
  90.         }
  91.  
  92.         dwSize = sizeof(lpszKeyValue);
  93.         lpszValueName = common ? "Common AppData" : "AppData";
  94.         if ( RegQueryValueEx( hkShellKey, lpszValueName, NULL, &dwType,
  95.                               lpszKeyValue, &dwSize ))
  96.         {
  97.             lpszValueName = common ? "Common Desktop" : "Desktop";
  98.             if ( !RegQueryValueEx( hkShellKey, lpszValueName, NULL, &dwType,
  99.                                   lpszKeyValue, &dwSize ))
  100.             {
  101.                 for (i = strlen(lpszKeyValue); 
  102.                      lpszKeyValue[i] != '\\' && lpszKeyValue[i] != '/';
  103.                      i-- );
  104.                 lpszKeyValue[i] = '\0';
  105.                 ckstrncat(lpszKeyValue,"Application Data",CKMAXPATH+1);
  106.             }
  107.             else {
  108.                 GetWindowsDirectory(lpszKeyValue,sizeof(lpszKeyValue));
  109.                 ckstrncat(lpszKeyValue,"\\Application Data",CKMAXPATH+1);
  110.             }
  111.             dwSize = strlen(lpszKeyValue)+1;
  112.             RegSetValueEx( hkShellKey, 
  113.                            common ? "Common AppData" : "AppData", 
  114.                            0, dwType,
  115.                            lpszKeyValue, dwSize);
  116.             ckstrncat(lpszKeyValue,"\\",CKMAXPATH+1);
  117.             zmkdir(lpszKeyValue);
  118.         }
  119.  
  120.         if ( dwType == REG_EXPAND_SZ ) {
  121.             char * env_buf = malloc( dwSize+1 );
  122.             if (env_buf) {
  123.                 strcpy(env_buf, lpszKeyValue);
  124.                 ExpandEnvironmentStrings( env_buf, lpszKeyValue, sizeof(lpszKeyValue) );
  125.                 free( env_buf );
  126.             }
  127.         }
  128.  
  129.         if (hkCurrentUser)
  130.             RegCloseKey(hkCurrentUser);
  131.         RegCloseKey( hkShellKey );
  132.         debug(F111,"GetAppData registry",lpszKeyValue,common);
  133.     }
  134.  
  135.     i = strlen(lpszKeyValue)-1;
  136.     if ( !(ISDIRSEP(lpszKeyValue[i])) ) {
  137.         lpszKeyValue[i+1] = '/';
  138.         lpszKeyValue[i+2] = '\0';
  139.     }
  140.     for (i-- ; i >= 0 ; i--) {
  141.         if ( lpszKeyValue[i] == '\\' )
  142.           lpszKeyValue[i] = '/';
  143.     }
  144.     debug(F111,"GetAppData returns",lpszKeyValue,common);
  145.     return(lpszKeyValue);
  146. #else /* NT */
  147.     return NULL;
  148. #endif /* NT */
  149. }
  150.  
  151. char *
  152. GetHomeDrive(void)
  153. {
  154. #ifdef NT
  155.     HKEY  hkCurrentUser=0;
  156.     HKEY  hkShellKey=0;
  157.     HKEY  hkSubKey=0;
  158.     static CHAR  lpszKeyValue[CKMAXPATH+1]="";
  159.     DWORD dwType=0;
  160.     DWORD dwSize=0;
  161.     CHAR *lpszValueName=NULL, *env;
  162.  
  163.     env = getenv("HOMEDRIVE");
  164.     if (env) {
  165.         ckstrncpy(lpszKeyValue,env,sizeof(lpszKeyValue));
  166.     } else 
  167.     {
  168.         if ( !isWin95() ) {
  169.             if ( !p_RegOpenCurrentUser ) {
  170.                 if ( !hAdvApi32 )
  171.                     hAdvApi32 = LoadLibrary("advapi32.dll");
  172.                 if (hAdvApi32)
  173.                     (FARPROC) p_RegOpenCurrentUser =
  174.                         GetProcAddress( hAdvApi32, "RegOpenCurrentUser");
  175.             }
  176.             if (p_RegOpenCurrentUser)
  177.                 p_RegOpenCurrentUser(KEY_READ,&hkCurrentUser);
  178.         }
  179.  
  180.         if ( RegOpenKeyEx(hkCurrentUser ? hkCurrentUser : HKEY_CURRENT_USER,
  181.                       "Volatile Environment", 
  182.                       0, KEY_READ, &hkShellKey) )
  183.         {
  184.             if (hkCurrentUser)
  185.                 RegCloseKey(hkCurrentUser);
  186.             return NULL;            /* failed */
  187.         }
  188.  
  189.         dwSize = sizeof(lpszKeyValue);
  190.         lpszValueName = "HOMEDRIVE";
  191.         if ( RegQueryValueEx( hkShellKey, lpszValueName, NULL, &dwType,
  192.                               lpszKeyValue, &dwSize ))
  193.         {
  194.             if (hkCurrentUser)
  195.                 RegCloseKey(hkCurrentUser);
  196.             RegCloseKey( hkShellKey );
  197.             return(NULL);
  198.         }
  199.  
  200.         if ( dwType == REG_EXPAND_SZ ) {
  201.             char * env_buf = malloc( dwSize+1 );
  202.             if (env_buf) {
  203.                 strcpy(env_buf, lpszKeyValue);
  204.                 ExpandEnvironmentStrings( env_buf, lpszKeyValue, sizeof(lpszKeyValue) );
  205.                 free( env_buf );
  206.             }
  207.         }
  208.  
  209.         if (hkCurrentUser)
  210.             RegCloseKey(hkCurrentUser);
  211.         RegCloseKey( hkShellKey );
  212.     }
  213.     return(lpszKeyValue);
  214. #else /* NT */
  215.     return NULL;
  216. #endif /* NT */
  217. }
  218.  
  219. char *
  220. GetHomePath(void)
  221. {
  222. #ifdef NT
  223.     HKEY  hkCurrentUser=0;
  224.     HKEY  hkShellKey=0;
  225.     HKEY  hkSubKey=0;
  226.     static CHAR  lpszKeyValue[CKMAXPATH+1]="";
  227.     DWORD dwType=0;
  228.     DWORD dwSize=0;
  229.     CHAR *lpszValueName=NULL, *env;
  230.     int   i;
  231.  
  232.     env = getenv("HOMEPATH");
  233.     if (env) {
  234.         ckstrncpy(lpszKeyValue,env,sizeof(lpszKeyValue));
  235.     } else 
  236.     {
  237.         if ( !isWin95() ) {
  238.             if ( !p_RegOpenCurrentUser ) {
  239.                 if ( !hAdvApi32 )
  240.                     hAdvApi32 = LoadLibrary("advapi32.dll");
  241.                 if (hAdvApi32)
  242.                     (FARPROC) p_RegOpenCurrentUser =
  243.                         GetProcAddress( hAdvApi32, "RegOpenCurrentUser");
  244.             }
  245.             if (p_RegOpenCurrentUser)
  246.                 p_RegOpenCurrentUser(KEY_READ,&hkCurrentUser);
  247.         }
  248.  
  249.         if ( RegOpenKeyEx(hkCurrentUser ? hkCurrentUser : HKEY_CURRENT_USER,
  250.                            "Volatile Environment", 
  251.                            0, KEY_READ, &hkShellKey) ) 
  252.         {
  253.             if (hkCurrentUser)
  254.                 RegCloseKey(hkCurrentUser);
  255.             return NULL;            /* failed */
  256.         }
  257.         dwSize = sizeof(lpszKeyValue);
  258.         lpszValueName = "HOMEPATH";
  259.         if ( RegQueryValueEx( hkShellKey, lpszValueName, NULL, &dwType,
  260.                            lpszKeyValue, &dwSize ))
  261.         {
  262.             if (hkCurrentUser)
  263.                 RegCloseKey(hkCurrentUser);
  264.             RegCloseKey( hkShellKey );
  265.             return(NULL);
  266.         }
  267.  
  268.         if ( dwType == REG_EXPAND_SZ ) {
  269.             char * env_buf = malloc( dwSize+1 );
  270.             if (env_buf) {
  271.                 strcpy(env_buf, lpszKeyValue);
  272.                 ExpandEnvironmentStrings( env_buf, lpszKeyValue, sizeof(lpszKeyValue) );
  273.                 free( env_buf );
  274.             }
  275.         }
  276.  
  277.         if (hkCurrentUser)
  278.             RegCloseKey(hkCurrentUser);
  279.         RegCloseKey( hkShellKey );
  280.     }
  281.  
  282.     i = strlen(lpszKeyValue)-1;
  283.     if ( !(ISDIRSEP(lpszKeyValue[i])) ) {
  284.         lpszKeyValue[i+1] = '/';
  285.         lpszKeyValue[i+2] = '\0';
  286.     }
  287.     for (i-- ; i >= 0 ; i--) {
  288.         if ( lpszKeyValue[i] == '\\' )
  289.           lpszKeyValue[i] = '/';
  290.     }
  291.  
  292.     return(lpszKeyValue);
  293. #else
  294.     return NULL;
  295. #endif /* NT */
  296. }
  297.  
  298. char *
  299. GetPersonal(void)
  300. {
  301. #ifdef NT
  302.     HKEY  hkCurrentUser=0;
  303.     HKEY  hkShellKey=0;
  304.     HKEY  hkSubKey=0;
  305.     static CHAR  lpszKeyValue[CKMAXPATH+1]="";
  306.     DWORD dwType=0;
  307.     DWORD dwSize=0;
  308.     CHAR *lpszValueName=NULL, *env;
  309.     int   i;
  310.  
  311.     if ( !isWin95() ) {
  312.         if ( !p_RegOpenCurrentUser ) {
  313.             if ( !hAdvApi32 )
  314.                 hAdvApi32 = LoadLibrary("advapi32.dll");
  315.             if (hAdvApi32)
  316.                 (FARPROC) p_RegOpenCurrentUser =
  317.                     GetProcAddress( hAdvApi32, "RegOpenCurrentUser");
  318.         }
  319.         if (p_RegOpenCurrentUser)
  320.             p_RegOpenCurrentUser(KEY_READ,&hkCurrentUser);
  321.     }
  322.  
  323.     if ( RegOpenKeyEx(hkCurrentUser ? hkCurrentUser : HKEY_CURRENT_USER,
  324.                        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", 
  325.                        0, KEY_READ, &hkShellKey) ) 
  326.     {
  327.         if (hkCurrentUser)
  328.             RegCloseKey(hkCurrentUser);
  329.         return NULL;            /* failed */
  330.     }
  331.     dwSize = sizeof(lpszKeyValue);
  332.     lpszValueName = "Personal";
  333.     if ( RegQueryValueEx( hkShellKey, lpszValueName, NULL, &dwType,
  334.                            lpszKeyValue, &dwSize ))
  335.     {
  336.         if (hkCurrentUser)
  337.             RegCloseKey(hkCurrentUser);
  338.         RegCloseKey( hkShellKey );
  339.         return(NULL);
  340.     }
  341.  
  342.     if ( dwType == REG_EXPAND_SZ ) {
  343.         char * env_buf = malloc( dwSize+1 );
  344.         if (env_buf) {
  345.             strcpy(env_buf, lpszKeyValue);
  346.             ExpandEnvironmentStrings( env_buf, lpszKeyValue, sizeof(lpszKeyValue) );
  347.             free( env_buf );
  348.         }
  349.     }
  350.  
  351.     if (hkCurrentUser)
  352.         RegCloseKey(hkCurrentUser);
  353.     RegCloseKey( hkShellKey );
  354.  
  355.     i = strlen(lpszKeyValue)-1;
  356.     if ( !(ISDIRSEP(lpszKeyValue[i])) ) {
  357.         lpszKeyValue[i+1] = '/';
  358.         lpszKeyValue[i+2] = '\0';
  359.     }
  360.     for (i-- ; i >= 0 ; i--) {
  361.         if ( lpszKeyValue[i] == '\\' )
  362.           lpszKeyValue[i] = '/';
  363.     }
  364.  
  365.     return(lpszKeyValue);
  366. #else
  367.     return NULL;
  368. #endif /* NT */
  369. }
  370.  
  371. char *
  372. GetDesktop(void)
  373. {
  374. #ifdef NT
  375.     HKEY  hkCurrentUser=0;
  376.     HKEY  hkShellKey=0;
  377.     HKEY  hkSubKey=0;
  378.     static CHAR  lpszKeyValue[CKMAXPATH+1]="";
  379.     DWORD dwType=0;
  380.     DWORD dwSize=0;
  381.     CHAR *lpszValueName=NULL, *env;
  382.     int   i;
  383.  
  384.     if ( !isWin95() ) {
  385.         if ( !p_RegOpenCurrentUser ) {
  386.             if ( !hAdvApi32 )
  387.                 hAdvApi32 = LoadLibrary("advapi32.dll");
  388.             if (hAdvApi32)
  389.                 (FARPROC) p_RegOpenCurrentUser =
  390.                     GetProcAddress( hAdvApi32, "RegOpenCurrentUser");
  391.         }
  392.         if (p_RegOpenCurrentUser)
  393.             p_RegOpenCurrentUser(KEY_READ,&hkCurrentUser);
  394.     }
  395.  
  396.     if ( RegOpenKeyEx(hkCurrentUser ? hkCurrentUser : HKEY_CURRENT_USER,
  397.                        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", 
  398.                        0, KEY_READ, &hkShellKey) ) 
  399.     {
  400.         if (hkCurrentUser)
  401.             RegCloseKey(hkCurrentUser);
  402.         return NULL;            /* failed */
  403.     }
  404.     dwSize = sizeof(lpszKeyValue);
  405.     lpszValueName = "Desktop";
  406.     if ( RegQueryValueEx( hkShellKey, lpszValueName, NULL, &dwType,
  407.                            lpszKeyValue, &dwSize ))
  408.     {
  409.         if (hkCurrentUser)
  410.             RegCloseKey(hkCurrentUser);
  411.         RegCloseKey( hkShellKey );
  412.         return(NULL);
  413.     }
  414.  
  415.     if ( dwType == REG_EXPAND_SZ ) {
  416.         char * env_buf = malloc( dwSize+1 );
  417.         if (env_buf) {
  418.             strcpy(env_buf, lpszKeyValue);
  419.             ExpandEnvironmentStrings( env_buf, lpszKeyValue, sizeof(lpszKeyValue) );
  420.             free( env_buf );
  421.         }
  422.     }
  423.  
  424.     if (hkCurrentUser)
  425.         RegCloseKey(hkCurrentUser);
  426.     RegCloseKey( hkShellKey );
  427.  
  428.     i = strlen(lpszKeyValue)-1;
  429.     if ( !(ISDIRSEP(lpszKeyValue[i])) ) {
  430.         lpszKeyValue[i+1] = '/';
  431.         lpszKeyValue[i+2] = '\0';
  432.     }
  433.     for (i-- ; i >= 0 ; i--) {
  434.         if ( lpszKeyValue[i] == '\\' )
  435.           lpszKeyValue[i] = '/';
  436.     }
  437.  
  438.     return(lpszKeyValue);
  439. #else
  440.     return NULL;
  441. #endif /* NT */
  442. }
  443.  
  444. #ifdef CK_LOGIN
  445. int
  446. IsSSPLogonAvail( void )
  447. {
  448. #ifdef NT
  449.     HKEY  hkCommandKey=0;
  450.     DWORD  dwKeyValue;
  451.     DWORD dwType=0;
  452.     DWORD dwSize=0;
  453.     CHAR *lpszValueName=NULL;
  454.  
  455.     if ( !isWin95() )
  456.         return(1);
  457.  
  458.     if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  459.                        "Security\\Provider", 0,
  460.                         KEY_READ, &hkCommandKey) )
  461.         return(-1);            /* failed */
  462.  
  463.     dwSize = sizeof(dwKeyValue);
  464.     lpszValueName = "Platform_Type";
  465.     if ( RegQueryValueEx( hkCommandKey, lpszValueName, NULL, &dwType,
  466.                            (char *)&dwKeyValue, &dwSize ))
  467.     {
  468.         RegCloseKey( hkCommandKey );
  469.         return(-1);
  470.     }
  471.  
  472.     RegCloseKey( hkCommandKey );
  473.     return(dwKeyValue);
  474. #else /* NT */
  475.     return(0);
  476. #endif /* NT */
  477. }
  478.  
  479. const char *
  480. SSPLogonDomain( void )
  481. {
  482. #ifdef NT
  483.     HKEY  hkCommandKey=0;
  484.     DWORD dwType=0;
  485.     DWORD dwSize=0;
  486.     CHAR *lpszValueName=NULL;
  487.     static char domain[64]="";
  488.  
  489.     if ( !isWin95() )
  490.         return(NULL);
  491.  
  492.     if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  493.                        "Security\\Provider", 0,
  494.                         KEY_READ, &hkCommandKey) )
  495.         return(NULL);            /* failed */
  496.  
  497.     dwSize = 64;
  498.     lpszValueName = "Container";
  499.     if ( RegQueryValueEx( hkCommandKey, lpszValueName, NULL, &dwType,
  500.                            (char *)&domain, &dwSize ))
  501.     {
  502.         RegCloseKey( hkCommandKey );
  503.         return(NULL);
  504.     }
  505.  
  506.     RegCloseKey( hkCommandKey );
  507.     return(domain[0] ? domain : NULL);
  508. #else /* NT */
  509.     return(NULL);
  510. #endif /* NT */
  511. }
  512. #endif /* CK_LOGIN */
  513.  
  514. #ifndef NOLOCAL
  515. char *
  516. GetEditorCommand( void )
  517. {
  518. #ifdef NT
  519.     HKEY  hkCommandKey=0;
  520.     HKEY  hkSubKey=0;
  521.     static CHAR  lpszKeyValue[CKMAXPATH+1];
  522.     DWORD dwType=0;
  523.     DWORD dwSize=0;
  524.     CHAR *lpszValueName=NULL;
  525.  
  526.     if ( RegOpenKeyEx(HKEY_CLASSES_ROOT,
  527.                        "txtfile\\shell\\open\\command", 0,
  528.                         KEY_READ, &hkCommandKey) )
  529.         return NULL;            /* failed */
  530.  
  531.     dwSize = sizeof(lpszKeyValue);
  532.     lpszValueName = NULL ;
  533.     if ( RegQueryValueEx( hkCommandKey, lpszValueName, NULL, &dwType,
  534.                            lpszKeyValue, &dwSize ))
  535.     {
  536.         RegCloseKey( hkCommandKey );
  537.         return(NULL);
  538.     }
  539.  
  540.     if ( dwType == REG_EXPAND_SZ ) {
  541.         char * env_buf = malloc( dwSize+1 );
  542.         if (env_buf) {
  543.             strcpy(env_buf, lpszKeyValue);
  544.             ExpandEnvironmentStrings( env_buf, lpszKeyValue, sizeof(lpszKeyValue) );
  545.             free( env_buf );
  546.         }
  547.     }
  548.  
  549. #ifdef BETADEBUG
  550.     printf("Editor: %s\n", lpszKeyValue);
  551. #endif
  552.  
  553.     RegCloseKey( hkCommandKey );
  554.     return(lpszKeyValue);
  555. #else /* NT */
  556.     static CHAR exename[CKMAXPATH+1];
  557.     _searchenv("epm.exe","PATH",exename);
  558.     if ( !exename[0] )
  559.         _searchenv("e.exe","PATH",exename);
  560.     if ( exename[0] )
  561.         return exename;
  562.     else
  563.         return NULL;
  564. #endif /* NT */
  565. }
  566.  
  567. char *
  568. GetBrowserCommand( void )
  569. {
  570. #ifdef NT
  571.     HKEY  hkCommandKey=0;
  572.     HKEY  hkSubKey=0;
  573.     static CHAR  lpszKeyValue[CKMAXPATH+1];
  574.     DWORD dwType=0;
  575.     DWORD dwSize=0;
  576.     CHAR *lpszValueName=NULL;
  577.  
  578.     if ( RegOpenKeyEx(HKEY_CLASSES_ROOT,
  579.                        "http\\shell\\open\\command", 0,
  580.                         KEY_READ, &hkCommandKey) )
  581.         return NULL;            /* failed */
  582.  
  583.     dwSize = sizeof(lpszKeyValue);
  584.     lpszValueName = NULL;
  585.     if ( RegQueryValueEx( hkCommandKey, lpszValueName, NULL, &dwType,
  586.                            lpszKeyValue, &dwSize ))
  587.     {
  588.         RegCloseKey( hkCommandKey );
  589.         return(NULL);
  590.     }
  591.  
  592.     if ( dwType == REG_EXPAND_SZ ) {
  593.         char * env_buf = malloc( dwSize+1 );
  594.         if (env_buf) {
  595.             strcpy(env_buf, lpszKeyValue);
  596.             ExpandEnvironmentStrings( env_buf, lpszKeyValue, sizeof(lpszKeyValue) );
  597.             free( env_buf );
  598.         }
  599.     }
  600.  
  601. #ifdef BETADEBUG
  602.         printf("Browser: %s\n", lpszKeyValue);
  603. #endif
  604.  
  605.     RegCloseKey( hkCommandKey );
  606.     return(lpszKeyValue);
  607. #else /* NT */
  608.     static CHAR exename[CKMAXPATH+1] ;
  609.     ULONG size = CKMAXPATH+1 ;
  610.  
  611.     if ( PrfQueryProfileData( HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
  612.                               "DefaultBrowserExe", exename, &size ) )
  613.         return exename;
  614.     _searchenv("netscape.exe","PATH",exename);
  615.     if ( !exename[0] )
  616.         _searchenv("explore.exe","PATH",exename);
  617.     if ( exename[0] )
  618.         return exename;
  619.     else
  620.         return NULL;
  621. #endif /* NT */
  622. }
  623.  
  624. char *
  625. GetFtpCommand( void )
  626. {
  627. #ifdef NT
  628.     HKEY  hkCommandKey=0;
  629.     HKEY  hkSubKey=0;
  630.     static CHAR  lpszKeyValue[CKMAXPATH+1];
  631.     DWORD dwType=0;
  632.     DWORD dwSize=0;
  633.     CHAR *lpszValueName=NULL;
  634.  
  635.     if ( RegOpenKeyEx(HKEY_CLASSES_ROOT,
  636.                        "ftp\\shell\\open\\command", 0,
  637.                         KEY_READ, &hkCommandKey) )
  638.         return NULL;            /* failed */
  639.  
  640.     dwSize = sizeof(lpszKeyValue);
  641.     lpszValueName = NULL;
  642.     if ( RegQueryValueEx( hkCommandKey, lpszValueName, NULL, &dwType,
  643.                            lpszKeyValue, &dwSize ))
  644.     {
  645.         RegCloseKey( hkCommandKey );
  646.         return(NULL);
  647.     }
  648.  
  649.     if ( dwType == REG_EXPAND_SZ ) {
  650.         char * env_buf = malloc( dwSize+1 );
  651.         if (env_buf) {
  652.             strcpy(env_buf, lpszKeyValue);
  653.             ExpandEnvironmentStrings( env_buf, lpszKeyValue, sizeof(lpszKeyValue) );
  654.             free( env_buf );
  655.         }
  656.     }
  657.  
  658. #ifdef BETADEBUG
  659.         printf("FTP: %s\n", lpszKeyValue);
  660. #endif
  661.  
  662.     RegCloseKey( hkCommandKey );
  663.     return(lpszKeyValue);
  664. #else /* NT */
  665.     static CHAR exename[CKMAXPATH+1] ;
  666.     ULONG size = CKMAXPATH+1 ;
  667.  
  668.     if ( PrfQueryProfileData( HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
  669.                               "DefaultFtpExe", exename, &size ) )
  670.         return exename;
  671.     _searchenv("ftp.exe","PATH",exename);
  672.     if ( exename[0] )
  673.         return exename;
  674.     else
  675.         return NULL;
  676. #endif /* NT */
  677. }
  678.  
  679. #ifdef BROWSER
  680. #ifdef NT
  681. void
  682. Real_Win32ShellExecute( char * object )
  683. {
  684.     extern HWND hwndConsole;
  685.     extern int priority;
  686. #ifdef COMMENT
  687.     SHELLEXECUTEINFO info;
  688.     BOOL  rc;
  689. #endif
  690.     HINSTANCE error;
  691.  
  692.     SetThreadPrty(priority,isWin95() ? 3 : 11);
  693.  
  694. #ifndef COMMENT
  695.     error = ShellExecute(hwndConsole, 0, object, 0, 0, SW_SHOWNORMAL);
  696. #else /* COMMENT */
  697.     memset(&info,0,sizeof(SHELLEXECUTEINFO));
  698.     info.cbSize = sizeof(SHELLEXECUTEINFO);
  699.     info.fMask = SEE_MASK_DOENVSUBST;
  700.     info.nShow = SW_SHOWNORMAL;
  701.     info.lpFile = object;
  702.     info.hwnd = hwndConsole;
  703.     rc = ShellExecuteEx( &info );
  704.     error = info.hInstApp;
  705.     CloseHandle(info.hProcess);
  706. #endif /* COMMENT */
  707.  
  708.     if (((DWORD)error) <= 32)
  709.     {
  710.         debug(F111,"Win32 ShellExecute failure",object,error);
  711.         switch ( (DWORD)error ) {
  712.         case 0:
  713.             debug(F110,"Win32 ShellExecute","The operating system is out of memory or resources.",0);
  714.             break;
  715.         case ERROR_BAD_FORMAT:
  716.             debug(F110,"Win32 ShellExecute","The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).",0);
  717.             break;
  718.         case SE_ERR_ACCESSDENIED:
  719.             debug(F110,"Win32 ShellExecute","The operating system denied access to the specified file.",0);
  720.             break;
  721.         case SE_ERR_ASSOCINCOMPLETE:
  722.             debug(F110,"Win32 ShellExecute","The filename association is incomplete or invalid.",0);
  723.             break;
  724.         case SE_ERR_DDEBUSY:
  725.             debug(F110,"Win32 ShellExecute","The DDE transaction could not be completed because other DDE transactions were being processed.",0);
  726.             break;
  727.         case SE_ERR_DDEFAIL:
  728.             debug(F110,"Win32 ShellExecute","The DDE transaction failed.",0);
  729.             break;
  730.         case SE_ERR_DDETIMEOUT:
  731.             debug(F110,"Win32 ShellExecute","The DDE transaction could not be completed because the request timed out.",0);
  732.             break;
  733.         case SE_ERR_DLLNOTFOUND:
  734.             debug(F110,"Win32 ShellExecute","The specified dynamic-link library was not found.",0);
  735.             break;
  736.         case SE_ERR_FNF:
  737.             debug(F110,"Win32 ShellExecute","The specified file was not found.",0);
  738.             break;
  739.         case SE_ERR_NOASSOC:
  740.             debug(F110,"Win32 ShellExecute","There is no application associated with the given filename extension.",0);
  741.             break;
  742.         case SE_ERR_OOM:
  743.             debug(F110,"Win32 ShellExecute","There was not enough memory to complete the operation.",0);
  744.             break;
  745.         case SE_ERR_PNF:
  746.             debug(F110,"Win32 ShellExecute","The specified path was not found.",0);
  747.             break;
  748.         case SE_ERR_SHARE:
  749.             debug(F110,"Win32 ShellExecute","A sharing violation occurred.",0);
  750.             break;
  751.         default:
  752.             debug(F110,"Win32 ShellExecute","Unknown error",0);
  753.         }
  754.     }
  755.     else {
  756.         debug(F111,"Win32 ShellExecute success",object,error);
  757. #ifdef COMMENT
  758.         Don't close the handle.  We have seen the handle returned from
  759.         Shell execute be equal to the KbdHandle or Video Handle.  Bad news.
  760.         CloseHandle((HINSTANCE)error);
  761. #endif /* COMMENT */
  762.     }
  763. }
  764.  
  765. int
  766. Win32ShellExecute( char * object )
  767. {
  768.     int rc = 0;
  769.     DWORD tid = _beginthread( Real_Win32ShellExecute,
  770. #ifndef NT
  771.                             0,
  772. #endif /* NT */
  773.                             65535,
  774.                             (void *)object
  775.                             ) ;
  776.     rc = (DWORD)tid != 0xffffffff;
  777.     return (rc);
  778. }
  779. #endif /* NT */
  780. #endif /* BROWSER */
  781.  
  782. void
  783. os2InitFromRegistry( void )
  784. {
  785. #ifndef NOPUSH
  786. #ifdef BROWSER
  787.     extern char browser[];
  788.     extern char browsopts[];
  789. #endif /* BROWSER */
  790. #ifndef NOFTP
  791. #ifdef SYSFTP
  792.     extern char ftpapp[];
  793.     extern char ftpopts[];
  794. #endif /* SYSFTP */
  795. #endif /* NOFTP */
  796.     extern char editor[];
  797.     extern char editopts[];
  798.     char * str,*p;
  799.     int quote = 0;
  800.  
  801.  
  802.     str = GetEditorCommand();
  803.     debug(F110,"GetEditorCommand",str,0);
  804.     if ( str ) {
  805.         quote=0;
  806.         for ( p = str ; *p ; p++ )
  807.         {
  808.             if ( *p == '"' )
  809.                 quote = !quote;
  810.             if (!quote && *p == ' ') {
  811.                 *p = '\0';
  812.                 p++;
  813.                 strcpy( editopts, p );
  814.                 break;
  815.             }
  816.         }
  817.         strcpy( editor, str );
  818.     }
  819.  
  820. #ifdef BROWSER
  821.     /* In Windows we default to nothing so that the ShellExecute code */
  822.     /* will be used instead of a specific application name.           */
  823.     str = GetBrowserCommand();
  824.     debug(F110,"GetBrowserCommand",str,0);
  825.     if ( str ) {
  826.         quote=0;
  827.         for ( p = str ; *p ; p++ )
  828.         {
  829.             if ( *p == '"' )
  830.                 quote = !quote;
  831.             if (!quote && *p == ' ') {
  832.                 *p = '\0';
  833.                 p++;
  834.                 strcpy( browsopts, p );
  835.                 break;
  836.             }
  837.         }
  838.         strcpy( browser, str );
  839.     }
  840. #endif /* BROWSER */
  841.  
  842. #ifndef NOFTP
  843. #ifdef SYSFTP
  844.     str = GetFtpCommand();
  845.     debug(F110,"GetFtpCommand",str,0);
  846. #ifdef COMMENT
  847.     if ( str ) {
  848.         quote=0;
  849.         for ( p = str ; *p ; p++ )
  850.         {
  851.             if ( *p == '"' )
  852.                 quote = !quote;
  853.             if (!quote && *p == ' ') {
  854.                 *p = '\0';
  855.                 p++;
  856.                 strcpy( ftpopts, p );
  857.                 break;
  858.             }
  859.         }
  860.         strcpy( ftpapp, str );
  861.     }
  862. #else
  863.     strcpy(ftpapp,"ftp.exe");
  864. #endif
  865. #endif /* SYSFTP */
  866. #endif /* NOFTP */
  867. #endif /* NOPUSH */
  868. }
  869. #endif /* NOLOCAL */
  870.