test-distrib.c 2.6 KB

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