xref: /original-bsd/local/toolchest/ksh/shlib/utos.c (revision 9e3bee04)
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 /* @(#)utos.c	1.1 */
14 
15 /*
16  *   UTOS.C
17  *
18  *   Programmer:  D. G. Korn
19  *
20  *         Date:  May 3, 1982
21  *
22  *
23  *
24  *   LTOS (SINT, BASE)
25  *
26  *        Return a pointer to a string denoting the value of
27  *        the signed long integer SINT in base BASE.
28  *
29  *   UTOS (USINT, BASE)
30  *
31  *        Return a pointer to a string denoting the value of
32  *        the unsigned long integer USINT in base BASE.
33  *
34  *
35  *
36  *   See Also:  arith(III)
37  */
38 
39 
40 #define BASEMAX	 (4+16*sizeof(int))
41 static char hexstr[BASEMAX];
42 extern char hdigits[];
43 char *utos();
44 char *ltos();
45 
46 /*
47  *   LTOS (SINT, BASE)
48  *
49  *        long USINT;
50  *
51  *        int BASE;
52  *
53  *   Return a pointer to a string denoting the value of SINT
54  *   in base BASE.  The string will be stored within HEXSTR.
55  *   It will begin with the base followed by a single '#'.
56  *   A minus sign will be prepended for negative numbers
57  *
58  */
59 
60 
61 char *ltos(sint,base)
62 long sint;
63 int base;
64 {
65 	register char *sp;
66 	register long l = (sint>=0?sint:-sint);
67 #ifdef pdp11
68 	sp = utos(l,base);
69 #else
70 	sp = utos((unsigned long)l,base);
71 #endif /* pdp11 */
72 	if(sint<0)
73 		*--sp = '-';
74 	return(sp);
75 }
76 
77 /*
78  *   UTOS (USINT, BASE)
79  *
80  *        unsigned USINT;
81  *
82  *        int BASE;
83  *
84  *   Return a pointer to a string denoting the value of USINT
85  *   in base BASE.  The string will be stored within HEXSTR.
86  *   It will begin with the base followed by a single '#'.
87  *
88  */
89 
90 
91 
92 char *utos(usint,base)
93 register int base;
94 #ifdef pdp11
95  /* unsigned longs are not supported on pdp11 */
96 long usint;
97 {
98 	long l = usint;
99 #else
100 unsigned long usint;
101 {
102 	register unsigned long l = usint;
103 #endif	/* pdp11 */
104 	register char *cp = hexstr+(BASEMAX-1);
105 	if(base < 2 || base > BASEMAX)
106 		return(cp);
107 	for(*cp = 0;cp > hexstr && l;l /= base)
108 		*--cp = hdigits[(l%base)<<1];
109 	if(usint==0)
110 		*--cp = '0';
111 	if(base==10)
112 		return(cp);
113 	*--cp = '#';
114 	*--cp = hdigits[(base%10)<<1];
115 	if(base /= 10)
116 		*--cp = hdigits[(base%10)<<1];
117 	return(cp);
118 }
119