1/* Test for ptsname/ptsname_r.
2 Copyright (C) 2014-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#define DEV_TTY "/dev/tty"
27#define PTSNAME_EINVAL "./ptsname-einval"
28
29static int
30do_single_test (int fd, char *buf, size_t buflen, int expected_err)
31{
32
33 int ret = ptsname_r (fd: fd, buf: buf, buflen: buflen);
34 int err = errno;
35
36 if (expected_err == 0)
37 {
38 if (ret != 0)
39 {
40 printf (format: "ptsname_r: expected: return = 0\n");
41 printf (format: " got: return = %d, errno = %d (%s)\n",
42 ret, err, strerror (errnum: err));
43 return 1;
44 }
45 }
46 else
47 {
48 if (ret == 0 || errno != expected_err)
49 {
50 printf (format: "ptsname_r: expected: return = %d, errno = %d (%s)\n",
51 -1, expected_err, strerror (errnum: expected_err));
52 printf (format: " got: return = %d, errno = %d (%s)\n",
53 ret, err, strerror (errnum: err));
54 return 1;
55 }
56 }
57
58 return 0;
59}
60
61static int
62do_test (void)
63{
64 char buf[512];
65 int result = 0;
66
67 /* Tests with a real PTS master. */
68 int fd = posix_openpt (O_RDWR);
69 if (fd != -1)
70 {
71 result |= do_single_test (fd, buf, buflen: sizeof (buf), expected_err: 0);
72 result |= do_single_test (fd, buf, buflen: 1, ERANGE);
73 close (fd: fd);
74 }
75 else
76 printf (format: "posix_openpt (O_RDWR) failed\nerrno %d (%s)\n",
77 errno, strerror (errno));
78
79 /* Test with a terminal device which is not a PTS master. */
80 fd = open (DEV_TTY, O_RDONLY);
81 if (fd != -1)
82 {
83 result |= do_single_test (fd, buf, buflen: sizeof (buf), ENOTTY);
84 close (fd: fd);
85 }
86 else
87 printf (format: "open (\"%s\", O_RDWR) failed\nerrno %d (%s)\n",
88 DEV_TTY, errno, strerror (errno));
89
90 /* Test with a file. */
91 fd = open (PTSNAME_EINVAL, O_RDWR | O_CREAT, 0600);
92 if (fd != -1)
93 {
94 result |= do_single_test (fd, buf, buflen: sizeof (buf), ENOTTY);
95 close (fd: fd);
96 unlink (PTSNAME_EINVAL);
97 }
98 else
99 printf (format: "open (\"%s\", O_RDWR | OCREAT) failed\nerrno %d (%s)\n",
100 PTSNAME_EINVAL, errno, strerror (errno));
101
102 return result;
103}
104
105#define TEST_FUNCTION do_test ()
106#include "../test-skeleton.c"
107

source code of glibc/login/tst-ptsname.c