Back to index

glibc  2.9
getpt.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <fcntl.h>
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #include <paths.h>
00025 #include <sys/statfs.h>
00026 
00027 #include "linux_fsinfo.h"
00028 
00029 /* Path to the master pseudo terminal cloning device.  */
00030 #define _PATH_DEVPTMX _PATH_DEV "ptmx"
00031 /* Directory containing the UNIX98 pseudo terminals.  */
00032 #define _PATH_DEVPTS _PATH_DEV "pts"
00033 
00034 /* Prototype for function that opens BSD-style master pseudo-terminals.  */
00035 int __bsd_getpt (void);
00036 
00037 /* Open a master pseudo terminal and return its file descriptor.  */
00038 int
00039 __posix_openpt (oflag)
00040      int oflag;
00041 {
00042   static int have_no_dev_ptmx;
00043   int fd;
00044 
00045   if (!have_no_dev_ptmx)
00046     {
00047       fd = __open (_PATH_DEVPTMX, oflag);
00048       if (fd != -1)
00049        {
00050          struct statfs fsbuf;
00051          static int devpts_mounted;
00052 
00053          /* Check that the /dev/pts filesystem is mounted
00054             or if /dev is a devfs filesystem (this implies /dev/pts).  */
00055          if (devpts_mounted
00056              || (__statfs (_PATH_DEVPTS, &fsbuf) == 0
00057                 && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
00058              || (__statfs (_PATH_DEV, &fsbuf) == 0
00059                 && fsbuf.f_type == DEVFS_SUPER_MAGIC))
00060            {
00061              /* Everything is ok.  */
00062              devpts_mounted = 1;
00063              return fd;
00064            }
00065 
00066          /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
00067              are not usable.  */
00068          __close (fd);
00069          have_no_dev_ptmx = 1;
00070        }
00071       else
00072        {
00073          if (errno == ENOENT || errno == ENODEV)
00074            have_no_dev_ptmx = 1;
00075          else
00076            return -1;
00077        }
00078     }
00079 
00080   return -1;
00081 }
00082 weak_alias (__posix_openpt, posix_openpt)
00083 
00084 
00085 int
00086 __getpt (void)
00087 {
00088   int fd = __posix_openpt (O_RDWR);
00089   if (fd == -1)
00090     fd = __bsd_getpt ();
00091   return fd;
00092 }
00093 
00094 
00095 #define PTYNAME1 "pqrstuvwxyzabcde";
00096 #define PTYNAME2 "0123456789abcdef";
00097 
00098 #define __getpt __bsd_getpt
00099 #define HAVE_POSIX_OPENPT
00100 #include <sysdeps/unix/bsd/getpt.c>