/* MLH/MH library of special calls - general, Mike Harrelson */ /* version 3.1 revision 9-9-08*/ /* Copyright (C) 1996-2008 Mike Harrelson The MLH Library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. The MLH Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU General Public License is often shipped with GNU software, and is generally kept in a file called COPYING or LICENSE. If you do not have a copy of the license, write to the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ /*libmlh.h | libmh.h */ #include #include #include #include #include #include /*library structures*/ struct ip_type { char address[64][16]; /*array of IP address pointers*/ short size; /*number addresses in list*/ }; struct hashtable { void **array; /*array of hash buckets*/ int hashsize; /*number of hash buckets*/ int count; /*number of entries in table for stats*/ void *first; /*pointer to first entry in table*/ void *last; /*pointer to last entry in table*/ int bcollisions; /*bucket collision stats*/ int usedbuckets; /*number of buckets in use for stats*/ int hashfunc; /*hash function type*/ }; typedef struct hashentry { /*used in hashtables*/ void *prev; /*previous in the bucket chain*/ void *next; /*next in the bucket chain*/ char *key; /*string serving as the unique key*/ void *data; /*pointer to data, could be NULL*/ int datasize; /*size of data, should be 0 if data is a pointer*/ void *listp; /*previous in the order list*/ void *listn; /*next in the order list*/ short type; /*user-defined type*/ } Hashentry; struct dlist_type { int size; /*number of items in dynamic list*/ int maxlistsize; /*number of items that can currently be stored*/ char **item; /*pointer to array of items*/ unsigned int gfactor; /*increment factor for growth*/ }; struct qstack { void *first; /*first item in qstack (bottom)*/ void *last; /*last item in qstack (top)*/ int size; /*number of items in qstack*/ int bsize; /*total number of bytes currently in qstack for stats*/ int maxitems; /*mandated limit of items allowed in qstack*/ }; struct linklist_item { /*used in qstacks*/ void *prev; /*previous item*/ void *next; /*next item*/ void *data; /*data in item*/ int dsize; /*size of data in item*/ short type; /*user-defined type*/ }; struct dstring { /*use dstring instead of String to avoid C++ conflicts*/ char *buff; /*ptr to allocated buffer*/ int bufsize; /*current size of allocated buffer*/ int len; /*length of string stored in buffer*/ unsigned short pattern; /*verify pattern*/ }; struct darray_type { int size; /*current number of items in array*/ int maxlistsize; /*current size of allocated space*/ void **items; /*pointer to array of pointers*/ short type; /*type of pointers, user-defined*/ }; /*dns_rec not used in libmh*/ struct dns_rec { unsigned char ip[32][96]; /*response records*/ unsigned short wt[32]; /*weights, if returning MX records*/ short icount; /*count of records in dns_rec.ip*/ short itype; /*type of responses*/ int ttl; /*reserved for TTL, not currently used*/ }; struct aio_type { int readfd; /*reader file desc.*/ int writefd; /*writer file desc.*/ int rflags; /*fcntl flags for readfd*/ int wflags; /*fcntl flags for writefd*/ unsigned char *readbuf; /*allocated read buffer*/ unsigned char *writebuf; /*allocated write buffer*/ int rdbufsz; /*size of read buffer*/ int wrbufsz; /*size of write buffer*/ int rderrno; /*errno after read operation*/ int wrerrno; /*errno after write operation*/ int readqsz; /*number of bytes read so far*/ int writeqsz; /*number of bytes buffered for write out*/ int rdreqsz; /*size of requested read data*/ }; struct timer { void *pptr; /*user supplied data pointer1*/ int ptype; /*type of data pointer1*/ void *sptr; /*2ndary user supplied data pointer2*/ int stype; /*type on 2ndary data pointer2*/ int operation; /*user defined operation*/ int state; /*user defined state*/ int stage; /*user defined stage*/ int counter[4]; /*incremental/decremental counter*/ struct timeval interval[4]; /*interval-duration of timers*/ struct timeval etime[4]; /*expire time of timers*/ int mode[4]; /*state of timers*/ }; #define TRUE 1 #define FALSE 0 #define YES 1 #define NO 0 #define ON 1 #define OFF 0 /*prototypes*/ /* String Functions */ int lib_strtoint(char*, int*); char *lib_inttostr(int, char*); double lib_strtofloat(char*, int*); char *lib_floattostr(double, char*); char *lib_all_tolower(char*); char *lib_all_toupper(char*); int lib_strip(char*, char*, short); int lib_bstrip(char*, int, int); int lib_strip_nonalphanum(char*, char, char*); int lib_chomp(char*); char *lib_chop(char*, int); char *lib_getsubstr(char*, short, short,char*); char *lib_strsub(char*, char*, short); char *lib_insertstr(char*, char*, short); char *lib_insertfiller(char*, char, short); char *lib_replacestr(char*, char*, char*); char *lib_sreplacestr(char*, char*, char*, short); char *lib_revstr(char*); int lib_countstr(char*, char*); char *lib_dsprintf(int*, char*, ...); char lib_strcap(char*, char, short); int lib_strip_whitespace(char*, char); int lib_strip_8bit(unsigned char*); int lib_strip_unprintable(char*); int lib_stripendchars(char*, char, char); char *lib_stringhex_conv(char*); char *lib_2hex_transform(unsigned char*, short, char*); unsigned char *lib_quote_convert(unsigned char*, char*); int lib_grep(char*, char*, char, char); char *lib_get_match_str(char*, char*, char, char*, unsigned char); char *lib_case_strstr(char*, char*); char *lib_replace_char(char*, char, char); int lib_fstrcmp(char*, char*, char); /* Input Functions */ int lib_getastr(char*, char*, int); int lib_gethstr(char*, char*, int); int lib_getvstr(char*, char*, int, char*); char *lib_dfgets(FILE*, int, short, int*); char lib_getvch(char*, char, char); char lib_getvchar(char*, char); char lib_getch(void); char lib_getche(void); short lib_keypressed(int); char lib_readkey(void); int lib_noecho(void); int lib_echo(void); int lib_linebufferctl(char); int lib_rwpoll(int,int); int lib_readpoll(int, int); int lib_readpoll2(int, int, int); int lib_mknonblockfd(int); int lib_mkblockfd(int); /* File Functions */ FILE *lib_fopenlock(char*, char*, char); int lib_fcloselock(FILE*); int lib_sflock(char*, int); int lib_sfunlock(char*, int); int lib_getsflockinfo(char*, char*, int); int lib_getumask(void); int lib_setumask(int); int lib_log(char*, char*,...); int lib_dlog(int, char*, char*); long long lib_fcopy(char*, char*, char); long lib_ftrans(FILE*, FILE*); long lib_fdtrans(int, int); int lib_append(char*, char*, char, char); int lib_fappend(int, int); int lib_flock(int, char, int); char *lib_getfilename(char*); char *lib_last_path_element(char*, char*); char *lib_getpathdir(char*, char*); int lib_condense_path(char*, char); int lib_get_file_ext(char*, char*); int lib_match_inode(char*, char*); int lib_match_fs(char*, char*); int lib_getfiletype(char*); int lib_compare_filenodes(char*, char*); int lib_compare_filecontent(char*, char*); int lib_is_sparsefile(char*); int lib_fcat(char*, int); int lib_diffstat(char*, struct stat*, struct stat*); long lib_getfilesize(char*, unsigned int*); int lib_getfileguid(char*, int*); int lib_getfileperms(char*); int lib_getfilesize2(char*, int*); int lib_isfileopen(char*); /* IPC/Network Functions */ int lib_open_tcp_connection(char*, int); int lib_init_tcp_connection(char*, int, int*); int lib_make_tcp_socket(int, short, long); int lib_acceptconnect(int, int*, char); int lib_gethostinfo(int, char*, char*); int lib_open_unix_socket(char*); int lib_make_unix_socket(char*, int); int lib_make_udp_socket(unsigned long, int); int lib_make_udp_addr(struct sockaddr_in*, unsigned long, int); int lib_nslookup(char*, char *); int lib_dnslookup(char*, struct ip_type*); int lib_revnslookup(char*, char*); int lib_check_cidr(char*, char*); int lib_check_ipblock(char*, char*); int lib_check_fqdn_match(char*, char*); int lib_hostlookup(char*, char*, char*); int lib_rhostlookup(char*, char*, char*); int lib_dns_query(char*, short, char*, struct dns_rec*, short, short); /* Date & Time Functions */ char lib_gettime(int*, int*, int*, char); char *lib_getdate(int*, int*, int*, char*, char*, int); char *lib_timestamp(char*, char*); char *lib_value_to_month(int, char*, char); int lib_month_to_value(char*); char *lib_value_to_dayofwk(int, char*, char); int lib_dayofwk_to_value(char*); long lib_days_from_epoch(int, int, int); unsigned int lib_seconds2units(unsigned int, int*, int*, int*, int*); char *lib_print_datetime(char*); /* Unix Processes Functions */ void lib_die(int, char*, ...); int lib_exitstat(int); void lib_do_nothing(int); int lib_timer(int); int lib_checkpid(int); int lib_daemonize(int); int lib_read_pidfile(char*); int lib_makepidfile(char*, char); int lib_free(void*); int lib_freeall(void*, ...); /* Math Functions */ double lib_random_float(long); unsigned char lib_random_byte(long); long lib_random_long(long); long lib_pwr(int, int); int lib_octaltodec(char*); int lib_dectoctal(int); char *lib_delimitint(int, char*); long lib_round(double, double*); int lib_setbit(unsigned int*, char, unsigned char); int lib_getbit(unsigned int, char); unsigned int lib_nextpowerof2(unsigned int); int lib_prime(unsigned long); unsigned long lib_nextprime(unsigned long); /* Hash Functions */ struct hashtable *lib_create_hashtable(int, int); int lib_delete_hashtable(struct hashtable*); int lib_add_hashrecord(struct hashtable*, char*, void*, int, int); int lib_edit_hashrecord(struct hashtable*, char*, void*, int, int); int lib_get_hashrecord(struct hashtable*, char*, void**, int*, int*); int lib_delete_hashrecord(struct hashtable*, char*); int lib_get_hashcount(struct hashtable*, int*, int*, int*, int*); int lib_get_bucketcount(struct hashtable*, char*,int *loc); int lib_get_hashlinear_ptrs(struct hashtable*, void**, void**); void *lib_get_hashlinear_rec(void*, void**, void**, int*, char**, int*); struct hashtable *lib_rehashtable(struct hashtable*, int, int); int lib_reposition_item(struct hashtable*, char*, int); int lib_rekey_hashitem(struct hashtable*, char*, char*); int lib_move_hashrecord(struct hashtable*, struct hashtable*, char*); int lib_freeall_hashtable(struct hashtable**, ...); int lib_dlist2hashtable(struct hashtable*, struct dlist_type*); /* Screen Functions */ void lib_sclear(int); void lib_shome(int); void lib_screen_reset(int); void lib_beep(int); /* Dynamic List Functions */ struct dlist_type* lib_create_dlist(int); int lib_destroy_dlist(struct dlist_type*); int lib_clear_dlist(struct dlist_type*); char *lib_dfsplit(char*, struct dlist_type*, char*, char*, char); int lib_dsplit(char*, struct dlist_type*, char*); int lib_dlist_replace(struct dlist_type*, int, char*); int lib_dlist_add(struct dlist_type *, char*); char *lib_dlist_join(char* , struct dlist_type*, char*); int lib_dlist_delete(struct dlist_type*, int); int lib_dlist_insert(struct dlist_type*, char*, int); int lib_dparse(char*, struct dlist_type*, char*, char); int lib_dlist_getdir(struct dlist_type*, char*, char); int lib_dlist_sort(struct dlist_type*, char); int lib_dlist_qsort(struct dlist_type*, char); int lib_dlist_exec(struct dlist_type*); int lib_getdlistcount(struct dlist_type*); int lib_getdlistsize(struct dlist_type*); int lib_dlist_blkdelete(struct dlist_type*, int, int, char); int lib_dlist_from_hashkeys(struct hashtable*, struct dlist_type*); int lib_dlist_swap(struct dlist_type*, int, int); int lib_dlist_reverse(struct dlist_type *); int lib_dlist_cgiparse(struct dlist_type*, char*); int lib_grep_dlist(struct dlist_type*, struct dlist_type*, char*, short); int lib_egrep_dlist(struct dlist_type*, struct dlist_type*, char*, short, char*); int lib_nuparse(char*, char*, struct dlist_type*, char*); int lib_dlist_sorted_uniq(struct dlist_type*, char); int lib_dlist_uniq(struct dlist_type*, char); int lib_dlist_compare(struct dlist_type*, struct dlist_type*, char, char); int lib_dlist_append(struct dlist_type*, struct dlist_type*, char); int lib_dlist_merge(struct dlist_type*, struct dlist_type*, char); int lib_dlist_copy(struct dlist_type*, struct dlist_type*); int lib_freeall_dlist(struct dlist_type**, ...); int lib_dlist_readtxtfile(struct dlist_type*, char*); /* QStack Functions */ struct qstack *lib_create_qstack(void); int lib_delete_qstack(struct qstack*); int lib_clear_qstack(struct qstack*); int lib_qstack_push(struct qstack*, void*, int, int); int lib_qstack_press(struct qstack*, void*, int, int); void *lib_qstack_pop(struct qstack*, int*, int*); void *lib_qstack_vpop(struct qstack*, int*, int*); void *lib_qstack_pull(struct qstack*, int*, int*); void *lib_qstack_vpull(struct qstack*, int*, int*); int lib_qstack_pointers(struct qstack*, void**, void**); void *lib_qstack_getlistptrs(void*, void**, void**, int*, int*); int lib_qstack_insert(struct qstack*, void*, char, void*, int, int); int lib_qstack_remove(struct qstack*,void*); int lib_qstack_size(struct qstack*, int*); int lib_qstack_setmaxsize(struct qstack*, int); void *lib_qstack_returnitem(struct qstack*, int , int*, int*); int lib_freeall_qstack(struct qstack**, ...); /* Encoding Functions */ char *lib_encode(char*, char*, int, int*, short); char *lib_decode(char*, char*, int, int*, short); char *lib_pscramble(unsigned char*, unsigned char*, int); char *lib_scramble(unsigned char*, char*, int, long); char *lib_unscramble(unsigned char*, char*, int, long); unsigned int lib_checksum(char*, int, char); int lib_bitrotate(unsigned char*, short, char); char *lib_hextobin(unsigned char*, char*, int*); char *lib_bintohex(unsigned char*, int, char*, int*); char *lib_invert(unsigned char*, int, char, char); int lib_getranbits(int, int); char *lib_makehash(unsigned char*, char*, int); char *lib_pwd_encrypt(char*, char*, int*); /*Dynamic String Functions*/ struct dstring *lib_createStr(char*); int lib_destroyStr(struct dstring*); int lib_setStr(struct dstring*, char*); int lib_resetStr(struct dstring*); char *lib_getStr(struct dstring*); int lib_Strlen(struct dstring*); int lib_Strtrunc(struct dstring*,int); char *lib_Strcpy(struct dstring*,char*); char *lib_Strncpy(struct dstring*,char*,int); char *lib_Strcat(struct dstring*,char*); char *lib_Strncat(struct dstring*,char*,int); int lib_Sprintf(struct dstring*,char*,...); int lib_Strcasecmp(struct dstring*, struct dstring*); int lib_Strncasecmp(struct dstring*, struct dstring*, int); int lib_Strcmp(struct dstring*, struct dstring*); int lib_Strncmp(struct dstring*, struct dstring*, int); int lib_insertStr(struct dstring*, char*, short); int lib_replaceStr(struct dstring*, char*, char*); int lib_sreplaceStr(struct dstring*, char*, char*, short); struct dstring *lib_Strdup(struct dstring*); int lib_freeall_Str(struct dstring**, ...); /* Dynamic Array Functions */ struct darray_type *lib_darray_create(int); int lib_darray_discard(struct darray_type*); int lib_darray_clear(struct darray_type*); int lib_darray_free(struct darray_type*); int lib_darray_add(struct darray_type*, void*); int lib_darray_insert(struct darray_type*, void*, int); void *lib_darray_delete(struct darray_type*, int); void *lib_darray_fastdel(struct darray_type*, int); void *lib_darray_get(struct darray_type*, int); void *lib_darray_replace(struct darray_type*, void*, int); int lib_darray_swap(struct darray_type*, int, int); int lib_darray_getsize(struct darray_type*); int lib_darray_blkdel(struct darray_type*, int, int, int); struct darray_type *lib_darray_blkpull(struct darray_type*, int, int); int lib_darray_blkinsert(struct darray_type*, struct darray_type*, int); struct darray_type *lib_darray_blkget(struct darray_type*, int, int); int lib_darray_dlisttrans(struct darray_type*, struct dlist_type*); int lib_freeall_darray(struct darray_type**, ...); /* Asychronous I/O functions */ struct aio_type *lib_aio_create(int, int, int, int); int lib_aio_discard(struct aio_type*,int); int lib_aio_resize(struct aio_type*, int*, int*, int); int lib_aio_read(struct aio_type*, char*, int, int*); int lib_aio_write(struct aio_type*, char*, int); int lib_aio_rewrite(struct aio_type*, int); int lib_aio_reread(struct aio_type*, int); int lib_aio_readret(struct aio_type*, char*, int); int lib_aio_readcancel(struct aio_type*); int lib_aio_writecancel(struct aio_type*); int lib_aio_getrdstat(struct aio_type*, int*, int*); int lib_aio_getwrstat(struct aio_type*, int*); int lib_aio_getbufstat(struct aio_type*, int*, int*); int lib_freeall_aio(struct aio_type**, ...); /* Timer Functions */ struct timer* lib_timer_create(void*, int, void*, int); int lib_timer_destroy(struct timer*); int lib_timer_counter(struct timer*, int, int); int lib_timer_fillset(struct timer*, int, int, int); int lib_timer_getset(struct timer*, int*, int*, int*); int lib_timer_setptrs(struct timer*, void*, int, int); int lib_timer_getptrs(struct timer*, void**, int*, void**, int*); int lib_timer_set(struct timer*, int, unsigned int, unsigned int); int lib_timer_getstate(struct timer*, int, int); int lib_timer_start(struct timer*, unsigned short); int lib_timer_stop(struct timer*, int); int lib_timer_check(struct timer*); int lib_freeall_timer(struct timer**, ...);