osdep-cygwin.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 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/stat.h>
  19. #include <event.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include "tmux.h"
  24. char *
  25. osdep_get_name(int fd, __unused char *tty)
  26. {
  27. FILE *f;
  28. char *path, *buf;
  29. size_t len;
  30. int ch;
  31. pid_t pgrp;
  32. if ((pgrp = tcgetpgrp(fd)) == -1)
  33. return (NULL);
  34. xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp);
  35. if ((f = fopen(path, "r")) == NULL) {
  36. free(path);
  37. return (NULL);
  38. }
  39. free(path);
  40. len = 0;
  41. buf = NULL;
  42. while ((ch = fgetc(f)) != EOF) {
  43. if (ch == '\0')
  44. break;
  45. buf = xrealloc(buf, len + 2);
  46. buf[len++] = ch;
  47. }
  48. if (buf != NULL)
  49. buf[len] = '\0';
  50. fclose(f);
  51. return (buf);
  52. }
  53. char *
  54. osdep_get_cwd(int fd)
  55. {
  56. static char target[MAXPATHLEN + 1];
  57. char *path;
  58. pid_t pgrp;
  59. ssize_t n;
  60. if ((pgrp = tcgetpgrp(fd)) == -1)
  61. return (NULL);
  62. xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp);
  63. n = readlink(path, target, MAXPATHLEN);
  64. free(path);
  65. if (n > 0) {
  66. target[n] = '\0';
  67. return (target);
  68. }
  69. return (NULL);
  70. }
  71. struct event_base *
  72. osdep_event_init(void)
  73. {
  74. return (event_init());
  75. }