source: trunk/minix/commands/aal/long2str.c@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.4 KB
RevLine 
[9]1/* $Header: /cvsup/minix/src/commands/aal/long2str.c,v 1.1.1.1 2005/04/21 14:53:57 beng Exp $ */
2/*
3 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4 * See the copyright notice in the ACK home directory, in the file "Copyright".
5 */
6/* Integer to String translator
7 -> base is a value from [-16,-2] V [2,16]
8 -> base < 0: see 'val' as unsigned value
9 -> no checks for buffer overflow and illegal parameters
10 (1985, EHB)
11*/
12
13#define MAXWIDTH 32
14
15char *
16long2str(val, base)
17 register long val;
18 register base;
19{
20 static char numbuf[MAXWIDTH];
21 static char vec[] = "0123456789ABCDEF";
22 register char *p = &numbuf[MAXWIDTH];
23 int sign = (base > 0);
24
25 *--p = '\0'; /* null-terminate string */
26 if (val) {
27 if (base > 0) {
28 if (val < 0L) {
29 long v1 = -val;
30 if (v1 == val)
31 goto overflow;
32 val = v1;
33 }
34 else
35 sign = 0;
36 }
37 else
38 if (base < 0) { /* unsigned */
39 base = -base;
40 if (val < 0L) { /* taken from Amoeba src */
41 register mod, i;
42 overflow:
43 mod = 0;
44 for (i = 0; i < 8 * sizeof val; i++) {
45 mod <<= 1;
46 if (val < 0)
47 mod++;
48 val <<= 1;
49 if (mod >= base) {
50 mod -= base;
51 val++;
52 }
53 }
54 *--p = vec[mod];
55 }
56 }
57 do {
58 *--p = vec[(int) (val % base)];
59 val /= base;
60 } while (val != 0L);
61 if (sign)
62 *--p = '-'; /* don't forget it !! */
63 }
64 else
65 *--p = '0'; /* just a simple 0 */
66 return p;
67}
Note: See TracBrowser for help on using the repository browser.