source: rtems/cpukit/libcsupport/src/ttyname.c @ 92119ed

4.115
Last change on this file since 92119ed was e137f0e, checked in by Joel Sherrill <joel.sherrill@…>, on 03/11/10 at 19:14:41

2010-03-11 Ken Peters <kptrs@…>

PR 1456/cpukit

  • libcsupport/src/ttyname.c: ttyname_r() when called directly (not via ttyname()) does not prefix the caller provided buffer with the predefined _PATH_DEV string (/dev/). Thus the directory search fails and no tty name is returned to the caller.
  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 1988, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by the University of
16 *      California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/*
35 *  $Id$
36 */
37
38#if HAVE_CONFIG_H
39#include "config.h"
40#endif
41
42#ifndef HAVE_TTYNAME
43
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <fcntl.h>
47#include <dirent.h>
48#include <termios.h>
49#include <unistd.h>
50#include <string.h>
51#include <paths.h>
52#include <_syslist.h>
53#include <errno.h>
54
55#include <rtems/libio_.h>
56#include <rtems/seterr.h>
57
58static char ttyname_buf[sizeof (_PATH_DEV) + MAXNAMLEN];
59
60/*
61 *  ttyname_r() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
62 */
63int ttyname_r(
64  int     fd,
65  char   *name,
66  size_t  namesize
67)
68{
69  struct stat sb;
70  struct termios tty;
71  struct dirent *dirp;
72  DIR *dp;
73  struct stat dsb;
74  char *rval;
75
76  /* Must be a terminal. */
77  if (tcgetattr (fd, &tty) < 0)
78    rtems_set_errno_and_return_minus_one(EBADF);
79
80  /* Must be a character device. */
81  if (fstat (fd, &sb) || !S_ISCHR (sb.st_mode))
82    rtems_set_errno_and_return_minus_one(EBADF);
83
84  if ((dp = opendir (_PATH_DEV)) == NULL)
85    rtems_set_errno_and_return_minus_one(EBADF);
86
87  /* Place the base directory in the path. */
88  strncpy (name, _PATH_DEV, namesize);
89
90  for (rval = NULL; (dirp = readdir (dp)) != NULL ;)
91    {
92      if (dirp->d_ino != sb.st_ino)
93        continue;
94      strcpy (name + sizeof (_PATH_DEV) - 1, dirp->d_name);
95      if (stat (name, &dsb) || sb.st_dev != dsb.st_dev ||
96          sb.st_ino != dsb.st_ino)
97        continue;
98      rval = name;
99      break;
100    }
101  (void) closedir (dp);
102  return 0;
103}
104
105/*
106 *  ttyname() - POSIX 1003.1b 4.7.2 - Determine Terminal Device Name
107 */
108
109char *ttyname(
110  int fd
111)
112{
113  if ( !ttyname_r( fd, ttyname_buf, sizeof(ttyname_buf) ) )
114    return ttyname_buf;
115  return NULL;
116}
117
118#endif
Note: See TracBrowser for help on using the repository browser.