osdep-aix.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/procfs.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <fcntl.h>
  22. #include "tmux.h"
  23. char *
  24. osdep_get_name(__unused int fd, char *tty)
  25. {
  26. struct psinfo p;
  27. char *path;
  28. ssize_t bytes;
  29. int f, ttyfd, retval;
  30. pid_t pgrp;
  31. if ((ttyfd = open(tty, O_RDONLY|O_NOCTTY)) == -1)
  32. return (NULL);
  33. retval = ioctl(ttyfd, TIOCGPGRP, &pgrp);
  34. close(ttyfd);
  35. if (retval == -1)
  36. return (NULL);
  37. xasprintf(&path, "/proc/%u/psinfo", (u_int) pgrp);
  38. f = open(path, O_RDONLY);
  39. free(path);
  40. if (f < 0)
  41. return (NULL);
  42. bytes = read(f, &p, sizeof(p));
  43. close(f);
  44. if (bytes != sizeof(p))
  45. return (NULL);
  46. return (xstrdup(p.pr_fname));
  47. }
  48. char *
  49. osdep_get_cwd(int fd)
  50. {
  51. static char target[MAXPATHLEN + 1];
  52. char *path;
  53. const char *ttypath;
  54. ssize_t n;
  55. pid_t pgrp;
  56. int len, retval, ttyfd;
  57. if ((ttypath = ptsname(fd)) == NULL)
  58. return (NULL);
  59. if ((ttyfd = open(ttypath, O_RDONLY|O_NOCTTY)) == -1)
  60. return (NULL);
  61. retval = ioctl(ttyfd, TIOCGPGRP, &pgrp);
  62. close(ttyfd);
  63. if (retval == -1)
  64. return (NULL);
  65. xasprintf(&path, "/proc/%u/cwd", (u_int) pgrp);
  66. n = readlink(path, target, MAXPATHLEN);
  67. free(path);
  68. if (n > 0) {
  69. target[n] = '\0';
  70. if ((len = strlen(target)) > 1 && target[len - 1] == '/')
  71. target[len - 1] = '\0';
  72. return (target);
  73. }
  74. return (NULL);
  75. }
  76. struct event_base *
  77. osdep_event_init(void)
  78. {
  79. return (event_init());
  80. }