1 f224a3fe 2021-08-24 xhr /* Author: Tatu Ylonen <ylo@cs.hut.fi>
2 f224a3fe 2021-08-24 xhr * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 f224a3fe 2021-08-24 xhr * All rights reserved
4 f224a3fe 2021-08-24 xhr * Versions of malloc and friends that check their results, and never return
5 f224a3fe 2021-08-24 xhr * failure (they call fatal if they encounter an error).
7 f224a3fe 2021-08-24 xhr * As far as I am concerned, the code I have written for this software
8 f224a3fe 2021-08-24 xhr * can be used freely for any purpose. Any derived versions of this
9 f224a3fe 2021-08-24 xhr * software must be clearly marked as such, and if the derived work is
10 f224a3fe 2021-08-24 xhr * incompatible with the protocol description in the RFC file, it must be
11 f224a3fe 2021-08-24 xhr * called by a name other than "ssh" or "Secure Shell".
14 f224a3fe 2021-08-24 xhr #include <sys/types.h>
16 f224a3fe 2021-08-24 xhr #include <stddef.h>
17 f224a3fe 2021-08-24 xhr #include <stdlib.h>
18 f224a3fe 2021-08-24 xhr #include <string.h>
20 f224a3fe 2021-08-24 xhr #include "log.h"
21 f224a3fe 2021-08-24 xhr #include "twind.h"
24 f224a3fe 2021-08-24 xhr xmalloc(size_t size)
28 f224a3fe 2021-08-24 xhr if (size == 0)
29 f224a3fe 2021-08-24 xhr fatal("xmalloc: zero size");
30 cebc5223 2021-08-25 xhr ptr = calloc(1, size);
31 f224a3fe 2021-08-24 xhr if (ptr == NULL)
32 f224a3fe 2021-08-24 xhr fatal("xmalloc: out of memory (allocating %zu bytes)", size);
37 f224a3fe 2021-08-24 xhr xstrdup(const char *str)
42 f224a3fe 2021-08-24 xhr len = strlen(str) + 1;
43 f224a3fe 2021-08-24 xhr cp = xmalloc(len);
44 f224a3fe 2021-08-24 xhr strlcpy(cp, str, len);
49 f224a3fe 2021-08-24 xhr * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
51 f224a3fe 2021-08-24 xhr * Permission to use, copy, modify, and distribute this software for any
52 f224a3fe 2021-08-24 xhr * purpose with or without fee is hereby granted, provided that the above
53 f224a3fe 2021-08-24 xhr * copyright notice and this permission notice appear in all copies.
55 f224a3fe 2021-08-24 xhr * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
56 f224a3fe 2021-08-24 xhr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
57 f224a3fe 2021-08-24 xhr * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
58 f224a3fe 2021-08-24 xhr * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
59 f224a3fe 2021-08-24 xhr * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
60 f224a3fe 2021-08-24 xhr * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
61 f224a3fe 2021-08-24 xhr * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
65 f224a3fe 2021-08-24 xhr #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) &&\
66 f224a3fe 2021-08-24 xhr !defined(__DragonFly__)
68 f224a3fe 2021-08-24 xhr * Copy string src to buffer dst of size dsize. At most dsize-1
69 f224a3fe 2021-08-24 xhr * chars will be copied. Always NUL terminates (unless dsize == 0).
70 f224a3fe 2021-08-24 xhr * Returns strlen(src); if retval >= dsize, truncation occurred.
73 f224a3fe 2021-08-24 xhr strlcpy(char *dst, const char *src, size_t dsize)
75 f224a3fe 2021-08-24 xhr const char *osrc = src;
76 f224a3fe 2021-08-24 xhr size_t nleft = dsize;
78 f224a3fe 2021-08-24 xhr /* Copy as many bytes as will fit. */
79 f224a3fe 2021-08-24 xhr if (nleft != 0) {
80 f224a3fe 2021-08-24 xhr while (--nleft != 0) {
81 f224a3fe 2021-08-24 xhr if ((*dst++ = *src++) == '\0')
86 f224a3fe 2021-08-24 xhr /* Not enough room in dst, add NUL and traverse rest of src. */
87 f224a3fe 2021-08-24 xhr if (nleft == 0) {
88 f224a3fe 2021-08-24 xhr if (dsize != 0)
89 f224a3fe 2021-08-24 xhr *dst = '\0'; /* NUL-terminate dst */
90 f224a3fe 2021-08-24 xhr while (*src++)
94 f224a3fe 2021-08-24 xhr return(src - osrc - 1); /* count does not include NUL */
96 f224a3fe 2021-08-24 xhr #endif /* __BSD__ */