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

4.115
Last change on this file since 3feb372 was ae5fe7e6, checked in by Chris Johns <chrisj@…>, on 10/27/14 at 01:09:41

cpukit: Add libdl with the Runtime Loader (RTL) code.

This is a merge of the RTL project.

  • Property mode set to 100644
File size: 2.3 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.com/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 <unistd.h>
26
27#include <rtems/libio_.h>
28
29#include <rtems/rtl/rtl.h>
30#include "rtl-find-file.h"
31#include "rtl-error.h"
32#include "rtl-string.h"
33#include "rtl-trace.h"
34
35#if WAF_BUILD
36#define rtems_filesystem_is_delimiter rtems_filesystem_is_separator
37#endif
38
39bool
40rtems_rtl_find_file (const char*  name,
41                     const char*  paths,
42                     const char** file_name,
43                     size_t*      size)
44{
45  struct stat sb;
46
47  *file_name = NULL;
48  *size = 0;
49
50  if (rtems_filesystem_is_delimiter (name[0]) || (name[0] == '.'))
51  {
52    if (stat (name, &sb) == 0)
53      *file_name = rtems_rtl_strdup (name);
54  }
55  else if (paths)
56  {
57    const char* start;
58    const char* end;
59    int         len;
60    char*       fname;
61
62    start = paths;
63    end = start + strlen (paths);
64    len = strlen (name);
65
66    while (!*file_name && (start != end))
67    {
68      const char* delimiter = strchr (start, ':');
69
70      if (delimiter == NULL)
71        delimiter = end;
72
73      /*
74       * Allocate the path fragment, separator, name, terminating nul. Form the
75       * path then see if the stat call works.
76       */
77
78      fname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
79                                   (delimiter - start) + 1 + len + 1, true);
80      if (!fname)
81      {
82        rtems_rtl_set_error (ENOMEM, "no memory searching for file");
83        return false;
84      }
85
86      memcpy (fname, start, delimiter - start);
87      fname[delimiter - start] = '/';
88      memcpy (fname + (delimiter - start) + 1, name, len);
89
90      if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD))
91        printf ("rtl: find-file: path: %s\n", fname);
92
93      if (stat (fname, &sb) < 0)
94        rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, fname);
95      else
96        *file_name = fname;
97
98      start = delimiter;
99      if (start != end)
100        ++start;
101    }
102  }
103
104  if (!*file_name)
105    return false;
106
107  *size = sb.st_size;
108
109  return true;
110}
Note: See TracBrowser for help on using the repository browser.