scheck.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. ** This file is in the public domain, so clarified as of
  3. ** 2006-07-17 by Arthur David Olson.
  4. */
  5. #ifndef lint
  6. #ifndef NOID
  7. static char elsieid[] = "%W%";
  8. #endif /* !defined lint */
  9. #endif /* !defined NOID */
  10. /*LINTLIBRARY*/
  11. #include "private.h"
  12. const char *
  13. scheck(string, format)
  14. const char * const string;
  15. const char * const format;
  16. {
  17. register char * fbuf;
  18. register const char * fp;
  19. register char * tp;
  20. register int c;
  21. register const char * result;
  22. char dummy;
  23. result = "";
  24. if (string == NULL || format == NULL)
  25. return result;
  26. fbuf = imalloc((int) (2 * strlen(format) + 4));
  27. if (fbuf == NULL)
  28. return result;
  29. fp = format;
  30. tp = fbuf;
  31. while ((*tp++ = c = *fp++) != '\0') {
  32. if (c != '%')
  33. continue;
  34. if (*fp == '%') {
  35. *tp++ = *fp++;
  36. continue;
  37. }
  38. *tp++ = '*';
  39. if (*fp == '*')
  40. ++fp;
  41. while (is_digit(*fp))
  42. *tp++ = *fp++;
  43. if (*fp == 'l' || *fp == 'h')
  44. *tp++ = *fp++;
  45. else if (*fp == '[')
  46. do *tp++ = *fp++;
  47. while (*fp != '\0' && *fp != ']');
  48. if ((*tp++ = *fp++) == '\0')
  49. break;
  50. }
  51. *(tp - 1) = '%';
  52. *tp++ = 'c';
  53. *tp = '\0';
  54. if (sscanf(string, fbuf, &dummy) != 1)
  55. result = (char *) format;
  56. ifree(fbuf);
  57. return result;
  58. }