3
0

getpty.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 tarball for details.
  7. */
  8. #include "libbb.h"
  9. #define DEBUG 0
  10. int xgetpty(char *line)
  11. {
  12. int p;
  13. #if ENABLE_FEATURE_DEVPTS
  14. p = open("/dev/ptmx", O_RDWR);
  15. if (p > 0) {
  16. const char *name;
  17. grantpt(p);
  18. unlockpt(p);
  19. name = ptsname(p);
  20. if (!name) {
  21. bb_perror_msg("ptsname error (is /dev/pts mounted?)");
  22. goto fail;
  23. }
  24. safe_strncpy(line, name, GETPTY_BUFSIZE);
  25. return p;
  26. }
  27. #else
  28. struct stat stb;
  29. int i;
  30. int j;
  31. strcpy(line, "/dev/ptyXX");
  32. for (i = 0; i < 16; i++) {
  33. line[8] = "pqrstuvwxyzabcde"[i];
  34. line[9] = '0';
  35. if (stat(line, &stb) < 0) {
  36. continue;
  37. }
  38. for (j = 0; j < 16; j++) {
  39. line[9] = j < 10 ? j + '0' : j - 10 + 'a';
  40. if (DEBUG)
  41. fprintf(stderr, "Trying to open device: %s\n", line);
  42. p = open(line, O_RDWR | O_NOCTTY);
  43. if (p >= 0) {
  44. line[5] = 't';
  45. return p;
  46. }
  47. }
  48. }
  49. #endif /* FEATURE_DEVPTS */
  50. USE_FEATURE_DEVPTS( fail:)
  51. bb_error_msg_and_die("open pty");
  52. return -1; /* never get here */
  53. }