]> granicus.if.org Git - sudo/blob - lib/util/key_val.c
9bbd0436b6b07f8d4a7e9e692ceb0e8ae82ef0ec
[sudo] / lib / util / key_val.c
1 /*
2  * Copyright (c) 2010-2012, 2014-2015 Todd C. Miller <Todd.Miller@sudo.ws>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /*
18  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
19  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
20  */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef HAVE_STRING_H
29 # include <string.h>
30 #endif /* HAVE_STRING_H */
31 #ifdef HAVE_STRINGS_H
32 # include <strings.h>
33 #endif /* HAVE_STRINGS_H */
34
35 #include "sudo_compat.h"
36 #include "sudo_debug.h"
37 #include "sudo_util.h"
38
39 /*
40  * Create a new key=value pair and return it.
41  * The caller is responsible for freeing the string.
42  */
43 char *
44 sudo_new_key_val_v1(const char *key, const char *val)
45 {
46     size_t key_len = strlen(key);
47     size_t val_len = strlen(val);
48     char *cp, *str;
49     debug_decl(sudo_new_key_val, SUDO_DEBUG_UTIL)
50
51     cp = str = malloc(key_len + 1 + val_len + 1);
52     if (cp != NULL) {
53         memcpy(cp, key, key_len);
54         cp += key_len;
55         *cp++ = '=';
56         memcpy(cp, val, val_len);
57         cp += val_len;
58         *cp = '\0';
59     }
60
61     debug_return_str(str);
62 }