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