OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.7 KiB

  1. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  2. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. #
  4. # Filename: package/.../dietlibc/ttyname_r.diff
  5. # Copyright (C) 2009 The OpenSDE Project
  6. #
  7. # More information can be found in the files COPYING and README.
  8. #
  9. # This patch file is dual-licensed. It is available under the license the
  10. # patched project is licensed under, as long as it is an OpenSource license
  11. # as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
  12. # of the GNU General Public License as published by the Free Software
  13. # Foundation; either version 2 of the License, or (at your option) any later
  14. # version.
  15. # --- SDE-COPYRIGHT-NOTE-END ---
  16. This patch replaces dietlibc functionality with uClibc equivalent.
  17. It is needed because modern software expects ttyname_r function to
  18. be available in the C library.
  19. --- ./include/unistd.h.orig 2009-11-09 15:39:52.000000000 +0100
  20. +++ ./include/unistd.h 2009-11-09 15:41:48.000000000 +0100
  21. @@ -105,6 +105,10 @@
  22. char *ttyname (int desc) __THROW;
  23. +/* Store at most BUFLEN characters of the pathname of the terminal FD is
  24. + open on in BUF. Return 0 on success, otherwise an error number. */
  25. +int ttyname_r (int __fd, char *__buf, size_t __buflen) __THROW;
  26. +
  27. int brk(void *end_data_segment) __THROW;
  28. void *sbrk(ptrdiff_t increment) __THROW;
  29. --- ./lib/ttyname.c.orig 2002-02-23 23:18:42.000000000 +0100
  30. +++ ./lib/ttyname.c 2009-11-09 15:43:20.000000000 +0100
  31. @@ -1,66 +1,119 @@
  32. -#include "dietfeatures.h"
  33. +#include <string.h>
  34. +#include <errno.h>
  35. +#include <assert.h>
  36. #include <unistd.h>
  37. +#include <dirent.h>
  38. #include <sys/stat.h>
  39. -#include <string.h>
  40. -#ifdef __linux__
  41. +/* Jan 1, 2004 Manuel Novoa III
  42. + *
  43. + * Kept the same approach, but rewrote the code for the most part.
  44. + * Fixed some minor issues plus (as I recall) one SUSv3 errno case.
  45. + */
  46. +
  47. +/* This is a fairly slow approach. We do a linear search through some
  48. + * directories looking for a match. Yes this is lame. But it should
  49. + * work, should be small, and will return names that match what is on
  50. + * disk. Another approach we could use would be to use the info in
  51. + * /proc/self/fd, but that is even more lame since it requires /proc */
  52. +
  53. +/* SUSv3 mandates TTY_NAME_MAX as 9. This is obviously insufficient.
  54. + * However, there is no need to waste space and support non-standard
  55. + * tty names either. So we compromise and use the following buffer
  56. + * length. (Erik and Manuel agreed that 32 was more than reasonable.)
  57. + *
  58. + * If you change this, also change _SC_TTY_NAME_MAX in libc/unistd/sysconf.c
  59. + */
  60. +#define TTYNAME_BUFLEN 32
  61. +
  62. +char *ttyname(int fd)
  63. +{
  64. + static char name[TTYNAME_BUFLEN];
  65. +
  66. + return ttyname_r(fd, name, TTYNAME_BUFLEN) ? NULL : name;
  67. +}
  68. +
  69. +static const char dirlist[] =
  70. +/* 12345670123 */
  71. +"\010/dev/vc/\0" /* Try /dev/vc first (be devfs compatible) */
  72. +"\011/dev/tts/\0" /* and /dev/tts next (be devfs compatible) */
  73. +"\011/dev/pty/\0" /* and /dev/pty next (be devfs compatible) */
  74. +"\011/dev/pts/\0" /* and try /dev/pts next */
  75. +"\005/dev/\0"; /* and try walking through /dev last */
  76. +
  77. +int ttyname_r(int fd, char *ubuf, size_t ubuflen)
  78. +{
  79. + struct dirent *d;
  80. + struct stat st;
  81. + struct stat dst;
  82. + const char *p;
  83. + char *s;
  84. + DIR *fp;
  85. + int rv;
  86. + int len;
  87. + char buf[TTYNAME_BUFLEN];
  88. +
  89. + if (fstat(fd, &st) < 0) {
  90. + return errno;
  91. + }
  92. +
  93. + rv = ENOTTY; /* Set up the default return value. */
  94. -#include <stdlib.h>
  95. + if (!isatty(fd)) {
  96. + goto DONE;
  97. + }
  98. -char *ttyname(int fd) {
  99. -#ifdef SLASH_PROC_OK
  100. - char ibuf[20];
  101. - static char obuf[20];
  102. - int len;
  103. - if (!isatty(fd)) return 0;
  104. - strcpy(ibuf,"/proc/self/fd/");
  105. - ibuf[__ltostr(ibuf+14,6,(unsigned long)fd,10,0)+14]=0;
  106. - if ((len=readlink(ibuf,obuf,sizeof(obuf)-1))<0) return 0;
  107. - obuf[len]=0;
  108. - return obuf;
  109. + for (p = dirlist ; *p ; p += 1 + p[-1]) {
  110. + len = *p++;
  111. +
  112. + assert(len + 2 <= TTYNAME_BUFLEN); /* dirname + 1 char + nul */
  113. +
  114. + strcpy(buf, p);
  115. + s = buf + len;
  116. + len = (TTYNAME_BUFLEN-2) - len; /* Available non-nul space. */
  117. +
  118. + if (!(fp = opendir(p))) {
  119. + continue;
  120. + }
  121. +
  122. + while ((d = readdir(fp)) != NULL) {
  123. + /* This should never trigger for standard names, but we
  124. + * check it to be safe. */
  125. + if (strlen(d->d_name) > len) { /* Too big? */
  126. + continue;
  127. + }
  128. +
  129. + strcpy(s, d->d_name);
  130. +
  131. + if ((lstat(buf, &dst) == 0)
  132. +#if 0
  133. + /* Stupid filesystems like cramfs fail to guarantee that
  134. + * st_ino and st_dev uniquely identify a file, contrary to
  135. + * SuSv3, so we cannot be quite so precise as to require an
  136. + * exact match. Settle for something less... Grumble... */
  137. + && (st.st_dev == dst.st_dev) && (st.st_ino == dst.st_ino)
  138. #else
  139. - static char buf[20];
  140. - struct stat s;
  141. - char *c=buf+8;
  142. - int n;
  143. - if (!isatty(fd)) return 0;
  144. - if (fstat(fd,&s)) return 0;
  145. - strcpy(buf,"/dev/tty");
  146. - if (S_ISCHR(s.st_mode)) {
  147. - n=minor(s.st_rdev);
  148. - switch (major(s.st_rdev)) {
  149. - case 4:
  150. - if (n>63) {
  151. - n-=64;
  152. - *c='S';
  153. - ++c;
  154. - }
  155. -num:
  156. - c[__ltostr(c,6,n,10,0)]=0;
  157. - break;
  158. - case 2:
  159. - buf[8]='p'-(n>>4);
  160. - buf[9]=n%4+'0';
  161. - if (buf[9]>'9') *c+='a'-'0';
  162. - buf[10]=0;
  163. - goto duh;
  164. - case 136:
  165. - case 137:
  166. - case 138:
  167. - case 139:
  168. - buf[7]='s';
  169. -duh:
  170. - buf[5]='p';
  171. - n+=(major(s.st_rdev)-136)<<8;
  172. - *c='/'; ++c;
  173. - goto num;
  174. - default:
  175. - return 0;
  176. - }
  177. - return buf;
  178. - }
  179. - return 0;
  180. + && S_ISCHR(dst.st_mode) && (st.st_rdev == dst.st_rdev)
  181. #endif
  182. + ) { /* Found it! */
  183. + closedir(fp);
  184. +
  185. + /* We treat NULL buf as ERANGE rather than EINVAL. */
  186. + rv = ERANGE;
  187. + if (ubuf && (strlen(buf) <= ubuflen)) {
  188. + strcpy(ubuf, buf);
  189. + rv = 0;
  190. + }
  191. + goto DONE;
  192. + }
  193. + }
  194. +
  195. + closedir(fp);
  196. + }
  197. +
  198. + DONE:
  199. + __set_errno(rv);
  200. +
  201. + return rv;
  202. }
  203. -#endif