case handling
	open enforces case significance, as does chdir - though these may need work
	as they only check the last path segement.
	More of an issue is rc is still built under mingw and not 9win so it does
	not benefit from my chdir() or open().

stack size
	app apps told to ask windows for 2Mb of stack and they touch all of it
	once at startup. sed (due to libregexp()) is a particular offender (user
	of much stack).

envronment performace
	Should only ingest the environment on first access from user code.
	Should also keep an environment changed flag so we can pass the current
	environment directly to create processes for speed if it is unchanged.

mounting devices (USB drives) from a dos(1) shell

	Windows does not mount disks; it mounts volumes.  However, the volume
	for a USBSTOR class device is not listed as a child node in the device
	tree.  Therefore, you have to enumerate all volumes and and do a bunch
	of string manipulation and comparisons to match up STORAGE\VOLUME
	nodes with USBSTOR nodes.

	All volume GUID values are enumerated with the FindFirstVolume set of
	functions.  The leading "\.\" and trailing "\" characters can be
	stripped and the resulting string then passed to QueryDosDevice.  This
	provides a device name.

	Next, one must enumerate all volumes using GUID_DEVINTERFACE_VOLUME
	with SetupDiGetClassDevs and friends.  Compare the device type and
	number of each volume to the USBSTOR device you are looking for using
	IOCTL_STORAGE_GET_DEVICE_NUMBER.  Once those are matched, you can get
	the device name from the volume and compare that to the other list of
	device names to find the volume GUID.

	Finally, the volume GUID can be successfully used with
	SetVolumeMountPoint.


libthread broken again - adding all of rfork I can.

added libmap and lib9pclient from p9p to hopefully make
	cpud work - then just need to port tls to libthread,
	and maybe dial() too (see libthread/main.c).

hget doesn't work with ftp sites due to the lack of fork() (a thread),
could (should) rewrite to use libthread.

announce() not even tried let alone debugged
	haven't done announce/listen/accept/reject yet (code commented in hget.c)

need to check setting local (source) port number/name in dial() works as it should

tls still expects plan9 devices - use cinap's code

libauth probably expects plan9 files too

-O3 causes strange problems - memset called from
fmtstrinit() becomes recursive and blo2ws off the top of the stack.
It really looks like the optimiser buggers up the code form memset,
however  changing the implementation does not fix this. Very odd,
but the optimiser will stay off for now until I get a mingw GCC upgrade.

disk/9660 suicides in writefiles()

join.c not ported, it is a mixture of libc and stdio which makes it hard.

libmemdraw failed due to a lack of pool()

should replace much of this with vanilla plan9
libraries being built but also a lib9 built and lib9 always
appears earlier in the link line - will this work, what if the
external ref is resolved in the current library (e.g. libc),
will the linker try to reparse from the begining of the library list...
I think it must, but I am unsure...

The only bit that doesn't fit is the assembly syntax is different (shame).

_winerr() is rubbish. Replace with print formats:
	%lR		getlasterror()
	%nR		NT error code
	%wR		winsock error
	%nR		wnet error
	werrstr("%s", _winerr()) then becomes werrstr("%lR");

