3
0

getpty.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini getpty implementation for busybox
  4. * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. #include "libbb.h"
  9. #define DEBUG 0
  10. int FAST_FUNC xgetpty(char *line)
  11. {
  12. int p;
  13. #if ENABLE_FEATURE_DEVPTS
  14. p = open("/dev/ptmx", O_RDWR);
  15. if (p >= 0) {
  16. grantpt(p); /* chmod+chown corresponding slave pty */
  17. unlockpt(p); /* (what does this do?) */
  18. # ifndef HAVE_PTSNAME_R
  19. {
  20. const char *name;
  21. name = ptsname(p); /* find out the name of slave pty */
  22. if (!name) {
  23. bb_simple_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
  24. }
  25. safe_strncpy(line, name, GETPTY_BUFSIZE);
  26. }
  27. # else
  28. /* find out the name of slave pty */
  29. if (ptsname_r(p, line, GETPTY_BUFSIZE-1) != 0) {
  30. bb_simple_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
  31. }
  32. line[GETPTY_BUFSIZE-1] = '\0';
  33. # endif
  34. return p;
  35. }
  36. #else
  37. struct stat stb;
  38. int i;
  39. int j;
  40. strcpy(line, "/dev/ptyXX");
  41. for (i = 0; i < 16; i++) {
  42. line[8] = "pqrstuvwxyzabcde"[i];
  43. line[9] = '0';
  44. if (stat(line, &stb) < 0) {
  45. continue;
  46. }
  47. for (j = 0; j < 16; j++) {
  48. line[9] = j < 10 ? j + '0' : j - 10 + 'a';
  49. if (DEBUG)
  50. fprintf(stderr, "Trying to open device: %s\n", line);
  51. p = open(line, O_RDWR | O_NOCTTY);
  52. if (p >= 0) {
  53. line[5] = 't';
  54. return p;
  55. }
  56. }
  57. }
  58. #endif /* FEATURE_DEVPTS */
  59. bb_simple_error_msg_and_die("can't find free pty");
  60. }