source: rtems/cpukit/libmisc/stringto/stringtounsignedchar.c @ 0b416759

5
Last change on this file since 0b416759 was c499856, checked in by Chris Johns <chrisj@…>, on Mar 20, 2014 at 9:10:47 PM

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 1.3 KB
Line 
1/**
2 * @file
3 *
4 * @brief Convert String to Unsigned Character (with validation)
5 * @ingroup libmisc_conv_help Conversion Helpers
6 */
7
8/*
9 *  COPYRIGHT (c) 2009.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  Copyright (c) 2011  Ralf Corsépius, Ulm, Germany.
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <errno.h>
24#include <stdlib.h>
25#include <limits.h>
26
27#include <rtems/stringto.h>
28
29/*
30 *  Instantiate an error checking wrapper for strtoul (unsigned char)
31 */
32
33rtems_status_code rtems_string_to_unsigned_char (
34  const char *s,
35  unsigned char *n,
36  char **endptr,
37  int base
38)
39{
40  unsigned long result;
41  char *end;
42
43  if ( !n )
44    return RTEMS_INVALID_ADDRESS;
45
46  errno = 0;
47  *n = 0;
48
49  result = strtoul( s, &end, base );
50
51  if ( endptr )
52    *endptr = end;
53
54  if ( end == s )
55    return RTEMS_NOT_DEFINED;
56
57  if ( ( errno == ERANGE ) &&
58    (( result == 0 ) || ( result == ULONG_MAX )))
59      return RTEMS_INVALID_NUMBER;
60
61#if (UCHAR_MAX < ULONG_MAX)
62  if ( result > UCHAR_MAX ) {
63    errno = ERANGE;
64    return RTEMS_INVALID_NUMBER;
65  }
66#endif
67
68  *n = result;
69
70  return RTEMS_SUCCESSFUL;
71}
Note: See TracBrowser for help on using the repository browser.