DragonOS/kernel/lib/stdlib.c

29 lines
587 B
C
Raw Normal View History

2022-09-09 16:18:18 +00:00
#include <common/stdlib.h>
/**
* @brief
*
* @param input
* @return const char*
*/
const char *ltoa(long input)
{
/* large enough for -9223372036854775808 */
static char buffer[21] = {0};
char *pos = buffer + sizeof(buffer) - 1;
int neg = input < 0;
unsigned long n = neg ? -input : input;
*pos-- = '\0';
do
{
*pos-- = '0' + n % 10;
n /= 10;
if (pos < buffer)
return pos + 1;
} while (n);
if (neg)
*pos-- = '-';
return pos + 1;
}