]>
Commit | Line | Data |
---|---|---|
f98ca075 | 1 | /* Test for some *_unlocked stdio interfaces. |
04277e02 | 2 | Copyright (C) 2004-2019 Free Software Foundation, Inc. |
f98ca075 RM |
3 | This file is part of the GNU C Library. |
4 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2004. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
f98ca075 RM |
19 | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
e15f7de6 | 23 | #include <libc-diag.h> |
f98ca075 RM |
24 | |
25 | int fd; | |
26 | static void do_prepare (void); | |
27 | static int do_test (void); | |
28 | #define PREPARE(argc, argv) do_prepare () | |
29 | #define TEST_FUNCTION do_test () | |
30 | #include "../test-skeleton.c" | |
31 | ||
32 | static int | |
33 | do_test (void) | |
34 | { | |
35 | const char blah[] = "BLAH"; | |
36 | char buf[strlen (blah) + 1]; | |
37 | FILE *fp, *f; | |
38 | const char *cp; | |
39 | char *wp; | |
40 | ||
41 | if ((fp = fdopen (fd, "w+")) == NULL) | |
42 | exit (1); | |
43 | ||
44 | flockfile (fp); | |
45 | ||
46 | f = fp; | |
47 | cp = blah; | |
c81e9deb JM |
48 | /* These tests deliberately use fwrite_unlocked with the size |
49 | argument specified as 0, which results in "division by zero" | |
50 | warnings from the expansion of that macro (in code that is not | |
51 | evaluated for a size of 0). This applies to the tests of | |
52 | fread_unlocked below as well. */ | |
53 | DIAG_PUSH_NEEDS_COMMENT; | |
54 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdiv-by-zero"); | |
f98ca075 RM |
55 | if (ftello (fp) != 0 |
56 | || fwrite_unlocked (blah, blah - blah, strlen (blah), f++) != 0 | |
57 | || f != fp + 1 | |
58 | || fwrite_unlocked ("", 5.0, 0, --f) != 0 | |
59 | || f != fp | |
60 | || fwrite_unlocked (cp++, 16, 0.25, fp) != 0 | |
61 | || cp != blah + 1 | |
62 | || fwrite_unlocked (--cp, 0.25, 16, fp) != 0 | |
63 | || cp != blah | |
64 | || fwrite_unlocked (blah, 0, -0.0, fp) != 0 | |
65 | || ftello (fp) != 0) | |
66 | { | |
67 | puts ("One of fwrite_unlocked tests failed"); | |
68 | exit (1); | |
69 | } | |
c81e9deb | 70 | DIAG_POP_NEEDS_COMMENT; |
f98ca075 RM |
71 | |
72 | if (fwrite_unlocked (blah, 1, strlen (blah) - 2, fp) != strlen (blah) - 2) | |
73 | { | |
74 | puts ("Could not write string into file"); | |
75 | exit (1); | |
76 | } | |
77 | ||
78 | if (putc_unlocked ('A' + 0x1000000, fp) != 'A') | |
79 | { | |
80 | puts ("putc_unlocked failed"); | |
81 | exit (1); | |
82 | } | |
83 | ||
84 | f = fp; | |
85 | cp = blah + strlen (blah) - 1; | |
86 | if (putc_unlocked (*cp++, f++) != 'H' | |
87 | || f != fp + 1 | |
88 | || cp != strchr (blah, '\0')) | |
89 | { | |
90 | puts ("fputc_unlocked failed"); | |
91 | exit (1); | |
92 | } | |
93 | ||
94 | if (ftello (fp) != (off_t) strlen (blah)) | |
95 | { | |
96 | printf ("Failed to write %zd bytes to temporary file", strlen (blah)); | |
97 | exit (1); | |
98 | } | |
99 | ||
100 | rewind (fp); | |
101 | ||
102 | f = fp; | |
103 | wp = buf; | |
104 | memset (buf, ' ', sizeof (buf)); | |
c81e9deb JM |
105 | /* See explanation above. */ |
106 | DIAG_PUSH_NEEDS_COMMENT; | |
107 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdiv-by-zero"); | |
f98ca075 RM |
108 | if (ftello (fp) != 0 |
109 | || fread_unlocked (buf, buf - buf, strlen (blah), f++) != 0 | |
110 | || f != fp + 1 | |
111 | || fread_unlocked (buf, 5.0, 0, --f) != 0 | |
112 | || f != fp | |
113 | || fread_unlocked (wp++, 16, 0.25, fp) != 0 | |
114 | || wp != buf + 1 | |
115 | || fread_unlocked (--wp, 0.25, 16, fp) != 0 | |
116 | || wp != buf | |
117 | || fread_unlocked (buf, 0, -0.0, fp) != 0 | |
118 | || ftello (fp) != 0 | |
119 | || memcmp (buf, " ", sizeof (buf)) != 0) | |
120 | { | |
121 | puts ("One of fread_unlocked tests failed"); | |
122 | exit (1); | |
123 | } | |
c81e9deb | 124 | DIAG_POP_NEEDS_COMMENT; |
f98ca075 RM |
125 | |
126 | if (fread_unlocked (buf, 1, strlen (blah) - 2, fp) != strlen (blah) - 2) | |
127 | { | |
128 | puts ("Could not read string from file"); | |
129 | exit (1); | |
130 | } | |
131 | ||
132 | if (getc_unlocked (fp) != 'A') | |
133 | { | |
134 | puts ("getc_unlocked failed"); | |
135 | exit (1); | |
136 | } | |
137 | ||
138 | f = fp; | |
139 | if (fgetc_unlocked (f++) != 'H' | |
140 | || f != fp + 1 | |
141 | || fgetc_unlocked (--f) != EOF | |
142 | || f != fp) | |
143 | { | |
144 | puts ("fgetc_unlocked failed"); | |
145 | exit (1); | |
146 | } | |
147 | ||
148 | if (ftello (fp) != (off_t) strlen (blah)) | |
149 | { | |
150 | printf ("Failed to read %zd bytes from temporary file", strlen (blah)); | |
151 | exit (1); | |
152 | } | |
153 | ||
154 | funlockfile (fp); | |
155 | fclose (fp); | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
160 | static void | |
161 | do_prepare (void) | |
162 | { | |
163 | fd = create_temp_file ("tst-unlockedio.", NULL); | |
164 | if (fd == -1) | |
165 | { | |
166 | printf ("cannot create temporary file: %m\n"); | |
167 | exit (1); | |
168 | } | |
169 | } |