-/*
- * $Source$
- * $Author$
- * $Header$
+/* $Id$
*
- * Copyright (C) 1987 by the Massachusetts Institute of Technology
- * For copying and distribution information, please see the file
- * <mit-copyright.h>.
+ * Miscellaneous string functions.
*
- * Miscellaneous string functions.
+ * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
*/
-#ifndef lint
-static char *rcsid_strs_c = "$Header$";
-#endif
-
#include <mit-copyright.h>
-#include <sys/types.h>
-#include <stdlib.h>
-#include <string.h>
+#include <moira.h>
+
#include <ctype.h>
-/*
- * Random string functions which should be in the C library..
- */
-
-/*
- * Make a copy of a string.
- */
-char *strsave(char *s)
-{
- int len;
- char *p;
- /* Kludge for sloppy string semantics */
- if (!s)
- {
- p = malloc(1);
- *p = '\0';
- return p;
- }
- len = strlen(s) + 1;
- p = malloc(len);
- if (p)
- memcpy(p, s, len);
- return p;
-}
+RCSID("$Header$");
/*
* Trim whitespace off both ends of a string.
/* skip to end of string */
if (*s == '\0')
{
- *save = '\0';
+ if (*save)
+ *save = '\0';
return save;
}
}
return s;
}
+
+#ifndef HAVE_STRLCPY
+/* Copy as much of SRC will fit into a DST of size SIZE, always
+ * NUL-terminating. (Originally from OpenBSD.)
+ */
+size_t strlcpy(char *dst, const char *src, size_t size)
+{
+ size_t len = strlen(src);
+
+ if (len < size)
+ memcpy(dst, src, len + 1);
+ else
+ {
+ memcpy(dst, src, size - 1);
+ dst[size - 1] = '\0';
+ }
+ return len;
+}
+#endif
+
+#ifndef HAVE_STRLCAT
+/* Catenate as must of SRC will fit onto the end of DST, which is
+ * in a buffer of size SIZE, always NUL-terminating. (Originally
+ * from OpenBSD.)
+ */
+size_t strlcat(char *dst, const char *src, size_t size)
+{
+ size_t dlen = strlen(dst);
+ size_t slen = strlen(src);
+
+ if (dlen + slen < size)
+ memcpy(dst + dlen, src, slen + 1);
+ else
+ {
+ memcpy(dst + dlen, src, size - dlen - 1);
+ dst[size - 1] = '\0';
+ }
+ return dlen + slen;
+}
+#endif