1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup rtl |
---|
7 | * |
---|
8 | * @brief RTEMS Run-Time Linker Error |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * COPYRIGHT (c) 2012-2013 Chris Johns <chrisj@rtems.org> |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
33 | * POSSIBILITY OF SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifdef HAVE_CONFIG_H |
---|
37 | #include "config.h" |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <errno.h> |
---|
41 | #include <fcntl.h> |
---|
42 | #include <inttypes.h> |
---|
43 | #include <stdlib.h> |
---|
44 | #include <stdio.h> |
---|
45 | #include <string.h> |
---|
46 | #include <unistd.h> |
---|
47 | |
---|
48 | #include <rtems/libio_.h> |
---|
49 | |
---|
50 | #include <rtems/rtl/rtl.h> |
---|
51 | #include "rtl-find-file.h" |
---|
52 | #include "rtl-error.h" |
---|
53 | #include "rtl-string.h" |
---|
54 | #include <rtems/rtl/rtl-trace.h> |
---|
55 | |
---|
56 | #if WAF_BUILD |
---|
57 | #define rtems_filesystem_is_delimiter rtems_filesystem_is_separator |
---|
58 | #endif |
---|
59 | |
---|
60 | bool |
---|
61 | rtems_rtl_find_file (const char* name, |
---|
62 | const char* paths, |
---|
63 | const char** file_name, |
---|
64 | size_t* size) |
---|
65 | { |
---|
66 | struct stat sb; |
---|
67 | |
---|
68 | *file_name = NULL; |
---|
69 | *size = 0; |
---|
70 | |
---|
71 | if (rtems_filesystem_is_delimiter (name[0]) || (name[0] == '.')) |
---|
72 | { |
---|
73 | if (stat (name, &sb) == 0) |
---|
74 | *file_name = rtems_rtl_strdup (name); |
---|
75 | } |
---|
76 | else if (paths) |
---|
77 | { |
---|
78 | const char* start; |
---|
79 | const char* end; |
---|
80 | int len; |
---|
81 | char* fname; |
---|
82 | |
---|
83 | start = paths; |
---|
84 | end = start + strlen (paths); |
---|
85 | len = strlen (name); |
---|
86 | |
---|
87 | while (!*file_name && (start != end)) |
---|
88 | { |
---|
89 | const char* delimiter = strchr (start, ':'); |
---|
90 | |
---|
91 | if (delimiter == NULL) |
---|
92 | delimiter = end; |
---|
93 | |
---|
94 | /* |
---|
95 | * Allocate the path fragment, separator, name, terminating nul. Form the |
---|
96 | * path then see if the stat call works. |
---|
97 | */ |
---|
98 | |
---|
99 | fname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, |
---|
100 | (delimiter - start) + 1 + len + 1, true); |
---|
101 | if (!fname) |
---|
102 | { |
---|
103 | rtems_rtl_set_error (ENOMEM, "no memory searching for file"); |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | memcpy (fname, start, delimiter - start); |
---|
108 | fname[delimiter - start] = '/'; |
---|
109 | memcpy (fname + (delimiter - start) + 1, name, len); |
---|
110 | |
---|
111 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD)) |
---|
112 | printf ("rtl: find-file: path: %s\n", fname); |
---|
113 | |
---|
114 | if (stat (fname, &sb) < 0) |
---|
115 | rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, fname); |
---|
116 | else |
---|
117 | *file_name = fname; |
---|
118 | |
---|
119 | start = delimiter; |
---|
120 | if (start != end) |
---|
121 | ++start; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | if (!*file_name) |
---|
126 | return false; |
---|
127 | |
---|
128 | *size = sb.st_size; |
---|
129 | |
---|
130 | return true; |
---|
131 | } |
---|