Back to index

glibc  2.9
tst-grantpt.c
Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <sys/stat.h>
00003 #include <fcntl.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <errno.h>
00007 #include <string.h>
00008 #include <unistd.h>
00009 
00010 static int
00011 test_ebadf (void)
00012 {
00013   int fd, ret, err;
00014 
00015   fd = posix_openpt (O_RDWR);
00016   if (fd == -1)
00017     {
00018       printf ("posix_openpt(O_RDWR) failed\nerrno %d (%s)\n",
00019              errno, strerror (errno));
00020       /* We don't fail because of this; maybe the system does not have
00021         SUS pseudo terminals.  */
00022       return 0;
00023     }
00024   unlockpt (fd);
00025   close (fd);
00026 
00027   ret = grantpt (fd);
00028   err = errno;
00029   if (ret != -1 || err != EBADF)
00030     {
00031       printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EBADF);
00032       printf ("           got: return = %d, errno = %d\n", ret, err);
00033       return 1;
00034     }
00035   return 0;
00036 }
00037 
00038 static int
00039 test_einval (void)
00040 {
00041   int fd, ret, err;
00042   const char file[] = "./grantpt-einval";
00043 
00044   fd = open (file, O_RDWR | O_CREAT, 0600);
00045   if (fd == -1)
00046     {
00047       printf ("open(\"%s\", O_RDWR) failed\nerrno %d (%s)\n",
00048              file, errno, strerror (errno));
00049       return 0;
00050     }
00051   unlink (file);
00052 
00053   ret = grantpt (fd);
00054   err = errno;
00055   if (ret != -1 || err != EINVAL)
00056     {
00057       printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EINVAL);
00058       printf ("           got: return = %d, errno = %d\n", ret, err);
00059       ret = 1;
00060     }
00061   else
00062     ret = 0;
00063 
00064   close (fd);
00065 
00066   return ret;
00067 }
00068 
00069 int
00070 main (void)
00071 {
00072   int result = 0;
00073 
00074   result += test_ebadf ();
00075   result += test_einval ();
00076 
00077   return result;
00078 }