asc_conv.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <time.h>
  2. #include <string.h>
  3. /*
  4. * Internal ascii conversion routine, avoid use of printf, it's a bit big!
  5. */
  6. /*
  7. * Modified Manuel Novoa III Jan 2001
  8. *
  9. * Removed static function "hit" and did time-field fills inline and
  10. * put day, hour, min, and sec conversions in a loop using a small
  11. * table to reduce code size.
  12. *
  13. * Made daysp[] and mons[] const to move them from bss to text.
  14. *
  15. * Also fixed day conversion ... ANSI says no leading 0.
  16. *
  17. */
  18. void __asctime(buffer, ptm)
  19. register char *buffer;
  20. struct tm *ptm;
  21. {
  22. static const char days[] = "SunMonTueWedThuFriSat";
  23. static const char mons[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  24. /* 012345678901234567890123456 */
  25. static const char template[] = "Err Err 00 00:00:00 0000\n";
  26. int tm_field[4];
  27. int tmp, i;
  28. char *p;
  29. /* Since we need memcpy below, use it here instead of strcpy. */
  30. memcpy(buffer, template, sizeof(template));
  31. if ((ptm->tm_wday >= 0) && (ptm->tm_wday <= 6)) {
  32. memcpy(buffer, days + 3 * (ptm->tm_wday), 3);
  33. }
  34. if ((ptm->tm_mon >= 0) && (ptm->tm_mon <= 11)) {
  35. memcpy(buffer + 4, mons + 3 * (ptm->tm_mon), 3);
  36. }
  37. tm_field[0] = ptm->tm_mday;
  38. tm_field[1] = ptm->tm_hour;
  39. tm_field[2] = ptm->tm_min;
  40. tm_field[3] = ptm->tm_sec;
  41. p = buffer + 9;
  42. for (i=0 ; i<4 ; i++) {
  43. tmp = tm_field[i];
  44. *p-- += tmp % 10;
  45. *p += (tmp/10) % 10;
  46. p += 4 ; /* skip to end of next field */
  47. }
  48. tmp = ptm->tm_year + 1900;
  49. p = buffer + 23;
  50. for (i=0 ; i<4 ; i++) {
  51. *p-- += tmp % 10;
  52. tmp /= 10;
  53. }
  54. if (buffer[8] == '0') {
  55. buffer[8] = ' ';
  56. }
  57. }