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