1 /*******************************************************************************
3 * Module Name: utnonansi - Non-ansi C library functions
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utnonansi")
51 * Non-ANSI C library functions - strlwr, strupr, stricmp, and "safe"
54 /*******************************************************************************
56 * FUNCTION: acpi_ut_strlwr (strlwr)
58 * PARAMETERS: src_string - The source string to convert
62 * DESCRIPTION: Convert a string to lowercase
64 ******************************************************************************/
65 void acpi_ut_strlwr(char *src_string
)
69 ACPI_FUNCTION_ENTRY();
75 /* Walk entire string, lowercasing the letters */
77 for (string
= src_string
; *string
; string
++) {
78 *string
= (char)tolower((int)*string
);
82 /*******************************************************************************
84 * FUNCTION: acpi_ut_strupr (strupr)
86 * PARAMETERS: src_string - The source string to convert
90 * DESCRIPTION: Convert a string to uppercase
92 ******************************************************************************/
94 void acpi_ut_strupr(char *src_string
)
98 ACPI_FUNCTION_ENTRY();
104 /* Walk entire string, uppercasing the letters */
106 for (string
= src_string
; *string
; string
++) {
107 *string
= (char)toupper((int)*string
);
111 /******************************************************************************
113 * FUNCTION: acpi_ut_stricmp (stricmp)
115 * PARAMETERS: string1 - first string to compare
116 * string2 - second string to compare
118 * RETURN: int that signifies string relationship. Zero means strings
121 * DESCRIPTION: Case-insensitive string compare. Implementation of the
122 * non-ANSI stricmp function.
124 ******************************************************************************/
126 int acpi_ut_stricmp(char *string1
, char *string2
)
132 c1
= tolower((int)*string1
);
133 c2
= tolower((int)*string2
);
138 while ((c1
== c2
) && (c1
));
143 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
144 /*******************************************************************************
146 * FUNCTION: acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
148 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
149 * functions. This is the size of the Destination buffer.
151 * RETURN: TRUE if the operation would overflow the destination buffer.
153 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
154 * the result of the operation will not overflow the output string
157 * NOTE: These functions are typically only helpful for processing
158 * user input and command lines. For most ACPICA code, the
159 * required buffer length is precisely calculated before buffer
160 * allocation, so the use of these functions is unnecessary.
162 ******************************************************************************/
164 u8
acpi_ut_safe_strcpy(char *dest
, acpi_size dest_size
, char *source
)
167 if (strlen(source
) >= dest_size
) {
171 strcpy(dest
, source
);
175 u8
acpi_ut_safe_strcat(char *dest
, acpi_size dest_size
, char *source
)
178 if ((strlen(dest
) + strlen(source
)) >= dest_size
) {
182 strcat(dest
, source
);
187 acpi_ut_safe_strncat(char *dest
,
189 char *source
, acpi_size max_transfer_length
)
191 acpi_size actual_transfer_length
;
193 actual_transfer_length
= ACPI_MIN(max_transfer_length
, strlen(source
));
195 if ((strlen(dest
) + actual_transfer_length
) >= dest_size
) {
199 strncat(dest
, source
, max_transfer_length
);