ttyname.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*
  2.  * Copyright (c) 1988 The Regents of the University of California.
  3.  * 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. #if defined(LIBC_SCCS) && !defined(lint)
  34. static char sccsid[] = "@(#)ttyname.c 5.10 (Berkeley) 5/6/91";
  35. #endif /* LIBC_SCCS and not lint */
  36. #include "config.h"
  37. #include <pthread.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <fcntl.h>
  41. #include <dirent.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <stdlib.h>
  45. static pthread_mutex_t ttyname_lock = PTHREAD_MUTEX_INITIALIZER;
  46. static pthread_key_t ttyname_key;
  47. static int ttyname_init = 0;
  48. extern void free();
  49. char * __ttyname_r_basic(int fd, char * buf, size_t len)
  50. {
  51. register struct dirent *dirp;
  52. register DIR *dp;
  53. struct stat dsb;
  54. struct stat sb;
  55. char * rval;
  56. int minlen;
  57.     rval = NULL;
  58. /* Must be a terminal. */
  59. if (! isatty_basic(fd))
  60. return(rval);
  61. /* Must be a character device. */
  62. if (machdep_sys_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
  63. return(rval);
  64. /* Must have enough room */
  65.     if (len <= sizeof(_PATH_PTY)) 
  66. return(rval);
  67. if ((dp = opendir(_PATH_PTY)) != NULL) {
  68. memcpy(buf, _PATH_PTY, sizeof(_PATH_PTY));
  69. for (rval = NULL; dirp = readdir(dp);) {
  70. if (dirp->d_fileno != sb.st_ino)
  71. continue;
  72. minlen = (len - (sizeof(_PATH_PTY) - 1)) < (dirp->d_namlen + 1) ?
  73.   (len - (sizeof(_PATH_PTY) - 1)) : (dirp->d_namlen + 1);
  74. memcpy (buf + sizeof(_PATH_PTY) - 1, dirp->d_name, minlen);
  75. if (stat(buf, &dsb) || sb.st_dev != dsb.st_dev ||
  76.      sb.st_ino != dsb.st_ino)
  77. continue;
  78. rval = buf;
  79. break;
  80. }
  81. (void)closedir(dp);
  82. }
  83. return(rval);
  84. }
  85. char * __ttyname_basic(int fd)
  86. {
  87. char *buf;
  88. pthread_mutex_lock (&ttyname_lock);
  89. if (ttyname_init == 0) {
  90. if (pthread_key_create(&ttyname_key, free)) {
  91. pthread_mutex_unlock (&ttyname_lock);
  92. return(NULL);
  93. }
  94. ttyname_init = 1;
  95. }
  96. pthread_mutex_unlock (&ttyname_lock);
  97. /* Must have thread specific data field to put data */
  98.     if ((buf = pthread_getspecific(ttyname_key)) == NULL) {
  99. if (buf = malloc(sizeof(_PATH_PTY) + MAXNAMLEN)) {
  100. if (pthread_setspecific(ttyname_key, buf) != OK) {
  101. free(buf);
  102. return(NULL);
  103. }
  104. } else {
  105. return(NULL);
  106. }
  107. }
  108. return(__ttyname_r_basic(fd, buf, sizeof(_PATH_PTY) + MAXNAMLEN));
  109. }
  110. char * ttyname_r(int fd, char * buf, size_t len)
  111. {
  112. char * ret;
  113. if (fd_lock(fd, FD_READ) == OK) {
  114.       ret = __ttyname_r_basic(fd_table[fd]->fd.i, buf, len);
  115. fd_unlock(fd, FD_READ);
  116. } else {
  117. ret = NULL;
  118. }
  119. return(ret);
  120. }
  121. char * ttyname(int fd)
  122. {
  123. char * ret;
  124. if (fd_lock(fd, FD_READ) == OK) {
  125.       ret = __ttyname_basic(fd_table[fd]->fd.i);
  126. fd_unlock(fd, FD_READ);
  127. } else {
  128. ret = NULL;
  129. }
  130. return(ret);
  131. }