tools/llvm: Do not build with symbols
[minix3.git] / tests / lib / libc / gen / t_ttyname.c
blob0c10c2496443d3bb2dead6755c31df67b140e99d
1 /* $NetBSD: t_ttyname.c,v 1.3 2011/05/01 18:14:01 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_ttyname.c,v 1.3 2011/05/01 18:14:01 jruoho Exp $");
34 #include <atf-c.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
41 static long ttymax = 0;
43 ATF_TC(ttyname_err);
44 ATF_TC_HEAD(ttyname_err, tc)
46 atf_tc_set_md_var(tc, "descr", "Test errors in ttyname(3)");
49 ATF_TC_BODY(ttyname_err, tc)
51 int fd;
53 fd = open("XXX", O_RDONLY);
55 if (fd < 0) {
57 errno = 0;
59 ATF_REQUIRE(isatty(fd) != -1);
60 ATF_REQUIRE(errno == EBADF);
62 errno = 0;
64 ATF_REQUIRE(ttyname(fd) == NULL);
65 ATF_REQUIRE(errno == EBADF);
68 fd = open("/etc/passwd", O_RDONLY);
70 if (fd >= 0) {
72 errno = 0;
74 ATF_REQUIRE(isatty(fd) != -1);
75 ATF_REQUIRE(errno == ENOTTY);
77 errno = 0;
79 ATF_REQUIRE(ttyname(fd) == NULL);
80 ATF_REQUIRE(errno == ENOTTY);
84 ATF_TC(ttyname_r_err);
85 ATF_TC_HEAD(ttyname_r_err, tc)
87 atf_tc_set_md_var(tc, "descr", "Test errors in ttyname_r(3)");
90 ATF_TC_BODY(ttyname_r_err, tc)
92 char sbuf[0];
93 char *buf;
94 int fd;
95 int rv;
97 buf = malloc(ttymax + 1);
99 if (buf == NULL)
100 return;
102 (void)memset(buf, '\0', ttymax + 1);
104 if (isatty(STDIN_FILENO) != 0) {
106 rv = ttyname_r(STDIN_FILENO, sbuf, sizeof(sbuf));
107 ATF_REQUIRE(rv == ERANGE);
110 rv = ttyname_r(-1, buf, ttymax);
111 ATF_REQUIRE(rv == EBADF);
113 fd = open("/etc/passwd", O_RDONLY);
115 if (fd >= 0) {
116 rv = ttyname_r(fd, buf, ttymax);
117 ATF_REQUIRE(rv == ENOTTY);
118 ATF_REQUIRE(close(fd) == 0);
121 free(buf);
124 ATF_TC(ttyname_r_stdin);
125 ATF_TC_HEAD(ttyname_r_stdin, tc)
127 atf_tc_set_md_var(tc, "descr", "Test ttyname_r(3) with stdin(3)");
130 ATF_TC_BODY(ttyname_r_stdin, tc)
132 const char *str;
133 char *buf;
134 int rv;
136 if (isatty(STDIN_FILENO) == 0)
137 return;
139 buf = malloc(ttymax + 1);
141 if (buf == NULL)
142 return;
144 (void)memset(buf, '\0', ttymax + 1);
146 str = ttyname(STDIN_FILENO);
147 rv = ttyname_r(STDIN_FILENO, buf, ttymax);
149 ATF_REQUIRE(rv == 0);
150 ATF_REQUIRE(str != NULL);
152 if (strcmp(str, buf) != 0)
153 atf_tc_fail("ttyname(3) and ttyname_r(3) conflict");
155 free(buf);
158 ATF_TC(ttyname_stdin);
159 ATF_TC_HEAD(ttyname_stdin, tc)
161 atf_tc_set_md_var(tc, "descr", "Test ttyname(3) with stdin(3)");
164 ATF_TC_BODY(ttyname_stdin, tc)
167 if (isatty(STDIN_FILENO) != 0)
168 ATF_REQUIRE(ttyname(STDIN_FILENO) != NULL);
170 (void)close(STDIN_FILENO);
172 ATF_REQUIRE(isatty(STDIN_FILENO) != 1);
173 ATF_REQUIRE(ttyname(STDIN_FILENO) == NULL);
176 ATF_TP_ADD_TCS(tp)
179 ttymax = sysconf(_SC_TTY_NAME_MAX);
180 ATF_REQUIRE(ttymax >= 0);
182 ATF_TP_ADD_TC(tp, ttyname_err);
183 ATF_TP_ADD_TC(tp, ttyname_r_err);
184 ATF_TP_ADD_TC(tp, ttyname_r_stdin);
185 ATF_TP_ADD_TC(tp, ttyname_stdin);
187 return atf_no_error();