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