source: rtems/cpukit/libcsupport/src/ttyname_r.c @ 47f7dc5

4.104.114.84.95
Last change on this file since 47f7dc5 was 47f7dc5, checked in by Joel Sherrill <joel.sherrill@…>, on 04/03/02 at 14:22:58

2002-04-02 Ralf Corsepius <corsepiu@…>

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