Merge from emacs-23 branch, up to 2010-05-20T22:16:19Z!juri@jurta.org.
[bpt/emacs.git] / lib-src / test-distrib.c
1 /* test-distrib.c --- testing distribution of nonprinting chars
2
3 Copyright (C) 1987, 1993, 1994, 1995, 1999, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 /* Break string in two parts to avoid buggy C compilers that ignore characters
28 after nulls in strings. */
29
30 char string1[] = "Testing distribution of nonprinting chars:\n\
31 Should be 0177: \177 Should be 0377: \377 Should be 0212: \212.\n\
32 Should be 0000: ";
33
34 char string2[] = ".\n\
35 This file is read by the `test-distribution' program.\n\
36 If you change it, you will make that program fail.\n";
37
38 char buf[300];
39
40 /* Like `read' but keeps trying until it gets SIZE bytes or reaches eof. */
41 int
42 cool_read (int fd, char *buf, size_t size)
43 {
44 ssize_t num;
45 ssize_t sofar = 0;
46
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
57 int
58 main (int argc, char **argv)
59 {
60 int fd;
61
62 if (argc != 2)
63 {
64 fprintf (stderr, "Usage: %s testfile\n", argv[0]);
65 exit (EXIT_FAILURE);
66 }
67 fd = open (argv[1], O_RDONLY);
68 if (fd < 0)
69 {
70 perror (argv[1]);
71 exit (EXIT_FAILURE);
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 (EXIT_FAILURE);
83 }
84 close (fd);
85 return EXIT_SUCCESS;
86 }
87
88
89 /* test-distrib.c ends here */