/soar/1.1/common/c/include/basic/misc.h
C++ Header | 524 lines | 368 code | 56 blank | 100 comment | 19 complexity | 719fa5fa4758f92be76c0037652e2163 MD5 | raw file
- #ifndef __BASIC_MISC_H__
- #define __BASIC_MISC_H__
- #if defined(__linux__)
- #ifndef _GNU_SOURCE
- #define _GNU_SOURCE
- #endif
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <inttypes.h>
- #include <time.h>
- #include <math.h>
- #include <string.h>
- #include <strings.h>
- #include <errno.h>
- #include <ctype.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/wait.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <pthread.h>
- #include <signal.h>
- #include <semaphore.h>
- #include <mqueue.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/mman.h>
- #include <sys/sem.h>
- #include <sys/resource.h>
- #include <dirent.h>
- #include <getopt.h>
- #include <regex.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <sys/un.h>
- #include <sys/select.h>
- #include <sys/poll.h>
- #include <netdb.h>
- #include <pwd.h>
- #if defined(__APPLE__) || defined(__FreeBSD__)
- #include <machine/endian.h>
- #else
- #include <endian.h>
- #endif
- __BEGIN_DECLS
- /*======*/
- /* ?? */
- /*======*/
- #define ZERO '\0' /* ??0 */
- #define ZEROS "" /* ??? */
- #define CHAR0 '0' /* ??'0' */
- #define RTN '\n' /* ?? */
- #define RTNS "\n" /* ???? */
- #define CRLF "\r\n" /* ??? */
- #define SLASH '/' /* ?? */
- #define BACKSLASH '\\' /* ??? */
- #define TAB '\t' /* TAB */
- #define TABS "\t" /* TAB? */
- #define SPACE ' ' /* ?? */
- #define SPACES " " /* ?????*/
- #define OPTION_FMT "%1d" /* ???? */
- #define SHORT_NUMBER_FMT "%06d" /* ????? */
- #define LONG_NUMBER_FMT "%012ld" /* ????? */
- #define LLONG_NUMBER_FMT "%012lld" /* ?????? */
- #define IS_TRUE 1 /* ?? */
- #define IS_FALSE 0 /* ?? */
- #define BIT_OF_BYTE 8 /* ???? */
- #define BIT_OF_CHAR BIT_OF_BYTE /* char?? */
- #define BIT_OF_INT (sizeof(int)*BIT_OF_CHAR) /* int?? */
- #define KB(n) ((n)*1024) /* n KB */
- #define MB(n) ((n)*KB(1024)) /* n MB */
- #define GB(n) ((n)*MB(1024)) /* n GB */
- #define MSG_SIZE_SIZE 6 /* ??????? */
- #define MSG_SIZE KB(1024) /* ???? */
- #define BUFFER_SIZE KB(1) /* ????? */
- #define FILE_NM_SIZE 80 /* ????? */
- #define LONG_FILE_NM_SIZE 200 /* ??????? */
- #define IP_SIZE 15 /* IP???? */
- #define OPTION_SIZE 1 /* ???? */
- #define SHORT_NUMBER_SIZE 6 /* ????? */
- #define LONG_NUMBER_SIZE 12 /* ????? */
- #define LLONG_NUMBER_SIZE 12 /* ?????? */
- #define NAME_SIZE 30 /* ???? */
- #define LONG_NAME_SIZE 200 /* ????? */
- #define PASSWD_MIN_SIZE 6 /* ???? */
- #define PASSWD_MAX_SIZE 15 /* ???? */
- #define PASSWD_ENC_SIZE 32 /* ??????? */
- /*======*/
- /* ?? */
- /*======*/
- #define FLAG_MULTI_THREAD 0x80000000 /* ??? */
- #define FLAG_LOCAL 0x40000000 /* ?? */
- #define FLAG_CASE_SENSITIVE 0x20000000 /* ????? */
- /*----- ???? -----*/
- #define CLR_BUF(_buf) (_buf)[0]=ZERO
- #define BUF_IS_CLR(_buf) ((_buf)[0]==ZERO)
- /*
- * 16????????
- * ???
- */
- extern char * /* ??str:?? NULL:??????0 */
- expand_to_str(
- unsigned char *hex, /* [IN] 16??? */
- int size, /* [IN] 16????? */
- char *str /* [OUT] ?? */
- );
- /*
- * ????16???
- */
- extern unsigned char * /* ??hex:?? NULL:??????0???2??? */
- collapse_from_str(
- char *str, /* [IN] ?? */
- int size, /* [IN] ???? */
- unsigned char *hex /* [OUT] 16??? */
- );
- /*
- * ?????????
- */
- extern char * /* ??str */
- trim_head_space(
- char *str /* [IN,OUT] ??? */
- );
- /*
- * ?????????
- */
- extern char * /* ??str */
- trim_tail_space(
- char *str /* [IN,OUT] ??? */
- );
- /*
- * ???????????????????????
- */
- extern char * /* ??str */
- trim_tail_space_notnull(
- char *str /* [IN,OUT] ??? */
- );
- /*
- * ??????
- */
- extern char * /* ??str */
- trim_redundant_space(
- char *str /* [IN,OUT] ??? */
- );
- /*
- * ?????????
- */
- extern char * /* ??str */
- trim_space(
- char *str /* [IN,OUT] ??? */
- );
- /*
- * ????????
- */
- extern char * /* ??str:?? NULL:?????<=0 */
- pad_space(
- char *str, /* [IN,OUT] ??? */
- int buflen /* [IN] ???????? */
- );
- /*
- * ?tab?????
- */
- extern char * /* ??str:?? NULL:??????? */
- expand_tab(
- char *str, /* [IN,OUT] ??? */
- int buflen, /* [IN] ???????? <=0:????? */
- int unit /* [IN] ???? <=0:??4?? */
- );
- /*
- * ????HASH?
- */
- extern unsigned int /* HASH?(???):?? 0:????????????0 */
- get_hash(
- unsigned char *data, /* [IN] ??? */
- int len /* [IN] ??HASH?????? */
- );
- /*
- * ??????
- */
- extern int /* 0:?? -1:???? */
- wrap_size(
- unsigned char *msg_in, /* [IN] ???? */
- int *msg_size, /* [IN] ?????? [OUT] ?????? */
- unsigned char *msg_out /* [OUT] ???????msg_in?? */
- );
- /*
- * ??????
- */
- extern int /* 0:?? -1:???? */
- unwrap_size(
- unsigned char *msg_in, /* [IN] ???? */
- int *msg_size, /* [IN] ?????? [OUT] ?????? */
- unsigned char *msg_out /* [OUT] ???????msg_in?? */
- );
- /*
- * ?????
- */
- extern int /* >0:????? <=0:???? */
- str_split(
- char *str, /* [IN] ????? */
- char delim, /* [IN] ????0:????tab???? */
- char ***arrp /* [OUT] ???? ?FREE_PTR_ARRAY?? */
- );
- /*
- * ???????????
- */
- extern char * /* ??str */
- str_toupper(
- char *str /* [IN,OUT] ??? */
- );
- /*
- * ???????????
- */
- extern char * /* ??str */
- str_tolower(
- char *str /* [IN,OUT] ??? */
- );
- /*
- * ?????
- */
- extern char * /* ?????????dest */
- str_replace(
- char *src, /* [IN] ???? */
- char *old, /* [IN] ?????? */
- char *rep, /* [IN] ????? */
- char *dest /* [OUT] ???????????????NULL????????????? */
- );
- /*
- * ?????
- */
- extern char * /* ????????str */
- str_insert(
- char *str, /* [IN,OUT] ???? */
- int start, /* [IN] ???? */
- char *ins /* [IN] ????? */
- );
- /*
- * ????????10??????
- */
- extern int /* 1:? 0:? */
- str_is_dec(
- char *str /* [IN] ??? */
- );
- /*
- * ????????16??????
- */
- extern int /* 1:? 0:? */
- str_is_hex(
- char *str /* [IN] ??? */
- );
- /*
- * ????10??????16???
- */
- extern int /* 16??? */
- dec_to_hex(
- int dec /* [IN] 10??? */
- );
- /*
- * ????
- */
- extern int /* ????? */
- pack_size(
- int org_size, /* [IN] ??? */
- int unit /* [IN] ???? */
- );
- /*----- ?? -----*/
- /*
- * CRC????
- */
- extern unsigned short /* 0:?? -1:???? */
- get_crc_16(
- unsigned char *p, /* [IN] ????? */
- int count /* [IN] ???? */
- );
- /*----- ???? -----*/
- #define ALLOC_MULTI(_ptr,_type,_num,_errlabel) \
- do \
- { \
- if(((_ptr)=(_type *)malloc(sizeof(_type)*(_num))) == NULL) \
- { \
- goto _errlabel; \
- } \
- } while(0)
- #define ALLOC(_ptr,_type,_errlabel) \
- ALLOC_MULTI(_ptr,_type,1,_errlabel)
- #define ALLOC_ARRAY(_ptr,_type,_num,_size,_errlabel) \
- do \
- { \
- int _inc; \
- \
- _inc=(_num); \
- ALLOC_MULTI(_ptr,_type,_inc,_errlabel); \
- _size=_inc; \
- } while(0)
- #define EXPAND_ARRAY(_ptr,_type,_num,_size,_errlabel) \
- do \
- { \
- int _inc; \
- _type *_ptr_tmp; \
- \
- _inc=(_num); \
- if((_ptr_tmp=(_type *)realloc((_ptr), \
- sizeof(_type)*((_size)+(_inc)))) == NULL) \
- { \
- goto _errlabel; \
- } \
- _size+=_inc; \
- _ptr=_ptr_tmp; \
- } while(0)
- #define ALLOC_MULTI_RETERR(_ptr,_type,_num,_errcode) \
- do \
- { \
- if(((_ptr)=(_type *)malloc(sizeof(_type)*(_num))) == NULL) \
- { \
- return(_errcode); \
- } \
- } while(0)
- #define ALLOC_RETERR(_ptr,_type,_errcode) \
- ALLOC_MULTI_RETERR(_ptr,_type,1,_errcode)
- #define ALLOC_ARRAY_RETERR(_ptr,_type,_num,_size,_errcode) \
- do \
- { \
- int _inc; \
- \
- _inc=(_num); \
- ALLOC_MULTI_RETERR(_ptr,_type,_inc,_errcode); \
- _size=_inc; \
- } while(0)
- #define EXPAND_ARRAY_RETERR(_ptr,_type,_num,_size,_errcode) \
- do \
- { \
- int _inc; \
- _type *_ptr_tmp; \
- \
- _inc=(_num); \
- if((_ptr_tmp=(_type *)realloc((_ptr), \
- sizeof(_type)*((_size)+(_inc)))) == NULL) \
- { \
- return(_errcode); \
- } \
- _size+=_inc; \
- _ptr=_ptr_tmp; \
- } while(0)
- #define ALLOC_MULTI_DOERR(_ptr,_type,_num,_doerr) \
- do \
- { \
- if(((_ptr)=(_type *)malloc(sizeof(_type)*(_num))) == NULL) \
- { \
- _doerr; \
- } \
- } while(0)
- #define ALLOC_DOERR(_ptr,_type,_doerr) \
- ALLOC_MULTI_DOERR(_ptr,_type,1,_doerr)
- #define ALLOC_ARRAY_DOERR(_ptr,_type,_num,_size,_doerr) \
- do \
- { \
- int _inc; \
- \
- _inc=(_num); \
- ALLOC_MULTI_DOERR(_ptr,_type,_inc,_doerr); \
- _size=_inc; \
- } while(0)
- #define EXPAND_ARRAY_DOERR(_ptr,_type,_num,_size,_doerr) \
- do \
- { \
- int _inc; \
- _type *_ptr_tmp; \
- \
- _inc=(_num); \
- if((_ptr_tmp=(_type *)realloc((_ptr), \
- sizeof(_type)*((_size)+(_inc)))) == NULL) \
- { \
- _doerr; \
- } \
- _size+=_inc; \
- _ptr=_ptr_tmp; \
- } while(0)
- #define FREE(_ptr) \
- do \
- { \
- if(_ptr) free(_ptr); \
- _ptr=NULL; \
- } while(0)
- #define FREE_PTR_ARRAY(_array, _size) \
- do \
- { \
- if(_array) \
- { \
- int _i; \
- for(_i=0; _i<_size; _i++) \
- { if(_array[_i]) free(_array[_i]); } \
- free(_array); _array=NULL; \
- } \
- } while(0)
- #define CLR_PTR_ARRAY(_array, _size) \
- do \
- { \
- if(_array) \
- { \
- int _i; \
- for(_i=0; _i<_size; _i++) \
- (_array[_i])=NULL; \
- } \
- } while(0)
- /*----- ??? -----*/
- /*
- * ???????
- */
- extern int /* ????? */
- random_int(
- int min_val, /* [IN] ??? */
- int max_val, /* [IN] ??? */
- unsigned int *seedp /* [IN,OUT] ?? */
- );
- /*
- * ????????
- */
- extern int
- random_array_int(
- int array[], /* [IN,OUT] ???? */
- int num /* [IN] ???? */
- );
- /*
- * ????
- */
- extern char * /* ??????password */
- random_password(
- char *password, /* [IN,OUT] ?? */
- int size /* [IN] ???? */
- );
- /*----- ?????? -----*/
- /*
- * ?????
- */
- extern char * /* ???????dirname */
- extract_dirname(
- char *filename, /* [IN] ????? */
- char *dirname /* [OUT] ??? */
- );
- /*
- * ?????
- */
- extern char * /* ???????basename */
- extract_basename(
- char *filename, /* [IN] ????? */
- char *basename /* [OUT] ??? */
- );
- /*
- * ????
- */
- extern int /* 0:?? -1:??????????? -2:?????? -3:?????? */
- remove_dir(
- char *path /* [IN] ?? */
- );
- /*
- * ????
- */
- extern int /* 0:?? -1:??????????? -2:?????? -3:mkdir?? */
- create_dir(
- char *dir /* [IN] ?? */
- );
- /*----- ?? -----*/
- #define DEEP_SLEEP select(0, NULL, NULL, NULL, NULL)
- __END_DECLS
- #endif