/src/sudo/lib/util/key_val.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * SPDX-License-Identifier: ISC  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 2010-2012, 2014-2015 Todd C. Miller <Todd.Miller@sudo.ws>  | 
5  |  |  *  | 
6  |  |  * Permission to use, copy, modify, and distribute this software for any  | 
7  |  |  * purpose with or without fee is hereby granted, provided that the above  | 
8  |  |  * copyright notice and this permission notice appear in all copies.  | 
9  |  |  *  | 
10  |  |  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
11  |  |  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
12  |  |  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
13  |  |  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
14  |  |  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
15  |  |  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
16  |  |  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
17  |  |  */  | 
18  |  |  | 
19  |  | #include <config.h>  | 
20  |  |  | 
21  |  | #include <stdlib.h>  | 
22  |  | #include <string.h>  | 
23  |  |  | 
24  |  | #include <sudo_compat.h>  | 
25  |  | #include <sudo_debug.h>  | 
26  |  | #include <sudo_util.h>  | 
27  |  |  | 
28  |  | /*  | 
29  |  |  * Create a new key=value pair and return it.  | 
30  |  |  * The caller is responsible for freeing the string.  | 
31  |  |  */  | 
32  |  | char *  | 
33  |  | sudo_new_key_val_v1(const char *key, const char *val)  | 
34  | 158k  | { | 
35  | 158k  |     size_t key_len = strlen(key);  | 
36  | 158k  |     size_t val_len = strlen(val);  | 
37  | 158k  |     char *cp, *str;  | 
38  | 158k  |     debug_decl(sudo_new_key_val, SUDO_DEBUG_UTIL);  | 
39  |  |  | 
40  | 158k  |     cp = str = malloc(key_len + 1 + val_len + 1);  | 
41  | 158k  |     if (cp != NULL) { | 
42  | 158k  |   memcpy(cp, key, key_len);  | 
43  | 158k  |   cp += key_len;  | 
44  | 158k  |   *cp++ = '=';  | 
45  | 158k  |   memcpy(cp, val, val_len);  | 
46  | 158k  |   cp += val_len;  | 
47  | 158k  |   *cp = '\0';  | 
48  | 158k  |     }  | 
49  |  |  | 
50  | 158k  |     debug_return_str(str);  | 
51  | 158k  | }  |