]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/tst-unlockedio.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / stdio-common / tst-unlockedio.c
CommitLineData
f98ca075 1/* Test for some *_unlocked stdio interfaces.
d4697bc9 2 Copyright (C) 2004-2014 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
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
f98ca075
RM
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23
24int fd;
25static void do_prepare (void);
26static int do_test (void);
27#define PREPARE(argc, argv) do_prepare ()
28#define TEST_FUNCTION do_test ()
29#include "../test-skeleton.c"
30
31static int
32do_test (void)
33{
34 const char blah[] = "BLAH";
35 char buf[strlen (blah) + 1];
36 FILE *fp, *f;
37 const char *cp;
38 char *wp;
39
40 if ((fp = fdopen (fd, "w+")) == NULL)
41 exit (1);
42
43 flockfile (fp);
44
45 f = fp;
46 cp = blah;
47 if (ftello (fp) != 0
48 || fwrite_unlocked (blah, blah - blah, strlen (blah), f++) != 0
49 || f != fp + 1
50 || fwrite_unlocked ("", 5.0, 0, --f) != 0
51 || f != fp
52 || fwrite_unlocked (cp++, 16, 0.25, fp) != 0
53 || cp != blah + 1
54 || fwrite_unlocked (--cp, 0.25, 16, fp) != 0
55 || cp != blah
56 || fwrite_unlocked (blah, 0, -0.0, fp) != 0
57 || ftello (fp) != 0)
58 {
59 puts ("One of fwrite_unlocked tests failed");
60 exit (1);
61 }
62
63 if (fwrite_unlocked (blah, 1, strlen (blah) - 2, fp) != strlen (blah) - 2)
64 {
65 puts ("Could not write string into file");
66 exit (1);
67 }
68
69 if (putc_unlocked ('A' + 0x1000000, fp) != 'A')
70 {
71 puts ("putc_unlocked failed");
72 exit (1);
73 }
74
75 f = fp;
76 cp = blah + strlen (blah) - 1;
77 if (putc_unlocked (*cp++, f++) != 'H'
78 || f != fp + 1
79 || cp != strchr (blah, '\0'))
80 {
81 puts ("fputc_unlocked failed");
82 exit (1);
83 }
84
85 if (ftello (fp) != (off_t) strlen (blah))
86 {
87 printf ("Failed to write %zd bytes to temporary file", strlen (blah));
88 exit (1);
89 }
90
91 rewind (fp);
92
93 f = fp;
94 wp = buf;
95 memset (buf, ' ', sizeof (buf));
96 if (ftello (fp) != 0
97 || fread_unlocked (buf, buf - buf, strlen (blah), f++) != 0
98 || f != fp + 1
99 || fread_unlocked (buf, 5.0, 0, --f) != 0
100 || f != fp
101 || fread_unlocked (wp++, 16, 0.25, fp) != 0
102 || wp != buf + 1
103 || fread_unlocked (--wp, 0.25, 16, fp) != 0
104 || wp != buf
105 || fread_unlocked (buf, 0, -0.0, fp) != 0
106 || ftello (fp) != 0
107 || memcmp (buf, " ", sizeof (buf)) != 0)
108 {
109 puts ("One of fread_unlocked tests failed");
110 exit (1);
111 }
112
113 if (fread_unlocked (buf, 1, strlen (blah) - 2, fp) != strlen (blah) - 2)
114 {
115 puts ("Could not read string from file");
116 exit (1);
117 }
118
119 if (getc_unlocked (fp) != 'A')
120 {
121 puts ("getc_unlocked failed");
122 exit (1);
123 }
124
125 f = fp;
126 if (fgetc_unlocked (f++) != 'H'
127 || f != fp + 1
128 || fgetc_unlocked (--f) != EOF
129 || f != fp)
130 {
131 puts ("fgetc_unlocked failed");
132 exit (1);
133 }
134
135 if (ftello (fp) != (off_t) strlen (blah))
136 {
137 printf ("Failed to read %zd bytes from temporary file", strlen (blah));
138 exit (1);
139 }
140
141 funlockfile (fp);
142 fclose (fp);
143
144 return 0;
145}
146
147static void
148do_prepare (void)
149{
150 fd = create_temp_file ("tst-unlockedio.", NULL);
151 if (fd == -1)
152 {
153 printf ("cannot create temporary file: %m\n");
154 exit (1);
155 }
156}