test-distrib.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* test-distrib.c --- testing distribution of nonprinting chars
  2. Copyright (C) 1987, 1993-1995, 1999, 2001-2012 Free Software Foundation, Inc.
  3. This file is part of GNU Emacs.
  4. GNU Emacs is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU Emacs is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. /* Break string in two parts to avoid buggy C compilers that ignore characters
  19. after nulls in strings. */
  20. static char string1[] = "Testing distribution of nonprinting chars:\n\
  21. Should be 0177: \177 Should be 0377: \377 Should be 0212: \212.\n\
  22. Should be 0000: ";
  23. static char string2[] = ".\n\
  24. This file is read by the `test-distribution' program.\n\
  25. If you change it, you will make that program fail.\n";
  26. /* Like `read' but keeps trying until it gets SIZE bytes or reaches eof. */
  27. static int
  28. cool_read (int fd, char *buf, size_t size)
  29. {
  30. ssize_t num;
  31. ssize_t sofar = 0;
  32. while (1)
  33. {
  34. if ((num = read (fd, buf + sofar, size - sofar)) == 0)
  35. return sofar;
  36. else if (num < 0)
  37. return num;
  38. sofar += num;
  39. }
  40. }
  41. int
  42. main (int argc, char **argv)
  43. {
  44. int fd;
  45. char buf[300];
  46. if (argc != 2)
  47. {
  48. fprintf (stderr, "Usage: %s testfile\n", argv[0]);
  49. exit (EXIT_FAILURE);
  50. }
  51. fd = open (argv[1], O_RDONLY);
  52. if (fd < 0)
  53. {
  54. perror (argv[1]);
  55. exit (EXIT_FAILURE);
  56. }
  57. if (cool_read (fd, buf, sizeof string1) != sizeof string1 ||
  58. strcmp (buf, string1) ||
  59. cool_read (fd, buf, sizeof string2) != sizeof string2 - 1 ||
  60. strncmp (buf, string2, sizeof string2 - 1))
  61. {
  62. fprintf (stderr, "Data in file `%s' has been damaged.\n\
  63. Most likely this means that many nonprinting characters\n\
  64. have been corrupted in the files of Emacs, and it will not work.\n",
  65. argv[1]);
  66. exit (EXIT_FAILURE);
  67. }
  68. close (fd);
  69. return EXIT_SUCCESS;
  70. }
  71. /* test-distrib.c ends here */