Warning: That file was not part of the compilation database. It may have many parsing errors.

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

Warning: That file was not part of the compilation database. It may have many parsing errors.