summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/lib/libc/stdio
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/netbsd-tests/lib/libc/stdio')
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_clearerr.c93
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_fflush.c175
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_fmemopen.c1181
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_fopen.c444
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_fputc.c194
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_mktemp.c54
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_popen.c135
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_printf.c204
-rw-r--r--contrib/netbsd-tests/lib/libc/stdio/t_scanf.c85
9 files changed, 2565 insertions, 0 deletions
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_clearerr.c b/contrib/netbsd-tests/lib/libc/stdio/t_clearerr.c
new file mode 100644
index 0000000..251804e
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_clearerr.c
@@ -0,0 +1,93 @@
+/* $NetBSD: t_clearerr.c,v 1.1 2011/05/01 16:36:37 jruoho Exp $ */
+
+/*
+ * Copyright (c) 2009, Stathis Kamperis
+ * All rights reserved.
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_clearerr.c,v 1.1 2011/05/01 16:36:37 jruoho Exp $");
+
+#include <atf-c.h>
+#include <errno.h>
+#include <stdio.h>
+
+static const char path[] = "/etc/passwd";
+
+ATF_TC(clearerr_basic);
+ATF_TC_HEAD(clearerr_basic, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of clearerr(3)");
+}
+
+ATF_TC_BODY(clearerr_basic, tc)
+{
+ char buf[2048];
+ FILE *fp;
+
+ fp = fopen(path, "r");
+ ATF_REQUIRE(fp != NULL);
+
+ while (feof(fp) == 0)
+ (void)fread(buf, sizeof(buf), 1, fp);
+
+ ATF_REQUIRE(feof(fp) != 0);
+
+ errno = 0;
+ clearerr(fp);
+
+ ATF_REQUIRE(errno == 0);
+ ATF_REQUIRE(feof(fp) == 0);
+ ATF_REQUIRE(fclose(fp) != EOF);
+}
+
+ATF_TC(clearerr_err);
+ATF_TC_HEAD(clearerr_err, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test that clearerr(3) does not fail");
+}
+
+ATF_TC_BODY(clearerr_err, tc)
+{
+ FILE *fp;
+
+ fp = fopen(path, "r");
+
+ ATF_REQUIRE(fp != NULL);
+ ATF_REQUIRE(fclose(fp) == 0);
+
+ errno = 0;
+ clearerr(fp);
+
+ ATF_REQUIRE(errno == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, clearerr_basic);
+ ATF_TP_ADD_TC(tp, clearerr_err);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_fflush.c b/contrib/netbsd-tests/lib/libc/stdio/t_fflush.c
new file mode 100644
index 0000000..70498c3
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_fflush.c
@@ -0,0 +1,175 @@
+/* $NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $");
+
+#include <atf-c.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static const char *path = "fflush";
+
+ATF_TC_WITH_CLEANUP(fflush_err);
+ATF_TC_HEAD(fflush_err, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test errors from fflush(3)");
+}
+
+ATF_TC_BODY(fflush_err, tc)
+{
+ FILE *f;
+
+#ifdef __FreeBSD__
+ atf_tc_expect_fail("the EOF invariant fails on FreeBSD; this is new");
+#endif
+
+ f = fopen(path, "w");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fflush(NULL) == 0);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ f = fopen(path, "r");
+ ATF_REQUIRE(f != NULL);
+
+ /*
+ * In NetBSD the call should fail if the supplied
+ * parameteris not an open stream or the stream is
+ * not open for writing.
+ */
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
+
+ ATF_REQUIRE(fclose(f) == 0);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EBADF, fflush(f) == EOF);
+
+ (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(fflush_err, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fflush_seek);
+ATF_TC_HEAD(fflush_seek, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test file offsets with fflush(3)");
+}
+
+ATF_TC_BODY(fflush_seek, tc)
+{
+ char buf[12];
+ int fd = -1;
+ FILE *f;
+
+ /*
+ * IEEE Std 1003.1-2008:
+ *
+ * "For a stream open for reading, if the file
+ * is not already at EOF, and the file is one
+ * capable of seeking, the file offset of the
+ * underlying open file description shall be
+ * adjusted so that the next operation on the
+ * open file description deals with the byte
+ * after the last one read from or written to
+ * the stream being flushed."
+ */
+ f = fopen(path, "w");
+ ATF_REQUIRE(f != NULL);
+
+ ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ f = fopen(path, "r+");
+ ATF_REQUIRE(f != NULL);
+
+ fd = fileno(f);
+ ATF_REQUIRE(fd != -1);
+
+ ATF_REQUIRE(fread(buf, 1, 3, f) == 3);
+ ATF_REQUIRE(fflush(f) == 0);
+ ATF_REQUIRE(fseek(f, 0, SEEK_CUR) == 0);
+
+ /*
+ * Verify that the offsets are right and that
+ * a read operation resumes at the correct location.
+ */
+ ATF_REQUIRE(ftell(f) == 3);
+ ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == 3);
+ ATF_REQUIRE(fgetc(f) == 'b');
+
+ ATF_REQUIRE(fclose(f) == 0);
+ ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(fflush_seek, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fpurge_err);
+ATF_TC_HEAD(fpurge_err, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test errors from fpurge(3)");
+}
+
+ATF_TC_BODY(fpurge_err, tc)
+{
+ FILE *f;
+
+ f = fopen(path, "w");
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EBADF, fpurge(f) == EOF);
+
+ (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(fpurge_err, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, fflush_err);
+ ATF_TP_ADD_TC(tp, fflush_seek);
+ ATF_TP_ADD_TC(tp, fpurge_err);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_fmemopen.c b/contrib/netbsd-tests/lib/libc/stdio/t_fmemopen.c
new file mode 100644
index 0000000..31b76d0
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_fmemopen.c
@@ -0,0 +1,1181 @@
+/* $NetBSD: t_fmemopen.c,v 1.4 2013/10/19 17:45:00 christos Exp $ */
+
+/*-
+ * Copyright (c)2010 Takehiko NOZAKI,
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+#if defined(__FreeBSD__) || defined(__NetBSD__)
+#include <atf-c.h>
+#else
+#if defined(__linux__)
+#define _GNU_SOURCE
+#include <features.h>
+#endif
+#include <assert.h>
+#include <stdio.h>
+#define ATF_TC(arg0) static void arg0##_head(void)
+#define ATF_TC_HEAD(arg0, arg1) static void arg0##_head()
+#define atf_tc_set_md_var(arg0, arg1, ...) do { \
+ printf(__VA_ARGS__); \
+ puts(""); \
+} while (/*CONSTCOND*/0)
+#define ATF_TC_BODY(arg0, arg1) static void arg0##_body()
+#define ATF_CHECK(arg0) assert(arg0)
+#define ATF_TP_ADD_TCS(arg0) int main(void)
+#define ATF_TP_ADD_TC(arg0, arg1) arg1##_head(); arg1##_body()
+#define atf_no_error() 0
+#endif
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+
+const char *mode_rwa[] = {
+ "r", "rb", "r+", "rb+", "r+b",
+ "w", "wb", "w+", "wb+", "w+b",
+ "a", "ab", "a+", "ab+", "a+b",
+ NULL
+};
+
+const char *mode_r[] = { "r", "rb", "r+", "rb+", "r+b", NULL };
+const char *mode_w[] = { "w", "wb", "w+", "wb+", "w+b", NULL };
+const char *mode_a[] = { "a", "ab", "a+", "ab+", "a+b", NULL };
+
+struct testcase {
+ const char *s;
+ off_t n;
+} testcases[] = {
+#define TESTSTR(s) { s, sizeof(s)-1 }
+ TESTSTR("\0he quick brown fox jumps over the lazy dog"),
+ TESTSTR("T\0e quick brown fox jumps over the lazy dog"),
+ TESTSTR("Th\0 quick brown fox jumps over the lazy dog"),
+ TESTSTR("The\0quick brown fox jumps over the lazy dog"),
+ TESTSTR("The \0uick brown fox jumps over the lazy dog"),
+ TESTSTR("The q\0ick brown fox jumps over the lazy dog"),
+ TESTSTR("The qu\0ck brown fox jumps over the lazy dog"),
+ TESTSTR("The qui\0k brown fox jumps over the lazy dog"),
+ TESTSTR("The quic\0 brown fox jumps over the lazy dog"),
+ TESTSTR("The quick\0brown fox jumps over the lazy dog"),
+ TESTSTR("The quick \0rown fox jumps over the lazy dog"),
+ TESTSTR("The quick b\0own fox jumps over the lazy dog"),
+ TESTSTR("The quick br\0wn fox jumps over the lazy dog"),
+ TESTSTR("The quick bro\0n fox jumps over the lazy dog"),
+ TESTSTR("The quick brow\0 fox jumps over the lazy dog"),
+ TESTSTR("The quick brown\0fox jumps over the lazy dog"),
+ TESTSTR("The quick brown \0ox jumps over the lazy dog"),
+ TESTSTR("The quick brown f\0x jumps over the lazy dog"),
+ TESTSTR("The quick brown fo\0 jumps over the lazy dog"),
+ TESTSTR("The quick brown fox\0jumps over the lazy dog"),
+ TESTSTR("The quick brown fox \0umps over the lazy dog"),
+ TESTSTR("The quick brown fox j\0mps over the lazy dog"),
+ TESTSTR("The quick brown fox ju\0ps over the lazy dog"),
+ TESTSTR("The quick brown fox jum\0s over the lazy dog"),
+ TESTSTR("The quick brown fox jump\0 over the lazy dog"),
+ TESTSTR("The quick brown fox jumps\0over the lazy dog"),
+ TESTSTR("The quick brown fox jumps \0ver the lazy dog"),
+ TESTSTR("The quick brown fox jumps o\0er the lazy dog"),
+ TESTSTR("The quick brown fox jumps ov\0r the lazy dog"),
+ TESTSTR("The quick brown fox jumps ove\0 the lazy dog"),
+ TESTSTR("The quick brown fox jumps over\0the lazy dog"),
+ TESTSTR("The quick brown fox jumps over \0he lazy dog"),
+ TESTSTR("The quick brown fox jumps over t\0e lazy dog"),
+ TESTSTR("The quick brown fox jumps over th\0 lazy dog"),
+ TESTSTR("The quick brown fox jumps over the\0lazy dog"),
+ TESTSTR("The quick brown fox jumps over the \0azy dog"),
+ TESTSTR("The quick brown fox jumps over the l\0zy dog"),
+ TESTSTR("The quick brown fox jumps over the la\0y dog"),
+ TESTSTR("The quick brown fox jumps over the laz\0 dog"),
+ TESTSTR("The quick brown fox jumps over the lazy\0dog"),
+ TESTSTR("The quick brown fox jumps over the lazy \0og"),
+ TESTSTR("The quick brown fox jumps over the lazy d\0g"),
+ TESTSTR("The quick brown fox jumps over the lazy do\0"),
+ TESTSTR("The quick brown fox jumps over the lazy dog"),
+ { NULL, 0 },
+};
+
+ATF_TC(test00);
+ATF_TC_HEAD(test00, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test00");
+}
+ATF_TC_BODY(test00, tc)
+{
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (p = &mode_rwa[0]; *p != NULL; ++p) {
+ fp = fmemopen(&buf[0], sizeof(buf), *p);
+/*
+ * Upon successful completion, fmemopen() shall return a pointer to the
+ * object controlling the stream.
+ */
+ ATF_CHECK(fp != NULL);
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+}
+
+ATF_TC(test01);
+ATF_TC_HEAD(test01, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test01");
+}
+ATF_TC_BODY(test01, tc)
+{
+ const char **p;
+ const char *mode[] = {
+ "r+", "rb+", "r+b",
+ "w+", "wb+", "w+b",
+ "a+", "ab+", "a+b",
+ NULL
+ };
+ FILE *fp;
+
+ for (p = &mode[0]; *p != NULL; ++p) {
+/*
+ * If a null pointer is specified as the buf argument, fmemopen() shall
+ * allocate size bytes of memory as if by a call to malloc().
+ */
+ fp = fmemopen(NULL, BUFSIZ, *p);
+ ATF_CHECK(fp != NULL);
+
+/*
+ * If buf is a null pointer, the initial position shall always be set
+ * to the beginning of the buffer.
+ */
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+}
+
+ATF_TC(test02);
+ATF_TC_HEAD(test02, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test02");
+}
+ATF_TC_BODY(test02, tc)
+{
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (p = &mode_r[0]; *p != NULL; ++p) {
+
+ memset(&buf[0], 0x1, sizeof(buf));
+ fp = fmemopen(&buf[0], sizeof(buf), *p);
+ ATF_CHECK(fp != NULL);
+
+/*
+ * This position is initially set to either the beginning of the buffer
+ * (for r and w modes)
+ */
+ ATF_CHECK((unsigned char)buf[0] == 0x1);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+/*
+ * The stream also maintains the size of the current buffer contents.
+ * For modes r and r+ the size is set to the value given by the size argument.
+ */
+#if !defined(__GLIBC__)
+ ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
+#endif
+ ATF_CHECK(fclose(fp) == 0);
+ }
+}
+
+ATF_TC(test03);
+ATF_TC_HEAD(test03, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test03");
+}
+ATF_TC_BODY(test03, tc)
+{
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (p = &mode_w[0]; *p != NULL; ++p) {
+
+ memset(&buf[0], 0x1, sizeof(buf));
+ fp = fmemopen(&buf[0], sizeof(buf), *p);
+ ATF_CHECK(fp != NULL);
+
+/*
+ * This position is initially set to either the beginning of the buffer
+ * (for r and w modes)
+ */
+ ATF_CHECK(buf[0] == '\0');
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+/*
+ * For modes w and w+ the initial size is zero
+ */
+ ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+}
+
+ATF_TC(test04);
+ATF_TC_HEAD(test04, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test04");
+}
+ATF_TC_BODY(test04, tc)
+{
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+/*
+ * or to the first null byte in the buffer (for a modes)
+ */
+ for (p = &mode_a[0]; *p != NULL; ++p) {
+
+ memset(&buf[0], 0x1, sizeof(buf));
+ fp = fmemopen(&buf[0], sizeof(buf), *p);
+ ATF_CHECK(fp != NULL);
+
+ ATF_CHECK((unsigned char)buf[0] == 0x1);
+
+/* If no null byte is found in append mode,
+ * the initial position is set to one byte after the end of the buffer.
+ */
+#if !defined(__GLIBC__)
+ ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
+#endif
+
+/*
+ * and for modes a and a+ the initial size is either the position of the
+ * first null byte in the buffer or the value of the size argument
+ * if no null byte is found.
+ */
+#if !defined(__GLIBC__)
+ ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
+#endif
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+}
+
+ATF_TC(test05);
+ATF_TC_HEAD(test05, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test05");
+}
+ATF_TC_BODY(test05, tc)
+{
+ const char **p;
+ FILE *fp;
+ char buf[BUFSIZ];
+
+ for (p = &mode_rwa[0]; *p != NULL; ++p) {
+/*
+ * Otherwise, a null pointer shall be returned, and errno shall be set
+ * to indicate the error.
+ */
+ errno = 0;
+ fp = fmemopen(NULL, (size_t)0, *p);
+ ATF_CHECK(fp == NULL);
+ ATF_CHECK(errno == EINVAL);
+
+ errno = 0;
+ fp = fmemopen((void *)&buf[0], 0, *p);
+ ATF_CHECK(fp == NULL);
+ ATF_CHECK(errno == EINVAL);
+ }
+}
+
+ATF_TC(test06);
+ATF_TC_HEAD(test06, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test06");
+}
+ATF_TC_BODY(test06, tc)
+{
+ const char **p;
+ const char *mode[] = { "", " ", "???", NULL };
+ FILE *fp;
+
+ for (p = &mode[0]; *p != NULL; ++p) {
+/*
+ * The value of the mode argument is not valid.
+ */
+ fp = fmemopen(NULL, 1, *p);
+ ATF_CHECK(fp == NULL);
+ ATF_CHECK(errno == EINVAL);
+ }
+}
+
+ATF_TC(test07);
+ATF_TC_HEAD(test07, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test07");
+}
+ATF_TC_BODY(test07, tc)
+{
+#if !defined(__GLIBC__)
+ const char **p;
+ const char *mode[] = {
+ "r", "rb",
+ "w", "wb",
+ "a", "ab",
+ NULL
+ };
+ FILE *fp;
+
+ for (p = &mode[0]; *p != NULL; ++p) {
+/*
+ * Because this feature is only useful when the stream is opened for updating
+ * (because there is no way to get a pointer to the buffer) the fmemopen()
+ * call may fail if the mode argument does not include a '+' .
+ */
+ errno = 0;
+ fp = fmemopen(NULL, 1, *p);
+ ATF_CHECK(fp == NULL);
+ ATF_CHECK(errno == EINVAL);
+ }
+#endif
+}
+
+ATF_TC(test08);
+ATF_TC_HEAD(test08, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test08");
+}
+ATF_TC_BODY(test08, tc)
+{
+#if !defined(__GLIBC__)
+ const char **p;
+ const char *mode[] = {
+ "r+", "rb+", "r+b",
+ "w+", "wb+", "w+b",
+ "a+", "ab+", "a+b",
+ NULL
+ };
+ FILE *fp;
+
+ for (p = &mode[0]; *p != NULL; ++p) {
+/*
+ * The buf argument is a null pointer and the allocation of a buffer of
+ * length size has failed.
+ */
+ fp = fmemopen(NULL, SIZE_MAX, *p);
+ ATF_CHECK(fp == NULL);
+ ATF_CHECK(errno == ENOMEM);
+ }
+#endif
+}
+
+/*
+ * test09 - test14:
+ * An attempt to seek a memory buffer stream to a negative position or to a
+ * position larger than the buffer size given in the size argument shall fail.
+ */
+
+ATF_TC(test09);
+ATF_TC_HEAD(test09, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test09");
+}
+ATF_TC_BODY(test09, tc)
+{
+ struct testcase *t;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+ off_t i;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ for (p = &mode_rwa[0]; *p != NULL; ++p) {
+
+ memcpy(&buf[0], t->s, t->n);
+ fp = fmemopen(&buf[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+
+/*
+ * test fmemopen_seek(SEEK_SET)
+ */
+ /* zero */
+ ATF_CHECK(fseeko(fp, (off_t)0, SEEK_SET) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ /* positive */
+ for (i = (off_t)1; i <= (off_t)t->n; ++i) {
+ ATF_CHECK(fseeko(fp, i, SEEK_SET) == 0);
+ ATF_CHECK(ftello(fp) == i);
+ }
+ /* positive + OOB */
+ ATF_CHECK(fseeko(fp, t->n + 1, SEEK_SET) == -1);
+ ATF_CHECK(ftello(fp) == t->n);
+
+ /* negative + OOB */
+ ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_SET) == -1);
+ ATF_CHECK(ftello(fp) == t->n);
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+const char *mode_rw[] = {
+ "r", "rb", "r+", "rb+", "r+b",
+ "w", "wb", "w+", "wb+", "w+b",
+ NULL
+};
+
+ATF_TC(test10);
+ATF_TC_HEAD(test10, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test10");
+}
+ATF_TC_BODY(test10, tc)
+{
+ struct testcase *t;
+ off_t i;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ for (p = &mode_rw[0]; *p != NULL; ++p) {
+
+ memcpy(&buf[0], t->s, t->n);
+ fp = fmemopen(&buf[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+
+/*
+ * test fmemopen_seek(SEEK_CUR)
+ */
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ /* zero */
+ ATF_CHECK(fseeko(fp, (off_t)0, SEEK_CUR) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ /* negative & OOB */
+ ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == -1);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ /* positive */
+ for (i = 0; i < (off_t)t->n; ++i) {
+ ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == 0);
+ ATF_CHECK(ftello(fp) == i + 1);
+ }
+
+ /* positive & OOB */
+ ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == -1);
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+ATF_TC(test11);
+ATF_TC_HEAD(test11, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test11");
+}
+ATF_TC_BODY(test11, tc)
+{
+ struct testcase *t;
+ off_t len, rest, i;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ /* test fmemopen_seek(SEEK_CUR) */
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ len = (off_t)strnlen(t->s, t->n);
+ rest = (off_t)t->n - len;
+ for (p = &mode_a[0]; *p != NULL; ++p) {
+
+ memcpy(&buf[0], t->s, t->n);
+ fp = fmemopen(&buf[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+/*
+ * test fmemopen_seek(SEEK_CUR)
+ */
+#if defined(__GLIBC__)
+ if (i < (off_t)t->n) {
+#endif
+ /* zero */
+ ATF_CHECK(fseeko(fp, (off_t)0, SEEK_CUR) == 0);
+ ATF_CHECK(ftello(fp) == len);
+
+ /* posive */
+ for (i = (off_t)1; i <= rest; ++i) {
+ ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == 0);
+ ATF_CHECK(ftello(fp) == len + i);
+ }
+
+ /* positive + OOB */
+ ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == -1);
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+
+ /* negative */
+ for (i = (off_t)1; i <= (off_t)t->n; ++i) {
+ ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)t->n - i);
+ }
+
+ /* negative + OOB */
+ ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == -1);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+#if defined(__GLIBC__)
+ }
+#endif
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+#ifndef __FreeBSD__
+ATF_TC(test12);
+ATF_TC_HEAD(test12, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test12");
+}
+ATF_TC_BODY(test12, tc)
+{
+ struct testcase *t;
+ off_t len, rest, i;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ /* test fmemopen_seek(SEEK_END) */
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ len = (off_t)strnlen(t->s, t->n);
+ rest = t->n - len;
+ for (p = &mode_r[0]; *p != NULL; ++p) {
+
+ memcpy(buf, t->s, t->n);
+ fp = fmemopen(&buf[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+
+/*
+ * test fmemopen_seek(SEEK_END)
+ */
+#if !defined(__GLIBC__)
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ /* zero */
+ ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == len);
+
+ /* positive + OOB */
+ ATF_CHECK(fseeko(fp, rest + 1, SEEK_END) == -1);
+ ATF_CHECK(ftello(fp) == len);
+
+ /* negative + OOB */
+ ATF_CHECK(fseeko(fp, -(len + 1), SEEK_END) == -1);
+ ATF_CHECK(ftello(fp) == len);
+
+ /* positive */
+ for (i = 1; i <= rest; ++i) {
+ ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == len + i);
+ }
+
+ /* negative */
+ for (i = 1; i < len; ++i) {
+ ATF_CHECK(fseeko(fp, -i, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == len - i);
+ }
+#endif
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+#endif
+
+ATF_TC(test13);
+ATF_TC_HEAD(test13, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test13");
+}
+ATF_TC_BODY(test13, tc)
+{
+ struct testcase *t;
+#ifndef __FreeBSD__
+ off_t i;
+#endif
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ /* test fmemopen_seek(SEEK_END) */
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ for (p = &mode_w[0]; *p != NULL; ++p) {
+
+ memcpy(buf, t->s, t->n);
+ fp = fmemopen(&buf[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+/*
+ * test fmemopen_seek(SEEK_END)
+ */
+#if !defined(__GLIBC__)
+ ATF_CHECK(ftello(fp) == (off_t)0);
+ ATF_CHECK(buf[0] == '\0');
+
+ /* zero */
+ ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ /* positive + OOB */
+ ATF_CHECK(fseeko(fp, (off_t)t->n + 1, SEEK_END) == -1);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+
+ /* negative + OOB */
+ ATF_CHECK(fseeko(fp, -1, SEEK_END) == -1);
+ ATF_CHECK(ftello(fp) == (off_t)0);
+#endif
+
+#ifndef __FreeBSD__
+ /* positive */
+ for (i = 1; i <= t->n; ++i) {
+ ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == i);
+ }
+#endif
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+ATF_TC(test14);
+ATF_TC_HEAD(test14, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test14");
+}
+ATF_TC_BODY(test14, tc)
+{
+ struct testcase *t;
+ off_t len, rest, i;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ /* test fmemopen_seek(SEEK_END) */
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ len = (off_t)strnlen(t->s, t->n);
+ rest = (off_t)t->n - len;
+ for (p = &mode_a[0]; *p != NULL; ++p) {
+
+ memcpy(buf, t->s, t->n);
+ fp = fmemopen(&buf[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+/*
+ * test fmemopen_seek(SEEK_END)
+ */
+#if !defined(__GLIBC__)
+ ATF_CHECK(ftello(fp) == len);
+
+ /* zero */
+ ATF_CHECK(fseeko(fp, 0, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == len);
+
+ /* positive + OOB */
+ ATF_CHECK(fseeko(fp, rest + 1, SEEK_END) == -1);
+ ATF_CHECK(ftello(fp) == len);
+
+ /* negative + OOB */
+ ATF_CHECK(fseeko(fp, -(len + 1), SEEK_END) == -1);
+ ATF_CHECK(ftello(fp) == len);
+
+#ifndef __FreeBSD__
+ /* positive */
+ for (i = 1; i <= rest; ++i) {
+ ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == len + i);
+ }
+#endif
+
+ /* negative */
+ for (i = 1; i < len; ++i) {
+ ATF_CHECK(fseeko(fp, -i, SEEK_END) == 0);
+ ATF_CHECK(ftello(fp) == len - i);
+ }
+#endif
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+const char *mode_rw1[] = {
+ "r", "rb", "r+", "rb+", "r+b",
+ "w+", "wb+",
+ NULL
+};
+
+#ifndef __FreeBSD__
+
+/* test15 - 18:
+ * When a stream open for writing is flushed or closed, a null byte is written
+ * at the current position or at the end of the buffer, depending on the size
+ * of the contents.
+ */
+
+ATF_TC(test15);
+ATF_TC_HEAD(test15, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test15");
+}
+ATF_TC_BODY(test15, tc)
+{
+ struct testcase *t;
+ const char **p;
+ char buf0[BUFSIZ];
+ FILE *fp;
+ int i;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ for (p = &mode_rw1[0]; *p != NULL; ++p) {
+
+ memcpy(&buf0[0], t->s, t->n);
+ fp = fmemopen(&buf0[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+/*
+ * test fmemopen_read + fgetc(3)
+ */
+ for (i = 0; i < t->n; ++i) {
+ ATF_CHECK(ftello(fp) == (off_t)i);
+ ATF_CHECK(fgetc(fp) == buf0[i]);
+ ATF_CHECK(feof(fp) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)i + 1);
+ }
+ ATF_CHECK(fgetc(fp) == EOF);
+ ATF_CHECK(feof(fp) != 0);
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+ATF_TC(test16);
+ATF_TC_HEAD(test16, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test16");
+}
+ATF_TC_BODY(test16, tc)
+{
+ struct testcase *t;
+ const char **p;
+ char buf0[BUFSIZ], buf1[BUFSIZ];
+ FILE *fp;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ for (p = &mode_rw1[0]; *p != NULL; ++p) {
+
+ memcpy(&buf0[0], t->s, t->n);
+ buf1[t->n] = 0x1;
+ fp = fmemopen(&buf0[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+/*
+ * test fmemopen_read + fread(4)
+ */
+ ATF_CHECK(ftello(fp) == (off_t)0);
+ ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp) == (size_t)t->n);
+ ATF_CHECK(feof(fp) != 0);
+ ATF_CHECK(memcmp(&buf0[0], &buf1[0], t->n) == 0);
+ ATF_CHECK((unsigned char)buf1[t->n] == 0x1);
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+const char *mode_a1[] = { "a+", "ab+", NULL };
+
+ATF_TC(test17);
+ATF_TC_HEAD(test17, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test17");
+}
+ATF_TC_BODY(test17, tc)
+{
+ struct testcase *t;
+ size_t len;
+ int i;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ len = strnlen(t->s, t->n);
+ for (p = &mode_a1[0]; *p != NULL; ++p) {
+
+ memcpy(&buf[0], t->s, t->n);
+ fp = fmemopen(&buf[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+/*
+ * test fmemopen_read + fgetc(3)
+ */
+#if defined(__GLIBC__)
+ if (i < t->n) {
+#endif
+ for (i = len; i < t->n; ++i) {
+ ATF_CHECK(ftello(fp) == (off_t)i);
+ ATF_CHECK(fgetc(fp) == buf[i]);
+ ATF_CHECK(feof(fp) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)i + 1);
+ }
+ ATF_CHECK(fgetc(fp) == EOF);
+ ATF_CHECK(feof(fp) != 0);
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+ rewind(fp);
+ for (i = 0; i < t->n; ++i) {
+ ATF_CHECK(ftello(fp) == (off_t)i);
+ ATF_CHECK(fgetc(fp) == buf[i]);
+ ATF_CHECK(feof(fp) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)i + 1);
+ }
+ ATF_CHECK(fgetc(fp) == EOF);
+ ATF_CHECK(feof(fp) != 0);
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+#if defined(__GLIBC__)
+ }
+#endif
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+ATF_TC(test18);
+ATF_TC_HEAD(test18, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test18");
+}
+ATF_TC_BODY(test18, tc)
+{
+ struct testcase *t;
+ size_t len;
+ const char **p;
+ char buf0[BUFSIZ], buf1[BUFSIZ];
+ FILE *fp;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ len = strnlen(t->s, t->n);
+ for (p = &mode_a1[0]; *p != NULL; ++p) {
+
+ memcpy(&buf0[0], t->s, t->n);
+ buf1[t->n - len] = 0x1;
+ fp = fmemopen(&buf0[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+/*
+ * test fmemopen_read + fread(3)
+ */
+#if defined(__GLIBC__)
+ if (i < t->n) {
+#endif
+ ATF_CHECK(ftello(fp) == (off_t)len);
+ ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp)
+ == t->n - len);
+ ATF_CHECK(feof(fp) != 0);
+ ATF_CHECK(!memcmp(&buf0[len], &buf1[0], t->n - len));
+ ATF_CHECK((unsigned char)buf1[t->n - len] == 0x1);
+ rewind(fp);
+ buf1[t->n] = 0x1;
+ ATF_CHECK(ftello(fp) == (off_t)0);
+ ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp)
+ == (size_t)t->n);
+ ATF_CHECK(feof(fp) != 0);
+ ATF_CHECK(!memcmp(&buf0[0], &buf1[0], t->n));
+ ATF_CHECK((unsigned char)buf1[t->n] == 0x1);
+#if defined(__GLIBC__)
+ }
+#endif
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+/*
+ * test19 - test22:
+ * If a stream open for update is flushed or closed and the last write has
+ * advanced the current buffer size, a null byte is written at the end of the
+ * buffer if it fits.
+ */
+
+const char *mode_rw2[] = {
+ "r+", "rb+", "r+b",
+ "w", "wb", "w+", "wb+", "w+b",
+ NULL
+};
+
+ATF_TC(test19);
+ATF_TC_HEAD(test19, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test19");
+}
+ATF_TC_BODY(test19, tc)
+{
+ struct testcase *t;
+ int i;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ for (p = &mode_rw2[0]; *p != NULL; ++p) {
+
+ memcpy(&buf[0], t->s, t->n);
+ buf[t->n] = 0x1;
+ fp = fmemopen(&buf[0], t->n + 1, *p);
+ ATF_CHECK(fp != NULL);
+ setbuf(fp, NULL);
+/*
+ * test fmemopen_write + fputc(3)
+ */
+ for (i = 0; i < t->n; ++i) {
+ ATF_CHECK(ftello(fp) == (off_t)i);
+ ATF_CHECK(fputc(t->s[i], fp) == t->s[i]);
+ ATF_CHECK(buf[i] == t->s[i]);
+ ATF_CHECK(ftello(fp) == (off_t)i + 1);
+ ATF_CHECK(buf[i] == t->s[i]);
+#if !defined(__GLIBC__)
+ ATF_CHECK(buf[i + 1] == '\0');
+#endif
+ }
+
+/* don't accept non nul character at end of buffer */
+ ATF_CHECK(fputc(0x1, fp) == EOF);
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+ ATF_CHECK(feof(fp) == 0);
+
+/* accept nul character at end of buffer */
+ ATF_CHECK(fputc('\0', fp) == '\0');
+ ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
+ ATF_CHECK(feof(fp) == 0);
+
+/* reach EOF */
+ ATF_CHECK(fputc('\0', fp) == EOF);
+ ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
+
+ /* compare */
+ ATF_CHECK(memcmp(&buf[0], t->s, t->n) == 0);
+ ATF_CHECK(buf[t->n] == '\0');
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+ATF_TC(test20);
+ATF_TC_HEAD(test20, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test20");
+}
+ATF_TC_BODY(test20, tc)
+{
+ struct testcase *t;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ for (p = &mode_rw2[0]; *p != NULL; ++p) {
+
+ memcpy(&buf[0], t->s, t->n);
+ buf[t->n] = 0x1;
+ fp = fmemopen(&buf[0], t->n + 1, *p);
+ ATF_CHECK(fp != NULL);
+ setbuf(fp, NULL);
+ ATF_CHECK(fwrite(t->s, 1, t->n, fp) == (size_t)t->n);
+/*
+ * test fmemopen_write + fwrite(3)
+ */
+#if !defined(__GLIBC__)
+ ATF_CHECK(buf[t->n] == '\0');
+
+/* don't accept non nul character at end of buffer */
+ ATF_CHECK(fwrite("\x1", 1, 1, fp) == 0);
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+ ATF_CHECK(feof(fp) == 0);
+#endif
+
+/* accept nul character at end of buffer */
+ ATF_CHECK(fwrite("\x0", 1, 1, fp) == 1);
+ ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
+ ATF_CHECK(feof(fp) == 0);
+
+/* reach EOF */
+ ATF_CHECK(fputc('\0', fp) == EOF);
+ ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
+
+/* compare */
+ ATF_CHECK(memcmp(&buf[0], t->s, t->n) == 0);
+ ATF_CHECK(buf[t->n] == '\0');
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+ATF_TC(test21);
+ATF_TC_HEAD(test21, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test21");
+}
+ATF_TC_BODY(test21, tc)
+{
+ struct testcase *t;
+ int len, i;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (t = &testcases[0]; t->s != NULL; ++t) {
+ len = strnlen(t->s, t->n);
+ for (p = &mode_a[0]; *p != NULL; ++p) {
+ memcpy(&buf[0], t->s, t->n);
+ fp = fmemopen(&buf[0], t->n, *p);
+ ATF_CHECK(fp != NULL);
+ setbuf(fp, NULL);
+/*
+ * test fmemopen_write + fputc(3)
+ */
+ if (len < t->n) {
+ for (i = len; i < t->n - 1; ++i) {
+ ATF_CHECK(ftello(fp) == (off_t)i);
+ ATF_CHECK(fputc(t->s[i - len], fp)
+ == t->s[i - len]);
+ ATF_CHECK(buf[i] == t->s[i - len]);
+ ATF_CHECK(ftello(fp) == (off_t)i + 1);
+#if !defined(__GLIBC__)
+ ATF_CHECK(buf[i + 1] == '\0');
+#endif
+ }
+
+/* don't accept non nul character at end of buffer */
+ ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
+ ATF_CHECK(fputc(0x1, fp) == EOF);
+ ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
+
+/* accept nul character at end of buffer */
+ ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
+ ATF_CHECK(fputc('\0', fp) == '\0');
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+ }
+
+/* reach EOF */
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+ ATF_CHECK(fputc('\0', fp) == EOF);
+ ATF_CHECK(ftello(fp) == (off_t)t->n);
+
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+}
+
+ATF_TC(test22);
+ATF_TC_HEAD(test22, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "test22");
+}
+ATF_TC_BODY(test22, tc)
+{
+ struct testcase *t0, *t1;
+ size_t len0, len1, nleft;
+ const char **p;
+ char buf[BUFSIZ];
+ FILE *fp;
+
+ for (t0 = &testcases[0]; t0->s != NULL; ++t0) {
+ len0 = strnlen(t0->s, t0->n);
+ for (t1 = &testcases[0]; t1->s != NULL; ++t1) {
+ len1 = strnlen(t1->s, t1->n);
+ for (p = &mode_a[0]; *p != NULL; ++p) {
+
+ memcpy(&buf[0], t0->s, t0->n);
+ fp = fmemopen(&buf[0], t0->n, *p);
+ ATF_CHECK(fp != NULL);
+ setbuf(fp, NULL);
+/*
+ * test fmemopen_write + fwrite(3)
+ */
+ nleft = t0->n - len0;
+#if !defined(__GLIBC__)
+ if (nleft == 0 || len1 == nleft - 1) {
+ ATF_CHECK(fwrite(t1->s, 1, t1->n, fp)
+ == nleft);
+ ATF_CHECK(ftell(fp) == t1->n);
+ } else {
+ ATF_CHECK(fwrite(t1->s, 1, t1->n, fp)
+ == nleft - 1);
+ ATF_CHECK(ftell(fp) == t1->n - 1);
+ }
+#endif
+ ATF_CHECK(fclose(fp) == 0);
+ }
+ }
+ }
+}
+#endif
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, test00);
+ ATF_TP_ADD_TC(tp, test01);
+ ATF_TP_ADD_TC(tp, test02);
+ ATF_TP_ADD_TC(tp, test03);
+ ATF_TP_ADD_TC(tp, test04);
+ ATF_TP_ADD_TC(tp, test05);
+ ATF_TP_ADD_TC(tp, test06);
+ ATF_TP_ADD_TC(tp, test07);
+ ATF_TP_ADD_TC(tp, test08);
+ ATF_TP_ADD_TC(tp, test09);
+ ATF_TP_ADD_TC(tp, test10);
+ ATF_TP_ADD_TC(tp, test11);
+#ifndef __FreeBSD__
+ ATF_TP_ADD_TC(tp, test12);
+#endif
+ ATF_TP_ADD_TC(tp, test13);
+ ATF_TP_ADD_TC(tp, test14);
+#ifndef __FreeBSD__
+ ATF_TP_ADD_TC(tp, test15);
+ ATF_TP_ADD_TC(tp, test16);
+ ATF_TP_ADD_TC(tp, test17);
+ ATF_TP_ADD_TC(tp, test18);
+ ATF_TP_ADD_TC(tp, test19);
+ ATF_TP_ADD_TC(tp, test20);
+ ATF_TP_ADD_TC(tp, test21);
+ ATF_TP_ADD_TC(tp, test22);
+#endif
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_fopen.c b/contrib/netbsd-tests/lib/libc/stdio/t_fopen.c
new file mode 100644
index 0000000..d9e2dd6
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_fopen.c
@@ -0,0 +1,444 @@
+/* $NetBSD: t_fopen.c,v 1.3 2011/09/14 14:34:37 martin Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_fopen.c,v 1.3 2011/09/14 14:34:37 martin Exp $");
+
+#include <atf-c.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <paths.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+static const char *path = "fopen";
+
+ATF_TC_WITH_CLEANUP(fdopen_close);
+ATF_TC_HEAD(fdopen_close, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "See that descriptors are closed");
+}
+
+ATF_TC_BODY(fdopen_close, tc)
+{
+ FILE *f;
+ int fd;
+
+ /*
+ * Check that the file descriptor
+ * used to fdopen(3) a stream is
+ * closed once the stream is closed.
+ */
+ fd = open(path, O_RDWR | O_CREAT);
+
+ ATF_REQUIRE(fd >= 0);
+
+ f = fdopen(fd, "w+");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fclose(f) == 0);
+ ATF_REQUIRE(close(fd) == -1);
+ ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(fdopen_close, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fdopen_err);
+ATF_TC_HEAD(fdopen_err, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test errors from fdopen(3)");
+}
+
+ATF_TC_BODY(fdopen_err, tc)
+{
+ int fd;
+
+ fd = open(path, O_RDONLY | O_CREAT);
+ ATF_REQUIRE(fd >= 0);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "w") == NULL);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "a") == NULL);
+
+ ATF_REQUIRE(close(fd) == 0);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EBADF, fdopen(fd, "r") == NULL);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EBADF, fdopen(-1, "w+") == NULL);
+
+ (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(fdopen_err, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fdopen_seek);
+ATF_TC_HEAD(fdopen_seek, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test stream position with fdopen(3)");
+}
+
+ATF_TC_BODY(fdopen_seek, tc)
+{
+ FILE *f;
+ int fd;
+
+ /*
+ * Verify that the file position associated
+ * with the stream corresponds with the offset
+ * set earlier for the file descriptor.
+ */
+ fd = open(path, O_RDWR | O_CREAT);
+
+ ATF_REQUIRE(fd >= 0);
+ ATF_REQUIRE(write(fd, "garbage", 7) == 7);
+ ATF_REQUIRE(lseek(fd, 3, SEEK_SET) == 3);
+
+ f = fdopen(fd, "r+");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(ftell(f) == 3);
+ ATF_REQUIRE(fclose(f) == 0);
+ ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(fdopen_seek, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fopen_err);
+ATF_TC_HEAD(fopen_err, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test errors from fopen(3)");
+}
+
+ATF_TC_BODY(fopen_err, tc)
+{
+ static const char *mode[] = {
+ "x", "xr", "xr", "+r+", "R", "W+", " aXX", "Xr", " r+", "" };
+
+ char buf[PATH_MAX + 1];
+ size_t i;
+ FILE *f;
+
+ f = fopen(path, "w+");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ /*
+ * Note that also "invalid" characters
+ * may follow the mode-string whenever
+ * the first character is valid.
+ */
+ for (i = 0; i < __arraycount(mode); i++) {
+
+ errno = 0;
+ f = fopen(path, mode[i]);
+
+ if (f == NULL && errno == EINVAL)
+ continue;
+
+ if (f != NULL)
+ (void)fclose(f);
+
+ atf_tc_fail_nonfatal("opened file as '%s'", mode[i]);
+ }
+
+ (void)unlink(path);
+ (void)memset(buf, 'x', sizeof(buf));
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EISDIR, fopen("/usr/bin", "w") == NULL);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(ENOENT, fopen("/a/b/c/d/e/f", "r") == NULL);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(ENAMETOOLONG, fopen(buf, "r+") == NULL);
+}
+
+ATF_TC_CLEANUP(fopen_err, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fopen_append);
+ATF_TC_HEAD(fopen_append, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test that append-mode works");
+}
+
+ATF_TC_BODY(fopen_append, tc)
+{
+ char buf[15];
+ FILE *f;
+
+ (void)memset(buf, 'x', sizeof(buf));
+
+ f = fopen(path, "w+");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ f = fopen(path, "a");
+
+ ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ f = fopen(path, "r");
+
+ ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
+ ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);
+
+ ATF_REQUIRE(fclose(f) == 0);
+ ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(fopen_append, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fopen_mode);
+ATF_TC_HEAD(fopen_mode, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test fopen(3) modes");
+}
+
+ATF_TC_BODY(fopen_mode, tc)
+{
+ size_t i;
+ FILE *f;
+
+ static const char *mode[] = {
+ "r", "r+", "w", "w+", "a", "a+",
+ "rb", "r+b", "wb", "w+b", "ab", "a+b"
+ "re", "r+e", "we", "w+e", "ae", "a+e"
+ "rf", "r+f", "wf", "w+f", "af", "a+f"
+ };
+
+ f = fopen(path, "w+");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ /*
+ * Verify that various modes work.
+ */
+ for (i = 0; i < __arraycount(mode); i++) {
+
+ f = fopen(path, mode[i]);
+
+ if (f != NULL) {
+ ATF_REQUIRE(fclose(f) == 0);
+ continue;
+ }
+
+ atf_tc_fail_nonfatal("failed to open file as %s", mode[i]);
+ }
+
+ (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(fopen_mode, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC(fopen_perm);
+ATF_TC_HEAD(fopen_perm, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test permissions with fopen(3)");
+ atf_tc_set_md_var(tc, "require.user", "unprivileged");
+}
+
+ATF_TC_BODY(fopen_perm, tc)
+{
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "a+") == NULL);
+
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "w+") == NULL);
+}
+
+ATF_TC(fopen_regular);
+ATF_TC_HEAD(fopen_regular, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test fopen(3) with 'f' mode");
+}
+
+ATF_TC_BODY(fopen_regular, tc)
+{
+ static const char *mode[] = { "rf", "r+f", "wf", "w+f", "af", "a+f" };
+ static const char *devs[] = { _PATH_DEVNULL };
+
+ size_t i, j;
+ FILE *f;
+
+ for (i = 0; i < __arraycount(devs); i++) {
+
+ for (j = 0; j < __arraycount(mode); j++) {
+
+ errno = 0;
+ f = fopen(devs[i], mode[j]);
+
+ if (f == NULL && errno == EFTYPE)
+ continue;
+
+ if (f != NULL)
+ (void)fclose(f);
+
+ atf_tc_fail_nonfatal("opened %s as %s",
+ devs[i], mode[j]);
+ }
+ }
+}
+
+ATF_TC_WITH_CLEANUP(fopen_seek);
+ATF_TC_HEAD(fopen_seek, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test initial stream position");
+}
+
+ATF_TC_BODY(fopen_seek, tc)
+{
+ FILE *f;
+
+ f = fopen(path, "w+");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ /*
+ * The position of the stream should be
+ * at the start, except for append-mode.
+ */
+ f = fopen(path, "r");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(ftello(f) == 0);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ f = fopen(path, "a");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(ftello(f) == 7);
+ ATF_REQUIRE(fclose(f) == 0);
+ ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(fopen_seek, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(freopen_std);
+ATF_TC_HEAD(freopen_std, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of freopen(3)");
+}
+
+ATF_TC_BODY(freopen_std, tc)
+{
+ FILE *std[2] = { stdin, stdout };
+ char buf[15];
+ size_t i;
+ FILE *f;
+
+ /*
+ * Associate a standard stream with a custom stream.
+ * Then write to the standard stream and verify that
+ * the result now appears in the custom stream.
+ */
+ for (i = 0; i < __arraycount(std); i++) {
+
+ (void)memset(buf, 'x', sizeof(buf));
+
+ f = fopen(path, "w+");
+ ATF_REQUIRE(f != NULL);
+
+ f = freopen(path, "w+", std[i]);
+ ATF_REQUIRE(f != NULL);
+
+ ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
+ ATF_REQUIRE(fprintf(std[i], "garbage") == 7);
+ ATF_REQUIRE(fclose(f) == 0);
+
+ f = fopen(path, "r");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
+ ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);
+
+ ATF_REQUIRE(fclose(f) == 0);
+ }
+
+ ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(freopen_std, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, fdopen_close);
+ ATF_TP_ADD_TC(tp, fdopen_err);
+ ATF_TP_ADD_TC(tp, fdopen_seek);
+ ATF_TP_ADD_TC(tp, fopen_append);
+ ATF_TP_ADD_TC(tp, fopen_err);
+ ATF_TP_ADD_TC(tp, fopen_mode);
+ ATF_TP_ADD_TC(tp, fopen_perm);
+#ifdef __NetBSD__
+ ATF_TP_ADD_TC(tp, fopen_regular);
+#endif
+ ATF_TP_ADD_TC(tp, fopen_seek);
+ ATF_TP_ADD_TC(tp, freopen_std);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_fputc.c b/contrib/netbsd-tests/lib/libc/stdio/t_fputc.c
new file mode 100644
index 0000000..ed7c0dc
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_fputc.c
@@ -0,0 +1,194 @@
+/* $NetBSD: t_fputc.c,v 1.1 2011/09/11 09:02:46 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_fputc.c,v 1.1 2011/09/11 09:02:46 jruoho Exp $");
+
+#include <atf-c.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+static const char *path = "fputc";
+static void puterr(int (*)(int, FILE *));
+static void putstr(int (*)(int, FILE *));
+
+static void
+puterr(int (*func)(int, FILE *))
+{
+ FILE *f;
+
+ f = fopen(path, "w+");
+
+ ATF_REQUIRE(f != NULL);
+ ATF_REQUIRE(fclose(f) == 0);
+ ATF_REQUIRE(unlink(path) == 0);
+ ATF_REQUIRE(func('x', f) == EOF);
+}
+
+static void
+putstr(int (*func)(int, FILE *))
+{
+ const char *str = "1234567890x";
+ unsigned short i = 0;
+ char buf[10];
+ FILE *f;
+
+ (void)memset(buf, 'x', sizeof(buf));
+
+ f = fopen(path, "w+");
+ ATF_REQUIRE(f != NULL);
+
+ while (str[i] != 'x') {
+ ATF_REQUIRE(func(str[i], f) == str[i]);
+ i++;
+ }
+
+ ATF_REQUIRE(fclose(f) == 0);
+
+ f = fopen(path, "r");
+ ATF_REQUIRE(f != NULL);
+
+ ATF_REQUIRE(fread(buf, 1, 10, f) == 10);
+ ATF_REQUIRE(strncmp(buf, str, 10) == 0);
+
+ ATF_REQUIRE(fclose(f) == 0);
+ ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_WITH_CLEANUP(fputc_basic);
+ATF_TC_HEAD(fputc_basic, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of fputc(3)");
+}
+
+ATF_TC_BODY(fputc_basic, tc)
+{
+ putstr(fputc);
+}
+
+ATF_TC_CLEANUP(fputc_basic, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(fputc_err);
+ATF_TC_HEAD(fputc_err, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test errors from fputc(3)");
+}
+
+ATF_TC_BODY(fputc_err, tc)
+{
+ puterr(fputc);
+}
+
+ATF_TC_CLEANUP(fputc_err, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(putc_basic);
+ATF_TC_HEAD(putc_basic, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of putc(3)");
+}
+
+ATF_TC_BODY(putc_basic, tc)
+{
+ putstr(putc);
+}
+
+ATF_TC_CLEANUP(putc_basic, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(putc_err);
+ATF_TC_HEAD(putc_err, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test errors from putc(3)");
+}
+
+ATF_TC_BODY(putc_err, tc)
+{
+ puterr(putc);
+}
+
+ATF_TC_CLEANUP(putc_err, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(putc_unlocked_basic);
+ATF_TC_HEAD(putc_unlocked_basic, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of putc_unlocked(3)");
+}
+
+ATF_TC_BODY(putc_unlocked_basic, tc)
+{
+ putstr(putc_unlocked);
+}
+
+ATF_TC_CLEANUP(putc_unlocked_basic, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(putc_unlocked_err);
+ATF_TC_HEAD(putc_unlocked_err, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test errors from putc_unlocked(3)");
+}
+
+ATF_TC_BODY(putc_unlocked_err, tc)
+{
+ puterr(putc_unlocked);
+}
+
+ATF_TC_CLEANUP(putc_unlocked_err, tc)
+{
+ (void)unlink(path);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, fputc_basic);
+ ATF_TP_ADD_TC(tp, fputc_err);
+ ATF_TP_ADD_TC(tp, putc_basic);
+ ATF_TP_ADD_TC(tp, putc_err);
+ ATF_TP_ADD_TC(tp, putc_unlocked_basic);
+ ATF_TP_ADD_TC(tp, putc_unlocked_err);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_mktemp.c b/contrib/netbsd-tests/lib/libc/stdio/t_mktemp.c
new file mode 100644
index 0000000..e423060
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_mktemp.c
@@ -0,0 +1,54 @@
+/* $NetBSD: t_mktemp.c,v 1.1 2013/04/22 21:05:12 christos Exp $ */
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_mktemp.c,v 1.1 2013/04/22 21:05:12 christos Exp $");
+
+#include <atf-c.h>
+#include <stdlib.h>
+
+ATF_TC(mktemp_not_exist);
+ATF_TC_HEAD(mktemp_not_exist, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test that mktemp does not fail on"
+ " a path that does not exist");
+}
+
+ATF_TC_BODY(mktemp_not_exist, tc)
+{
+ char template[] = "I will barf/XXXXXX";
+ ATF_REQUIRE(mktemp(template) != NULL);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, mktemp_not_exist);
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_popen.c b/contrib/netbsd-tests/lib/libc/stdio/t_popen.c
new file mode 100644
index 0000000..699498c
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_popen.c
@@ -0,0 +1,135 @@
+/* $NetBSD: t_popen.c,v 1.4 2013/02/15 23:27:19 christos Exp $ */
+
+/*-
+ * Copyright (c) 1999 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Matthias Scheler.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__COPYRIGHT("@(#) Copyright (c) 1999\
+ The NetBSD Foundation, Inc. All rights reserved.");
+#endif /* not lint */
+
+#ifndef lint
+__RCSID("$NetBSD: t_popen.c,v 1.4 2013/02/15 23:27:19 christos Exp $");
+#endif /* not lint */
+
+#include <atf-c.h>
+
+#include <sys/param.h>
+
+#include <errno.h>
+#include <err.h>
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+#define _PATH_CAT "/bin/cat"
+#define BUFSIZE (640*1024)
+ /* 640KB ought to be enough for everyone. */
+#define DATAFILE "popen.data"
+
+#define TEST_ERROR(a) \
+ do \
+ { \
+ warn(a); \
+ atf_tc_fail("Check stderr for error details."); \
+ } while ( /*CONSTCOND*/ 0 )
+
+ATF_TC_WITH_CLEANUP(popen_zeropad);
+ATF_TC_HEAD(popen_zeropad, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "output format zero padding");
+}
+
+ATF_TC_BODY(popen_zeropad, tc)
+{
+ char *buffer, command[MAXPATHLEN];
+ int idx, in;
+ FILE *my_pipe;
+
+ if ((buffer = malloc(BUFSIZE)) == NULL)
+ atf_tc_skip("Unable to allocate buffer.");
+
+ srand ((unsigned int)time(NULL));
+
+ for (idx = 0; idx < BUFSIZE; idx++)
+ buffer[idx]=(char)rand();
+
+ (void)snprintf(command, sizeof(command), "%s >%s",
+ _PATH_CAT, DATAFILE);
+
+ if ((my_pipe = popen(command, "w")) == NULL)
+ TEST_ERROR("popen write");
+
+ if (fwrite(buffer, sizeof(char), BUFSIZE, my_pipe) != BUFSIZE)
+ TEST_ERROR("fwrite");
+
+ if (pclose(my_pipe) == -1)
+ TEST_ERROR("pclose");
+
+ (void)snprintf(command, sizeof(command), "%s %s", _PATH_CAT, DATAFILE);
+
+ if ((my_pipe = popen(command, "r")) == NULL)
+ TEST_ERROR("popen read");
+
+ idx = 0;
+ while ((in = fgetc(my_pipe)) != EOF)
+ if (idx == BUFSIZE) {
+ errno = EFBIG;
+ TEST_ERROR("read");
+ }
+ else if ((char)in != buffer[idx++]) {
+ errno = EINVAL;
+ TEST_ERROR("read");
+ }
+
+ if (idx < BUFSIZE) {
+ errno = EIO;
+ TEST_ERROR("read");
+ }
+
+ if (pclose(my_pipe) == -1)
+ TEST_ERROR("pclose");
+}
+
+ATF_TC_CLEANUP(popen_zeropad, tc)
+{
+ (void)unlink(DATAFILE);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, popen_zeropad);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_printf.c b/contrib/netbsd-tests/lib/libc/stdio/t_printf.c
new file mode 100644
index 0000000..5ef58f2
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_printf.c
@@ -0,0 +1,204 @@
+/* $NetBSD: t_printf.c,v 1.8 2012/04/11 16:21:42 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <atf-c.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <time.h>
+#include <stdlib.h>
+
+#ifndef __NetBSD__
+#include <signal.h>
+#endif
+
+ATF_TC(snprintf_c99);
+ATF_TC_HEAD(snprintf_c99, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "Test printf(3) C99 conformance (PR lib/22019)");
+}
+
+ATF_TC_BODY(snprintf_c99, tc)
+{
+ char s[4];
+
+ (void)memset(s, '\0', sizeof(s));
+ (void)snprintf(s, sizeof(s), "%#.o", 0);
+ (void)printf("printf = %#.o\n", 0);
+ (void)fprintf(stderr, "snprintf = %s", s);
+
+ ATF_REQUIRE(strlen(s) == 1);
+ ATF_REQUIRE(s[0] == '0');
+}
+
+ATF_TC(snprintf_dotzero);
+ATF_TC_HEAD(snprintf_dotzero, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "PR lib/32951: %%.0f formats (0.0,0.5] to \"0.\"");
+}
+
+ATF_TC_BODY(snprintf_dotzero, tc)
+{
+ char s[4];
+
+ ATF_CHECK(snprintf(s, sizeof(s), "%.0f", 0.1) == 1);
+ ATF_REQUIRE_STREQ(s, "0");
+}
+
+ATF_TC(snprintf_posarg);
+ATF_TC_HEAD(snprintf_posarg, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "test for positional arguments");
+}
+
+ATF_TC_BODY(snprintf_posarg, tc)
+{
+ char s[16];
+
+ ATF_CHECK(snprintf(s, sizeof(s), "%1$d", -23) == 3);
+ ATF_REQUIRE_STREQ(s, "-23");
+}
+
+ATF_TC(snprintf_posarg_width);
+ATF_TC_HEAD(snprintf_posarg_width, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "test for positional arguments with "
+ "field width");
+}
+
+ATF_TC_BODY(snprintf_posarg_width, tc)
+{
+ char s[16];
+
+ ATF_CHECK(snprintf(s, sizeof(s), "%1$*2$d", -23, 4) == 4);
+ ATF_REQUIRE_STREQ(s, " -23");
+}
+
+ATF_TC(snprintf_posarg_error);
+ATF_TC_HEAD(snprintf_posarg_error, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "test for positional arguments out "
+ "of bounds");
+}
+
+ATF_TC_BODY(snprintf_posarg_error, tc)
+{
+ char s[16], fmt[32];
+
+#ifndef __NetBSD__
+ atf_tc_expect_signal(SIGSEGV,
+ "some non-NetBSD platforms including FreeBSD don't validate "
+ "negative size; testcase blows up with SIGSEGV");
+#endif
+
+ snprintf(fmt, sizeof(fmt), "%%%zu$d", SIZE_MAX / sizeof(size_t));
+
+ ATF_CHECK(snprintf(s, sizeof(s), fmt, -23) == -1);
+}
+
+ATF_TC(snprintf_float);
+ATF_TC_HEAD(snprintf_float, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "test that floating conversions don't"
+ " leak memory");
+}
+
+ATF_TC_BODY(snprintf_float, tc)
+{
+ union {
+ double d;
+ uint64_t bits;
+ } u;
+ uint32_t ul, uh;
+ time_t now;
+ char buf[1000];
+ struct rlimit rl;
+
+ rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
+ ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
+ rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
+ ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
+
+ time(&now);
+ srand(now);
+ for (size_t i = 0; i < 10000; i++) {
+ ul = rand();
+ uh = rand();
+ u.bits = (uint64_t)uh << 32 | ul;
+ ATF_CHECK(snprintf(buf, sizeof buf, " %.2f", u.d) != -1);
+ }
+}
+
+ATF_TC(sprintf_zeropad);
+ATF_TC_HEAD(sprintf_zeropad, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Test output format zero padding (PR lib/44113)");
+}
+
+ATF_TC_BODY(sprintf_zeropad, tc)
+{
+ char str[1024];
+
+ ATF_CHECK(sprintf(str, "%010f", 0.0) == 10);
+ ATF_REQUIRE_STREQ(str, "000.000000");
+
+ /* ieeefp */
+#ifndef __vax__
+ /* printf(3) should ignore zero padding for nan/inf */
+ ATF_CHECK(sprintf(str, "%010f", NAN) == 10);
+ ATF_REQUIRE_STREQ(str, " nan");
+ ATF_CHECK(sprintf(str, "%010f", INFINITY) == 10);
+ ATF_REQUIRE_STREQ(str, " inf");
+#endif
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, snprintf_c99);
+ ATF_TP_ADD_TC(tp, snprintf_dotzero);
+ ATF_TP_ADD_TC(tp, snprintf_posarg);
+ ATF_TP_ADD_TC(tp, snprintf_posarg_width);
+ ATF_TP_ADD_TC(tp, snprintf_posarg_error);
+ ATF_TP_ADD_TC(tp, snprintf_float);
+ ATF_TP_ADD_TC(tp, sprintf_zeropad);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/lib/libc/stdio/t_scanf.c b/contrib/netbsd-tests/lib/libc/stdio/t_scanf.c
new file mode 100644
index 0000000..194cd17
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libc/stdio/t_scanf.c
@@ -0,0 +1,85 @@
+/* $NetBSD: t_scanf.c,v 1.3 2012/03/18 07:00:51 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <atf-c.h>
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+#define NUM -0x1234
+#define STRNUM ___STRING(NUM)
+
+ATF_TC(sscanf_neghex);
+ATF_TC_HEAD(sscanf_neghex, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "PR lib/21691: %%i and %%x fail with negative hex numbers");
+}
+
+ATF_TC_BODY(sscanf_neghex, tc)
+{
+ int i;
+
+ sscanf(STRNUM, "%i", &i);
+ ATF_REQUIRE(i == NUM);
+
+ sscanf(STRNUM, "%x", &i);
+ ATF_REQUIRE(i == NUM);
+}
+
+ATF_TC(sscanf_whitespace);
+ATF_TC_HEAD(sscanf_whitespace, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "verify sscanf skips all whitespace");
+}
+
+ATF_TC_BODY(sscanf_whitespace, tc)
+{
+ const char str[] = "\f\n\r\t\v%z";
+ char c;
+
+#ifndef __NetBSD__
+ atf_tc_expect_fail("fails on FreeBSD and some variants of Linux");
+#endif
+
+ /* set of "white space" symbols from isspace(3) */
+ c = 0;
+ (void)sscanf(str, "%%%c", &c);
+ ATF_REQUIRE(c == 'z');
+}
+
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, sscanf_neghex);
+ ATF_TP_ADD_TC(tp, sscanf_whitespace);
+
+ return atf_no_error();
+}
OpenPOWER on IntegriCloud