File indexing completed on 2025-05-11 08:24:16
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
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
0096
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 }