File:  [MW Coherent from dump] / coherent / b / lib / libc / gen / getcwd.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed May 29 04:56:35 2019 UTC (7 years ago) by root
Branches: MarkWilliams, MAIN
CVS tags: relic, HEAD
coherent

/*
 * libc/gen/getcwd.c
 */

#include <stddef.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>

extern	char	*_getwd();
extern	char	*malloc();

/*
 * Set errno and return NULL.
 */
static
char *
error(n) register int n;
{
	errno = n;
	return NULL;
}

/*
 * Get current working directory.
 * Cf. SVID Appendix 2.2 and Posix 5.2.2.
 * This calls the COHERENT function _getwd(),
 * which takes no args and returns a pointer
 * to a statically allocated string.
 */
char *
getcwd(buf, size) char *buf; size_t size;
{
	register char *dir;
	register size_t len;

	if (size == 0)
		return error(EINVAL);
	if ((dir = _getwd()) == NULL)
		return NULL;			/* cannot find cwd */
	len = strlen(dir);
	if (size < len + 1)
		return error(ERANGE);
	/*
	 * The following behavior is specified by SVID,
	 * Posix says the behavior is undefined in this case.
	 */
	if (buf == NULL && (buf = malloc(size)) == NULL)
		return error(ENOMEM);
	return strcpy(buf, dir);	
}

/* end of libc/gen/getcwd.c */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.