| 1 | /* netconn.c -- is a particular file descriptor a network connection?. */
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 2002-2005 Free Software Foundation, Inc.
|
|---|
| 4 |
|
|---|
| 5 | This file is part of GNU Bash, the Bourne Again SHell.
|
|---|
| 6 |
|
|---|
| 7 | Bash is free software; you can redistribute it and/or modify it
|
|---|
| 8 | under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 10 | any later version.
|
|---|
| 11 |
|
|---|
| 12 | Bash is distributed in the hope that it will be useful, but WITHOUT
|
|---|
| 13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|---|
| 14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|---|
| 15 | License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with Bash; see the file COPYING. If not, write to the Free
|
|---|
| 19 | Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include <config.h>
|
|---|
| 23 |
|
|---|
| 24 | #include <bashtypes.h>
|
|---|
| 25 | #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H)
|
|---|
| 26 | # include <sys/file.h>
|
|---|
| 27 | #endif
|
|---|
| 28 | #include <posixstat.h>
|
|---|
| 29 | #include <filecntl.h>
|
|---|
| 30 |
|
|---|
| 31 | #include <errno.h>
|
|---|
| 32 |
|
|---|
| 33 | #if defined (HAVE_UNISTD_H)
|
|---|
| 34 | # include <unistd.h>
|
|---|
| 35 | #endif
|
|---|
| 36 |
|
|---|
| 37 | /* The second and subsequent conditions must match those used to decide
|
|---|
| 38 | whether or not to call getpeername() in isnetconn(). */
|
|---|
| 39 | #if defined (HAVE_SYS_SOCKET_H) && defined (HAVE_GETPEERNAME) && !defined (SVR4_2)
|
|---|
| 40 | # include <sys/socket.h>
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | #ifdef __OS2__
|
|---|
| 44 | typedef __socklen_t socklen_t;
|
|---|
| 45 | #endif
|
|---|
| 46 |
|
|---|
| 47 | /* Is FD a socket or network connection? */
|
|---|
| 48 | int
|
|---|
| 49 | isnetconn (fd)
|
|---|
| 50 | int fd;
|
|---|
| 51 | {
|
|---|
| 52 | #if defined (HAVE_GETPEERNAME) && !defined (SVR4_2) && !defined (__BEOS__)
|
|---|
| 53 | int rv;
|
|---|
| 54 | socklen_t l;
|
|---|
| 55 | struct sockaddr sa;
|
|---|
| 56 |
|
|---|
| 57 | l = sizeof(sa);
|
|---|
| 58 | rv = getpeername(fd, &sa, &l);
|
|---|
| 59 | /* Posix.2 says getpeername can return these errors. */
|
|---|
| 60 | return ((rv < 0 && (errno == ENOTSOCK || errno == ENOTCONN || errno == EINVAL)) ? 0 : 1);
|
|---|
| 61 | #else /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */
|
|---|
| 62 | # if defined (SVR4) || defined (SVR4_2)
|
|---|
| 63 | /* Sockets on SVR4 and SVR4.2 are character special (streams) devices. */
|
|---|
| 64 | struct stat sb;
|
|---|
| 65 |
|
|---|
| 66 | if (isatty (fd))
|
|---|
| 67 | return (0);
|
|---|
| 68 | if (fstat (fd, &sb) < 0)
|
|---|
| 69 | return (0);
|
|---|
| 70 | # if defined (S_ISFIFO)
|
|---|
| 71 | if (S_ISFIFO (sb.st_mode))
|
|---|
| 72 | return (0);
|
|---|
| 73 | # endif /* S_ISFIFO */
|
|---|
| 74 | return (S_ISCHR (sb.st_mode));
|
|---|
| 75 | # else /* !SVR4 && !SVR4_2 */
|
|---|
| 76 | # if defined (S_ISSOCK) && !defined (__BEOS__)
|
|---|
| 77 | struct stat sb;
|
|---|
| 78 |
|
|---|
| 79 | if (fstat (fd, &sb) < 0)
|
|---|
| 80 | return (0);
|
|---|
| 81 | return (S_ISSOCK (sb.st_mode));
|
|---|
| 82 | # else /* !S_ISSOCK || __BEOS__ */
|
|---|
| 83 | return (0);
|
|---|
| 84 | # endif /* !S_ISSOCK || __BEOS__ */
|
|---|
| 85 | # endif /* !SVR4 && !SVR4_2 */
|
|---|
| 86 | #endif /* !HAVE_GETPEERNAME || SVR4_2 || __BEOS__ */
|
|---|
| 87 | }
|
|---|