Back to index

glibc  2.9
ptsname.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998,2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <paths.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <sys/stat.h>
00024 #include <unistd.h>
00025 
00026 /* Static buffer for `ptsname'.  */
00027 static char buffer[sizeof (_PATH_TTY) + 2];
00028 
00029 
00030 /* Return the pathname of the pseudo terminal slave assoicated with
00031    the master FD is open on, or NULL on errors.
00032    The returned storage is good until the next call to this function.  */
00033 char *
00034 ptsname (int fd)
00035 {
00036   return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
00037 }
00038 
00039 
00040 /* Store at most BUFLEN characters of the pathname of the slave pseudo
00041    terminal associated with the master FD is open on in BUF.
00042    Return 0 on success, otherwise an error number.  */
00043 int
00044 __ptsname_r (int fd, char *buf, size_t buflen)
00045 {
00046   int save_errno = errno;
00047   struct stat st;
00048 
00049   if (buf == NULL)
00050     {
00051       __set_errno (EINVAL);
00052       return EINVAL;
00053     }
00054 
00055   if (!__isatty (fd))
00056     /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY).  */
00057     return errno;
00058 
00059   if (buflen < strlen (_PATH_TTY) + 3)
00060     {
00061       __set_errno (ERANGE);
00062       return ERANGE;
00063     }
00064 
00065   if (__ttyname_r (fd, buf, buflen) != 0)
00066     return errno;
00067 
00068   buf[sizeof (_PATH_DEV) - 1] = 't';
00069 
00070   if (__stat (buf, &st) < 0)
00071     return errno;
00072 
00073   __set_errno (save_errno);
00074   return 0;
00075 }
00076 weak_alias (__ptsname_r, ptsname_r)