Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/util/key_val.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2010-2012, 2014-2015 Todd C. Miller <[email protected]>
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
{
35
size_t key_len = strlen(key);
36
size_t val_len = strlen(val);
37
char *cp, *str;
38
debug_decl(sudo_new_key_val, SUDO_DEBUG_UTIL);
39
40
cp = str = malloc(key_len + 1 + val_len + 1);
41
if (cp != NULL) {
42
memcpy(cp, key, key_len);
43
cp += key_len;
44
*cp++ = '=';
45
memcpy(cp, val, val_len);
46
cp += val_len;
47
*cp = '\0';
48
}
49
50
debug_return_str(str);
51
}
52
53