date and time created 88/07/21 17:34:19 by marc
authorMarc Teitelbaum <marc@ucbvax.Berkeley.EDU>
Fri, 22 Jul 1988 08:34:19 +0000 (00:34 -0800)
committerMarc Teitelbaum <marc@ucbvax.Berkeley.EDU>
Fri, 22 Jul 1988 08:34:19 +0000 (00:34 -0800)
SCCS-vsn: local/toolchest/ksh/sh/string.c 1.1

usr/src/local/toolchest/ksh/sh/string.c [new file with mode: 0644]

diff --git a/usr/src/local/toolchest/ksh/sh/string.c b/usr/src/local/toolchest/ksh/sh/string.c
new file mode 100644 (file)
index 0000000..b161316
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+
+ *      Copyright (c) 1984, 1985, 1986 AT&T
+ *      All Rights Reserved
+
+ *      THIS IS UNPUBLISHED PROPRIETARY SOURCE 
+ *      CODE OF AT&T.
+ *      The copyright notice above does not 
+ *      evidence any actual or intended
+ *      publication of such source code.
+
+ */
+/*
+ * string processing routines for Korn shell
+ *
+ */
+
+#include       "defs.h"
+#ifdef MULTIBYTE
+#include       "national.h"
+#endif /* MULTIBYTE */
+
+/* The following routines are defined by this module */
+char   *itos();
+char   *movstr();
+char   *substitute();
+char   *simple();
+void   trim();
+
+/* The following routines are referenced by this module */
+extern char    *utos();
+extern char    *strrchr();
+
+/*
+ * converts integer n into an unsigned decimal string
+ */
+
+char *itos(n)
+int n;
+{
+#ifdef pdp11
+       return(utos((long)n,10));
+#else
+       return(utos((unsigned long)n,10));
+#endif /* pdp11 */
+}
+
+
+/*
+ * look for the substring <old> in <string> and replace with <new>
+ * The new string is put on top of the stack
+ */
+
+char *substitute(string,old,new,newstring)
+char *string;
+char *old;
+char *new;
+char *newstring;
+{
+       register char *sp = string;
+       register char *dp;
+       register char *cp;
+       char *savesp = NIL;
+       dp = newstring;
+       if(*sp==0)
+               return(NIL);
+       if(*(cp=old) == 0)
+               goto found;
+       do
+       {
+       /* skip to first character which matches start of old */
+               while(*sp && (savesp==sp || *sp != *cp))
+               {
+#ifdef MULTIBYTE
+                       /* skip a whole character at a time */
+                       int c = *sp;
+                       c = echarset(c);
+                       c = in_csize(c) + (c>=2);
+                       while(c-- > 0)
+#endif /* MULTIBYTE */
+                       *dp++ = *sp++;
+               }
+               if(*sp == 0)
+                       return(NIL);
+               savesp = sp;
+               for(;*cp;cp++)
+               {
+                       if(*cp != *sp++)
+                               break;
+               }
+               if(*cp==0)
+               /* match found */
+                       goto found;
+               sp = savesp;
+               cp = old;
+       }
+       while(*sp);
+       return(NIL);
+
+found:
+       /* copy new */
+       dp = movstr(new,dp);
+       /* copy rest of string */
+       movstr(sp,dp);
+       return(newstring);
+}
+
+/*
+ * given a pathname return the base name
+ */
+
+char *simple(name)
+register char *name;
+{
+       char *start = name;
+       while (*name)
+               if ((*name++ == '/') && *name)  /* don't trim trailing / */
+                       start = name;
+       return (start);
+}
+
+/*
+ * TRIM(at)
+ * Remove quote bit from characters in at and eliminate quoted nulls.
+ */
+
+void   trim(at)
+char * at;
+{
+       register char *sp = at;
+       register char *dp;
+       register int c;
+       if(sp)
+       {
+               dp = sp;
+               while(c= *sp++)
+               {
+                       if(c == ESCAPE)
+                               c = *sp++;
+                       if(c)
+                               *dp++ = c;
+               }
+               *dp = 0;
+       }
+}
+
+/*
+ * copy string a to string b and return a pointer to the end of the string
+ */
+
+char *movstr(a,b)
+register char *a,*b;
+{
+       while(*b++ = *a++);
+       return(--b);
+}
+