xref: /original-bsd/local/toolchest/ksh/shlib/rjust.c (revision 79cf7955)
1 /*
2 
3  *      Copyright (c) 1984, 1985, 1986 AT&T
4  *      All Rights Reserved
5 
6  *      THIS IS UNPUBLISHED PROPRIETARY SOURCE
7  *      CODE OF AT&T.
8  *      The copyright notice above does not
9  *      evidence any actual or intended
10  *      publication of such source code.
11 
12  */
13 /* @(#)rjust.c	1.1 */
14 
15 /*
16  *   RJUST.C
17  *
18  *   Programmer:  D. G. Korn
19  *
20  *        Owner:  D. A. Lambeth
21  *
22  *         Date:  April 17, 1980
23  *
24  *
25  *
26  *   RJUST (STR, SIZE, FILL)
27  *
28  *      Right-justify STR so that it contains no more than
29  *      SIZE non-blank characters.  If necessary, pad with
30  *      the character FILL.
31  *
32  *
33  *
34  *   See Also:
35  */
36 
37 #ifdef KSHELL
38 #include	"shtype.h"
39 #else
40 #include	<ctype.h>
41 #endif	/* KSHELL */
42 
43 /*
44  *   RJUST (STR, SIZE, FILL)
45  *
46  *        char *STR;
47  *
48  *        int SIZE;
49  *
50  *        char FILL;
51  *
52  *   Right-justify STR so that it contains no more than
53  *   SIZE characters.  If STR contains fewer than SIZE
54  *   characters, left-pad with FILL.  Trailing blanks
55  *   in STR will be ignored.
56  *
57  *   If the leftmost digit in STR is not a digit, FILL
58  *   will default to a blank.
59  */
60 
61 void	rjust(str,size,fill)
62 char *str,fill;
63 int size;
64 {
65 	register int n;
66 	register char *cp,*sp;
67 	n = strlen(str);
68 
69 	/* ignore trailing blanks */
70 
71 	for(cp=str+n;n && *--cp == ' ';n--);
72 	if (n == size) return;
73 	if(n > size)
74         {
75         	*(str+n) = 0;
76         	for (sp = str, cp = str+n-size; sp <= str+size; *sp++ = *cp++);
77         	return;
78         }
79 	else *(sp = str+size) = 0;
80 	if (n == 0)
81         {
82         	while (sp > str)
83                		*--sp = ' ';
84         	return;
85         }
86 	while(n--)
87 		*--sp = *cp--;
88 	if(!isdigit(*str))
89 		fill = ' ';
90 	while(sp>str)
91 		*--sp = fill;
92 	return;
93 }
94 
95