-/*
- * $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 lint
-
#include <mit-copyright.h>
-#include <sys/types.h>
-#include <strings.h>
+#include <moira.h>
+
#include <ctype.h>
-extern char *malloc(), *realloc();
+RCSID("$Header$");
-/*
- * Random string functions which should be in the C library..
- */
-
-/*
- * Make a copy of a string.
- */
-char *
-strsave(s)
- char *s;
-{
- register int len;
- register char *p;
- /* Kludge for sloppy string semantics */
- if (!s) {
- printf("NULL != \"\" !!!!\r\n");
- p = malloc(1);
- *p = '\0';
- return p;
- }
- len = strlen(s) + 1;
- p = malloc((u_int)len);
- if (p) bcopy(s, p, len);
- return p;
-}
/*
* Trim whitespace off both ends of a string.
*/
-char *strtrim(save)
- register char *save;
+char *strtrim(char *save)
{
- register char *t, *s;
+ char *t, *s;
- s = save;
- while (isspace(*s)) s++;
- /* skip to end of string */
- if (*s == '\0') {
+ s = save;
+ while (isspace(*s))
+ s++;
+ /* skip to end of string */
+ if (*s == '\0')
+ {
+ if (*save)
*save = '\0';
- return(save);
+ return save;
}
- for (t = s; *t; t++) continue;
- while (t > s) {
- --t;
- if (!isspace(*t)) {
- t++;
- break;
+ for (t = s; *t; t++)
+ continue;
+ while (t > s)
+ {
+ --t;
+ if (!isspace(*t))
+ {
+ t++;
+ break;
}
}
+ if (*t)
*t = '\0';
- return s;
+ return s;
}
/* Modify a string for all of the letters to be uppercase. */
-char *uppercase(s)
-char *s;
+char *uppercase(char *s)
{
- register char *p;
+ char *p;
- for (p = s; *p; p++)
+ for (p = s; *p; p++)
+ {
if (islower(*p))
*p = toupper(*p);
- return(s);
+ }
+ return s;
}
-char *lowercase(s)
-char *s;
+char *lowercase(char *s)
{
- register char *p;
+ char *p;
- for (p = s; *p; p++)
+ for (p = s; *p; p++)
+ {
if (isupper(*p))
*p = tolower(*p);
- return(s);
+ }
+ 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