1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2014-2020. 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 * Author: lishunda
13 * Create: 2014-02-25
14 */
15
16 #include "securec.h"
17
18 #if SECUREC_ENABLE_SNPRINTF
19 /*
20 * <FUNCTION DESCRIPTION>
21 * The snprintf_s function is equivalent to the snprintf function
22 * except for the parameter destMax/count and the explicit runtime-constraints violation
23 * The snprintf_s function formats and stores count or fewer characters in
24 * strDest and appends a terminating null. Each argument (if any) is converted
25 * and output according to the corresponding format specification in format.
26 * The formatting is consistent with the printf family of functions; If copying
27 * occurs between strings that overlap, the behavior is undefined.
28 *
29 * <INPUT PARAMETERS>
30 * strDest Storage location for the output.
31 * destMax The size of the storage location for output. Size
32 * in bytes for snprintf_s or size in words for snwprintf_s.
33 * count Maximum number of character to store.
34 * format Format-control string.
35 * ... Optional arguments.
36 *
37 * <OUTPUT PARAMETERS>
38 * strDest is updated
39 *
40 * <RETURN VALUE>
41 * return the number of characters written, not including the terminating null
42 * return -1 if an error occurs.
43 * return -1 if count < destMax and the output string has been truncated
44 *
45 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
46 *
47 */
snprintf_s(char * strDest,size_t destMax,size_t count,const char * format,...)48 int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...)
49 {
50 int ret; /* If initialization causes e838 */
51 va_list argList;
52
53 va_start(argList, format);
54 ret = vsnprintf_s(strDest, destMax, count, format, argList);
55 va_end(argList);
56 (void)argList; /* To clear e438 last value assigned not used , the compiler will optimize this code */
57
58 return ret;
59 }
60 #if SECUREC_IN_KERNEL
61 EXPORT_SYMBOL(snprintf_s);
62 #endif
63 #endif
64
65 #if SECUREC_SNPRINTF_TRUNCATED
66 /*
67 * <FUNCTION DESCRIPTION>
68 * The snprintf_truncated_s function is equivalent to the snprintf function
69 * except for the parameter destMax/count and the explicit runtime-constraints violation
70 * The snprintf_truncated_s function formats and stores count or fewer characters in
71 * strDest and appends a terminating null. Each argument (if any) is converted
72 * and output according to the corresponding format specification in format.
73 * The formatting is consistent with the printf family of functions; If copying
74 * occurs between strings that overlap, the behavior is undefined.
75 *
76 * <INPUT PARAMETERS>
77 * strDest Storage location for the output.
78 * destMax The size of the storage location for output. Size
79 * in bytes for snprintf_truncated_s or size in words for snwprintf_s.
80 * format Format-control string.
81 * ... Optional arguments.
82 *
83 * <OUTPUT PARAMETERS>
84 * strDest is updated
85 *
86 * <RETURN VALUE>
87 * return the number of characters written, not including the terminating null
88 * return -1 if an error occurs.
89 * return destMax-1 if output string has been truncated
90 *
91 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
92 *
93 */
snprintf_truncated_s(char * strDest,size_t destMax,const char * format,...)94 int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...)
95 {
96 int ret; /* If initialization causes e838 */
97 va_list argList;
98
99 va_start(argList, format);
100 ret = vsnprintf_truncated_s(strDest, destMax, format, argList);
101 va_end(argList);
102 (void)argList; /* To clear e438 last value assigned not used , the compiler will optimize this code */
103
104 return ret;
105 }
106 #if SECUREC_IN_KERNEL
107 EXPORT_SYMBOL(snprintf_truncated_s);
108 #endif
109
110 #endif
111
112