internat.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * ----------------------------------------------------------------------------
  3. * "THE BEER-WARE LICENSE" (Revision 42):
  4. * <joerg@FreeBSD.ORG> wrote this file. As long as you retain this notice you
  5. * can do whatever you want with this stuff. If we meet some day, and you think
  6. * this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
  7. * ----------------------------------------------------------------------------
  8. */
  9. #include "file.h"
  10. #include <string.h>
  11. #include <memory.h>
  12. #define F 0
  13. #define T 1
  14. /*
  15. * List of characters that look "reasonable" in international
  16. * language texts. That's almost all characters :), except a
  17. * few in the control range of ASCII (all the known international
  18. * charactersets share the bottom half with ASCII).
  19. */
  20. static char maybe_internat[256] = {
  21. F, F, F, F, F, F, F, F, T, T, T, T, T, T, F, F, /* 0x0X */
  22. F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F, /* 0x1X */
  23. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x2X */
  24. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x3X */
  25. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x4X */
  26. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x5X */
  27. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x6X */
  28. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F, /* 0x7X */
  29. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x8X */
  30. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x9X */
  31. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xaX */
  32. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xbX */
  33. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xcX */
  34. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xdX */
  35. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xeX */
  36. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T /* 0xfX */
  37. };
  38. /* Maximal length of a line we consider "reasonable". */
  39. #define MAXLINELEN 300
  40. int
  41. internatmagic(buf, nbytes)
  42. unsigned char *buf;
  43. int nbytes;
  44. {
  45. int i;
  46. unsigned char *cp;
  47. nbytes--;
  48. /* First, look whether there are "unreasonable" characters. */
  49. for (i = 0, cp = buf; i < nbytes; i++, cp++)
  50. if (!maybe_internat[*cp])
  51. return 0;
  52. /*
  53. * Now, look whether the file consists of lines of
  54. * "reasonable" length.
  55. */
  56. for (i = 0; i < nbytes;) {
  57. cp = (unsigned char *) memchr(buf, '\n', nbytes - i);
  58. if (cp == NULL) {
  59. /* Don't fail if we hit the end of buffer. */
  60. if (i + MAXLINELEN >= nbytes)
  61. break;
  62. else
  63. return 0;
  64. }
  65. if (cp - buf > MAXLINELEN)
  66. return 0;
  67. i += (cp - buf + 1);
  68. buf = cp + 1;
  69. }
  70. ckfputs("International language text", stdout);
  71. return 1;
  72. }