Home | History | Annotate | Download | only in String
      1 /** @file
      2     Concatenation 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  <Uefi.h>
     14 #include  <Library/BaseLib.h>
     15 
     16 #include  <LibConfig.h>
     17 
     18 #include  <string.h>
     19 
     20 /** The strcat function appends a copy of the string pointed to by s2
     21     (including the terminating null character) to the end of the string pointed
     22     to by s1. The initial character of s2 overwrites the null character at the
     23     end of s1. If copying takes place between objects that overlap, the
     24     behavior is undefined.
     25 
     26     @return   The strcat function returns the value of s1.
     27 **/
     28 char *
     29 strcat(char * __restrict s1, const char * __restrict s2)
     30 {
     31   return AsciiStrCat( s1, s2);
     32 }
     33 
     34 /** The strncat function appends not more than n characters (a null character
     35     and characters that follow it are not appended) from the array pointed to
     36     by s2 to the end of the string pointed to by s1. The initial character of
     37     s2 overwrites the null character at the end of s1. A terminating null
     38     character is always appended to the result. If copying takes place
     39     between objects that overlap, the behavior is undefined.
     40 
     41     @return   The strncat function returns the value of s1.
     42 **/
     43 char *
     44 strncat(char * __restrict s1, const char * __restrict s2, size_t n)
     45 {
     46   return AsciiStrnCat( s1, s2, n);
     47 }
     48 
     49 /** The strncatX function appends not more than n characters (a null character
     50     and characters that follow it are not appended) from the array pointed to
     51     by s2 to the end of the string pointed to by s1. The initial character of
     52     s2 overwrites the null character at the end of s1. The result is always
     53     terminated with a null character. If copying takes place between objects
     54     that overlap, the behavior is undefined.
     55 
     56     strncatX exists because normal strncat does not indicate if the operation
     57     was terminated because of exhausting n or reaching the end of s2.
     58 
     59     @return   The strncatX function returns 0 if the operation was terminated
     60               because it reached the end of s1.  Otherwise, a non-zero value is
     61               returned indicating how many characters remain in s1.
     62 **/
     63 int
     64 strncatX(char * __restrict s1, const char * __restrict s2, size_t n)
     65 {
     66   int NumLeft;
     67 
     68   // Find s1's terminating NUL
     69   for( ; n != 0; --n) {
     70     if( *s1++ == '\0')  break;
     71   }
     72 
     73   // Now copy *s2 into s1, overwriting s1's terminating NUL
     74   for( --s1; n != 0; --n) {
     75     if((*s1++ = *s2++) == '\0')  break;
     76   }
     77   NumLeft = (int)n;
     78 
     79   // Guarantee that s1 is NUL terminated.
     80   *--s1 = '\0';
     81 
     82   return NumLeft;   // Zero if we ran out of buffer ( strlen(s1) < strlen(s2) )
     83 }
     84