source: rtems/cpukit/libdl/rtl-find-file.c @ 4408603

Last change on this file since 4408603 was 990adc5, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 13, 2017 at 7:20:30 AM

libdl: Include <rtems/rtl/rtl-*.h>

Prepare for header file move to common include directory.

Update #3254.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/*
2 *  COPYRIGHT (c) 2012-2013 Chris Johns <chrisj@rtems.org>
3 *
4 *  The license and distribution terms for this file may be
5 *  found in the file LICENSE in this distribution or at
6 *  http://www.rtems.org/license/LICENSE.
7 */
8/**
9 * @file
10 *
11 * @ingroup rtl
12 *
13 * @brief RTEMS Run-Time Linker Error
14 */
15
16#if HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <errno.h>
21#include <fcntl.h>
22#include <inttypes.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <rtems/libio_.h>
29
30#include <rtems/rtl/rtl.h>
31#include "rtl-find-file.h"
32#include "rtl-error.h"
33#include "rtl-string.h"
34#include <rtems/rtl/rtl-trace.h>
35
36#if WAF_BUILD
37#define rtems_filesystem_is_delimiter rtems_filesystem_is_separator
38#endif
39
40bool
41rtems_rtl_find_file (const char*  name,
42                     const char*  paths,
43                     const char** file_name,
44                     size_t*      size)
45{
46  struct stat sb;
47
48  *file_name = NULL;
49  *size = 0;
50
51  if (rtems_filesystem_is_delimiter (name[0]) || (name[0] == '.'))
52  {
53    if (stat (name, &sb) == 0)
54      *file_name = rtems_rtl_strdup (name);
55  }
56  else if (paths)
57  {
58    const char* start;
59    const char* end;
60    int         len;
61    char*       fname;
62
63    start = paths;
64    end = start + strlen (paths);
65    len = strlen (name);
66
67    while (!*file_name && (start != end))
68    {
69      const char* delimiter = strchr (start, ':');
70
71      if (delimiter == NULL)
72        delimiter = end;
73
74      /*
75       * Allocate the path fragment, separator, name, terminating nul. Form the
76       * path then see if the stat call works.
77       */
78
79      fname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
80                                   (delimiter - start) + 1 + len + 1, true);
81      if (!fname)
82      {
83        rtems_rtl_set_error (ENOMEM, "no memory searching for file");
84        return false;
85      }
86
87      memcpy (fname, start, delimiter - start);
88      fname[delimiter - start] = '/';
89      memcpy (fname + (delimiter - start) + 1, name, len);
90
91      if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD))
92        printf ("rtl: find-file: path: %s\n", fname);
93
94      if (stat (fname, &sb) < 0)
95        rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, fname);
96      else
97        *file_name = fname;
98
99      start = delimiter;
100      if (start != end)
101        ++start;
102    }
103  }
104
105  if (!*file_name)
106    return false;
107
108  *size = sb.st_size;
109
110  return true;
111}
Note: See TracBrowser for help on using the repository browser.