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

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

diff --git a/usr/src/local/toolchest/ksh/shlib/rjust.c b/usr/src/local/toolchest/ksh/shlib/rjust.c
new file mode 100644 (file)
index 0000000..46c1956
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+
+ *      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.
+
+ */
+/* @(#)rjust.c 1.1 */
+
+/*
+ *   RJUST.C
+ *
+ *   Programmer:  D. G. Korn
+ *
+ *        Owner:  D. A. Lambeth
+ *
+ *         Date:  April 17, 1980
+ *
+ *
+ *
+ *   RJUST (STR, SIZE, FILL)
+ *
+ *      Right-justify STR so that it contains no more than 
+ *      SIZE non-blank characters.  If necessary, pad with
+ *      the character FILL.
+ *
+ *
+ *
+ *   See Also:  
+ */
+
+#ifdef KSHELL
+#include       "shtype.h"
+#else
+#include       <ctype.h>
+#endif /* KSHELL */
+
+/*
+ *   RJUST (STR, SIZE, FILL)
+ *
+ *        char *STR;
+ *
+ *        int SIZE;
+ *
+ *        char FILL;
+ *
+ *   Right-justify STR so that it contains no more than
+ *   SIZE characters.  If STR contains fewer than SIZE
+ *   characters, left-pad with FILL.  Trailing blanks
+ *   in STR will be ignored.
+ *
+ *   If the leftmost digit in STR is not a digit, FILL
+ *   will default to a blank.
+ */
+
+void   rjust(str,size,fill)
+char *str,fill;
+int size;
+{
+       register int n;
+       register char *cp,*sp;
+       n = strlen(str);
+
+       /* ignore trailing blanks */
+
+       for(cp=str+n;n && *--cp == ' ';n--);
+       if (n == size) return;
+       if(n > size)
+        {
+               *(str+n) = 0;
+               for (sp = str, cp = str+n-size; sp <= str+size; *sp++ = *cp++);
+               return;
+        }
+       else *(sp = str+size) = 0;
+       if (n == 0)  
+        {
+               while (sp > str)
+                               *--sp = ' ';
+               return;
+        }
+       while(n--)
+               *--sp = *cp--;
+       if(!isdigit(*str))
+               fill = ' ';
+       while(sp>str)
+               *--sp = fill;
+       return;
+}
+