|
Packit |
fd8b60 |
/* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* Permission to use, copy, modify, and distribute this software for any
|
|
Packit |
fd8b60 |
* purpose with or without fee is hereby granted, provided that the above
|
|
Packit |
fd8b60 |
* copyright notice and this permission notice appear in all copies.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
Packit |
fd8b60 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
Packit |
fd8b60 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
Packit |
fd8b60 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
Packit |
fd8b60 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
Packit |
fd8b60 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
Packit |
fd8b60 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Provide strlcpy and strlcat for platforms that don't have them. */
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#include "k5-platform.h"
|
|
Packit |
fd8b60 |
#include <sys/types.h>
|
|
Packit |
fd8b60 |
#include <string.h>
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copy src to string dst of size siz. At most siz-1 characters
|
|
Packit |
fd8b60 |
* will be copied. Always NUL terminates (unless siz == 0).
|
|
Packit |
fd8b60 |
* Returns strlen(src); if retval >= siz, truncation occurred.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
size_t
|
|
Packit |
fd8b60 |
krb5int_strlcpy(char *dst, const char *src, size_t siz)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
char *d = dst;
|
|
Packit |
fd8b60 |
const char *s = src;
|
|
Packit |
fd8b60 |
size_t n = siz;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Copy as many bytes as will fit */
|
|
Packit |
fd8b60 |
if (n != 0) {
|
|
Packit |
fd8b60 |
while (--n != 0) {
|
|
Packit |
fd8b60 |
if ((*d++ = *s++) == '\0')
|
|
Packit |
fd8b60 |
break;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Not enough room in dst, add NUL and traverse rest of src */
|
|
Packit |
fd8b60 |
if (n == 0) {
|
|
Packit |
fd8b60 |
if (siz != 0)
|
|
Packit |
fd8b60 |
*d = '\0'; /* NUL-terminate dst */
|
|
Packit |
fd8b60 |
while (*s++)
|
|
Packit |
fd8b60 |
;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
return(s - src - 1); /* count does not include NUL */
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Appends src to string dst of size siz (unlike strncat, siz is the
|
|
Packit |
fd8b60 |
* full size of dst, not space left). At most siz-1 characters
|
|
Packit |
fd8b60 |
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
|
|
Packit |
fd8b60 |
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
|
|
Packit |
fd8b60 |
* If retval >= siz, truncation occurred.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
size_t
|
|
Packit |
fd8b60 |
krb5int_strlcat(char *dst, const char *src, size_t siz)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
char *d = dst;
|
|
Packit |
fd8b60 |
const char *s = src;
|
|
Packit |
fd8b60 |
size_t n = siz;
|
|
Packit |
fd8b60 |
size_t dlen;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Find the end of dst and adjust bytes left but don't go past end */
|
|
Packit |
fd8b60 |
while (n-- != 0 && *d != '\0')
|
|
Packit |
fd8b60 |
d++;
|
|
Packit |
fd8b60 |
dlen = d - dst;
|
|
Packit |
fd8b60 |
n = siz - dlen;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (n == 0)
|
|
Packit |
fd8b60 |
return(dlen + strlen(s));
|
|
Packit |
fd8b60 |
while (*s != '\0') {
|
|
Packit |
fd8b60 |
if (n != 1) {
|
|
Packit |
fd8b60 |
*d++ = *s;
|
|
Packit |
fd8b60 |
n--;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
s++;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
*d = '\0';
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
return(dlen + (s - src)); /* count does not include NUL */
|
|
Packit |
fd8b60 |
}
|