This commit was generated by cvs2svn to compensate for changes in r2786,
[chise/xemacs-chise.git.1] / lib-src / run.c
1 /* run -- Wrapper program for console mode programs under Windows(TM)
2  * Copyright (C) 1998  Charles S. Wilson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 /*
20  * This program is based on the runemacs.c distributed with XEmacs 21.0
21  *
22  * Simple program to start gnu-win32 X11 programs (and native XEmacs) 
23  * with its console window hidden.
24  *
25  * This program is provided purely for convenience, since most users will
26  * use XEmacs in windowing (GUI) mode, and will not want to have an extra
27  * console window lying around. Ditto for desktop shortcuts to gnu-win32 
28  * X11 executables.
29  */
30
31
32 #define WIN32
33
34 #include <windows.h>
35 #include <string.h>
36 #include <malloc.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40
41 #include "run.h"
42
43 #if defined(__CYGWIN__)
44  #include <sys/types.h>
45  #include <sys/stat.h>
46  #include <sys/cygwin.h>
47  #include <sys/unistd.h>
48 WinMainCRTStartup() { mainCRTStartup(); }
49 #else
50  #include <direct.h>
51 #endif
52
53
54 char buffer[1024];
55
56 int WINAPI
57 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
58 {
59    int wait_for_child = FALSE;
60    int compact_invocation = FALSE;
61    DWORD ret_code = 0;
62
63
64    char execname[FILENAME_MAX];
65    char execpath[MAX_PATH];
66    char* argv[MAX_ARGS+1]; /* leave extra slot for compact_invocation argv[0] */
67    int argc;
68    int i,j;
69    char exec[MAX_PATH + FILENAME_MAX + 100];
70    char cmdline2[MAX_ARGS * MAX_PATH];
71
72    compact_invocation = get_exec_name_and_path(execname,execpath);
73
74    if (compact_invocation)
75    {
76       argv[0] = execname;
77       argc = parse_cmdline_to_arg_array(&(argv[1]),cmdline);
78       argc++;
79    }
80    else
81    {
82       argc = parse_cmdline_to_arg_array(argv,cmdline);
83       if (argc >= 1)
84          strcpy(execname,argv[0]);
85    }
86    /* at this point, execpath is defined, as are argv[] and execname */   
87 #ifdef DEBUG
88    j = sprintf(buffer,"\nexecname : %s\nexecpath : %s\n",execname,execpath);
89    for (i = 0; i < argc; i++)
90       j += sprintf(buffer+j,"argv[%d]\t: %s\n",i,argv[i]);
91    Trace((buffer));
92 #endif
93
94    if (execname == NULL)
95       error("you must supply a program name to run");
96
97 #if defined(__CYGWIN__)
98    /* this insures that we search for symlinks before .exe's */
99    if (compact_invocation)
100       strip_exe(execname);
101 #endif
102
103    process_execname(exec,execname,execpath);
104    Trace(("exec\t%s\nexecname\t%s\nexecpath\t%s\n",
105          exec,execname,execpath));
106
107    wait_for_child = build_cmdline(cmdline2,exec,argc,argv);
108    Trace((cmdline2));
109
110    xemacs_special(exec);
111    ret_code = start_child(cmdline2,wait_for_child);
112    if (compact_invocation)
113       for (i = 1; i < argc; i++) // argv[0] was not malloc'ed
114          free(argv[i]);
115    else
116       for (i = 0; i < argc; i++)
117          free(argv[i]);
118    return (int) ret_code;
119 }
120 int start_child(char* cmdline, int wait_for_child)
121 {
122    STARTUPINFO start;
123    SECURITY_ATTRIBUTES sec_attrs;
124    PROCESS_INFORMATION child;
125    int retval;
126
127    memset (&start, 0, sizeof (start));
128    start.cb = sizeof (start);
129    start.dwFlags = STARTF_USESHOWWINDOW;
130    start.wShowWindow = SW_HIDE;
131       
132    sec_attrs.nLength = sizeof (sec_attrs);
133    sec_attrs.lpSecurityDescriptor = NULL;
134    sec_attrs.bInheritHandle = FALSE;
135
136    if (CreateProcess (NULL, cmdline, &sec_attrs, NULL, TRUE, 0,
137                       NULL, NULL, &start, &child))
138    {
139       if (wait_for_child)
140       {
141          WaitForSingleObject (child.hProcess, INFINITE);
142          GetExitCodeProcess (child.hProcess, &retval);
143       }
144       CloseHandle (child.hThread);
145       CloseHandle (child.hProcess);
146    }
147    else
148       error("could not start %s",cmdline);
149    return retval;
150 }
151 void xemacs_special(char* exec)
152 {
153   /*
154    * if we're trying to run xemacs, AND this file was in %emacs_dir%\bin,
155    * then set emacs_dir environment variable 
156    */
157    char* p;
158    char* p2;
159    char exec2[MAX_PATH + FILENAME_MAX + 100];
160    char tmp[MAX_PATH + FILENAME_MAX + 100];
161    strcpy(exec2,exec);
162    /* this depends on short-circuit evaluation */
163    if ( ((p = strrchr(exec2,'\\')) && stricmp(p,"\\xemacs") == 0) ||
164         ((p = strrchr(exec2,'/')) && stricmp(p,"/xemacs") == 0) ||
165         ((p = strrchr(exec2,'\\')) && stricmp(p,"\\xemacs.exe") == 0) ||
166         ((p = strrchr(exec2,'/')) && stricmp(p,"/xemacs.exe") == 0) )
167    {
168       if ( ((p2 = strrchr(p, '\\')) && stricmp(p2, "\\bin") == 0) ||
169            ((p2 = strrchr(p, '/')) && stricmp(p2, "/bin") == 0) )
170       {
171          *p2 = '\0';   
172 #if defined(__CYGWIN__)
173          CYGWIN_CONV_TO_POSIX_PATH((exec2,tmp));
174          strcpy(exec2,tmp);
175 #else /* NATIVE xemacs DOS-style paths with forward slashes */
176          for (p = exec2; *p; p++)
177             if (*p == '\\') *p = '/';
178 #endif
179          SetEnvironmentVariable ("emacs_dir", exec2);
180       }
181    }
182 }
183 int build_cmdline(char* new_cmdline, char* exec, int argc, char* argv[])
184 {
185    int retval = FALSE;
186    int first_arg = 1;
187    int i;
188    int char_cnt = 0;
189    /*
190     * look for "-wait" as first true argument; we'll apply that ourselves
191     */
192    if ((argc >= 2) && (stricmp(argv[1],"-wait") == 0))
193    {
194       retval = TRUE;
195       first_arg++;
196    }
197
198    char_cnt = strlen(exec);
199    for (i = first_arg; i < argc; i++)
200       char_cnt += strlen(argv[i]);
201    if (char_cnt > MAX_ARGS*MAX_PATH) /* then we ran out of room */
202       error("command line too long -\n%s",new_cmdline);
203    
204    strcpy(new_cmdline,exec);
205    for (i = first_arg; i < argc; i++)
206    {
207       strcat(new_cmdline," ");
208       strcat(new_cmdline,argv[i]);
209    }
210    return retval;
211 }
212 /* process exec_arg : if it
213  * NATIVE:
214  *  1) starts with '\\' or '/', it's a root-path and leave it alone
215  *  2) starts with 'x:\\' or 'x:/', it's a root-path and leave it alone
216  *  3) starts with '.\\' or './', two possible meanings:
217  *       1) exec is in the current directory
218  *       2) exec in same directory as this program
219  *  4) otherwise, search path (and _prepend_ "." to the path!!!)
220  *  5) convert all '/' to '\\'
221  * CYGWIN
222  *  1) starts with '\\' or '/', it's a root-path and leave it alone
223  *  2) starts with 'x:\\' or 'x:/', it's a root-path and leave it alone
224  *  3) starts with '.\\' or './', two possible meanings:
225  *       1) exec is in the current directory
226  *       2) exec in same directory as this program
227  *  4) otherwise, search path (and _prepend_ "." to the path!!!)
228  *  5) convert to cygwin-style path to resolve symlinks within the pathspec
229  *  6) check filename: if it's a symlink, resolve it by peeking inside
230  *  7) convert to win32-style path+filename since we're using Windows 
231  *       createProcess() to launch
232  */
233 void process_execname(char *exec, const char* execname,const char* execpath )
234 {
235    char* orig_pathlist;
236    char* pathlist;
237    char exec_tmp[MAX_PATH + FILENAME_MAX + 100];
238    char exec_tmp2[MAX_PATH + FILENAME_MAX + 100];
239    char buf[MAX_PATH + FILENAME_MAX + 100];
240    int i,j;
241
242    /* 
243     * STARTS WITH / or \ 
244     * execpath NOT used
245     */
246    if ((execname[0] == '\\') || (execname[0] == '/'))
247    {
248 #if defined(__CYGWIN__)
249       strcpy(exec_tmp,execname);
250 #else    
251       exec_tmp[0] = ((char) (_getdrive() + ((int) 'A') - 1));
252       exec_tmp[1] = ':';
253       exec_tmp[2] = '\0';
254       strcat(exec_tmp,execname);
255 #endif
256       Trace(("/ -\nexec_tmp\t%s\nexecname\t%s\nexecpath\t%s\n",
257              exec_tmp,execname,execpath));
258       if (! fileExistsMulti(exec_tmp2,NULL,exec_tmp,exts,NUM_EXTENSIONS) )
259       {
260           j = 0;
261           for (i = 0; i < NUM_EXTENSIONS; i++)
262               j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
263           error("Couldn't locate %s\nI tried appending the following "
264                 "extensions: \n%s",exec_tmp,buf);
265       }
266       Trace((exec_tmp2));
267    }
268    /* 
269     * STARTS WITH x:\ or x:/
270     * execpath NOT used
271     */
272     else if ((strlen(execname) > 3) && // avoid boundary errors
273        (execname[1] == ':') &&
274        ((execname[2] == '\\') || (execname[2] == '/')))
275    {
276       strcpy(exec_tmp,execname);       
277       Trace(("x: -\nexec_tmp\t%s\nexecname\t%s\nexecpath\t%s\n",
278              exec_tmp,execname,execpath));
279       if (! fileExistsMulti(exec_tmp2,NULL,exec_tmp,exts,NUM_EXTENSIONS) )
280       {
281           j = 0;
282           for (i = 0; i < NUM_EXTENSIONS; i++)
283               j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
284           error("Couldn't locate %s\nI tried appending the following "
285                 "extensions: \n%s",exec_tmp,buf);
286       }
287       Trace((exec_tmp2));
288    }
289    /* 
290     * STARTS WITH ./ or .\
291     */
292    else if ((execname[0] == '.') &&
293             ((execname[1] == '\\') || (execname[1] == '/')))
294    {
295       if (((char*) getcwd(exec_tmp,MAX_PATH))==NULL)
296          error("can't find current working directory");
297       if (! fileExistsMulti(exec_tmp2,exec_tmp,&(execname[2]),
298                             exts,NUM_EXTENSIONS) )
299           if (! fileExistsMulti(exec_tmp2,execpath,&(execname[2]),
300                                 exts,NUM_EXTENSIONS) )
301           {
302               j = 0;
303               for (i = 0; i < NUM_EXTENSIONS; i++)
304                   j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
305               error("Couldn't locate %s\n"
306                     "I looked in the following directories:\n [1]: %s\n [2]: %s\n"
307                     "I also tried appending the following "
308                     "extensions: \n%s",execname,exec_tmp,execpath,buf);
309           }
310       Trace((exec_tmp2));
311    }
312    /*
313     * OTHERWISE, SEARCH PATH (prepend '.' and run.exe's directory)
314     * can't use fileExistsMulti because we want to search entire path
315     * for exts[0], then for exts[1], etc.
316     */
317    else
318    {
319       orig_pathlist = getenv("PATH");
320       if ((pathlist = malloc (strlen(orig_pathlist)
321                               + strlen(".") 
322                               + strlen(execpath)+ 3)) == NULL)
323          error("internal error - out of memory");
324       strcpy(pathlist,".");
325       strcat(pathlist,SEP_CHARS);
326       strcat(pathlist,execpath);
327       strcat(pathlist,SEP_CHARS);
328       strcat(pathlist,orig_pathlist);
329
330       Trace((pathlist));
331       for (i = 0; i < NUM_EXTENSIONS; i++)
332       {
333           strcpy(exec_tmp,execname);
334           strcat(exec_tmp,exts[i]);
335           pfopen(exec_tmp2,exec_tmp,pathlist);
336           if (fileExists(NULL,NULL,exec_tmp2))
337               break;
338           exec_tmp2[0] = '\0';
339       }
340       Trace(("exec_tmp\t%s\npathlist\t%s\n",exec_tmp2,pathlist));
341
342       free(pathlist);
343       if (exec_tmp2[0] == '\0')
344       {
345           j = 0;
346           for (i = 0; i < NUM_EXTENSIONS; i++)
347               j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
348           error("Couldn't find %s anywhere.\n"
349                 "I even looked in the PATH \n"
350                 "I also tried appending the following "
351                 "extensions: \n%s",execname,buf);
352       }
353    }
354 /*
355  * At this point, we know that exec_tmp2 contains a filename
356  * and we know that exec_tmp2 exists.
357  */
358 #if defined(__CYGWIN__)
359    {
360       struct stat stbuf;
361       char sym_link_name[MAX_PATH+1];
362       char real_name[MAX_PATH+1];
363       char dummy[MAX_PATH+1];
364
365       strcpy(exec_tmp,exec_tmp2);
366
367       CYGWIN_CONV_TO_POSIX_PATH((exec_tmp,sym_link_name));
368       Trace((sym_link_name));
369       
370       if (lstat(sym_link_name, &stbuf) == 0)
371       {
372          if ((stbuf.st_mode & S_IFLNK) == S_IFLNK)
373          {
374             if (readlink(sym_link_name, real_name, sizeof(real_name)) == -1)
375                error("problem reading symbolic link for %s",exec_tmp);
376             else
377             {
378                 // if realname starts with '/' it's a rootpath 
379                 if (real_name[0] == '/')
380                     strcpy(exec_tmp2,real_name);
381                 else // otherwise, it's relative to the symlink's location
382                 {
383                    CYGWIN_SPLIT_PATH((sym_link_name,exec_tmp2,dummy));
384                    if (!endsWith(exec_tmp2,PATH_SEP_CHAR_STR))
385                       strcat(exec_tmp2,PATH_SEP_CHAR_STR);
386                    strcat(exec_tmp2,real_name);
387                 }
388             }
389          }
390          else /* NOT a symlink */
391             strcpy(exec_tmp2, sym_link_name);
392       }
393       else
394          error("can't locate executable - %s",sym_link_name);
395    }
396    CYGWIN_CONV_TO_FULL_WIN32_PATH((exec_tmp2,exec));
397 #else                                   
398    strcpy (exec, exec_tmp2);
399 #endif  
400 }
401 int endsWith(const char* s1, const char* s2)
402 {
403     int len1;
404     int len2;
405     int retval = FALSE;
406     len1 = strlen(s1);
407     len2 = strlen(s2);
408     if (len1 - len2 >= 0)
409         if (stricmp(&(s1[len1-len2]),s2) == 0)
410             retval = TRUE;
411     return retval;
412 }void strip_exe(char* s)
413 {
414    if ((strlen(s) > 4) && // long enough to have .exe extension
415        // second part not evaluated (short circuit) if exec_arg too short
416        (stricmp(&(s[strlen(s)-4]),".exe") == 0))
417       s[strlen(s)-4] = '\0';
418 }
419 void error(char* fmt, ...)
420 {
421    char buf[4096];
422    int j;
423    va_list args;
424    va_start(args, fmt);
425    j =   sprintf(buf,    "Error: ");
426    j += vsprintf(buf + j,fmt,args);
427    j +=  sprintf(buf + j,"\n");
428    va_end(args);
429    MessageBox(NULL, buf, "Run.exe", MB_ICONSTOP);
430    exit(1);
431 }
432 void message(char* fmt, ...)
433 {
434    char buf[10000];
435    int j;
436    va_list args;
437    va_start(args, fmt);
438    j = vsprintf(buf,fmt,args);
439    j +=  sprintf(buf + j,"\n");
440    va_end(args);
441    MessageBox(NULL, buf, "Run.exe Message", MB_ICONSTOP);
442 }
443 void Trace_(char* fmt, ...)
444 {
445    char buf[10000];
446    int j;
447    va_list args;
448    va_start(args, fmt);
449    j = vsprintf(buf,fmt,args);
450    j +=  sprintf(buf + j,"\n");
451    va_end(args);
452    MessageBox(NULL, buf, "Run.exe DEBUG", MB_ICONSTOP);
453 }
454 /*
455  * Uses system info to determine the path used to invoke run
456  * Also attempts to deduce the target execname if "compact_invocation"
457  * method was used.
458  *
459  * returns TRUE if compact_invocation method was used
460  *   (and target execname was deduced successfully)
461  * otherwise returns FALSE, and execname == run or run.exe
462  */
463 int get_exec_name_and_path(char* execname, char* execpath)
464 {
465    char modname[MAX_PATH];
466    char* tmp_execname;
467    char* p;
468    int retval = FALSE;
469
470    if (!GetModuleFileName (NULL, modname, MAX_PATH))
471       error("internal error - can't find my own name");
472    if ((p = strrchr (modname, '\\')) == NULL)
473       error("internal error - my own name has no path\n%s",modname);
474    tmp_execname = p + 1;
475    p[0] = '\0';
476    // if invoked by a name like "runxemacs" then strip off
477    // the "run" and let "xemacs" be execname.
478    // To check for this, make that:
479    //   1) first three chars are "run"
480    //   2) but the string doesn't end there, or start ".exe"
481    // Also, set "compact_invocation" TRUE
482    if ( ((tmp_execname[0] == 'r') || (tmp_execname[0] == 'R')) &&
483         ((tmp_execname[1] == 'u') || (tmp_execname[1] == 'U')) &&
484         ((tmp_execname[2] == 'n') || (tmp_execname[2] == 'N')) &&
485         ((tmp_execname[3] != '.') && (tmp_execname[3] != '\0')) )
486    {
487       tmp_execname += 3;
488       retval = TRUE;
489    }
490    else
491       tmp_execname = NULL;
492
493    if (tmp_execname == NULL)
494       strcpy(execname,"");
495    else
496       strcpy(execname,tmp_execname);
497 #if defined(__CYGWIN__)
498    CYGWIN_CONV_TO_POSIX_PATH((modname,execpath));
499 #else
500    strcpy(execpath,modname);
501 #endif
502    return retval;
503 }
504 /*
505  * works like strtok, but:
506  * double quotes (") suspends tokenizing until closing " reached
507  * CYGWIN ONLY:
508  *   additionally, backslash escapes next character, even if that
509  *   next character is a delimiter. Or a double quote.
510  *   WARNING: this means that backslash may NOT be a delimiter 
511  */
512 char* my_strtok(char* s, const char* delim, char** lasts)
513 {
514    char *spanp;
515    int c, sc;
516    char *tok;
517    
518    if ((s == NULL) && ((s = *lasts) == NULL))
519       return NULL;
520    /* Skip leading delimiters */
521 cont:
522    c = *s++;
523    for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
524       if (c == sc)
525          goto cont;
526    }
527    if (c == 0) {                /* no non-delimiter characters */
528       *lasts = NULL;
529       return (NULL);
530    }
531    tok = s - 1;
532    /*
533     * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
534     * Note that delim must have one NUL; we stop if we see that, too.
535     * If we see a double quote, continue until next double quote, then
536     *   start scanning for delimiters again.
537     * CYGWIN ONLY: if we see a backslash, just copy next character -
538     *   don't consider it as a delimiter even if it is in delim string.
539     */
540    for (;;) {
541       /* if this c is ", then scan until we find next " */
542       if (c == '\"')
543          while ((c = *s++) != '\"')
544             if (c == 0) /* oops, forgot to close the ", clean up & return */
545             {
546                s = NULL;
547                *lasts = s;
548                return (tok);
549             }
550 #if defined(__CYGWIN__)
551       if (c == '\\')
552       {
553          c = *s++; /* skip the backslash */
554          if (c == 0) /* if escaped character is end-of-string, clean up & return */
555          {
556             s = NULL;
557             *lasts = s;
558             return (tok);
559          }
560          c = *s++; /* otherwise, skip the escaped character */
561       }
562 #endif   
563       spanp = (char *)delim;
564       do {
565          if ((sc = *spanp++) == c) {
566             if (c == 0)
567                s = NULL;
568             else
569                s[-1] = 0;
570             *lasts = s;
571             return (tok);
572          }
573       } while (sc != 0);
574       c = *s++;
575    }
576    /* NOTREACHED */
577 }
578 int parse_cmdline_to_arg_array(char* argv[MAX_ARGS], char* cmdline)
579 {
580    char seps[] = " \t\n";
581    char* token;
582    int argc = 0;
583    char* lasts;
584
585    token = my_strtok(cmdline, seps, &lasts);
586    while ((token != NULL) && (argc < MAX_ARGS))
587    {
588       if ((argv[argc] = malloc(strlen(token)+1)) == NULL)
589       {
590          error("internal error - out of memory");
591       }
592       strcpy(argv[argc++],token);
593       token = my_strtok(NULL,seps,&lasts);
594    }
595    if (argc >= MAX_ARGS)
596       error("too many arguments on commandline\n%s",cmdline);
597    return argc;
598 }
599 /* Taken from pfopen.c by David Engel (5-Jul-97).
600  * Original comments appear below. Superseded by next comment block.
601  *
602  *  Written and released to the public domain by David Engel.
603  *
604  *  This function attempts to open a file which may be in any of
605  *  several directories.  It is particularly useful for opening
606  *  configuration files.  For example, PROG.EXE can easily open
607  *  PROG.CFG (which is kept in the same directory) by executing:
608  *
609  *      cfg_file = pfopen("PROG.CFG", "r", getenv("PATH"));
610  *
611  *  NULL is returned if the file can't be opened.
612  */
613
614 /*
615  * This function attempts to locate a file which may be in any of
616  * several directories. Unlike the original pfopen, it does not
617  * return a FILE pointer to the opened file, but rather returns
618  * the fully-qualified filename of the first match found. Returns
619  * empty string if not found.
620  */
621 char *pfopen(char *retval, const char *name, const char *dirs)
622 {
623     char *ptr;
624     char *tdirs;
625     char returnval[MAX_PATH + FILENAME_MAX + 100];
626     int foundit = FALSE;
627     
628     returnval[0] = '\0';
629
630     if (dirs == NULL || dirs[0] == '\0')
631         return NULL;
632
633     if ((tdirs = malloc(strlen(dirs)+1)) == NULL)
634         return NULL;
635
636     strcpy(tdirs, dirs);
637
638     for (ptr = strtok(tdirs, SEP_CHARS); (foundit == FALSE) && ptr != NULL;
639          ptr = strtok(NULL, SEP_CHARS))
640     {
641        foundit = fileExists(returnval,ptr,name);
642     }
643
644     free(tdirs);
645     if (!foundit)
646         retval[0] = '\0';
647     else
648         strcpy(retval,returnval);
649     return retval;
650 }
651 int fileExistsMulti(char* fullname, const char* path, 
652                     const char* name_noext, const char* exts[],
653                     const int extcnt)
654 {
655     char tryName[MAX_PATH + FILENAME_MAX];
656     int i = 0;
657     int retval = FALSE;
658     fullname[0] = '\0';
659     for (i = 0; i < extcnt; i++)
660     {
661         strcpy(tryName,name_noext);
662         strcat(tryName,exts[i]);
663         if (fileExists(fullname, path, tryName) == TRUE)
664         {
665             retval = TRUE;
666             break;
667         }
668         fullname[0] = '\0';
669     }
670     return retval;
671 }
672 int fileExists(char* fullname, const char* path, const char* name)
673 {
674    int retval = FALSE;
675    FILE* file;
676    size_t len;
677    char work[FILENAME_MAX];
678    char work2[MAX_PATH + FILENAME_MAX + 100];
679    if (path != NULL)
680    {
681       strcpy(work, path);
682       len = strlen(work);
683       if (len && work[len-1] != '/' && work[len-1] != '\\')
684          strcat(work, PATH_SEP_CHAR_STR);
685    }
686    else
687       work[0]='\0';
688    
689    strcat(work, name);
690 #if defined(__CYGWIN__)
691    CYGWIN_CONV_TO_POSIX_PATH((work, work2)); 
692 #else
693    strcpy(work2,work);
694 #endif
695
696 #ifdef DEBUGALL
697    Trace(("looking for...\t%s\n",work2));
698 #endif
699
700    file = fopen(work2, "rb");
701    if (file != NULL)
702    {
703       if (fullname != NULL)
704          strcpy(fullname,work2);
705       retval = TRUE;
706       fclose(file);
707    }
708    return retval;
709 }