2 * $Id: stdfn.h,v 1.32 2006/05/13 09:22:17 mikulik Exp $
5 /* GNUPLOT - stdfn.h */
8 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
10 * Permission to use, copy, and distribute this software and its
11 * documentation for any purpose with or without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and
13 * that both that copyright notice and this permission notice appear
14 * in supporting documentation.
16 * Permission to modify the software is granted, but not the right to
17 * distribute the complete modified source code. Modifications are to
18 * be distributed as patches to the released version. Permission to
19 * distribute binaries produced by compiling modified sources is granted,
21 * 1. distribute the corresponding source modifications from the
22 * released version in the form of a patch file along with the binaries,
23 * 2. add special version identification to distinguish your version
24 * in addition to the base release version number,
25 * 3. provide your name and address as the primary contact for the
26 * support of your modified version, and
27 * 4. retain our contact information in regard to use of the base
29 * Permission to distribute the released version of the source code along
30 * with corresponding source modifications in the form of a patch file is
31 * granted with same provisions 2 through 4 for binary distributions.
33 * This software is provided "as is" without express or implied warranty
34 * to the extent permitted by applicable law.
37 /* get prototypes or declarations for string and stdlib functions and deal
38 with missing functions like strchr. */
40 /* we will assume the ANSI/Posix/whatever situation as default.
41 the header file is called string.h and the index functions are called
42 strchr, strrchr. Exceptions have to be listed explicitly */
60 # define memcpy(d,s,n) bcopy((s),(d),(n))
63 # define memmove(d,s,n) bcopy((s),(d),(n))
67 char * memcpy __PROTO((char *, char *, size_t));
69 #endif /* HAVE_BCOPY */
78 char *strchr __PROTO((const char *, int));
84 # define strrchr rindex
88 size_t gp_strcspn __PROTO((const char *, const char *));
89 # define strcspn gp_strcspn
93 char *strstr __PROTO((const char *, const char *));
103 # endif /* HAVE_MALLOC_H */
110 #else /* HAVE_STDLIB_H */
113 # ifndef EXIT_FAILURE
114 # define EXIT_FAILURE (1)
116 # ifndef EXIT_SUCCESS
117 # define EXIT_SUCCESS (0)
120 # ifdef VAXC /* replacement values suppress some messages */
128 # ifndef EXIT_FAILURE
129 # define EXIT_FAILURE 0x10000002
131 # ifndef EXIT_SUCCESS
132 # define EXIT_SUCCESS 1
135 #endif /* HAVE_STDLIB_H */
137 /* Deal with varargs functions */
138 #if defined(HAVE_VFPRINTF) || defined(HAVE_DOPRNT)
141 # define VA_START(args, lastarg) va_start(args, lastarg)
143 # include <varargs.h>
144 # define VA_START(args, lastarg) va_start(args)
145 # endif /* !STDC_HEADERS */
146 #else /* HAVE_VFPRINTF || HAVE_DOPRNT */
147 # define va_dcl char *a1, char *a2, char *a3, char *a4, char *a5, char *a6, char *a7, char *a8
148 #endif /* !(HAVE_VFPRINTF || HAVE_DOPRNT) */
153 # ifdef HAVE_LIBC_H /* NeXT uses libc instead of unistd */
156 #endif /* HAVE_UNISTD_H */
164 #ifndef HAVE_STRERROR
165 char *strerror __PROTO((int));
167 extern char *sys_errlist[];
170 #ifdef HAVE_SYS_TYPES_H
171 # include <sys/types.h>
174 #ifdef HAVE_SYS_STAT_H
175 # include <sys/stat.h>
177 /* This is all taken from GNU fileutils lib/filemode.h */
181 # define S_IRUSR S_IREAD
183 # define S_IRUSR 00400
189 # define S_IWUSR S_IWRITE
191 # define S_IWUSR 00200
197 # define S_IXUSR S_IEXEC
199 # define S_IXUSR 00100
203 #ifdef STAT_MACROS_BROKEN
214 #endif /* STAT_MACROS_BROKEN. */
216 #if !defined(S_ISBLK) && defined(S_IFBLK)
217 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
219 #if !defined(S_ISCHR) && defined(S_IFCHR)
220 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
222 #if !defined(S_ISDIR) && defined(S_IFDIR)
223 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
225 #if !defined(S_ISREG) && defined(S_IFREG)
226 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
228 #if !defined(S_ISFIFO) && defined(S_IFIFO)
229 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
231 #if !defined(S_ISLNK) && defined(S_IFLNK)
232 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
234 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
235 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
237 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
238 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
239 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
241 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
242 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
245 #endif /* HAVE_SYS_STAT_H */
250 # ifdef HAVE_VALUES_H
252 # endif /* HAVE_VALUES_H */
253 #endif /* HAVE_LIMITS_H */
255 /* ctime etc, should also define time_t and struct tm */
260 #ifndef HAVE_TIME_T_IN_TIME_H
264 #if defined(PIPES) && (defined(VMS) || (defined(OSK) && defined(_ANSI_EXT))) || defined(PIPES) && defined(AMIGA_SC_6_1)
265 FILE *popen __PROTO((char *, char *));
266 int pclose __PROTO((FILE *));
273 /* Some older platforms, namely SunOS 4.x, don't define this. */
275 # define DBL_EPSILON 2.2204460492503131E-16
286 /* Normally in <math.h> */
288 # define M_PI 3.14159265358979323846
291 # define M_PI_2 1.57079632679489661923
294 # define M_LN10 2.3025850929940456840e0
297 /* Use definitions from float.h and math.h if we found them */
298 #if defined(DBL_MIN_10_EXP)
299 # define E_MINEXP (DBL_MIN_10_EXP * M_LN10)
301 #if defined(DBL_MAX_10_EXP)
302 # define E_MAXEXP (DBL_MAX_10_EXP * M_LN10)
305 #ifndef HAVE_STRCASECMP
307 # define strcasecmp stricmp
309 int gp_stricmp __PROTO((const char *, const char *));
310 # define strcasecmp gp_stricmp
314 #ifndef HAVE_STRNCASECMP
315 # ifdef HAVE_STRNICMP
316 # define strncasecmp strnicmp
318 int gp_strnicmp __PROTO((const char *, const char *, size_t));
319 # define strncasecmp gp_strnicmp
324 # if defined(HAVE_GETCWD)
325 # if defined(__EMX__)
326 # define GP_GETCWD(path,len) _getcwd2 (path, len)
328 # define GP_GETCWD(path,len) getcwd (path, len)
329 # endif /* __EMX__ */
331 # define GP_GETCWD(path,len) getwd (path)
336 # include <windows.h>
339 /* sleep delay time, where delay is a double value */
340 #if defined(HAVE_USLEEP)
341 # define GP_SLEEP(delay) usleep((unsigned int) ((delay)*1e6))
345 #elif defined(__EMX__)
346 # define GP_SLEEP(delay) _sleep2((unsigned int) ((delay)*1e3))
351 # define GP_SLEEP(delay) win_sleep((DWORD) 1000*delay)
359 # define GP_SLEEP(delay) usleep ((unsigned long) (delay+0.5))
361 # define GP_SLEEP(delay) sleep ((unsigned int) (delay+0.5))
366 # define GP_ATEXIT(x) atexit((x))
367 #elif defined(HAVE_ON_EXIT)
368 # define GP_ATEXIT(x) on_exit((x),0)
370 # define GP_ATEXIT(x) /* you lose */
378 /* Definitions for debugging */
383 # define DEBUG_WHERE do { fprintf(stderr,"%s:%d ",__FILE__,__LINE__); } while (0)
384 # define FPRINTF(a) do { DEBUG_WHERE; fprintf a; } while (0)
386 # define DEBUG_WHERE /* nought */
387 # define FPRINTF(a) /* nought */
392 #define INT_STR_LEN (3*sizeof(int))
395 /* HBB 20010223: moved this whole block from syscfg.h to here. It
396 * needs both "syscfg.h" and <float.h> to have been #include'd before
397 * this, since it relies on stuff like DBL_MAX */
399 /* There is a bug in the NEXT OS. This is a workaround. Lookout for
400 * an OS correction to cancel the following dinosaur
402 * Hm, at least with my setup (compiler version 3.1, system 3.3p1),
403 * DBL_MAX is defined correctly and HUGE and HUGE_VAL are both defined
404 * as 1e999. I have no idea to which OS version the bugfix below
405 * applies, at least wrt. HUGE, it is inconsistent with the current
406 * version. Since we are using DBL_MAX anyway, most of this isn't
407 * really needed anymore.
410 #if defined ( NEXT ) && NX_CURRENT_COMPILER_RELEASE<310
411 # if defined ( DBL_MAX)
414 # define DBL_MAX 1.7976931348623157e+308
416 # define HUGE DBL_MAX
418 # define HUGE_VAL DBL_MAX
419 #endif /* NEXT && NX_CURRENT_COMPILER_RELEASE<310 */
422 * Note about VERYLARGE: This is the upper bound double (or float, if PC)
423 * numbers. This flag indicates very large numbers. It doesn't have to
424 * be the absolutely biggest number on the machine.
425 * If your machine doesn't have HUGE, or float.h,
426 * define VERYLARGE here.
429 #define VERYLARGE 1e37
431 * To get an appropriate value for VERYLARGE, we can use DBL_MAX (or
432 * FLT_MAX on PCs), HUGE or HUGE_VAL. DBL_MAX is usually defined in
433 * float.h and is the largest possible double value. HUGE and HUGE_VAL
434 * are either DBL_MAX or +Inf (IEEE special number), depending on the
435 * compiler. +Inf may cause problems with some buggy fp
436 * implementations, so we better avoid that. The following should work
437 * better than the previous setup (which used HUGE in preference to
440 /* Now define VERYLARGE. This is usually DBL_MAX/2 - 1. On MS-DOS however
441 * we use floats for memory considerations and thus use FLT_MAX.
444 #ifndef COORDVAL_FLOAT
446 # define VERYLARGE (DBL_MAX/2-1)
448 #else /* COORDVAL_FLOAT */
450 # define VERYLARGE (FLT_MAX/2-1)
452 #endif /* COORDVAL_FLOAT */
457 # define VERYLARGE (HUGE/2-1)
458 # elif defined(HUGE_VAL)
459 # define VERYLARGE (HUGE_VAL/2-1)
461 /* as a last resort */
462 # define VERYLARGE (1e37)
463 /* # warning "using last resort 1e37 as VERYLARGE define, please check your headers" */
464 /* Maybe add a note somewhere in the install docs instead */
466 #endif /* VERYLARGE */
468 /* _POSIX_PATH_MAX is too small for practical purposes */
469 #ifdef HAVE_SYS_PARAM_H
470 # include <sys/param.h>
474 # define PATH_MAX 1024
476 # define PATH_MAX MAXPATHLEN
480 /* Concatenate a path name and a file name. The file name
481 * may or may not end with a "directory separation" character.
482 * Path must not be NULL, but can be empty
484 #define PATH_CONCAT(path,file) \
488 if (*p && (*p != DIRSEP1) && (*p != DIRSEP2)) { \
489 if (*p) p++; *p++ = DIRSEP1; *p = NUL; \
491 strcat (path, file); \
495 # define inrange(z,min,max) \
496 (((min)<(max)) ? (((z)>=(min)) && ((z)<=(max))) : \
497 (((z)>=(max)) && ((z)<=(min))))
500 /* HBB 20030117: new macro to simplify clipping operations in the
501 * presence of possibly reverted axes */
503 # define cliptorange(z,min,max) \
505 if ((min) < (max)) { \
508 else if ((z) < (min)) \
513 else if ((z) < (max)) \
519 /* both min/max and MIN/MAX are defined by some compilers.
520 * we are now on GPMIN / GPMAX
522 #define GPMAX(a,b) ( (a) > (b) ? (a) : (b) )
523 #define GPMIN(a,b) ( (a) < (b) ? (a) : (b) )
525 /* Prototypes from "stdfn.c" */
527 char *safe_strncpy __PROTO((char *, const char *, size_t));
529 unsigned int sleep __PROTO((unsigned int));
532 double gp_strtod __PROTO((const char *str, char **endptr));