Use 64bit filesystem functions for x32
[glibc.git] / libio / bug-fclose1.c
blobf1e09f5d4764b8423fdb7ee17a4a878254c90585
1 // BZ #12724
3 static void do_prepare (void);
4 #define PREPARE(argc, argv) do_prepare ()
5 static int do_test (void);
6 #define TEST_FUNCTION do_test()
7 #include "../test-skeleton.c"
10 static int fd;
13 static void
14 do_prepare (void)
16 fd = create_temp_file ("bug-fclose1.", NULL);
17 if (fd == -1)
19 printf ("cannot create temporary file: %m\n");
20 exit (1);
25 static int
26 do_test (void)
28 static const char pattern[] = "hello world";
30 /* Prepare a seekable file. */
31 if (write (fd, pattern, sizeof pattern) != sizeof pattern)
33 printf ("cannot write pattern: %m\n");
34 return 1;
36 if (lseek (fd, 1, SEEK_SET) != 1)
38 printf ("cannot seek after write: %m\n");
39 return 1;
42 /* Create an output stream visiting the file; when it is closed, all
43 other file descriptors visiting the file must see the new file
44 position. */
45 int fd2 = dup (fd);
46 if (fd2 < 0)
48 printf ("cannot duplicate descriptor for writing: %m\n");
49 return 1;
51 FILE *f = fdopen (fd2, "w");
52 if (f == NULL)
54 printf ("first fdopen failed: %m\n");
55 return 1;
57 if (fputc (pattern[1], f) != pattern[1])
59 printf ("fputc failed: %m\n");
60 return 1;
62 if (fclose (f) != 0)
64 printf ("first fclose failed: %m\n");
65 return 1;
67 errno = 0;
68 if (lseek (fd2, 0, SEEK_CUR) != -1)
70 printf ("lseek after fclose after write did not fail\n");
71 return 1;
73 if (errno != EBADF)
75 printf ("lseek after fclose after write did not fail with EBADF: %m\n");
76 return 1;
78 off_t o = lseek (fd, 0, SEEK_CUR);
79 if (o != 2)
81 printf ("\
82 lseek on original descriptor after first fclose returned %ld, expected 2\n",
83 (long int) o);
84 return 1;
87 /* Likewise for an input stream. */
88 fd2 = dup (fd);
89 if (fd2 < 0)
91 printf ("cannot duplicate descriptor for reading: %m\n");
92 return 1;
94 f = fdopen (fd2, "r");
95 if (f == NULL)
97 printf ("second fdopen failed: %m\n");
98 return 1;
100 char c = fgetc (f);
101 if (c != pattern[2])
103 printf ("getc returned %c, expected %c\n", c, pattern[2]);
104 return 1;
106 if (fclose (f) != 0)
108 printf ("second fclose failed: %m\n");
109 return 1;
111 errno = 0;
112 if (lseek (fd2, 0, SEEK_CUR) != -1)
114 printf ("lseek after fclose after read did not fail\n");
115 return 1;
117 if (errno != EBADF)
119 printf ("lseek after fclose after read did not fail with EBADF: %m\n");
120 return 1;
122 o = lseek (fd, 0, SEEK_CUR);
123 if (o != 3)
125 printf ("\
126 lseek on original descriptor after second fclose returned %ld, expected 3\n",
127 (long int) o);
128 return 1;
131 return 0;