1 /* $Id$ 2 * 3 * HUSKYLIB: common defines, types and functions for HUSKY 4 * 5 * This is part of The HUSKY Fidonet Software project: 6 * see http://husky.sourceforge.net for details 7 * 8 * 9 * HUSKYLIB is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; either 12 * version 2 of the License, or (at your option) any later version. 13 * 14 * HUSKYLIB is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; see file COPYING. If not, write to the 21 * Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 * 23 * See also http://www.gnu.org, license may be found here. 24 */ 25 26 #ifndef HUSKY_HUSKYLIB_H__ 27 #define HUSKY_HUSKYLIB_H__ 28 29 /* standard headers */ 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <time.h> 33 #include <stddef.h> 34 35 /* huskylib headers */ 36 #include "compiler.h" 37 #include "huskyext.h" 38 39 #ifdef __MSVC__ 40 #include <crtdbg.h> 41 #endif 42 #include "calendar.h" 43 #include "crc.h" 44 #include "cvtdate.h" 45 /*#include "dirlayer.h" Don't include this file to prevent conflicts, please!!! */ 46 #include "fexist.h" 47 #include "ffind.h" 48 #include "ftnaddr.h" 49 #include "locking.h" 50 #include "log.h" 51 #include "memory.h" 52 #include "recode.h" 53 #include "strext.h" 54 #include "temp.h" 55 #include "tree.h" 56 #include "unused.h" 57 #include "xstr.h" 58 59 #ifndef HUSKYLIB_VER_MAJOR 60 #define HUSKYLIB_VER_MAJOR 1 61 #endif 62 #ifndef HUSKYLIB_VER_MINOR 63 #define HUSKYLIB_VER_MINOR 9 64 #endif 65 #ifndef HUSKYLIB_VER_PATCH 66 #define HUSKYLIB_VER_PATCH 0 67 #endif 68 #ifndef HUSKYLIB_VER_BRANCH 69 #define HUSKYLIB_VER_BRANCH BRANCH_CURRENT 70 #endif 71 72 /* values for 5th parameter of GenVersionStr() */ 73 typedef enum { 74 BRANCH_CURRENT=1, BRANCH_STABLE=2, BRANCH_RELEASE=3 75 }branch_t; 76 77 /*-- flush.c --*/ 78 79 /* fflush() analog with file hanle as argument */ 80 #ifdef __DOS__ 81 /* flushasm.asm for DOS, redefined for known implementations in flush.c */ 82 HUSKYEXT void pascal far flush_handle2(int fh); 83 #endif 84 85 /* Compiler-independent fflush() implementation */ 86 HUSKYEXT void _fast flush_handle(FILE * fp); 87 88 89 /*-- adcase.c -------------------------------------------------------------*/ 90 91 /* ATTENTION: The adaptcase routine builds an internal cache which never 92 * expires. If you ever add files to or remove files to a subdirectory and 93 * later want to have adaptcase map this particular file name properly, 94 * you must call adaptcase_refresh_dir() with the subdirectory path name 95 * as argument! 96 */ 97 98 HUSKYEXT void adaptcase_refresh_dir(const char *directory); 99 HUSKYEXT void adaptcase(char *); 100 101 102 /*-- tdelay.c -------------------------------------------------------------*/ 103 /* Compiler-independent sleep() implementation with precisious to milliseconds */ 104 HUSKYEXT void _fast tdelay(int); 105 106 typedef struct { 107 unsigned long sec; /* seconds */ 108 unsigned long msec; /* milliseconds */ 109 } hs_time; 110 111 /* Get snapshot of current time with precision up to milliseconds */ 112 HUSKYEXT void husky_SetTimer(hs_time *timer_ctx); 113 114 /* Return difference between now and previous time snapshot */ 115 HUSKYEXT dword husky_GetTimer(hs_time *timer_ctx); 116 117 118 /*-- genmsgid.c --*/ 119 HUSKYEXT dword _XPENTRY GenMsgId(char *seqdir, unsigned long max_outrun); 120 HUSKYEXT dword _XPENTRY GenMsgIdEx(char *seqdir, unsigned long max_outrun, 121 dword (*altGenMsgId)(void), char **errstr); 122 123 /*-- setfsize.c --*/ 124 HUSKYEXT int _fast setfsize(int fd, long size); 125 126 /*-- mapfile.c --*/ 127 /* Mapping file to memory implementation for several OS. */ 128 HUSKYEXT void* MapFile(char* fname); 129 130 /* getfree.c */ 131 /* Check free disk space */ 132 /* named husky_GetDiskFreeSpace() to avoid conflict with windows's GetDiskFreeSpace() */ 133 HUSKYEXT unsigned long husky_GetDiskFreeSpace(const char *path); 134 #define fc_GetDiskFreeSpace(p) husky_GetDiskFreeSpace(p) /*for compatibility with old code; to be removed */ 135 136 /* ioutil.c */ 137 HUSKYEXT word getUINT16(FILE *in); 138 /*DOC 139 Input: in is an file stream opened for reading. 140 Output: getUINT16 returns an word 141 FZ: the UINT15 is read from the stream using the method lowByte, highByte. 142 */ 143 144 HUSKYEXT int fputUINT16(FILE *out, word word); 145 /*DOC 146 Input: out is an file opened for writing. 147 word is the word which should be written 148 Output: fputUIN16 returns the return of the second fputc call. 149 FZ: fputUINT16 writes word into the stream using the order lowByte, highByte. 150 */ 151 152 HUSKYEXT signed int fgetsUntil0(UCHAR *str, size_t n, FILE *f, char *filter); 153 /*DOC 154 Input: n-1 chars are read at most. 155 str is a buffer with the length n. 156 f is a file stream opened for reading. 157 filter is a string with characters that shoudn't be reading 158 Output: fgetsUntil0 returns the number of chars read including the last \0 159 FZ: fgetsUntil0 reads chars into the buffer until eof(f) || n-1 are read || a \0 is encountered. 160 */ 161 162 HUSKYEXT char *shell_expand(char *str); 163 /*DOC 164 Input: str is a \0 terminated string which must have been malloc'ed 165 Ouput: a pointer to a \0 terminated string is returned which must be free'd 166 FZ: shell_expand expands the strings just like ~/.msged to /home/mtt/.msged 167 see sh(1) for further explanations 168 Note: *str re-allocated if need 169 */ 170 171 /* will be moved to huskylib */ 172 HUSKYEXT int move_file(const char *from, const char *to, const int force_rewrite); 173 /* DOC 174 Input: source and destination filename 175 Output: 0 if OK, != 0 and errno set on error 176 FZ: Move a file, works even over file system boundaries, 177 replace file if the destination file already exists and force_rewrite !=0 178 */ 179 180 /* will be moved to huskylib */ 181 HUSKYEXT int copy_file(const char *from, const char *to, const int force_rewrite); 182 /* DOC 183 * copy file into other location 184 * rewrite existing file if third parameter not zero 185 * return 0 if success; else return -1 and set errno 186 */ 187 188 /* cmdcall() 189 * Call external command (using spawnvp() if possible to prevent drop command 190 * exit code by buggy shell, e.g. command.com) 191 * Return exit code of the executed command. 192 */ 193 #if defined(HAS_spawnvp) && ( defined(__DOS__) || defined(__WIN32__) ) 194 HUSKYEXT int cmdcall(const char *cmd); 195 #else 196 # define cmdcall(cmd) system(cmd) 197 #endif 198 199 /* Converts decimal value to octal [useful for chmod()] */ 200 HUSKYEXT unsigned int dec2oct(unsigned int decimal); 201 202 /* try to create and lock lockfile */ 203 /* returns -1 if fail */ 204 /* returns filedescriptor of lockfile if success */ 205 HUSKYEXT int lockFile(const char *lockfile, int advisoryLock); 206 207 #ifndef createLockFile 208 #define createLockFile(x) lockFile(x, 0) 209 #endif 210 211 /* close and remove lockfile */ 212 HUSKYEXT int FreelockFile(const char *lockfile, int fh); 213 214 /* will be moved to huskylib */ 215 HUSKYEXT char *GetFilenameFromPathname(const char* pathname); 216 /* Get the object name from the end of a full or partial pathname. 217 The GetFilenameFromPathname function gets the file (or directory) name 218 from the end of a full or partial pathname. Returns The file (or directory) 219 name 220 */ 221 222 /* will be moved to huskylib */ 223 #define basename(f) GetFilenameFromPathname(f) 224 225 /* will be moved to huskylib */ 226 /* Get the object name from the end of a full or partial pathname (OS-independed). 227 This function gets the file (or directory) name from the end of a full 228 or partial pathname for any path style: UNIX, DOS or mixed (mixed style 229 may be used in Windows NT OS family). 230 Returns the file (or directory) name: pointer to part of all original pathname. 231 */ 232 HUSKYEXT char *OS_independed_basename(const char *pathname); 233 234 /* will be moved to huskylib */ 235 /* Return directory part of pathname (without filename, '/' or '\\' present at end) 236 * Return value is pointer to malloc'ed string; 237 * if pathname is filenfme without directory return current directory (./ or .\) 238 */ 239 HUSKYEXT char *GetDirnameFromPathname(const char* pathname); 240 241 #define dirname(f) GetDirnameFromPathname(f) 242 243 /* will be moved to huskylib */ 244 HUSKYEXT void fillCmdStatement(char *cmd, const char *call, const char *archive, const char *file, const char *path); 245 246 /*-- patmat.c --*/ 247 /*HUSKYEXT int patmat( const char *raw, const char *pat ); 248 HUSKYEXT int patimat(char *raw, char *pat );*/ 249 HUSKYEXT int xpatmat(const char *raw, const char *pat, const int ncase); 250 #define patmat(raw, pat) xpatmat(raw, pat, 0) 251 #define patimat(raw, pat) xpatmat(raw, pat, 1) 252 253 /*-- qksort.c --*/ 254 /* quick sort integer array */ 255 HUSKYEXT void _fast qksort(int a[], size_t n); 256 257 /*-- cmpfname.c --*/ 258 /* compare file names on all known file systems */ 259 HUSKYEXT int cmpfnames(const char *file1, const char *file2); 260 261 262 /*-- genverst.c --*/ 263 264 /* Generate version string like 265 * programname/platform[-compiler] <major>.<minor>.<patchlevel>-<branch> [<cvs date>] 266 * 267 * Return malloc'ed pointer 268 * 269 * Examples: 270 * "program/w32-MVC 1.2.3-release" 271 * "program/DPMI-DJGPP 1.2.3-stable 01-10-2002" 272 * "program/FreeBSD 1.3.0-current 01-10-2002" 273 * 274 * Require cvs_date.h in module hearer files directory 275 */ 276 277 HUSKYEXT char *GenVersionStr( const char *programname, unsigned major, 278 unsigned minor, unsigned patchlevel, unsigned branch, const char *cvsdate ); 279 280 281 /*-- version.c --*/ 282 283 /* Check version of huskylib library 284 * return zero if test failed; non-zero if passed 285 * test cvs need for DLL version only, using #include <fidoconf/cvsdate.h> 286 */ 287 HUSKYEXT int CheckHuskylibVersion( int need_major, int need_minor, 288 int need_patch, branch_t need_branch, const char *cvs ); 289 290 #endif /*__HUSKYLIB_H__ */ 291