]> andersk Git - moira.git/blobdiff - lib/strs.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / strs.c
index 65ad3f5ad364490675d61c68921252904a9a261b..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 <sys/types.h>
-#include <strings.h>
 #include <ctype.h>
 
-extern char *malloc(), *realloc();
+RCSID("$Header$");
 
 /*
- * Random string functions which should be in the C library..
+ * Trim whitespace off both ends of a string.
  */
+char *strtrim(char *save)
+{
+  char *t, *s;
 
-/*
- * Make a copy of a string.
- */
-char *
-strsave(s)
-    char *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;
+}
+
+
+/* Modify a string for all of the letters to be uppercase. */
+
+char *uppercase(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;
+  char *p;
+
+  for (p = s; *p; p++)
+    {
+      if (islower(*p))
+       *p = toupper(*p);
     }
-    len = strlen(s) + 1;
-    p = malloc((u_int)len);
-    if (p) bcopy(s, p, len);
-    return p;
+  return s;
 }
-/*
- * Trim whitespace off both ends of a string.
- */
-char *strtrim(s)
-    register char *s;
+
+
+char *lowercase(char *s)
 {
-    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 *p;
+
+  for (p = s; *p; p++)
+    {
+      if (isupper(*p))
+       *p = tolower(*p);
     }
-    *t = '\0';
-    return s;
+  return s;
 }
 
-/*
- * Case insensitive string compare.
+#ifndef HAVE_STRLCPY
+/* Copy as much of SRC will fit into a DST of size SIZE, always
+ * NUL-terminating. (Originally from OpenBSD.)
  */
-
-int cistrcmp(cp1, cp2)
-    char *cp1, *cp2;
+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.038942 seconds and 4 git commands to generate.