bug in rc:
	ls | read 							# this works fine
	ls | while(s=`{read}) echo $s		# this dies with:
		read: error reading <stdin>: invalid handle

		ls doesnnotexist |[2] cat > nul: # prints the error
	whilst
		ls doesnnotexist >[2] nul:		 # prints nothing

native 9win implementation of rc(1) and mk(1) are waiting for rfork(RFFDG)

/sys/src/cmd/cpu
	lifted cinap's wcpu and hacked the code a little.
	needs network apis:
          int   dial(char *addr, char *local, char *dir, int *cfdp)
          int   hangup(int ctl)
          int   announce(char *addr, char *dir)
          int   listen(char *dir, char *newdir)
          int   accept(int ctl, char *dir)
          int   reject(int ctl, char *dir, char *cause)
          char* netmkaddr(char *addr, char *defnet, char *defservice)
          void  setnetmtpt(char *to, int tolen, char *from)
          NetConnInfo*  getnetconninfo(char *conndir, int fd)
          void freenetconninfo(NetConnInfo*)

	also need v4parseip and friends

truerand  ==  BOOL WINAPI CryptGenRandom( HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer);

auth/factotum ???

win64 needs tas and setjmp/longmp assembly - p9p can help with tas...

_linktarg()/QTLINK works for win32 symlinks but should  understand dfs redirects

^d causes gdb to hang - doesn't like zero length reads but expects ^Z (sigh)
	maybe first push a ^Z down the pipe before the zero length write in dosd?

interrupting gmake can cause the shell to exit too (very odd).

wstat doesn't set perms in acls properly. probably should set and remove
	readonly attribute depending on whether we have any write bits set.

absolute paths
	I could have a rule that all absolute paths with a leading slash
	have $plan9 appended but those rooted by a drive letter are not.
	This would make /bin/rc work better - though I would have to drop
	/win32/bin/rc. It would also fix a few progs that know the abs path
	of data files (yacc, spell, suggest etc). however is it just too ugly?

	I could even go the whole hog and make all paths rooted at $plan9 and
	make the drive letters only appear at /n/c /n/d etc as previously this
	might work if we have dokan to make sure windows stuff sees the same
	environment.

	try "grep '"/' /sys/src/cmd/*.c to see the scale of the problem.

	I could just rewrite paths in open(2)
		/dev/cons as con: (no good for dos(1) sessions),
		/sys/... as $plan9/sys/...
		/tmp/... as c:/Temp/...
		/lib/words as $plan9/lib/words
		idiff runs /bin/diff 
		I could even emuate /fd/0 in the file layer to autogenerate another stdin file with a duplicate handle.

		try dc (!ls) and p (!ls) or tar (cvfz)

		how far to go?

driver letters
	implicit assumption in a few places that drive letters are only
	alphas, Netware experience tells me there are some other characters,
	but that is probably all historic.

cleanname
	added basic support for drive letters but
	this probably needs more work as d: and d:/ are not 
	equally valid to all win32 apis so we may need to do
	some more rewriting which could cause the string to grow
	(h:fred -> h:/fred) and this will break cleanname's API (sigh).

rfork
	started to implement rfork properly so we can do rfork/spawn
	but only the framework is done so fat. Spawn also contains code
	to reformat environments but its commented out.

	need to import environment on first getenv()/putenv() or rfork(RFENVG)
	
	need to export environment in spawn

	need to hold environment and fd table in seperate structures pointed to by proc.

	RFNOTEG only needs a flag so it can detach spawned processes. could enable/disable notes
	on rfork/_revert but I doubt its worth it.

tar
	still calls execl for preprocessors for gz bz2 etc, needs rfork() really

mk and fork/exec
	Trying to fake fork & exec using vfork.
	added a vfork state var in proc, which records
	if forked and if exec'ed.

	if fork'ed and exec'ed and we exit then we want to 
	restore the state to how it was before the fork
	and leap back to the fork().

	so exec() really calls spawn().

	to do this I think we need a trampoline func. This needs to save it state
	in a special stack area we call through from main (and is thus never free'ed).
	This picks up the return address from the proc structure and returns to it.

	should implement nowait for child too

	Mmmm, maybe... 
	#define rfork(x)	(rfork(x), setjmp(_getproc()->forkret))
	Nope, fails if fork calls rfork() as fork()'s stack has now gone.
	Dam.

--------------------------------


timezone
	windows uses GMT for our timezone (London) and calls its daylight time
	GMT dayling saving rather than BST. This seems to be a Bug in windows they call a feature!
	I have hacked it manually to BST.

error messages
	maybe we should have $plan9/sys/lib/win/errmsgs which maps numeric error numbers to nice friendly strings,
	we search the file using binary search (code from look.c), and not really a performance problem as
	it is only ever on the error path - we would want to make sure we don't search it for common cases
	like "no more directory entries", or even keep a cache of the most recent 100 error strings read.
	then again, maybe its not worth it. probably the best way would be to have the cache in a dll, but
	that means we need the dll like cygwin - the thin end of the wedge.

file - put back stuff about special files
	maybe work out how to add back in con: nul: etc

remember
	must use a static var rather than TLS for getproc() as a new thread has no TLS(),
	(i.e. createProcess() dies in ps.c, and so do any ConsoleCommandHandlers() - they 
	run in a new thread started by the kernel.

	spawn gets called with /bin/rc in several places. we remap this
	one case to $plan9/bin/rc 

--------------------------------


yacc is a mess of open files, should be hotter on closing them so temp files can be
deleted on exit - windows doesn't allow you to delete an open file, and Bio
doesn't support ORCLOSE it seems.

yacc now honours $plan9 rather than being passed the path to its parsers

--------------------------------
Fork

	the longjmp in mutate cannot work without copying and relocating the stack as
	when rfork() returns for the first time the stack is unwound and may be overwritten by
	somthing else.
	
	I may get away with saving and restoring the whole stack.
	
	--------------
	
	maybe I should redefine fork so it forks file descriptors and notes when asked,
	it marks the proc as forked and uses that environment for its exec - this would
	allow pcc.c to work as designed. If we do this then we start down the road of
	file table groups etc... Back towards a full plan9 kernel.

-------------

Kernel as a DLL

	Still the biggest problem is the lack of local/private filesystem mounts in dokan.
	Looks like this is straightforward, need to look for "Kernel object namespace".
	
	Ok, so, as soon as I go to a DLL (or an exe that I attach to), I need to ensure the
	kernel owns all file handles, memory etc as files can be shared between processes,
	and memory (e.g. process table entries) cannot just be deallocated, it must be unlinked.
	
	so this means the kernel must have its own process and threads, the system calls
	buts be IPC with no user threads entering the kernel, only data. The thread should
	suspend itself, and then it gets resumed when the systemcall completes. There could
	be a plan9 non-preemptive thread blocked waiting for a systemcall to come in, which
	gets unblocked when the systemcall comes in.
	
	concidering I have only about 20 systemcalls this is not tooo painful.
	
	-------------
	
	Interesting idea. how about if the kernel becomes just a 9p multiplexer only.
	process managment happens internal to each app (in a dll but not in the kernel).
	
	I could talk 9p of windows IPC (named pipes) for performance.
	
	pipe() opens an entry in '#/' - Ah, this fails, we could not pipe out to a non-plan9 app
	we must have a proper named pipe in this applications namespace to do this.
	
	-------------------------------
	
	the only way (it seems) to hold data in an DLL is to use a memory mapped file.
	We can put a pool on top of it and mark it as delete on close so it gets tidied up
	properly. Maybe need to name it with the username and windows session... not sure.
	
	we could start with a little dll holding just the SID to user name cache.
	
	maybe add proc and file tables once that works and then look at doing all
	the rest. All static vars in the Plan9 code would have to be replaced with
	data which is attached at boot (ugh).


----------------------------
#include <windows.h>
#include <stdio.h>
#include <process.h>

main(int argc, char **argv)
{
	STARTUPINFO Si;
	PROCESS_INFORMATION Pi;
	BOOL rc;
	DWORD ThreadId;
	HANDLE hThread;

	if(argc != 2) {
		printf("Usage: %s <Exename>\n", argv[0]);
		return 0;
	}

	memset(&Si, 0, sizeof(Si));
	memset(&Pi, 0, sizeof(Pi));

	Si.cb = sizeof(Si);

	rc = CreateProcess(NULL, argv[1], NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);

	if(rc == FALSE) {
		printf("CreateProcess failed, rc=%d\n", GetLastError());
		return 0;
	}

	hThread = CreateRemoteThread(Pi.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)
	    (GetProcAddress(GetModuleHandle("KERNEL32.DLL"), "ExitThread")), 0, 0, &ThreadId);

	if(hThread == NULL) {
		printf("Unable to create remote thread\n");
		TerminateProcess(Pi.hProcess, 0);
		goto Exit;
	}

	WaitForSingleObject(hThread, INFINITE);
	CloseHandle(hThread);

	WaitForSingleObject(Pi.hProcess, INFINITE);

Exit:
	CloseHandle(Pi.hThread);
	CloseHandle(Pi.hProcess);

	return 0;

}
