foundation2_fluffos_v1/
foundation2_fluffos_v1/bin/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/ChangeLog.old/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/Win32/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/compat/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/compat/simuls/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/include/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/clone/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/command/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/data/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/etc/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/include/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/inherit/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/inherit/master/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/log/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/compiler/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/efuns/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/operators/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/u/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/tmp/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/windows/
foundation2_fluffos_v1/lib/cfg/
foundation2_fluffos_v1/lib/cmds/adm/
foundation2_fluffos_v1/lib/daemon/save/
foundation2_fluffos_v1/lib/daemon/services/
foundation2_fluffos_v1/lib/daemon/soul/
foundation2_fluffos_v1/lib/doc/build/
foundation2_fluffos_v1/lib/doc/classes/
foundation2_fluffos_v1/lib/doc/driver/
foundation2_fluffos_v1/lib/doc/driver/applies/
foundation2_fluffos_v1/lib/doc/driver/applies/interactive/
foundation2_fluffos_v1/lib/doc/driver/concepts/
foundation2_fluffos_v1/lib/doc/driver/driver/
foundation2_fluffos_v1/lib/doc/driver/efuns/arrays/
foundation2_fluffos_v1/lib/doc/driver/efuns/buffers/
foundation2_fluffos_v1/lib/doc/driver/efuns/calls/
foundation2_fluffos_v1/lib/doc/driver/efuns/compile/
foundation2_fluffos_v1/lib/doc/driver/efuns/filesystem/
foundation2_fluffos_v1/lib/doc/driver/efuns/floats/
foundation2_fluffos_v1/lib/doc/driver/efuns/functions/
foundation2_fluffos_v1/lib/doc/driver/efuns/general/
foundation2_fluffos_v1/lib/doc/driver/efuns/mappings/
foundation2_fluffos_v1/lib/doc/driver/efuns/numbers/
foundation2_fluffos_v1/lib/doc/driver/efuns/parsing/
foundation2_fluffos_v1/lib/doc/driver/lpc/constructs/
foundation2_fluffos_v1/lib/doc/driver/lpc/types/
foundation2_fluffos_v1/lib/doc/driver/platforms/
foundation2_fluffos_v1/lib/doc/efun/
foundation2_fluffos_v1/lib/doc/etc/
foundation2_fluffos_v1/lib/doc/faq/
foundation2_fluffos_v1/lib/doc/help/creator/
foundation2_fluffos_v1/lib/doc/help/player/
foundation2_fluffos_v1/lib/doc/lpc/basic/
foundation2_fluffos_v1/lib/doc/lpc/data_types/
foundation2_fluffos_v1/lib/doc/lpc/etc/
foundation2_fluffos_v1/lib/doc/lpc/intermediate/
foundation2_fluffos_v1/lib/doc/lpc/types/
foundation2_fluffos_v1/lib/doc/mudlib/
foundation2_fluffos_v1/lib/doc/mudlib/Features/
foundation2_fluffos_v1/lib/domains/Examples/armour/
foundation2_fluffos_v1/lib/domains/Examples/etc/
foundation2_fluffos_v1/lib/domains/Examples/npc/
foundation2_fluffos_v1/lib/domains/Examples/room/
foundation2_fluffos_v1/lib/domains/Examples/virtual/
foundation2_fluffos_v1/lib/domains/Examples/virtual/exaA/
foundation2_fluffos_v1/lib/domains/Examples/virtual/exaB/
foundation2_fluffos_v1/lib/domains/Examples/weapon/
foundation2_fluffos_v1/lib/domains/School/doc/
foundation2_fluffos_v1/lib/domains/School/room/
foundation2_fluffos_v1/lib/domains/School/room/Classes/
foundation2_fluffos_v1/lib/domains/School/room/Offices/
foundation2_fluffos_v1/lib/domains/Standard/
foundation2_fluffos_v1/lib/domains/Standard/pools/
foundation2_fluffos_v1/lib/domains/Standard/std/
foundation2_fluffos_v1/lib/domains/Standard/xtra/
foundation2_fluffos_v1/lib/include/
foundation2_fluffos_v1/lib/lib/obj/
foundation2_fluffos_v1/lib/news/
foundation2_fluffos_v1/lib/save/
foundation2_fluffos_v1/lib/secure/cfg/
foundation2_fluffos_v1/lib/secure/cmds/player/
foundation2_fluffos_v1/lib/secure/lib/
foundation2_fluffos_v1/old/
foundation2_fluffos_v1/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