fmtulp.c 634 B

1234567891011121314151617181920212223242526272829
  1. #include <format.h>
  2. /* Could have merged it with fmti{32,64}, but the problem is that
  3. padding only makes sense on unsigned integers, and it's not likely
  4. that we will ever pad 64-bit integers on 32-bit arches.
  5. So, let's keep it a separate function with a arch-neutral long arg. */
  6. char* fmtulp(char* buf, char* end, unsigned long num, int pad)
  7. {
  8. int len;
  9. unsigned long n;
  10. for(len = 1, n = num; n >= 10; len++)
  11. n /= 10;
  12. if(len < pad)
  13. len = pad;
  14. int i;
  15. char* e = buf + len;
  16. char* p = e - 1; /* len >= 1 so e > buf */
  17. for(i = 0; i < len; i++, p--, num /= 10)
  18. if(p < end)
  19. *p = '0' + num % 10;
  20. return e;
  21. }