getpty.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 getpty(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. return -1;
  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. return -1;
  51. }