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