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