1 /* HSCUTL.H (c) Copyright Roger Bowler, 1999-2009 */ 2 /* Host-specific functions header file */ 3 4 /*-------------------------------------------------------------------*/ 5 /* Implementation of functions used in Hercules that may */ 6 /* be missing on some platform ports, or other convenient */ 7 /* miscellaneous global utility functions. */ 8 /*-------------------------------------------------------------------*/ 9 10 #ifndef __HSCUTL_H__ 11 #define __HSCUTL_H__ 12 13 #include "hercules.h" 14 15 #ifndef _HSCUTL_C_ 16 #ifndef _HUTIL_DLL_ 17 #define HUT_DLL_IMPORT DLL_IMPORT 18 #else /* _HUTIL_DLL_ */ 19 #define HUT_DLL_IMPORT extern 20 #endif /* _HUTIL_DLL_ */ 21 #else 22 #define HUT_DLL_IMPORT DLL_EXPORT 23 #endif 24 25 #ifndef _HSCUTL2_C_ 26 #ifndef _HUTIL_DLL_ 27 #define HU2_DLL_IMPORT DLL_IMPORT 28 #else /* _HUTIL_DLL_ */ 29 #define HU2_DLL_IMPORT extern 30 #endif /* _HUTIL_DLL_ */ 31 #else 32 #define HU2_DLL_IMPORT DLL_EXPORT 33 #endif 34 35 /********************************************************************* 36 The following couple of Hercules 'utility' functions may be defined 37 elsewhere depending on which host platform we're being built for... 38 For Windows builds (e.g. MingW32), the functionality for the below 39 functions is defined in 'w32util.c'. For other host build platforms 40 (e.g. Linux, Apple, etc), the functionality for the below functions 41 is defined right here in 'hscutil.c'... 42 *********************************************************************/ 43 44 #if defined(_MSVC_) 45 46 /* The w32util.c module will provide the below functionality... */ 47 48 #else 49 50 /* THIS module (hscutil.c) is to provide the below functionality.. */ 51 52 /* 53 Returns outpath as a host filesystem compatible filename path. 54 This is a Cygwin-to-MSVC transitional period helper function. 55 On non-Windows platforms it simply copies inpath to outpath. 56 On Windows it converts inpath of the form "/cygdrive/x/foo.bar" 57 to outpath in the form "x:/foo.bar" for Windows compatibility. 58 */ 59 char *hostpath( char *outpath, const char *inpath, size_t buffsize ); 60 61 /* Poor man's "fcntl( fd, F_GETFL )"... */ 62 /* (only returns access-mode flags and not any others) */ 63 int get_file_accmode_flags( int fd ); 64 65 /* Initialize/Deinitialize sockets package... */ 66 int socket_init( void ); 67 int socket_deinit( void ); 68 69 /* Set socket to blocking or non-blocking mode... */ 70 int socket_set_blocking_mode( int sfd, int blocking_mode ); 71 72 /* Determine whether a file descriptor is a socket or not... */ 73 /* (returns 1==true if it's a socket, 0==false otherwise) */ 74 int socket_is_socket( int sfd ); 75 76 /* Set the SO_KEEPALIVE option and timeout values for a 77 socket connection to detect when client disconnects */ 78 void socket_keepalive( int sfd, int idle_time, int probe_interval, 79 int probe_count ); 80 81 #endif // !defined(_MSVC_) 82 83 /*********************************************************************/ 84 85 #if !defined(_MSVC_) && !defined(HAVE_STRERROR_R) 86 HUT_DLL_IMPORT void strerror_r_init(void); 87 HUT_DLL_IMPORT int strerror_r(int, char *, size_t); 88 #endif 89 90 #if defined(OPTION_CONFIG_SYMBOLS) 91 HUT_DLL_IMPORT void set_symbol(const char *,const char *); 92 HUT_DLL_IMPORT const char *get_symbol(const char *); 93 HUT_DLL_IMPORT char *resolve_symbol_string(const char *); 94 HUT_DLL_IMPORT void kill_all_symbols(void); 95 HUT_DLL_IMPORT void list_all_symbols(void); 96 #endif 97 98 #ifdef _MSVC_ 99 100 #ifndef HAVE_ID_T 101 #define HAVE_ID_T 102 typedef unsigned long id_t; 103 #endif 104 105 HU2_DLL_IMPORT int getpriority(int, id_t); 106 HU2_DLL_IMPORT int setpriority(int, id_t, int); 107 108 #ifndef HAVE_SYS_RESOURCE_H 109 #define PRIO_PROCESS 0 110 #define PRIO_PGRP 1 111 #define PRIO_USER 2 112 #endif 113 114 // Win32 'winbase.h' updates 115 116 // REALTIME_PRIORITY_CLASS 256 // -20 117 // HIGH_PRIORITY_CLASS 128 // -15 118 119 #ifndef ABOVE_NORMAL_PRIORITY_CLASS 120 #define ABOVE_NORMAL_PRIORITY_CLASS 32768 // -8 121 #endif 122 123 // NORMAL_PRIORITY_CLASS 32 // 0 124 125 #ifndef BELOW_NORMAL_PRIORITY_CLASS 126 #define BELOW_NORMAL_PRIORITY_CLASS 16384 // 8 127 #endif 128 // IDLE_PRIORITY_CLASS 64 // 15 129 130 #endif // _MSVC_ 131 132 #if !defined(HAVE_STRLCPY) 133 /* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ 134 /* 135 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 136 * 137 * Permission to use, copy, modify, and distribute this software for any 138 * purpose with or without fee is hereby granted, provided that the above 139 * copyright notice and this permission notice appear in all copies. 140 */ 141 /* 142 * Copy src to string dst of size siz. At most siz-1 characters 143 * will be copied. Always NUL terminates (unless siz == 0). 144 * Returns strlen(src); if retval >= siz, truncation occurred. 145 */ 146 /* ** NOTE ** returns 'size_t' and NOT 'char*' like strncpy! */ 147 HUT_DLL_IMPORT size_t 148 strlcpy(char *dst, const char *src, size_t siz); 149 #endif 150 151 #if !defined(HAVE_STRLCAT) 152 /* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ 153 /* 154 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 155 * 156 * Permission to use, copy, modify, and distribute this software for any 157 * purpose with or without fee is hereby granted, provided that the above 158 * copyright notice and this permission notice appear in all copies. 159 */ 160 /* 161 * Appends src to string dst of size siz (unlike strncat, siz is the 162 * full size of dst, not space left). At most siz-1 characters 163 * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 164 * Returns strlen(src) + MIN(siz, strlen(initial dst)). 165 * If retval >= siz, truncation occurred. 166 */ 167 /* ** NOTE ** returns 'size_t' and NOT 'char*' like strncat! */ 168 HUT_DLL_IMPORT size_t 169 strlcat(char *dst, const char *src, size_t siz); 170 #endif 171 172 /* Subtract/add gettimeofday struct timeval */ 173 HUT_DLL_IMPORT int timeval_subtract (struct timeval *beg_timeval, struct timeval *end_timeval, struct timeval *dif_timeval); 174 HUT_DLL_IMPORT int timeval_add (struct timeval *dif_timeval, struct timeval *accum_timeval); 175 176 /* 177 Easier to use timed_wait_condition that waits for 178 the specified relative amount of time without you 179 having to build an absolute timeout time yourself 180 */ 181 HUT_DLL_IMPORT int timed_wait_condition_relative_usecs 182 ( 183 COND* pCOND, // ptr to condition to wait on 184 LOCK* pLOCK, // ptr to controlling lock (must be held!) 185 U32 usecs, // max #of microseconds to wait 186 struct timeval* pTV // [OPTIONAL] ptr to tod value (may be NULL) 187 ); 188 189 // TEST 190 HUT_DLL_IMPORT void cause_crash(); 191 192 /* Read/write to socket functions */ 193 HUT_DLL_IMPORT int hprintf(int s,char *fmt,...); 194 HUT_DLL_IMPORT int hwrite(int s,const char *,size_t); 195 HUT_DLL_IMPORT int hgetc(int s); 196 HUT_DLL_IMPORT char *hgets(char *b,size_t c,int s); 197 198 #if !defined(_MSVC_) 199 /* Hercules file open */ 200 HUT_DLL_IMPORT int hopen(const char* path, int oflag, ...); 201 #endif // !defined(_MSVC_) 202 203 /* Posix 1003.e capabilities */ 204 #if defined(OPTION_CAPABILITIES) 205 HUT_DLL_IMPORT int drop_privileges(int c); 206 HUT_DLL_IMPORT int drop_all_caps(void); 207 #endif 208 209 #endif /* __HSCUTL_H__ */ 210