github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/c/c11_support.c (about)

     1  /*
     2   Copyright 2017 Intel Corporation
     3  
     4   Licensed under the Apache License, Version 2.0 (the "License");
     5   you may not use this file except in compliance with the License.
     6   You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10   Unless required by applicable law or agreed to in writing, software
    11   distributed under the License is distributed on an "AS IS" BASIS,
    12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   See the License for the specific language governing permissions and
    14   limitations under the License.
    15  ------------------------------------------------------------------------------
    16  */
    17  
    18  #include <errno.h>
    19  #include "c11_support.h"
    20  
    21  #ifndef __STDC_LIB_EXT1__
    22  int strncpy_s(char *dest, size_t sizeInBytes, const char *src, size_t count)
    23  {
    24      if (count == 0 && dest == NULL && sizeInBytes == 0) {
    25          return 0;
    26      }
    27  
    28      if (dest == NULL || sizeInBytes <= 0) {
    29          return EINVAL;
    30      }
    31  
    32      if (count == 0) {
    33          *dest = 0;
    34          return 0;
    35      }
    36  
    37      if (src == NULL) {
    38          *dest = 0;
    39          return EINVAL;
    40      }
    41  
    42      char *p = dest;
    43      size_t availableSize = sizeInBytes;
    44  
    45      if (count == ((size_t) - 1)) {
    46          while ((*p++ = *src++) != 0 && --availableSize > 0);
    47      } else {
    48          while ((*p++ = *src++) != 0 && --availableSize > 0 && --count > 0);
    49          if (count == 0) {
    50              p = NULL;
    51          }
    52      }
    53  
    54      if (availableSize == 0)
    55      {
    56          if (count == ((size_t) - 1))
    57          {
    58              dest[sizeInBytes - 1] = 0;
    59              return STRUNCATE;
    60          }
    61          *dest = 0;
    62          return ERANGE;
    63      }
    64      return 0;
    65  }
    66  
    67  #endif // #ifdef __STDC_LIB_EXT1__