source: rtems/cpukit/libcsupport/src/ttyname.c @ cefc9aea

4.115
Last change on this file since cefc9aea was cefc9aea, checked in by Alex Ivanov <alexivanov97@…>, on 12/15/12 at 12:23:36

libcsupport: Doxygen Enhancement Task #8

http://www.google-melange.com/gci/task/view/google/gci2012/7996208

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