chiark / gitweb /
Apply https://sourceware.org/git/?p=glibc.git;a=commit;h=d5dd6189d506068ed11c8bfa1e1e...
[eglibc.git] / login / tst-grantpt.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <unistd.h>
9
10 static int
11 test_ebadf (void)
12 {
13   int fd, ret, err;
14
15   fd = posix_openpt (O_RDWR);
16   if (fd == -1)
17     {
18       printf ("posix_openpt(O_RDWR) failed\nerrno %d (%s)\n",
19               errno, strerror (errno));
20       /* We don't fail because of this; maybe the system does not have
21          SUS pseudo terminals.  */
22       return 0;
23     }
24   unlockpt (fd);
25   close (fd);
26
27   ret = grantpt (fd);
28   err = errno;
29   if (ret != -1 || err != EBADF)
30     {
31       printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EBADF);
32       printf ("           got: return = %d, errno = %d\n", ret, err);
33       return 1;
34     }
35   return 0;
36 }
37
38 static int
39 test_einval (void)
40 {
41   int fd, ret, err;
42   const char file[] = "./grantpt-einval";
43
44   fd = open (file, O_RDWR | O_CREAT, 0600);
45   if (fd == -1)
46     {
47       printf ("open(\"%s\", O_RDWR) failed\nerrno %d (%s)\n",
48               file, errno, strerror (errno));
49       return 0;
50     }
51   unlink (file);
52
53   ret = grantpt (fd);
54   err = errno;
55   if (ret != -1 || err != EINVAL)
56     {
57       printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EINVAL);
58       printf ("           got: return = %d, errno = %d\n", ret, err);
59       ret = 1;
60     }
61   else
62     ret = 0;
63
64   close (fd);
65
66   return ret;
67 }
68
69 int
70 main (void)
71 {
72   int result = 0;
73
74   result += test_ebadf ();
75   result += test_einval ();
76
77   return result;
78 }