[comp.unix.wizards] name of current dir in kernel

honzo@4gl.UUCP (Honzo Svasek) (11/24/88)

On a system V release 2 system I want to 'catch' all open and close
calls. I have no source, so I patched the symbol table of the link kit
library and make _open into Xopen.

With the following piece of code I am able to diplay the file that
has been opened. (read on after the code)


#include <sys/param.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/signal.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <sys/sysarg.h>


extern struct user u;

/* The assembler code is 80286 assembler.
***************************************/

asm("	.text");
asm("	.globl	_open");
asm("_open:");
/* save u.u_dirp on the stack
****************************/
asm("	push _u+336");
asm("	push _u+334");
asm("	push ax");
asm("	call Xopen");
asm("	call _Owedge");
asm("   pop  ax");
asm("   pop  ax");
asm("   pop  ax");
asm("   ret");

Owedge(dirp)
uaddr_t dirp;
{
	static char name[65];
	char *cp = name;
	int c;

	/* if some error occured, no sense in going on
	*********************************************/
	if (u.u_error)
		return;

	/* copy the filename from user space
	***********************************/
	while ((c = fubyte(dirp++)) > 0) {
		*cp++ = (char) c;
	}
	*cp = '\0';

	printf("%d %s %s\n", u.u_uid, u.u_comm, name);
}

This works quite well and is interesting to look at! However if the open call
did not use the full path name, i still do not know which file we are
talking about.

What i am looking for is an EASY and FAST way to get the name of the
current directory of the program that is running. i spent the whole day
with M.J. Bach, a very faded copy of the Lions book and my /usr/include/sys/*
files, but have not seen the light yet.

As getcwd uses popen, i fear there might not be a simple solution.

Maybe one of you wizards can do some magic, and show me the light?


I_I(           _  
I I ) honzo svasek  <honzo@4gl.nl>
---------------------------------------------------
Svasek Software Consultants
IJsselkade 24, 3401 RC IJsselstein, the Netherlands
---------------------------------------------------

PS for the curious: I want to be able to process some files when they are
opened, before the are given to the user program. These file contain data
from a database on another machine, and have to be updated just before
they are used on this machine.

guy@auspex.UUCP (Guy Harris) (11/26/88)

>What i am looking for is an EASY and FAST way to get the name of the
>current directory of the program that is running.

The kernel generally does not keep that information around.  On many
systems, "getwd" runs "pwd", which basically starts at the current
directory, looks at that directory's parent to see what the name of the
current directory is in that parent directory, and repeats the process
until it hits the root directory.  Other systems have a library routine
that encapsulates "pwd"s algorithm.

This is not quick, in general.  You may be able to speed it up by:

	1) noting that some shells set up an environment variable that
	   purports to be the current directory; you can get that
	   variable, "stat" both it and "." and make sure it really *is*
	   the current directory, and if so use its value

	2) getting the current directory only once and remembering it;
	   if your program executes a "chdir" system call, it can either
	   recompute the current directory or attempt to combine the
	   argument to "chdir" with the remembered current directory.