Removed some unnecessary includes.
[wine] / dlls / shell32 / shell.c
1 /*
2  *                              Shell Library Functions
3  *
4  *  1998 Marcus Meissner
5  */
6
7 #include "config.h"
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <ctype.h>
13
14 #include "dlgs.h"
15 #include "shellapi.h"
16 #include "shlobj.h"
17 #include "shlwapi.h"
18 #include "windef.h"
19 #include "winerror.h"
20 #include "winreg.h"
21
22 #include "wine/shell16.h"
23 #include "wine/winbase16.h"
24 #include "wine/winuser16.h"
25 #include "shell32_main.h"
26
27 #include "debugtools.h"
28
29 DEFAULT_DEBUG_CHANNEL(shell);
30 DECLARE_DEBUG_CHANNEL(exec);
31
32
33 typedef struct {     /* structure for dropped files */
34  WORD     wSize;
35  POINT16  ptMousePos;
36  BOOL16   fInNonClientArea;
37  /* memory block with filenames follows */
38 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
39
40 static const char*      lpstrMsgWndCreated = "OTHERWINDOWCREATED";
41 static const char*      lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
42 static const char*      lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
43
44 static HWND16   SHELL_hWnd = 0;
45 static HHOOK    SHELL_hHook = 0;
46 static UINT16   uMsgWndCreated = 0;
47 static UINT16   uMsgWndDestroyed = 0;
48 static UINT16   uMsgShellActivate = 0;
49 HINSTANCE16     SHELL_hInstance = 0;
50 HINSTANCE SHELL_hInstance32;
51 static int SHELL_Attach = 0;
52
53 /***********************************************************************
54  * DllEntryPoint [SHELL.101]
55  *
56  * Initialization code for shell.dll. Automatically loads the
57  * 32-bit shell32.dll to allow thunking up to 32-bit code.
58  *
59  * RETURNS:
60  */
61 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
62                                 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
63 {
64     TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
65           Reason, hInst, ds, HeapSize, res1, res2);
66
67     switch(Reason)
68     {
69     case DLL_PROCESS_ATTACH:
70         if (SHELL_Attach++) break;
71         SHELL_hInstance = hInst;
72         if(!SHELL_hInstance32)
73         {
74             if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
75             {
76                 ERR("Could not load sibling shell32.dll\n");
77                 return FALSE;
78             }
79         }
80         break;
81
82     case DLL_PROCESS_DETACH:
83         if(!--SHELL_Attach)
84         {
85             SHELL_hInstance = 0;
86             if(SHELL_hInstance32)
87                 FreeLibrary(SHELL_hInstance32);
88         }
89         break;
90     }
91     return TRUE;
92 }
93
94 /*************************************************************************
95  *                              DragAcceptFiles         [SHELL.9]
96  */
97 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
98 {
99   DragAcceptFiles(hWnd, b);
100 }
101
102 /*************************************************************************
103  *                              DragQueryFile           [SHELL.11]
104  */
105 UINT16 WINAPI DragQueryFile16(
106         HDROP16 hDrop,
107         WORD wFile,
108         LPSTR lpszFile,
109         WORD wLength)
110 {
111         LPSTR lpDrop;
112         UINT i = 0;
113         LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop); 
114    
115         TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
116     
117         if(!lpDropFileStruct) goto end;
118     
119         lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
120         wFile = (wFile==0xffff) ? 0xffffffff : wFile;
121
122         while (i++ < wFile)
123         {
124           while (*lpDrop++); /* skip filename */
125           if (!*lpDrop) 
126           {
127             i = (wFile == 0xFFFFFFFF) ? i : 0; 
128             goto end;
129           }
130         }
131     
132         i = strlen(lpDrop);
133         i++;
134         if (!lpszFile ) goto end;   /* needed buffer size */
135         i = (wLength > i) ? i : wLength;
136         lstrcpynA (lpszFile,  lpDrop,  i);
137 end:
138         GlobalUnlock16(hDrop);
139         return i;
140 }
141
142 /*************************************************************************
143  *                              DragFinish              [SHELL.12]
144  */
145 void WINAPI DragFinish16(HDROP16 h)
146 {
147     TRACE("\n");
148     GlobalFree16((HGLOBAL16)h);
149 }
150
151
152 /*************************************************************************
153  *                              DragQueryPoint          [SHELL.13]
154  */
155 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
156 {
157   LPDROPFILESTRUCT16 lpDropFileStruct;  
158   BOOL16           bRet;
159   TRACE("\n");
160   lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
161   
162   memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
163   bRet = lpDropFileStruct->fInNonClientArea;
164   
165   GlobalUnlock16(hDrop);
166   return bRet;
167 }
168
169 /*************************************************************************
170  *      SHELL_FindExecutable [Internal]
171  *
172  * Utility for code sharing between FindExecutable and ShellExecute
173  */
174 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile, 
175                                          LPCSTR lpOperation,
176                                          LPSTR lpResult)
177 { char *extension = NULL; /* pointer to file extension */
178     char tmpext[5];         /* local copy to mung as we please */
179     char filetype[256];     /* registry name for this filetype */
180     LONG filetypelen=256;   /* length of above */
181     char command[256];      /* command from registry */
182     LONG commandlen=256;    /* This is the most DOS can handle :) */
183     char buffer[256];       /* Used to GetProfileString */
184     HINSTANCE retval=31;  /* default - 'No association was found' */
185     char *tok;              /* token pointer */
186     int i;                  /* random counter */
187     char xlpFile[256] = ""; /* result of SearchPath */
188
189   TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
190
191     lpResult[0]='\0'; /* Start off with an empty return string */
192
193     /* trap NULL parameters on entry */
194     if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
195   { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
196            lpFile, lpOperation, lpResult);
197         return 2; /* File not found. Close enough, I guess. */
198     }
199
200     if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
201   { TRACE("SearchPathA returned non-zero\n");
202         lpFile = xlpFile;
203     }
204
205     /* First thing we need is the file's extension */
206     extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
207                                         /* File->Run in progman uses */
208                                         /* .\FILE.EXE :( */
209   TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
210
211     if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
212   { WARN("Returning 31 - No association\n");
213         return 31; /* no association */
214     }
215
216     /* Make local copy & lowercase it for reg & 'programs=' lookup */
217     lstrcpynA( tmpext, extension, 5 );
218     CharLowerA( tmpext );
219   TRACE("%s file\n", tmpext);
220     
221     /* Three places to check: */
222     /* 1. win.ini, [windows], programs (NB no leading '.') */
223     /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
224     /* 3. win.ini, [extensions], extension (NB no leading '.' */
225     /* All I know of the order is that registry is checked before */
226     /* extensions; however, it'd make sense to check the programs */
227     /* section first, so that's what happens here. */
228
229     /* See if it's a program - if GetProfileString fails, we skip this
230      * section. Actually, if GetProfileString fails, we've probably
231      * got a lot more to worry about than running a program... */
232     if ( GetProfileStringA("windows", "programs", "exe pif bat com",
233                                                   buffer, sizeof(buffer)) > 0 )
234   { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
235
236                 tok = strtok(buffer, " \t"); /* ? */
237                 while( tok!= NULL)
238                   {
239                         if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
240                           {
241                                 strcpy(lpResult, xlpFile);
242                                 /* Need to perhaps check that the file has a path
243                                  * attached */
244         TRACE("found %s\n", lpResult);
245         return 33;
246
247                 /* Greater than 32 to indicate success FIXME According to the
248                  * docs, I should be returning a handle for the
249                  * executable. Does this mean I'm supposed to open the
250                  * executable file or something? More RTFM, I guess... */
251                           }
252                         tok=strtok(NULL, " \t");
253                   }
254           }
255
256     /* Check registry */
257     if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
258                          &filetypelen ) == ERROR_SUCCESS )
259     {
260         filetype[filetypelen]='\0';
261         TRACE("File type: %s\n", filetype);
262
263         /* Looking for ...buffer\shell\lpOperation\command */
264         strcat( filetype, "\\shell\\" );
265         strcat( filetype, lpOperation );
266         strcat( filetype, "\\command" );
267         
268         if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
269                              &commandlen ) == ERROR_SUCCESS )
270         {
271             LPSTR tmp;
272             char param[256];
273             LONG paramlen = 256;
274
275
276             /* Get the parameters needed by the application 
277                from the associated ddeexec key */ 
278             tmp = strstr(filetype,"command");
279             tmp[0] = '\0';
280             strcat(filetype,"ddeexec");
281
282             if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,&paramlen ) == ERROR_SUCCESS)
283             {
284               strcat(command," ");
285               strcat(command,param);
286               commandlen += paramlen;
287             }
288
289             /* Is there a replace() function anywhere? */
290             command[commandlen]='\0';
291             strcpy( lpResult, command );
292             tok=strstr( lpResult, "%1" );
293             if (tok != NULL)
294             {
295                 tok[0]='\0'; /* truncate string at the percent */
296                 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
297                 tok=strstr( command, "%1" );
298                 if ((tok!=NULL) && (strlen(tok)>2))
299                 {
300                     strcat( lpResult, &tok[2] );
301                 }
302             }
303             retval=33; /* FIXME see above */
304         }
305     }
306     else /* Check win.ini */
307     {
308         /* Toss the leading dot */
309         extension++;
310         if ( GetProfileStringA( "extensions", extension, "", command,
311                                   sizeof(command)) > 0)
312           {
313                 if (strlen(command)!=0)
314                   {
315                         strcpy( lpResult, command );
316                         tok=strstr( lpResult, "^" ); /* should be ^.extension? */
317                         if (tok != NULL)
318                           {
319                                 tok[0]='\0';
320                                 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
321                                 tok=strstr( command, "^" ); /* see above */
322                                 if ((tok != NULL) && (strlen(tok)>5))
323                                   {
324                                         strcat( lpResult, &tok[5]);
325                                   }
326                           }
327                         retval=33; /* FIXME - see above */
328                   }
329           }
330         }
331
332     TRACE("returning %s\n", lpResult);
333     return retval;
334 }
335
336 /*************************************************************************
337  *                              ShellExecute            [SHELL.20]
338  */
339 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
340                                    LPCSTR lpFile, LPCSTR lpParameters,
341                                    LPCSTR lpDirectory, INT16 iShowCmd )
342 {   HINSTANCE16 retval=31;
343     char old_dir[1024];
344     char cmd[1024] = "";
345
346     TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
347                 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
348                 lpParameters ? lpParameters : "<null>", 
349                 lpDirectory ? lpDirectory : "<null>", iShowCmd);
350
351     if (lpFile==NULL) return 0; /* should not happen */
352     if (lpOperation==NULL) /* default is open */
353       lpOperation="open";
354
355     if (lpDirectory)
356     { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
357         SetCurrentDirectoryA( lpDirectory );
358     }
359
360     /* First try to execute lpFile with lpParameters directly */ 
361     strcpy(cmd,lpFile);
362     strcat(cmd,lpParameters ? lpParameters : "");
363
364     retval = WinExec16( cmd, iShowCmd );
365
366     /* Unable to execute lpFile directly
367        Check if we can match an application to lpFile */
368     if(retval < 32)
369     { 
370       cmd[0] = '\0';
371       retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
372
373       if (retval > 32)  /* Found */
374       {
375         if (lpParameters)
376         {
377             strcat(cmd," ");
378             strcat(cmd,lpParameters);
379         }
380         retval = WinExec16( cmd, iShowCmd );
381       }
382       else if(PathIsURLA((LPSTR)lpFile))    /* File not found, check for URL */
383       {
384         char lpstrProtocol[256];
385         LONG cmdlen = 512;
386         LPSTR lpstrRes;
387         INT iSize;
388       
389         lpstrRes = strchr(lpFile,':');
390         iSize = lpstrRes - lpFile;
391         
392         /* Looking for ...protocol\shell\lpOperation\command */
393         strncpy(lpstrProtocol,lpFile,iSize);
394         lpstrProtocol[iSize]='\0';
395         strcat( lpstrProtocol, "\\shell\\" );
396         strcat( lpstrProtocol, lpOperation );
397         strcat( lpstrProtocol, "\\command" );
398         
399         /* Remove File Protocol from lpFile */
400         /* In the case file://path/file     */
401         if(!strncasecmp(lpFile,"file",iSize))
402         {
403           lpFile += iSize;
404           while(*lpFile == ':') lpFile++;
405         }
406         
407
408         /* Get the application for the protocol and execute it */
409         if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
410                              &cmdlen ) == ERROR_SUCCESS )
411         {
412             LPSTR tok;
413             LPSTR tmp;
414             char param[256] = "";
415             LONG paramlen = 256;
416
417             /* Get the parameters needed by the application 
418                from the associated ddeexec key */ 
419             tmp = strstr(lpstrProtocol,"command");
420             tmp[0] = '\0';
421             strcat(lpstrProtocol,"ddeexec");
422
423             if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,&paramlen ) == ERROR_SUCCESS)
424             {
425               strcat(cmd," ");
426               strcat(cmd,param);
427               cmdlen += paramlen;
428             }
429             
430             /* Is there a replace() function anywhere? */
431             cmd[cmdlen]='\0';
432
433             tok=strstr( cmd, "%1" );
434             if (tok != NULL)
435             {
436                 tok[0]='\0'; /* truncate string at the percent */
437                 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
438                 tok=strstr( cmd, "%1" );
439                 if ((tok!=NULL) && (strlen(tok)>2))
440                 {
441                     strcat( cmd, &tok[2] );
442                 }
443             }
444  
445             retval = WinExec16( cmd, iShowCmd );
446         }
447       }
448     /* Check if file specified is in the form www.??????.*** */
449       else if(!strncasecmp(lpFile,"www",3))
450       {
451         /* if so, append lpFile http:// and call ShellExecute */ 
452         char lpstrTmpFile[256] = "http://" ;
453         strcat(lpstrTmpFile,lpFile);
454         retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
455       }
456     }
457     if (lpDirectory)
458       SetCurrentDirectoryA( old_dir );
459     return retval;
460 }
461
462 /*************************************************************************
463  *             FindExecutable   (SHELL.21)
464  */
465 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
466                                      LPSTR lpResult )
467 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
468 }
469
470
471 /*************************************************************************
472  *             AboutDlgProc   (SHELL.33)
473  */
474 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
475                                LPARAM lParam )
476 { return AboutDlgProc( hWnd, msg, wParam, lParam );
477 }
478
479
480 /*************************************************************************
481  *             ShellAbout   (SHELL.22)
482  */
483 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
484                             HICON16 hIcon )
485 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
486 }
487
488 /*************************************************************************
489  *                      InternalExtractIcon             [SHELL.39]
490  *
491  * This abortion is called directly by Progman
492  */
493 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
494                                      LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
495 {
496     HGLOBAL16 hRet = 0;
497     HICON16 *RetPtr = NULL;
498     OFSTRUCT ofs;
499     HFILE hFile;
500
501         TRACE("(%04x,file %s,start %d,extract %d\n", 
502                        hInstance, lpszExeFileName, nIconIndex, n);
503
504         if( !n )
505           return 0;
506
507         hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
508
509         hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
510         RetPtr = (HICON16*)GlobalLock16(hRet);
511
512         if (hFile == HFILE_ERROR)
513         { /* not found - load from builtin module if available */
514           HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
515
516           if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
517             hInst = LoadLibraryA(lpszExeFileName);
518           if (hInst)
519           {
520             int i;
521             for (i=nIconIndex; i < nIconIndex + n; i++)
522               RetPtr[i-nIconIndex] =
523                       (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
524             FreeLibrary(hInst);
525             return hRet;
526           }
527           GlobalFree16( hRet );
528           return 0;
529         }
530
531         if (nIconIndex == (UINT16)-1)  /* get number of icons */
532         {
533             RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
534         }
535         else
536         {
537             HRESULT res;
538             HICON *icons;
539             icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
540             res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
541                                         GetSystemMetrics(SM_CXICON),
542                                         GetSystemMetrics(SM_CYICON),
543                                         icons, 0, n, 0 );
544             if (!res)
545             {
546                 int i;
547                 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
548             }
549             else
550             {
551                 GlobalFree16( hRet );
552                 hRet = 0;
553             }
554             HeapFree( GetProcessHeap(), 0, icons );
555         }
556         return hRet;
557 }
558
559 /*************************************************************************
560  *             ExtractIcon   (SHELL.34)
561  */
562 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
563         UINT16 nIconIndex )
564 {   TRACE("\n");
565     return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
566 }
567
568 /*************************************************************************
569  *             ExtractIconEx   (SHELL.40)
570  */
571 HICON16 WINAPI ExtractIconEx16(
572         LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
573         HICON16 *phiconSmall, UINT16 nIcons
574 ) {
575     HICON       *ilarge,*ismall;
576     UINT16      ret;
577     int         i;
578
579     if (phiconLarge)
580         ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
581     else
582         ilarge = NULL;
583     if (phiconSmall)
584         ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
585     else
586         ismall = NULL;
587     ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
588     if (ilarge) {
589         for (i=0;i<nIcons;i++)
590             phiconLarge[i]=ilarge[i];
591         HeapFree(GetProcessHeap(),0,ilarge);
592     }
593     if (ismall) {
594         for (i=0;i<nIcons;i++)
595             phiconSmall[i]=ismall[i];
596         HeapFree(GetProcessHeap(),0,ismall);
597     }
598     return ret;
599 }
600
601 /*************************************************************************
602  *                              ExtractAssociatedIcon   [SHELL.36]
603  * 
604  * Return icon for given file (either from file itself or from associated
605  * executable) and patch parameters if needed.
606  */
607 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
608 {       HICON16 hIcon;
609         WORD wDummyIcon = 0;
610
611         TRACE("\n");
612
613         if(lpiIcon == NULL)
614             lpiIcon = &wDummyIcon;
615
616         hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
617
618         if( hIcon < 2 )
619         { if( hIcon == 1 ) /* no icons found in given file */
620           { char  tempPath[0x80];
621             UINT16  uRet = FindExecutable16(lpIconPath,NULL,tempPath);
622
623             if( uRet > 32 && tempPath[0] )
624             { strcpy(lpIconPath,tempPath);
625               hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
626               if( hIcon > 2 ) 
627                 return hIcon;
628             }
629             else hIcon = 0;
630           }
631
632           if( hIcon == 1 ) 
633             *lpiIcon = 2;   /* MSDOS icon - we found .exe but no icons in it */
634           else
635             *lpiIcon = 6;   /* generic icon - found nothing */
636
637           GetModuleFileName16(hInst, lpIconPath, 0x80);
638           hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
639         }
640         return hIcon;
641 }
642
643 /*************************************************************************
644  *                              ExtractAssociatedIconA (SHELL32.@)
645  * 
646  * Return icon for given file (either from file itself or from associated
647  * executable) and patch parameters if needed.
648  */
649 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
650 {       TRACE("\n");
651         return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
652 }
653
654 /*************************************************************************
655  *                              FindEnvironmentString   [SHELL.38]
656  *
657  * Returns a pointer into the DOS environment... Ugh.
658  */
659 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
660 { UINT16 l;
661
662   TRACE("\n");
663
664   l = strlen(entry); 
665   for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
666   { if( strncasecmp(lpEnv, entry, l) ) 
667       continue;
668         if( !*(lpEnv+l) )
669             return (lpEnv + l);                 /* empty entry */
670         else if ( *(lpEnv+l)== '=' )
671             return (lpEnv + l + 1);
672     }
673     return NULL;
674 }
675
676 /**********************************************************************/
677
678 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
679 { SEGPTR  spEnv;
680   LPSTR lpEnv,lpString;
681   TRACE("\n");
682     
683   spEnv = GetDOSEnvironment16();
684
685   lpEnv = MapSL(spEnv);
686   lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL; 
687
688     if( lpString )              /*  offset should be small enough */
689         return spEnv + (lpString - lpEnv);
690     return (SEGPTR)NULL;
691 }
692
693 /*************************************************************************
694  *                              DoEnvironmentSubst      [SHELL.37]
695  *
696  * Replace %KEYWORD% in the str with the value of variable KEYWORD
697  * from "DOS" environment.
698  */
699 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
700 {
701   LPSTR   lpEnv = MapSL(GetDOSEnvironment16());
702   LPSTR   lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
703   LPSTR   lpstr = str;
704   LPSTR   lpbstr = lpBuffer;
705
706   CharToOemA(str,str);
707
708   TRACE("accept %s\n", str);
709
710   while( *lpstr && lpbstr - lpBuffer < length )
711    {
712      LPSTR lpend = lpstr;
713
714      if( *lpstr == '%' )
715        {
716           do { lpend++; } while( *lpend && *lpend != '%' );
717           if( *lpend == '%' && lpend - lpstr > 1 )      /* found key */
718             {
719                LPSTR lpKey;
720               *lpend = '\0';  
721                lpKey = SHELL_FindString(lpEnv, lpstr+1);
722                if( lpKey )                              /* found key value */
723                  {
724                    int l = strlen(lpKey);
725
726                    if( l > length - (lpbstr - lpBuffer) - 1 )
727                      {
728            WARN("-- Env subst aborted - string too short\n");
729                       *lpend = '%';
730                        break;
731                      }
732                    strcpy(lpbstr, lpKey);
733                    lpbstr += l;
734                  }
735                else break;
736               *lpend = '%';
737                lpstr = lpend + 1;
738             }
739           else break;                                   /* back off and whine */
740
741           continue;
742        } 
743
744      *lpbstr++ = *lpstr++;
745    }
746
747  *lpbstr = '\0';
748   if( lpstr - str == strlen(str) )
749     {
750       strncpy(str, lpBuffer, length);
751       length = 1;
752     }
753   else
754       length = 0;
755
756   TRACE("-- return %s\n", str);
757
758   OemToCharA(str,str);
759   HeapFree( GetProcessHeap(), 0, lpBuffer);
760
761   /*  Return str length in the LOWORD
762    *  and 1 in HIWORD if subst was successful.
763    */
764  return (DWORD)MAKELONG(strlen(str), length);
765 }
766
767 /*************************************************************************
768  *                              ShellHookProc           [SHELL.103]
769  * System-wide WH_SHELL hook.
770  */
771 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
772 {
773     TRACE("%i, %04x, %08x\n", code, wParam, 
774                                                       (unsigned)lParam );
775     if( SHELL_hHook && SHELL_hWnd )
776     {
777         UINT16  uMsg = 0;
778         switch( code )
779         {
780             case HSHELL_WINDOWCREATED:          uMsg = uMsgWndCreated;   break;
781             case HSHELL_WINDOWDESTROYED:        uMsg = uMsgWndDestroyed; break;
782             case HSHELL_ACTIVATESHELLWINDOW:    uMsg = uMsgShellActivate;
783         }
784         PostMessageA( SHELL_hWnd, uMsg, wParam, 0 );
785     }
786     return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
787 }
788
789 /*************************************************************************
790  *                              RegisterShellHook       [SHELL.102]
791  */
792 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
793
794     TRACE("%04x [%u]\n", hWnd, uAction );
795
796     switch( uAction )
797     { 
798     case 2:  /* register hWnd as a shell window */
799         if( !SHELL_hHook )
800         { 
801             HMODULE16 hShell = GetModuleHandle16( "SHELL" );
802             HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
803             SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
804             if ( SHELL_hHook )
805             { 
806                 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
807                 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
808                 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
809             } 
810             else 
811                 WARN("-- unable to install ShellHookProc()!\n");
812         }
813
814         if ( SHELL_hHook )
815             return ((SHELL_hWnd = hWnd) != 0);
816         break;
817
818     default:
819         WARN("-- unknown code %i\n", uAction );
820         SHELL_hWnd = 0; /* just in case */
821     }
822     return FALSE;
823 }
824
825
826 /***********************************************************************
827  *           DriveType   (SHELL.262)
828  */
829 UINT16 WINAPI DriveType16( UINT16 drive )
830 {
831     UINT ret;
832     char path[] = "A:\\";
833     path[0] += drive;
834     ret = GetDriveTypeA(path);
835     switch(ret)  /* some values are not supported in Win16 */
836     {
837     case DRIVE_CDROM:
838         ret = DRIVE_REMOTE;
839         break;
840     case DRIVE_NO_ROOT_DIR:
841         ret = DRIVE_UNKNOWN;
842         break;
843     }
844     return ret;
845 }