Mercurial > libbuf
comparison buf-dup.c @ 0:b1991ee4451d
misc: initial import
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 29 Oct 2020 17:24:30 +0100 |
parents | |
children | 939fe74cd80a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b1991ee4451d |
---|---|
1 /* | |
2 * buf-dup.c -- simple string buffer for C | |
3 * | |
4 * Copyright (c) 2019-2020 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or 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 <assert.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 | |
23 #include "buf.h" | |
24 | |
25 bool | |
26 buf_dup(struct buf *b, const struct buf *src) | |
27 { | |
28 assert(b); | |
29 assert(src); | |
30 | |
31 if (!src->data) | |
32 return true; | |
33 if (!(b->data = BUF_MALLOC(src->length + 1))) | |
34 return false; | |
35 | |
36 strcpy(b->data, src->data); | |
37 b->capacity = src->length; | |
38 b->length = src->length; | |
39 | |
40 return true; | |
41 } |