#include "file64.h"
#include "tcplib.h"
#include "lib.h"

#define iffree(x) if ((x)!=NULL) {free(x); (x) = NULL;}
int ini_reload(void);

typedef struct TI_LIST {struct TANY *any; struct TI_LIST *next;} Ti_list;
typedef struct TI_PAIR {struct TANY *var; struct TANY *value; struct TI_PAIR *next;} Ti_pair;
typedef struct TANY {int type; char *string; int string_len; bigint number; struct TI_LIST *list; struct TI_PAIR *pair; }  Tany;

typedef struct THREADDATA {Thread *th; void *p1; } ThreadData;
char *abs_path(char *path, char *result);

typedef struct THREADLIST {
	Thread *th; int lastcpu; time_t last; 
	char *threadname; int thid; void *data; 
	int showlast;
	char *state;
	struct THREADLIST *next;
} Threadlist;

Wordlist *main_thread_info(void);
char *mini_logpath(char *fname, char *tmp);
int quota_get_used(char *obj);
int quota_add(char *obj, double x);
int file_md5(FILE *f,int maxitem,char *md5);
char* get_userdom(char *userdom, char *user, char *dom);
char *tlib_tempfile(char *fname);

ThreadData *td_new(void);
void td_free(ThreadData *td);
int is_exit(void);
int main_nthreads(void);
int peak_nthreads(void);
void t_mutex_dump(void);
int is_badhost(void);
int slib_lock_object_real(char *obj, int timeout, char *file, int line, char *whence);
int slib_unlock_object_real(char *obj, char *file, int line);

int do_stop(void);
#define slib_lock_object(o,t)	slib_lock_object_real(o,t,__FILE__,__LINE__,NULL)
#define slib_unlock_object(o)	slib_unlock_object_real(o,__FILE__,__LINE__)

int get_next_tid(void);
int ti_dict_add_any_bin(Tany *base, char *var, int varlen, Tany *value);
void t_mutex_init(void);
void t_mutex_release(void);
Threadlist *main_thread_new(char *threadname, char *src, int line);
int main_thread_free(Threadlist *tlist);
int main_thread_store(Threadlist *tl, Thread *th);
int clib_accept_list(Chan *vlisten[], Chan **newchan);
int clib_listen_list(Chan *clist[], int max, char *list, int dflt_port);
int th_web_run(Chan *c);

int start_torrent(char *torrent_file, char *path, int id, int start_thread, Bitmap *bm, char *owner, char *access_owner, char *access_read, time_t created, int isseed, char *reason, int *pri, int ishosted);
int ti_write_file(Tany *meta, char *torrentfile);
char *my_ip(void);
int my_port(void);
char *memdup(char *mem, int n);
char *work_dir(void);

char *log_file(char *file, char *tmp);

char *log_dir(void);

int test_port(void);

int th_proxy_run(Chan *c);
int do_install(int argc, char *argv[]);
int do_uninstall(void);
int main_thread_dump(void);
int storrent_running(char *pidfile);

int ulimit_stack_get(void);



char *home_path(void);
void t_mutex_init(void);

char *work_dir(void);
char *log_file(char *file, char *tmp);
char *log_dir(void);
#define t_mutex_wait() t_mutex_wait_real(__FILE__,__LINE__)
int t_mutex_wait_real(char *file, int line);
int main_thread_free_me(void);
