long2str.c

Go to the documentation of this file.
00001 /* $Header: /opt/proj/minix/cvsroot/src/commands/aal/long2str.c,v 1.1.1.1 2005/04/21 14:53:57 beng Exp $ */
00002 /*
00003  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
00004  * See the copyright notice in the ACK home directory, in the file "Copyright".
00005  */
00006 /* Integer to String translator
00007         -> base is a value from [-16,-2] V [2,16]
00008         -> base < 0: see 'val' as unsigned value
00009         -> no checks for buffer overflow and illegal parameters
00010         (1985, EHB)
00011 */
00012 
00013 #define MAXWIDTH 32
00014 
00015 char *
00016 long2str(val, base)
00017         register long val;
00018         register base;
00019 {
00020         static char numbuf[MAXWIDTH];
00021         static char vec[] = "0123456789ABCDEF";
00022         register char *p = &numbuf[MAXWIDTH];
00023         int sign = (base > 0);
00024 
00025         *--p = '\0';            /* null-terminate string        */
00026         if (val) {
00027                 if (base > 0) {
00028                         if (val < 0L) {
00029                                 long v1 = -val;
00030                                 if (v1 == val)
00031                                         goto overflow;
00032                                 val = v1;
00033                         }
00034                         else
00035                                 sign = 0;
00036                 }
00037                 else
00038                 if (base < 0) {                 /* unsigned */
00039                         base = -base;
00040                         if (val < 0L) { /* taken from Amoeba src */
00041                                 register mod, i;
00042                         overflow:
00043                                 mod = 0;
00044                                 for (i = 0; i < 8 * sizeof val; i++) {
00045                                         mod <<= 1;
00046                                         if (val < 0)
00047                                                 mod++;
00048                                         val <<= 1;
00049                                         if (mod >= base) {
00050                                                 mod -= base;
00051                                                 val++;
00052                                         }
00053                                 }
00054                                 *--p = vec[mod];
00055                         }
00056                 }
00057                 do {
00058                         *--p = vec[(int) (val % base)];
00059                         val /= base;
00060                 } while (val != 0L);
00061                 if (sign)
00062                         *--p = '-';     /* don't forget it !!   */
00063         }
00064         else
00065                 *--p = '0';             /* just a simple 0      */
00066         return p;
00067 }

Generated on Fri Apr 14 22:56:37 2006 for minix by  doxygen 1.4.6