#include <stddef.h>
#include <stdlib.h>	/* both of these added for malloc() */
#include <stdio.h>
#include <errno.h>
#include <osbind.h>
#include <unistd.h>
#include <support.h>

/*******************************************************************
getcwd: returns current working directory. By ERS.
This routine is in the public domain.
********************************************************************/

char *getcwd __PROTO((char *, int));

char *getcwd(buf, size)
char *buf; int size;
{
	volatile char path[FILENAME_MAX];
	char drv;
#ifndef __STDC__
	extern char *malloc();
#else
	void *malloc(size_t);
#endif

	if (!buf)
		if ((buf = (char *) malloc((size_t)size)) == 0)
			return NULL;
	path[2] = '\0';
	(void)Dgetpath(path+2, 0);
	drv = Dgetdrv();
	path[0] = drv + 'A';
	path[1] = ':';
#if 0	/* most programs will be happier without the extra slash */
	if (!path[2]) {
		path[2] = '\\';
		path[3] = '\0';
	}
#endif
	_full_dos2unx(path, buf);	/* convert DOS filename to unix */
	return buf;
}

/*
 * char *getwd(char *buf)
 *	return cwd in buf
 */
char *getwd(buf)
char *buf;
{
	return getcwd(buf, FILENAME_MAX);
}
