In article <[email protected]>, [email protected]
says...
>
> Hi!
> It happens to me all the time that I want to spawn a new xterm in the
> same directory that I am currently in, for example when I want to open a
> file with vim but keep a shell in the same directory.
>
> xterm actually has a nice builtin function for this:
> spawn-new-terminal()
>
> you can affect that function via the xterm*VT100.translations resource.
>
> however the implementation of this is Linux-specific and it is not
> activated in the OpenBSD build process.
>
> Here's a exerpt from HandleSpawnTerminal() in xenocara/app/xterm/misc.c:
>
> /*
> * Determine the current working directory of the child so that we can
> * spawn a new terminal in the same directory.
> *
> * If we cannot get the CWD of the child, just use our own.
> */
> if (screen->pid) {
> char child_cwd_link[sizeof(PROCFS_ROOT) + 80];
> sprintf(child_cwd_link, PROCFS_ROOT "/%lu/cwd", (unsigned long)
> screen->pid);
> child_cwd = Readlink(child_cwd_link);
> }
>
> So it obviously tries to access procfs to get that value. Is there a way
> to access the same value via the OpenBSD API?
>
> Best regards,
> Jona
If you haven't already, read mount_procfs(8).
The fstab syntax seems a bit picky, I use:
/proc /proc procfs rw,linux 0 0
Unfortunately, one of the fields NOT exposed is "cwd".
Other than that, it's certainly possible to do it, as the fstat(1)
command can do it from userland. fstat(1) accomplishes this through
kvm_openfiles(3) and kvm_getfile2(3).
Since you know the PID you're tracking down, you can use kvm_getfile2
with the KERN_FILE_BYPID argument, then you have to iterate through the
list of returned files until you find one with the KERN_FILE_CDIR set,
that's your working directory.
For a good example of how to accomplish this, look at
/usr/src/usr.bin/fstat/fstat.c. If you don't have sources unpacked, you
can either a) untar them, b) get them through CVS or siblings, or c)
[really, part of b) but whatever] look at the source code available from
http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/fstat/fstat.c?
rev=1.67;content-type=text/plain
I'm not entirely comfortable with the thought of an xterm that uses
kvm_* functions, but if that's what you need...
-Adam Thompson
<[email protected]>