/*
	cprompt - print out actual directory name, i.e. if you are in SYS:Tools/Commodities then
	cprompt will result "Commodities". It is reentrant and therefore a resident program. You
	can use it in your shell's prompt like this: PROMPT "`cprompt`>" if you load cprompt as a resident;
	this will increase the execution speed.
															by Daniel Balster

	recompile it with: gcc -s -O9 -m68040 -nostdlib cprompt.c -o cprompt
*/

int _main (void) { return main(); }
void __main (void) {}

#include <exec/exec.h>
#include <inline/exec.h>
#include <dos/dos.h>
#include <inline/dos.h>

char *versionstring = "$VER: cprompt 1.1 (26.2.1995) by Daniel Balster";
struct ExecBase *SysBase = NULL;
struct DosLibrary *DOSBase = NULL;

int main (void)
{
	int colormode = 0;

	if (SysBase==NULL) SysBase = (struct ExecBase *) *(ULONG*)4;
	if (DOSBase==NULL) DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library",37L);

	if (DOSBase)
	{
		BPTR curdirlock = CurrentDir(0);
		char strbuf[256];		/* CHECK STACKSIZE ELSE THIS CRASH YOUR MACHINE */
		int i = 0;

		if (NameFromLock (curdirlock,strbuf,256))
		{
			if (strbuf[0]=='N')
			if (strbuf[1]=='e')
			if (strbuf[2]=='t')
			if (strbuf[3]=='w')
			if (strbuf[4]=='o')
			if (strbuf[5]=='r')
			if (strbuf[6]=='k')
			if (strbuf[7]==':')
				colormode = 1;

			while (strbuf[i++]);				/* do a simple strlen() */
			while ((strbuf[--i]!='/') && (strbuf[i]!=':') && (i>=0));	/* scan for / and : */
			if (!strbuf[++i]) i=0;				/* next character is a zero -> print whole string ! */

			if (colormode==1) PutStr("\033[32m");

			PutStr (&(strbuf[i]));			/* print out this result */

			if (colormode) PutStr("\033[0m");
		}
		else PrintFault (IoErr(),0);

		CurrentDir (curdirlock);
		CloseLibrary ((struct Library*)DOSBase);
		DOSBase = NULL;
	}
	else return RETURN_FAIL;

	return RETURN_OK;
}
