source: rtems/c/src/lib/libc/ttyname_r.c @ cff0d64

4.104.114.84.95
Last change on this file since cff0d64 was 9c49db4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/01 at 18:26:44

2001-01-08 Ralf Corsepius <corsepiu@…>

  • configure.in: Add libc/config.h
  • libc/Makefile.am: Add INCLUDES += -I. to pickup config.h
  • libc/.cvsignore: Add config.h and stamp-h
  • libc/*.c: Add config.h support.
  • Property mode set to 100644
File size: 3.4 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 <paths.h>
54#include <_syslist.h>
55#include <errno.h>
56
57#include <rtems/libio_.h>
58
59/*
60 *  ttyname_r() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
61 */
62
63int ttyname_r(
64  int   fd,
65  char *name,
66  int   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    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    set_errno_and_return_minus_one(EBADF);
83
84  if ((dp = opendir (_PATH_DEV)) == NULL)
85    set_errno_and_return_minus_one(EBADF);
86
87  for (rval = NULL; (dirp = readdir (dp)) != NULL ;)
88    {
89      if (dirp->d_ino != sb.st_ino)
90        continue;
91      strcpy (name + sizeof (_PATH_DEV) - 1, dirp->d_name);
92      if (stat (name, &dsb) || sb.st_dev != dsb.st_dev ||
93          sb.st_ino != dsb.st_ino)
94        continue;
95      (void) closedir (dp);
96      rval = name;
97      break;
98    }
99  (void) closedir (dp);
100  return 0;
101}
102
103static char buf[sizeof (_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
104
105/*
106 *  ttyname() - POSIX 1003.1b 4.7.2 - Demetermine Terminal Device Name
107 */
108
109char *ttyname(
110  int fd
111)
112{
113  if ( !ttyname_r( fd, buf, sizeof(buf) ) )
114    return buf;
115  return NULL;
116}
117
Note: See TracBrowser for help on using the repository browser.