1 | /* Test program for ungetc/ftell interaction bug. */
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 |
|
---|
5 | static void do_prepare (void);
|
---|
6 | #define PREPARE(argc, argv) do_prepare ()
|
---|
7 | static int do_test (void);
|
---|
8 | #define TEST_FUNCTION do_test ()
|
---|
9 | #include <test-skeleton.c>
|
---|
10 |
|
---|
11 | static const char pattern[] = "12345";
|
---|
12 | static char *temp_file;
|
---|
13 |
|
---|
14 | static void
|
---|
15 | do_prepare (void)
|
---|
16 | {
|
---|
17 | int fd = create_temp_file ("bug-ungetc.", &temp_file);
|
---|
18 | if (fd == -1)
|
---|
19 | {
|
---|
20 | printf ("cannot create temporary file: %m\n");
|
---|
21 | exit (1);
|
---|
22 | }
|
---|
23 | write (fd, pattern, sizeof (pattern));
|
---|
24 | close (fd);
|
---|
25 | }
|
---|
26 |
|
---|
27 | static int
|
---|
28 | do_test (void)
|
---|
29 | {
|
---|
30 | int i;
|
---|
31 | FILE *f;
|
---|
32 | char buf[10];
|
---|
33 | long offset, diff;
|
---|
34 | int result = 0;
|
---|
35 |
|
---|
36 | f = fopen (temp_file, "rw");
|
---|
37 |
|
---|
38 | rewind (f);
|
---|
39 | for (i = 0; i < 3; i++)
|
---|
40 | printf ("%c\n", getc (f));
|
---|
41 | offset = ftell (f);
|
---|
42 | printf ("offset = %ld\n", offset);
|
---|
43 | if (ungetc ('4', f) != '4')
|
---|
44 | {
|
---|
45 | printf ("ungetc failed\n");
|
---|
46 | abort ();
|
---|
47 | }
|
---|
48 | printf ("offset after ungetc = %ld\n", ftell (f));
|
---|
49 |
|
---|
50 | i = fread ((void *) buf, 4, (size_t) 1, f);
|
---|
51 | printf ("read %d (%c), offset = %ld\n", i, buf[0], ftell (f));
|
---|
52 | diff = ftell (f) - offset;
|
---|
53 | if (diff != 3)
|
---|
54 | {
|
---|
55 | printf ("ftell did not update properly. got %ld, expected 3\n", diff);
|
---|
56 | result = 1;
|
---|
57 | }
|
---|
58 | fclose (f);
|
---|
59 |
|
---|
60 | return result;
|
---|
61 | }
|
---|