Add support for large files. Merge glibc 2.1.2.
[bpt/emacs.git] / lib-src / test-distrib.c
CommitLineData
33b78bd6
RM
1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
042c85b8
JB
5#include <stdio.h>
6
9ee8cf9e
AS
7#ifdef HAVE_UNISTD_H
8#include <unistd.h>
9#endif
10
33b78bd6
RM
11#ifndef O_RDONLY
12#define O_RDONLY 0
13#endif
14
042c85b8
JB
15/* Break string in two parts to avoid buggy C compilers that ignore characters
16 after nulls in strings. */
17
18char string1[] = "Testing distribution of nonprinting chars:\n\
19Should be 0177: \177 Should be 0377: \377 Should be 0212: \212.\n\
20Should be 0000: ";
21
22char string2[] = ".\n\
23This file is read by the `test-distribution' program.\n\
24If you change it, you will make that program fail.\n";
25
26char buf[300];
27
28/* Like `read' but keeps trying until it gets SIZE bytes or reaches eof. */
29int
30cool_read (fd, buf, size)
31 int fd;
32 char *buf;
33 int size;
34{
35 int num, sofar = 0;
36
37 while (1)
38 {
39 if ((num = read (fd, buf + sofar, size - sofar)) == 0)
40 return sofar;
41 else if (num < 0)
42 return num;
43 sofar += num;
44 }
45}
46
340ff9de 47int
d2d92f7a
JB
48main (argc, argv)
49 int argc;
50 char **argv;
042c85b8 51{
d2d92f7a 52 int fd;
042c85b8 53
d2d92f7a
JB
54 if (argc != 2)
55 {
56 fprintf (stderr, "Usage: %s testfile\n", argv[0]);
57 exit (2);
58 }
33b78bd6 59 fd = open (argv[1], O_RDONLY);
042c85b8
JB
60 if (fd < 0)
61 {
d2d92f7a 62 perror (argv[1]);
042c85b8
JB
63 exit (2);
64 }
65 if (cool_read (fd, buf, sizeof string1) != sizeof string1 ||
66 strcmp (buf, string1) ||
67 cool_read (fd, buf, sizeof string2) != sizeof string2 - 1 ||
68 strncmp (buf, string2, sizeof string2 - 1))
69 {
d2d92f7a 70 fprintf (stderr, "Data in file `%s' has been damaged.\n\
042c85b8 71Most likely this means that many nonprinting characters\n\
d2d92f7a
JB
72have been corrupted in the files of Emacs, and it will not work.\n",
73 argv[1]);
042c85b8
JB
74 exit (2);
75 }
76 close (fd);
77#ifdef VMS
78 exit (1); /* On VMS, success is 1. */
042c85b8 79#endif
340ff9de 80 return (0);
042c85b8 81}