source: rtems/cpukit/score/src/objectgetnameasstring.c

Last change on this file was 4a0e418, checked in by Joel Sherrill <joel@…>, on 02/16/22 at 21:09:20

score/src/[n-s]*.c: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup RTEMSScoreObject
7 *
8 * @brief This source file contains the implementation of
9 *   _Objects_Name_to_string() and _Objects_Get_name_as_string().
10 */
11
12/*
13 *  COPYRIGHT (c) 1989-2008.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifdef HAVE_CONFIG_H
39#include "config.h"
40#endif
41
42#include <rtems/score/threadimpl.h>
43
44/*
45 * Do not use isprint() from <ctypes.h> since this depends on the heavy weight
46 * C locale support of Newlib.
47 */
48static bool _Objects_Name_char_is_printable( char c )
49{
50  unsigned char uc;
51
52  uc = (unsigned char) c;
53  return uc >= ' ' && uc <= '~';
54}
55
56size_t _Objects_Name_to_string(
57  Objects_Name  name,
58  bool          is_string,
59  char         *buffer,
60  size_t        buffer_size
61)
62{
63  char        lname[ 5 ];
64  const char *s;
65  char       *d;
66  size_t      i;
67
68  if ( is_string ) {
69    s = name.name_p;
70  } else {
71    lname[ 0 ] = (name.name_u32 >> 24) & 0xff;
72    lname[ 1 ] = (name.name_u32 >> 16) & 0xff;
73    lname[ 2 ] = (name.name_u32 >>  8) & 0xff;
74    lname[ 3 ] = (name.name_u32 >>  0) & 0xff;
75    lname[ 4 ] = '\0';
76    s = lname;
77  }
78
79  d = buffer;
80  i = 1;
81
82  if ( s != NULL ) {
83    while ( *s != '\0' ) {
84      if ( i < buffer_size ) {
85        *d = _Objects_Name_char_is_printable(*s) ? *s : '*';
86        ++d;
87      }
88
89      ++s;
90      ++i;
91    }
92  }
93
94  if ( buffer_size > 0 ) {
95    *d = '\0';
96  }
97
98  return i - 1;
99}
100
101/*
102 *  This method objects the name of an object and returns its name
103 *  in the form of a C string.  It attempts to be careful about
104 *  overflowing the user's string and about returning unprintable characters.
105 */
106
107char *_Objects_Get_name_as_string(
108  Objects_Id        id,
109  size_t            length,
110  char             *name
111)
112{
113  const Objects_Information *information;
114  const Objects_Control     *the_object;
115  ISR_lock_Context           lock_context;
116  Objects_Id                 tmpId;
117
118  if ( length == 0 )
119    return NULL;
120
121  if ( name == NULL )
122    return NULL;
123
124  tmpId = (id == OBJECTS_ID_OF_SELF) ? _Thread_Get_executing()->Object.id : id;
125
126  information = _Objects_Get_information_id( tmpId );
127  if ( !information )
128    return NULL;
129
130  the_object = _Objects_Get( tmpId, &lock_context, information );
131  if ( the_object == NULL ) {
132    return NULL;
133  }
134
135  _Objects_Name_to_string(
136    the_object->name,
137    _Objects_Has_string_name( information ),
138    name,
139    length
140  );
141
142  _ISR_lock_ISR_enable( &lock_context );
143  return name;
144}
Note: See TracBrowser for help on using the repository browser.