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