Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:16

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup rtl
0007  *
0008  * @brief RTEMS Run-Time Linker Error
0009  */
0010 
0011 /*
0012  *  COPYRIGHT (c) 2012-2013 Chris Johns <chrisj@rtems.org>
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <errno.h>
0041 #include <fcntl.h>
0042 #include <inttypes.h>
0043 #include <stdlib.h>
0044 #include <stdio.h>
0045 #include <string.h>
0046 #include <unistd.h>
0047 
0048 #include <rtems/libio_.h>
0049 
0050 #include <rtems/rtl/rtl.h>
0051 #include "rtl-find-file.h"
0052 #include "rtl-error.h"
0053 #include "rtl-string.h"
0054 #include <rtems/rtl/rtl-trace.h>
0055 
0056 #if WAF_BUILD
0057 #define rtems_filesystem_is_delimiter rtems_filesystem_is_separator
0058 #endif
0059 
0060 bool
0061 rtems_rtl_find_file (const char*  name,
0062                      const char*  paths,
0063                      const char** file_name,
0064                      size_t*      size)
0065 {
0066   struct stat sb;
0067 
0068   *file_name = NULL;
0069   *size = 0;
0070 
0071   if (rtems_filesystem_is_delimiter (name[0]) || (name[0] == '.'))
0072   {
0073     if (stat (name, &sb) == 0)
0074       *file_name = rtems_rtl_strdup (name);
0075   }
0076   else if (paths)
0077   {
0078     const char* start;
0079     const char* end;
0080     int         len;
0081     char*       fname;
0082 
0083     start = paths;
0084     end = start + strlen (paths);
0085     len = strlen (name);
0086 
0087     while (!*file_name && (start != end))
0088     {
0089       const char* delimiter = strchr (start, ':');
0090 
0091       if (delimiter == NULL)
0092         delimiter = end;
0093 
0094       /*
0095        * Allocate the path fragment, separator, name, terminating nul. Form the
0096        * path then see if the stat call works.
0097        */
0098 
0099       fname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
0100                                    (delimiter - start) + 1 + len + 1, true);
0101       if (!fname)
0102       {
0103         rtems_rtl_set_error (ENOMEM, "no memory searching for file");
0104         return false;
0105       }
0106 
0107       memcpy (fname, start, delimiter - start);
0108       fname[delimiter - start] = '/';
0109       memcpy (fname + (delimiter - start) + 1, name, len);
0110 
0111       if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD))
0112         printf ("rtl: find-file: path: %s\n", fname);
0113 
0114       if (stat (fname, &sb) < 0)
0115         rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, fname);
0116       else
0117         *file_name = fname;
0118 
0119       start = delimiter;
0120       if (start != end)
0121         ++start;
0122     }
0123   }
0124 
0125   if (!*file_name)
0126     return false;
0127 
0128   *size = sb.st_size;
0129 
0130   return true;
0131 }