1 /* Platform-Specific Win32 Functions
2    Copyright (C) 2003  Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20 
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
24 
25 /* Written by Mohan Embar <gnustuff@thisiscool.com>, March 2003. */
26 
27 #ifdef WIN32
28 
29 #include "config.h"
30 #include "system.h"
31 
32 #define WIN32_LEAN_AND_MEAN
33 #include <windows.h>
34 #undef WIN32_LEAN_AND_MEAN
35 
36 /* Simulate an open() failure with ENOENT */
37 static int
38 file_not_found (void);
39 
40 static int
file_not_found(void)41 file_not_found (void)
42 {
43   errno = ENOENT;
44   return -1;
45 }
46 
47 int
jcf_open_exact_case(const char * filename,int oflag)48 jcf_open_exact_case (const char *filename, int oflag)
49 {
50   int filename_len = strlen (filename);
51   int found_file_len;
52   HANDLE found_file_handle;
53   WIN32_FIND_DATA fd;
54 
55   /* See if we can find this file. */
56   found_file_handle = FindFirstFile (filename, &fd);
57   if (found_file_handle == INVALID_HANDLE_VALUE)
58     return file_not_found ();
59   FindClose (found_file_handle);
60 
61   found_file_len = strlen (fd.cFileName);
62 
63   /* This should never happen. */
64   if (found_file_len > filename_len)
65     return file_not_found ();
66 
67   /* Here, we're only actually comparing the filename and not
68      checking the case of any containing directory components.
69      Although we're not fully obeying our contract, checking
70      all directory components would be tedious and time-consuming
71      and it's a pretty safe assumption that mixed-case package
72      names are a fringe case.... */
73   if (strcmp (filename + filename_len - found_file_len, fd.cFileName))
74     {
75       /* Reject this because it is not a perfect-case match. */
76       /* printf("************\nRejected:\n%s\n%s\n************\n\n", filename, fd.cFileName); */
77       return file_not_found ();
78     }
79   else
80     {
81       return open (filename, oflag);
82     }
83 }
84 
85 #endif /* WIN32 */
86