merentha_fluffos_v2/
merentha_fluffos_v2/bin/
merentha_fluffos_v2/fluffos-2.9-ds2.03/
merentha_fluffos_v2/fluffos-2.9-ds2.03/ChangeLog.old/
merentha_fluffos_v2/fluffos-2.9-ds2.03/Win32/
merentha_fluffos_v2/fluffos-2.9-ds2.03/compat/
merentha_fluffos_v2/fluffos-2.9-ds2.03/compat/simuls/
merentha_fluffos_v2/fluffos-2.9-ds2.03/include/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/clone/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/command/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/data/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/etc/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/include/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/inherit/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/inherit/master/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/log/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/single/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/single/tests/compiler/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/single/tests/efuns/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/single/tests/operators/
merentha_fluffos_v2/fluffos-2.9-ds2.03/testsuite/u/
merentha_fluffos_v2/fluffos-2.9-ds2.03/tmp/
merentha_fluffos_v2/fluffos-2.9-ds2.03/windows/
merentha_fluffos_v2/lib/cfg/
merentha_fluffos_v2/lib/cfg/races/
merentha_fluffos_v2/lib/cmds/abilities/
merentha_fluffos_v2/lib/cmds/actions/
merentha_fluffos_v2/lib/cmds/spells/
merentha_fluffos_v2/lib/daemon/include/
merentha_fluffos_v2/lib/daemon/services/
merentha_fluffos_v2/lib/doc/
merentha_fluffos_v2/lib/doc/building/
merentha_fluffos_v2/lib/doc/help/classes/
merentha_fluffos_v2/lib/doc/help/general/
merentha_fluffos_v2/lib/doc/help/races/
merentha_fluffos_v2/lib/doc/help/skills/
merentha_fluffos_v2/lib/doc/help/stats/
merentha_fluffos_v2/lib/doc/man/efuns/
merentha_fluffos_v2/lib/doc/man/lfuns/
merentha_fluffos_v2/lib/doc/news/
merentha_fluffos_v2/lib/doc/old/
merentha_fluffos_v2/lib/doc/old/concepts/
merentha_fluffos_v2/lib/doc/old/lpc/constructs/
merentha_fluffos_v2/lib/doc/old/lpc/types/
merentha_fluffos_v2/lib/domains/ROOMS/
merentha_fluffos_v2/lib/domains/obj/armour/
merentha_fluffos_v2/lib/domains/obj/monsters/
merentha_fluffos_v2/lib/domains/obj/other/
merentha_fluffos_v2/lib/domains/obj/weapons/
merentha_fluffos_v2/lib/realms/petrarch/
merentha_fluffos_v2/lib/save/daemons/
merentha_fluffos_v2/lib/save/rid/
merentha_fluffos_v2/lib/save/users/a/
merentha_fluffos_v2/lib/save/users/p/
merentha_fluffos_v2/lib/save/users/t/
merentha_fluffos_v2/lib/std/login/
merentha_fluffos_v2/lib/std/obj/
merentha_fluffos_v2/win32/
#include "std.h"
#include "../lpc_incl.h"
#include "async.h"
#include "../function.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "../config.h"
#include "../interpret.h"
#include "../file.h"
#if defined(F_ASYNC_READ) || defined(F_ASYNC_WRITE)

static struct request *reqs = NULL;
void add_req(struct request *req){
	req->next = reqs;
	reqs = req;
}

int add_read(const char *fname, function_to_call_t *fun){
	aiob *aio = MALLOC(sizeof(aiob));
	memset(aio, 0, sizeof(aiob));
	printf("fname: %s\n", fname);
	int fd = open(fname, O_RDONLY);
	aio->aio_fildes = fd;
	aio->aio_buf = MALLOC(READ_FILE_MAX_SIZE);
	aio->aio_nbytes = READ_FILE_MAX_SIZE;
	struct request *req = MALLOC(sizeof(struct request));
	req->aio = aio;
	req->fun = fun;
	req->type = aread;
	add_req(req);
	return aio_read(aio);
}

int add_write(const char *fname, char *buf, int size, char append, function_to_call_t *fun){
	aiob *aio = MALLOC(sizeof(aiob));
	memset(aio, 0, sizeof(aiob));
	int fd = open(fname, append?O_CREAT|O_WRONLY|O_APPEND:O_CREAT|O_WRONLY, S_IRWXU|S_IRWXG);
	aio->aio_fildes = fd;
	aio->aio_buf = buf;
	aio->aio_nbytes = size;
	struct request *req = MALLOC(sizeof(struct request));
	req->aio = aio;
	req->fun = fun;
	req->type = awrite;
	add_req(req);
	return aio_write(aio);
}

void handle_read(struct request *req, int val){
	aiob *aio = req->aio;
	close(aio->aio_fildes);
	if(val){
		push_number(val);
		call_efun_callback(req->fun, 1);
		free_funp(req->fun->f.fp);
		return;
	}
	val = aio_return(aio);
	if(val < 0){
		push_number(val);
		call_efun_callback(req->fun, 1);
		free_funp(req->fun->f.fp);
		return;
	}
	char *file = MALLOC(val+1);
	memcpy(file, (char *)(aio->aio_buf), val);
	file[val]=0;
	push_malloced_string(file);
	call_efun_callback(req->fun, 1);
	free_funp(req->fun->f.fp);
}

void handle_write(struct request *req, int val){
	aiob *aio = req->aio;
	close(aio->aio_fildes);
	if(val){
		push_number(val);
		call_efun_callback(req->fun, 1);
		free_funp(req->fun->f.fp);
		return;
	}
	val = aio_return(aio);
	if(val < 0){
		push_number(val);
		call_efun_callback(req->fun, 1);
		free_funp(req->fun->f.fp);
		return;
	}
	push_undefined();
	call_efun_callback(req->fun, 1);
	free_funp(req->fun->f.fp);
}

void check_reqs() {
	struct request **check = &reqs;
	while (*check) {
		int val = aio_error((*check)->aio);
		if (val != EINPROGRESS) {
			switch ((*check)->type) {
			case aread:
				handle_read(*check, val);
				break;
			case awrite:
				handle_write(*check, val);
				break;
			default:
				fatal("unknown async type\n");
			}
			struct request *tmp = *check;
			*check = (*check)->next;
			fflush(0);
			FREE((char *)(tmp->aio->aio_buf));
			FREE(tmp->aio);
			FREE(tmp->fun);
			FREE(tmp);
		} else {
			struct request *tmp = *check;
			if(tmp->next){
				fflush(0);
				check = &((*check)->next);
			} else
				check = 0;
		}
	}
}

#ifdef F_ASYNC_READ

void f_async_read(){
	function_to_call_t *cb = MALLOC(sizeof(function_to_call_t));
	memset(cb, 0, sizeof(function_to_call_t));
	process_efun_callback(1, cb, F_ASYNC_READ);
	cb->f.fp->hdr.ref++;
	add_read(check_valid_path((sp-1)->u.string, current_object, "read_file", 0), cb);
	pop_2_elems();
}
#endif

#ifdef F_ASYNC_WRITE
void f_async_write(){
	char *buf = MALLOC(strlen((sp-1)->u.string));
	strcpy(buf, (sp-1)->u.string);
	function_to_call_t *cb = MALLOC(sizeof(function_to_call_t));
	memset(cb, 0, sizeof(function_to_call_t));
	process_efun_callback(2, cb, F_ASYNC_WRITE);
	cb->f.fp->hdr.ref++;
	add_write(check_valid_path((sp-2)->u.string, current_object, "write_file", 1), buf, strlen(buf), 0, cb);
	pop_3_elems();
}
#endif
#endif