summaryrefslogtreecommitdiff
path: root/printdec.c
blob: c061c228f96506bacaf22a78b03abf7f23de25a8 (plain)
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
54
55
56
57
58
59
60
#include <u.h>
#include <libc.h>

void
swap(char *a, char *b)
{
	char tmp;

	tmp = *a;
	*a = *b;
	*b = tmp;
}

void
srev(char *s, char *e)
{
	while(s < e)
		swap(s++, e--);
}

void
printdec(int n)
{
	char buf[16];
	char *p, *e;
	int r;

	p = buf;
	e = buf+sizeof(buf)-1;
	while(n > 0 && p < e){
		r = n%10;
		*p++ = '0'+r;
		n /= 10;
	}
	*p = 0;
	print("%s\n", buf);
}

void
usage(void)
{
	fprint(2, "usage: %s number\n", argv0);
	exits("usage");
}

void
main(int argc, char *argv[])
{
	int n;

	ARGBEGIN{
	default: usage();
	}ARGEND
	if(argc != 1)
		usage();
	srev(argv[0], argv[0]+strlen(argv[0])-1);
	n = strtol(argv[0], nil, 10);
	printdec(n);
	exits(0);
}