aboutsummaryrefslogtreecommitdiff
path: root/sys/fmtio/itoc.x
blob: 726fbc40309e33b1bde4f89464d653bdcbd6aabc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<mach.h>
include	<ctype.h>

# ITOC -- Integer to character string.  We do not resolve this into a call
# to GLTOC for reasons of efficiency.

int procedure itoc (ival, str, maxch)

int	ival, maxch
char	str[ARB]

char	buf[MAX_DIGITS]
int	b_op, s_op, num, temp
int	gstrcpy()

begin	
	s_op = 1

	if (IS_INDEFI (ival)) {
	    return (gstrcpy ("INDEF", str, maxch))
	} else if (ival < 0) {
	    str[1] = '-'
	    s_op = 2
	    num = -ival
	} else
	    num = ival

	# Encode nonnegative number in BUF, least significant digits first.

	b_op = 0
	repeat {
	    temp = num / 10
	    b_op = b_op + 1
	    buf[b_op] = TO_DIGIT (num - temp * 10)
	    num = temp
	} until (num == 0)

	# Copy encoded number to output string, reversing the order of the
	# digits so that the most significant digits are first.

	while (b_op > 0) {
	    if (s_op > maxch)
		return (gstrcpy ("**********", str, maxch))
	    str[s_op] = buf[b_op]
	    s_op = s_op + 1
	    b_op = b_op - 1
	}

	str[s_op] = EOS
	return (s_op - 1)
end