1/* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved. 3 * Licensed under Mulan PSL v2. 4 * You can use this software according to the terms and conditions of the Mulan PSL v2. 5 * You may obtain a copy of Mulan PSL v2 at: 6 * http://license.coscl.org.cn/MulanPSL2 7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 8 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 9 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 10 * See the Mulan PSL v2 for more details. 11 * Description: snprintf_s function 12 * Create: 2014-02-25 13 */ 14 15#include "securec.h" 16 17#if SECUREC_ENABLE_SNPRINTF 18/* 19 * <FUNCTION DESCRIPTION> 20 * The snprintf_s function is equivalent to the snprintf function 21 * except for the parameter destMax/count and the explicit runtime-constraints violation 22 * The snprintf_s function formats and stores count or fewer characters in 23 * strDest and appends a terminating null. Each argument (if any) is converted 24 * and output according to the corresponding format specification in format. 25 * The formatting is consistent with the printf family of functions; If copying 26 * occurs between strings that overlap, the behavior is undefined. 27 * 28 * <INPUT PARAMETERS> 29 * strDest Storage location for the output. 30 * destMax The size of the storage location for output. Size 31 * in bytes for snprintf_s or size in words for snwprintf_s. 32 * count Maximum number of character to store. 33 * format Format-control string. 34 * ... Optional arguments. 35 * 36 * <OUTPUT PARAMETERS> 37 * strDest is updated 38 * 39 * <RETURN VALUE> 40 * return the number of characters written, not including the terminating null 41 * return -1 if an error occurs. 42 * return -1 if count < destMax and the output string has been truncated 43 * 44 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid 45 * 46 */ 47int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...) 48{ 49 int ret; /* If initialization causes e838 */ 50 va_list argList; 51 52 va_start(argList, format); 53 ret = vsnprintf_s(strDest, destMax, count, format, argList); 54 va_end(argList); 55 (void)argList; /* To clear e438 last value assigned not used , the compiler will optimize this code */ 56 57 return ret; 58} 59#if SECUREC_EXPORT_KERNEL_SYMBOL 60EXPORT_SYMBOL(snprintf_s); 61#endif 62#endif 63 64#if SECUREC_SNPRINTF_TRUNCATED 65/* 66 * <FUNCTION DESCRIPTION> 67 * The snprintf_truncated_s function is equivalent to the snprintf function 68 * except for the parameter destMax/count and the explicit runtime-constraints violation 69 * The snprintf_truncated_s function formats and stores count or fewer characters in 70 * strDest and appends a terminating null. Each argument (if any) is converted 71 * and output according to the corresponding format specification in format. 72 * The formatting is consistent with the printf family of functions; If copying 73 * occurs between strings that overlap, the behavior is undefined. 74 * 75 * <INPUT PARAMETERS> 76 * strDest Storage location for the output. 77 * destMax The size of the storage location for output. Size 78 * in bytes for snprintf_truncated_s or size in words for snwprintf_s. 79 * format Format-control string. 80 * ... Optional arguments. 81 * 82 * <OUTPUT PARAMETERS> 83 * strDest is updated 84 * 85 * <RETURN VALUE> 86 * return the number of characters written, not including the terminating null 87 * return -1 if an error occurs. 88 * return destMax-1 if output string has been truncated 89 * 90 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid 91 * 92 */ 93int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...) 94{ 95 int ret; /* If initialization causes e838 */ 96 va_list argList; 97 98 va_start(argList, format); 99 ret = vsnprintf_truncated_s(strDest, destMax, format, argList); 100 va_end(argList); 101 (void)argList; /* To clear e438 last value assigned not used , the compiler will optimize this code */ 102 103 return ret; 104} 105#if SECUREC_EXPORT_KERNEL_SYMBOL 106EXPORT_SYMBOL(snprintf_truncated_s); 107#endif 108 109#endif 110 111