summaryrefslogtreecommitdiff
path: root/StdLib/LibC/String/Misc.c
blob: 99328252ed5023c107fb6d5c5fe2a6d6c038e00d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/** @file
    Miscellaneous Functions for <string.h>.

    Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
    This program and the accompanying materials are licensed and made available under
    the terms and conditions of the BSD License that accompanies this distribution.
    The full text of the license may be found at
    http://opensource.org/licenses/bsd-license.php.

    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
//#include  <sys/EfiCdefs.h>

#include  <Uefi.h>
#include  <Library/BaseLib.h>
#include  <Library/BaseMemoryLib.h>
#include  <Library/PcdLib.h>
#include  <Library/PrintLib.h>

#include  <LibConfig.h>

#include  <errno.h>
#include  <limits.h>
#include  <string.h>

extern char *sys_errlist[];

/** The memset function copies the value of c (converted to an unsigned char)
    into each of the first n characters of the object pointed to by s.

    @return   The memset function returns the value of s.
**/
void *
memset(void *s, int c, size_t n)
{
  return SetMem( s, (UINTN)n, (UINT8)c);
}

int
strerror_r(int errnum, char *buf, size_t buflen)
{
  const char   *estring;
  INTN          i;
  int           retval = 0;

  if( (errnum < 0) || (errnum >= EMAXERRORVAL)) {
    (void) AsciiSPrint( buf, ASCII_STRING_MAX, "Unknown Error: %d.", errnum);
    retval = EINVAL;
  }
  else {
    estring = sys_errlist[errnum];
    for( i = buflen; i > 0; --i) {
      if( (*buf++ = *estring++) == '\0') {
        break;
      }
    }
    if(i == 0) {
      retval = ERANGE;
    }
  }
  return retval;
}

/** The strerror function maps the number in errnum to a message string.
    Typically, the values for errnum come from errno, but strerror shall map
    any value of type int to a message.

    The implementation shall behave as if no library function calls the
    strerror function.

    @return   The strerror function returns a pointer to the string, the
              contents of which are locale specific.  The array pointed to
              shall not be modified by the program, but may be overwritten by
              a subsequent call to the strerror function.
**/
char *
strerror(int errnum)
{
  static char errorbuf[ASCII_STRING_MAX];
  int         status;

  status = strerror_r(errnum, errorbuf, sizeof(errorbuf));
  if(status != 0) {
    errno = status;
  }
  return errorbuf;
}

/** The strlen function computes the length of the string pointed to by s.

    @return   The strlen function returns the number of characters that
              precede the terminating null character.
**/
size_t
strlen(const char *s)
{
  return (size_t)AsciiStrLen( s);
}