internat.c 2.7 KB

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