#include "bind.h"

#include <unistd.h>
#include <stdio.h>
#include <assert.h>

_luafn(getcwd) { /* getcwd() */
	size_t bufsz = 256;
	/* VLA resize hack */ for (;;) {
		char cwd [bufsz];
		if (getcwd(cwd, bufsz) == NULL) {
			assert(bufsz < (2 * 1024*1024)); /* sanity check */
			bufsz = bufsz * 2;
			continue;
		}
		lua_pushstring(l, cwd);
		return 1;
	}
}

_luafn(isatty) { /* isatty(stream) */
	luaL_Stream* stream = lua_touserdata(l, 1);
	if (stream == NULL) return 0;
	int fd = fileno(stream -> f);
	_luaret_bool(isatty(fd));
}

_luaAPI(posix,
	_export(getcwd);
	_export(isatty));