]> andersk Git - moira.git/blobdiff - lib/strs.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / strs.c
index 86f96c9e1dd689e1e6c77f2b47c4caf8b0149831..36598ed2f7a2fff586b85b548fd295b4497ffc5d 100644 (file)
-/*
- *     $Source$
- *     $Author$
- *     $Header$
+/* $Id$
  *
- *     Copyright (C) 1987 by the Massachusetts Institute of Technology
+ * 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 <moira.h>
 
+#include <ctype.h>
 
-/*
- * Random string functions which should be in the C library..
- */
+RCSID("$Header$");
 
-/*
- * Make a copy of a string.
- */
-char *
-strsave(s)
-    char *s;
-{
-    register int len = strlen(s) + 1;
-    register char *p = malloc((u_int)len);
-    if (p) bcopy(s, p, len);
-    return p;
-}
 /*
  * Trim whitespace off both ends of a string.
  */
-char *strtrim(s)
-    register char *s;
+char *strtrim(char *save)
 {
-    register char *t;
-    
-    while (isspace(*s)) s++;
-    /* skip to end of string */
-
-    for (t = s; *t; t++) continue; 
-    while (t > s) {
-       --t;
-       if (!isspace(*t)) {
-           t++;
-           break;
+  char *t, *s;
+
+  s = save;
+  while (isspace(*s))
+    s++;
+  /* skip to end of string */
+  if (*s == '\0')
+    {
+      if (*save)
+       *save = '\0';
+      return save;
+    }
+
+  for (t = s; *t; t++)
+    continue;
+  while (t > s)
+    {
+      --t;
+      if (!isspace(*t))
+       {
+         t++;
+         break;
        }
     }
+  if (*t)
     *t = '\0';
-    return s;
+  return s;
 }
 
-/*
- * Case insensitive string compare.
- */
 
-int cistrcmp(cp1, cp2)
-    char *cp1, *cp2;
+/* Modify a string for all of the letters to be uppercase. */
+
+char *uppercase(char *s)
+{
+  char *p;
+
+  for (p = s; *p; p++)
+    {
+      if (islower(*p))
+       *p = toupper(*p);
+    }
+  return s;
+}
+
+
+char *lowercase(char *s)
+{
+  char *p;
+
+  for (p = s; *p; p++)
+    {
+      if (isupper(*p))
+       *p = tolower(*p);
+    }
+  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)
 {
-    register int c1, c2;
-    
-    do {
-       if (isupper(c1 = (*cp1++))) c1 = tolower(c1);
-       if (isupper(c2 = (*cp2++))) c2 = tolower(c2);
-       if (c1 != c2) return c1-c2;
-    } while (c1 && c2);
-    return 0;
+  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
This page took 0.059457 seconds and 4 git commands to generate.