1*11be35a1SLionel Sambuc /* $NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc * by Jukka Ruohonen.
9*11be35a1SLionel Sambuc *
10*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc * are met:
13*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc *
19*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc */
31*11be35a1SLionel Sambuc #include <sys/cdefs.h>
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $");
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <atf-c.h>
35*11be35a1SLionel Sambuc #include <errno.h>
36*11be35a1SLionel Sambuc #include <stdio.h>
37*11be35a1SLionel Sambuc #include <unistd.h>
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambuc static const char *path = "fflush";
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fflush_err);
ATF_TC_HEAD(fflush_err,tc)42*11be35a1SLionel Sambuc ATF_TC_HEAD(fflush_err, tc)
43*11be35a1SLionel Sambuc {
44*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test errors from fflush(3)");
45*11be35a1SLionel Sambuc }
46*11be35a1SLionel Sambuc
ATF_TC_BODY(fflush_err,tc)47*11be35a1SLionel Sambuc ATF_TC_BODY(fflush_err, tc)
48*11be35a1SLionel Sambuc {
49*11be35a1SLionel Sambuc FILE *f;
50*11be35a1SLionel Sambuc
51*11be35a1SLionel Sambuc f = fopen(path, "w");
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc ATF_REQUIRE(f != NULL);
54*11be35a1SLionel Sambuc ATF_REQUIRE(fflush(NULL) == 0);
55*11be35a1SLionel Sambuc ATF_REQUIRE(fclose(f) == 0);
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc f = fopen(path, "r");
58*11be35a1SLionel Sambuc ATF_REQUIRE(f != NULL);
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc /*
61*11be35a1SLionel Sambuc * In NetBSD the call should fail if the supplied
62*11be35a1SLionel Sambuc * parameteris not an open stream or the stream is
63*11be35a1SLionel Sambuc * not open for writing.
64*11be35a1SLionel Sambuc */
65*11be35a1SLionel Sambuc errno = 0;
66*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc ATF_REQUIRE(fclose(f) == 0);
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc errno = 0;
71*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc (void)unlink(path);
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc
ATF_TC_CLEANUP(fflush_err,tc)76*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fflush_err, tc)
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc (void)unlink(path);
79*11be35a1SLionel Sambuc }
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fflush_seek);
ATF_TC_HEAD(fflush_seek,tc)82*11be35a1SLionel Sambuc ATF_TC_HEAD(fflush_seek, tc)
83*11be35a1SLionel Sambuc {
84*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test file offsets with fflush(3)");
85*11be35a1SLionel Sambuc }
86*11be35a1SLionel Sambuc
ATF_TC_BODY(fflush_seek,tc)87*11be35a1SLionel Sambuc ATF_TC_BODY(fflush_seek, tc)
88*11be35a1SLionel Sambuc {
89*11be35a1SLionel Sambuc char buf[12];
90*11be35a1SLionel Sambuc int fd = -1;
91*11be35a1SLionel Sambuc FILE *f;
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc /*
94*11be35a1SLionel Sambuc * IEEE Std 1003.1-2008:
95*11be35a1SLionel Sambuc *
96*11be35a1SLionel Sambuc * "For a stream open for reading, if the file
97*11be35a1SLionel Sambuc * is not already at EOF, and the file is one
98*11be35a1SLionel Sambuc * capable of seeking, the file offset of the
99*11be35a1SLionel Sambuc * underlying open file description shall be
100*11be35a1SLionel Sambuc * adjusted so that the next operation on the
101*11be35a1SLionel Sambuc * open file description deals with the byte
102*11be35a1SLionel Sambuc * after the last one read from or written to
103*11be35a1SLionel Sambuc * the stream being flushed."
104*11be35a1SLionel Sambuc */
105*11be35a1SLionel Sambuc f = fopen(path, "w");
106*11be35a1SLionel Sambuc ATF_REQUIRE(f != NULL);
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
109*11be35a1SLionel Sambuc ATF_REQUIRE(fclose(f) == 0);
110*11be35a1SLionel Sambuc
111*11be35a1SLionel Sambuc f = fopen(path, "r+");
112*11be35a1SLionel Sambuc ATF_REQUIRE(f != NULL);
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc fd = fileno(f);
115*11be35a1SLionel Sambuc ATF_REQUIRE(fd != -1);
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc ATF_REQUIRE(fread(buf, 1, 3, f) == 3);
118*11be35a1SLionel Sambuc ATF_REQUIRE(fflush(f) == 0);
119*11be35a1SLionel Sambuc ATF_REQUIRE(fseek(f, 0, SEEK_CUR) == 0);
120*11be35a1SLionel Sambuc
121*11be35a1SLionel Sambuc /*
122*11be35a1SLionel Sambuc * Verify that the offsets are right and that
123*11be35a1SLionel Sambuc * a read operation resumes at the correct location.
124*11be35a1SLionel Sambuc */
125*11be35a1SLionel Sambuc ATF_REQUIRE(ftell(f) == 3);
126*11be35a1SLionel Sambuc ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == 3);
127*11be35a1SLionel Sambuc ATF_REQUIRE(fgetc(f) == 'b');
128*11be35a1SLionel Sambuc
129*11be35a1SLionel Sambuc ATF_REQUIRE(fclose(f) == 0);
130*11be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
131*11be35a1SLionel Sambuc }
132*11be35a1SLionel Sambuc
ATF_TC_CLEANUP(fflush_seek,tc)133*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fflush_seek, tc)
134*11be35a1SLionel Sambuc {
135*11be35a1SLionel Sambuc (void)unlink(path);
136*11be35a1SLionel Sambuc }
137*11be35a1SLionel Sambuc
138*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fpurge_err);
ATF_TC_HEAD(fpurge_err,tc)139*11be35a1SLionel Sambuc ATF_TC_HEAD(fpurge_err, tc)
140*11be35a1SLionel Sambuc {
141*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test errors from fpurge(3)");
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc
ATF_TC_BODY(fpurge_err,tc)144*11be35a1SLionel Sambuc ATF_TC_BODY(fpurge_err, tc)
145*11be35a1SLionel Sambuc {
146*11be35a1SLionel Sambuc FILE *f;
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc f = fopen(path, "w");
149*11be35a1SLionel Sambuc ATF_REQUIRE(f != NULL);
150*11be35a1SLionel Sambuc ATF_REQUIRE(fclose(f) == 0);
151*11be35a1SLionel Sambuc
152*11be35a1SLionel Sambuc errno = 0;
153*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EBADF, fpurge(f) == EOF);
154*11be35a1SLionel Sambuc
155*11be35a1SLionel Sambuc (void)unlink(path);
156*11be35a1SLionel Sambuc }
157*11be35a1SLionel Sambuc
ATF_TC_CLEANUP(fpurge_err,tc)158*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fpurge_err, tc)
159*11be35a1SLionel Sambuc {
160*11be35a1SLionel Sambuc (void)unlink(path);
161*11be35a1SLionel Sambuc }
162*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)163*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
164*11be35a1SLionel Sambuc {
165*11be35a1SLionel Sambuc
166*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, fflush_err);
167*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, fflush_seek);
168*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, fpurge_err);
169*11be35a1SLionel Sambuc
170*11be35a1SLionel Sambuc return atf_no_error();
171*11be35a1SLionel Sambuc }
172