internat.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef lint
  13. FILE_RCSID("@(#)$Id: internat.c,v 1.4 1998/06/27 13:23:39 christos Exp $")
  14. #endif
  15. #define F 0
  16. #define T 1
  17. /*
  18. * List of characters that look "reasonable" in international
  19. * language texts. That's almost all characters :), except a
  20. * few in the control range of ASCII (all the known international
  21. * charactersets share the bottom half with ASCII).
  22. */
  23. static char maybe_internat[256] = {
  24. F, F, F, F, F, F, F, F, T, T, T, T, T, T, F, F, /* 0x0X */
  25. F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F, /* 0x1X */
  26. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x2X */
  27. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x3X */
  28. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x4X */
  29. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x5X */
  30. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x6X */
  31. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F, /* 0x7X */
  32. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x8X */
  33. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x9X */
  34. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xaX */
  35. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xbX */
  36. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xcX */
  37. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xdX */
  38. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xeX */
  39. T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T /* 0xfX */
  40. };
  41. /* Maximal length of a line we consider "reasonable". */
  42. #define MAXLINELEN 300
  43. int
  44. internatmagic(buf, nbytes)
  45. unsigned char *buf;
  46. int nbytes;
  47. {
  48. int i;
  49. unsigned char *cp;
  50. nbytes--;
  51. /* First, look whether there are "unreasonable" characters. */
  52. for (i = 0, cp = buf; i < nbytes; i++, cp++)
  53. if (!maybe_internat[*cp])
  54. return 0;
  55. /*
  56. * Now, look whether the file consists of lines of
  57. * "reasonable" length.
  58. */
  59. for (i = 0; i < nbytes;) {
  60. cp = (unsigned char *) memchr(buf, '\n', nbytes - i);
  61. if (cp == NULL) {
  62. /* Don't fail if we hit the end of buffer. */
  63. if (i + MAXLINELEN >= nbytes)
  64. break;
  65. else
  66. return 0;
  67. }
  68. if (cp - buf > MAXLINELEN)
  69. return 0;
  70. i += (cp - buf + 1);
  71. buf = cp + 1;
  72. }
  73. ckfputs("International language text", stdout);
  74. return 1;
  75. }