Back to index

glibc  2.9
getpt.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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 <string.h>
00023 #include <unistd.h>
00024 
00025 
00026 /* Prefix for master pseudo terminal nodes.  */
00027 #define _PATH_PTY "/dev/pty"
00028 
00029 
00030 /* Letters indicating a series of pseudo terminals.  */
00031 #ifndef PTYNAME1
00032 #define PTYNAME1 "pqrsPQRS"
00033 #endif
00034 const char __libc_ptyname1[] attribute_hidden = PTYNAME1;
00035 
00036 /* Letters indicating the position within a series.  */
00037 #ifndef PTYNAME2
00038 #define PTYNAME2 "0123456789abcdefghijklmnopqrstuv";
00039 #endif
00040 const char __libc_ptyname2[] attribute_hidden = PTYNAME2;
00041 
00042 
00043 /* Open a master pseudo terminal and return its file descriptor.  */
00044 int
00045 __getpt (void)
00046 {
00047   char buf[sizeof (_PATH_PTY) + 2];
00048   const char *p, *q;
00049   char *s;
00050 
00051   s = __mempcpy (buf, _PATH_PTY, sizeof (_PATH_PTY) - 1);
00052   /* s[0] and s[1] will be filled in the loop.  */
00053   s[2] = '\0';
00054 
00055   for (p = __libc_ptyname1; *p != '\0'; ++p)
00056     {
00057       s[0] = *p;
00058 
00059       for (q = __libc_ptyname2; *q != '\0'; ++q)
00060        {
00061          int fd;
00062 
00063          s[1] = *q;
00064 
00065          fd = __open (buf, O_RDWR);
00066          if (fd != -1)
00067            return fd;
00068 
00069          if (errno == ENOENT)
00070            return -1;
00071        }
00072     }
00073 
00074   __set_errno (ENOENT);
00075   return -1;
00076 }
00077 
00078 #undef __getpt
00079 weak_alias (__getpt, getpt)
00080 
00081 #ifndef HAVE_POSIX_OPENPT
00082 /* We cannot define posix_openpt in general for BSD systems.  */
00083 int
00084 __posix_openpt (int oflag)
00085 {
00086   __set_errno (ENOSYS);
00087   return -1;
00088 }
00089 weak_alias (__posix_openpt, posix_openpt)
00090 
00091 stub_warning (posix_openpt)
00092 # include <stub-tag.h>
00093 #endif