• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @file
2     Miscellaneous Functions for <string.h>.
3 
4     Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
5     This program and the accompanying materials are licensed and made available under
6     the terms and conditions of the BSD License that accompanies this distribution.
7     The full text of the license may be found at
8     http://opensource.org/licenses/bsd-license.php.
9 
10     THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11     WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 //#include  <sys/EfiCdefs.h>
14 
15 #include  <Uefi.h>
16 #include  <Library/BaseLib.h>
17 #include  <Library/BaseMemoryLib.h>
18 #include  <Library/PcdLib.h>
19 #include  <Library/PrintLib.h>
20 
21 #include  <LibConfig.h>
22 
23 #include  <errno.h>
24 #include  <limits.h>
25 #include  <string.h>
26 
27 extern char *sys_errlist[];
28 
29 #if !((defined(MDE_CPU_ARM) || defined(MDE_CPU_AARCH64)) && defined(__GNUC__))
30 /** The memset function copies the value of c (converted to an unsigned char)
31     into each of the first n characters of the object pointed to by s.
32 
33     @return   The memset function returns the value of s.
34 **/
35 void *
memset(void * s,int c,size_t n)36 memset(void *s, int c, size_t n)
37 {
38   return SetMem( s, (UINTN)n, (UINT8)c);
39 }
40 #endif
41 
42 int
strerror_r(int errnum,char * buf,size_t buflen)43 strerror_r(int errnum, char *buf, size_t buflen)
44 {
45   const char   *estring;
46   INTN          i;
47   int           retval = 0;
48 
49   if( (errnum < 0) || (errnum >= EMAXERRORVAL)) {
50     (void) AsciiSPrint( buf, ASCII_STRING_MAX, "Unknown Error: %d.", errnum);
51     retval = EINVAL;
52   }
53   else {
54     estring = sys_errlist[errnum];
55     for( i = buflen; i > 0; --i) {
56       if( (*buf++ = *estring++) == '\0') {
57         break;
58       }
59     }
60     if(i == 0) {
61       retval = ERANGE;
62     }
63   }
64   return retval;
65 }
66 
67 /** The strerror function maps the number in errnum to a message string.
68     Typically, the values for errnum come from errno, but strerror shall map
69     any value of type int to a message.
70 
71     The implementation shall behave as if no library function calls the
72     strerror function.
73 
74     @return   The strerror function returns a pointer to the string, the
75               contents of which are locale specific.  The array pointed to
76               shall not be modified by the program, but may be overwritten by
77               a subsequent call to the strerror function.
78 **/
79 char *
strerror(int errnum)80 strerror(int errnum)
81 {
82   static char errorbuf[ASCII_STRING_MAX];
83   int         status;
84 
85   status = strerror_r(errnum, errorbuf, sizeof(errorbuf));
86   if(status != 0) {
87     errno = status;
88   }
89   return errorbuf;
90 }
91 
92 /** The strlen function computes the length of the string pointed to by s.
93 
94     @return   The strlen function returns the number of characters that
95               precede the terminating null character.
96 **/
97 size_t
strlen(const char * s)98 strlen(const char *s)
99 {
100   return (size_t)AsciiStrLen( s);
101 }
102