Icons are changed
[gnuplot] / src / stdfn.h
1 /*
2  * $Id: stdfn.h,v 1.32 2006/05/13 09:22:17 mikulik Exp $
3  */
4
5 /* GNUPLOT - stdfn.h */
6
7 /*[
8  * Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
9  *
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.
15  *
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,
20  * provided you
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
28  *    software.
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.
32  *
33  * This software is provided "as is" without express or implied warranty
34  * to the extent permitted by applicable law.
35 ]*/
36
37 /* get prototypes or declarations for string and stdlib functions and deal
38    with missing functions like strchr. */
39
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 */
43
44 #ifndef STDFN_H
45 #define STDFN_H
46
47 #include <ctype.h>
48 #include <stdio.h>
49
50 #include "syscfg.h"
51
52 #ifdef HAVE_STRING_H
53 # include <string.h>
54 #else
55 # include <strings.h>
56 #endif
57
58 #ifdef HAVE_BCOPY
59 # ifndef HAVE_MEMCPY
60 #  define memcpy(d,s,n) bcopy((s),(d),(n))
61 # endif
62 # ifndef HAVE_MEMMOVE
63 #  define memmove(d,s,n) bcopy((s),(d),(n))
64 # endif
65 #else
66 # ifndef HAVE_MEMCPY
67 char * memcpy __PROTO((char *, char *, size_t));
68 # endif
69 #endif /* HAVE_BCOPY */
70
71 #ifndef HAVE_STRCHR
72 # ifdef strchr
73 #  undef strchr
74 # endif
75 # ifdef HAVE_INDEX
76 #  define strchr index
77 # else
78 char *strchr __PROTO((const char *, int));
79 # endif
80 # ifdef strrchr
81 #  undef strrchr
82 # endif
83 # ifdef HAVE_RINDEX
84 #  define strrchr rindex
85 # endif
86 #endif
87 #ifndef HAVE_STRCSPN
88 size_t gp_strcspn __PROTO((const char *, const char *));
89 # define strcspn gp_strcspn
90 #endif
91
92 #ifndef HAVE_STRSTR
93 char *strstr __PROTO((const char *, const char *));
94 #endif
95
96 #ifndef HAVE_STDLIB_H
97 # ifdef HAVE_MALLOC_H
98 #  include <malloc.h>
99 # else
100 void free();
101 char *malloc();
102 char *realloc();
103 # endif /* HAVE_MALLOC_H */
104 char *getenv();
105 int system();
106 double atof();
107 int atoi();
108 long atol();
109 double strtod();
110 #else /* HAVE_STDLIB_H */
111 # include <stdlib.h>
112 # ifndef VMS
113 #  ifndef EXIT_FAILURE
114 #   define EXIT_FAILURE (1)
115 #  endif
116 #  ifndef EXIT_SUCCESS
117 #   define EXIT_SUCCESS (0)
118 #  endif
119 # else /* VMS */
120 #  ifdef VAXC            /* replacement values suppress some messages */
121 #   ifdef  EXIT_FAILURE
122 #    undef EXIT_FAILURE
123 #   endif
124 #   ifdef  EXIT_SUCCESS
125 #    undef EXIT_SUCCESS
126 #   endif
127 #  endif /* VAXC */
128 #  ifndef  EXIT_FAILURE
129 #   define EXIT_FAILURE  0x10000002
130 #  endif
131 #  ifndef  EXIT_SUCCESS
132 #   define EXIT_SUCCESS  1
133 #  endif
134 # endif /* VMS */
135 #endif /* HAVE_STDLIB_H */
136
137 /* Deal with varargs functions */
138 #if defined(HAVE_VFPRINTF) || defined(HAVE_DOPRNT)
139 # ifdef STDC_HEADERS
140 #  include <stdarg.h>
141 #  define VA_START(args, lastarg) va_start(args, lastarg)
142 # else
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) */
149
150 #ifdef HAVE_UNISTD_H
151 # include <unistd.h>
152 #else
153 # ifdef HAVE_LIBC_H /* NeXT uses libc instead of unistd */
154 #  include <libc.h>
155 # endif
156 #endif /* HAVE_UNISTD_H */
157
158 #ifdef HAVE_ERRNO_H
159 # include <errno.h>
160 #endif
161 # ifdef EXTERN_ERRNO
162 extern int errno;
163 #endif
164 #ifndef HAVE_STRERROR
165 char *strerror __PROTO((int));
166 extern int sys_nerr;
167 extern char *sys_errlist[];
168 #endif
169
170 #ifdef HAVE_SYS_TYPES_H
171 # include <sys/types.h>
172 #endif
173
174 #ifdef HAVE_SYS_STAT_H
175 # include <sys/stat.h>
176
177 /* This is all taken from GNU fileutils lib/filemode.h */
178
179 #if !S_IRUSR
180 # if S_IREAD
181 #  define S_IRUSR S_IREAD
182 # else
183 #  define S_IRUSR 00400
184 # endif
185 #endif
186
187 #if !S_IWUSR
188 # if S_IWRITE
189 #  define S_IWUSR S_IWRITE
190 # else
191 #  define S_IWUSR 00200
192 # endif
193 #endif
194
195 #if !S_IXUSR
196 # if S_IEXEC
197 #  define S_IXUSR S_IEXEC
198 # else
199 #  define S_IXUSR 00100
200 # endif
201 #endif
202
203 #ifdef STAT_MACROS_BROKEN
204 # undef S_ISBLK
205 # undef S_ISCHR
206 # undef S_ISDIR
207 # undef S_ISFIFO
208 # undef S_ISLNK
209 # undef S_ISMPB
210 # undef S_ISMPC
211 # undef S_ISNWK
212 # undef S_ISREG
213 # undef S_ISSOCK
214 #endif /* STAT_MACROS_BROKEN.  */
215
216 #if !defined(S_ISBLK) && defined(S_IFBLK)
217 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
218 #endif
219 #if !defined(S_ISCHR) && defined(S_IFCHR)
220 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
221 #endif
222 #if !defined(S_ISDIR) && defined(S_IFDIR)
223 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
224 #endif
225 #if !defined(S_ISREG) && defined(S_IFREG)
226 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
227 #endif
228 #if !defined(S_ISFIFO) && defined(S_IFIFO)
229 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
230 #endif
231 #if !defined(S_ISLNK) && defined(S_IFLNK)
232 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
233 #endif
234 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
235 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
236 #endif
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)
240 #endif
241 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
242 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
243 #endif
244
245 #endif /* HAVE_SYS_STAT_H */
246
247 #ifdef HAVE_LIMITS_H
248 # include <limits.h>
249 #else
250 # ifdef HAVE_VALUES_H
251 #  include <values.h>
252 # endif /* HAVE_VALUES_H */
253 #endif /* HAVE_LIMITS_H */
254
255 /* ctime etc, should also define time_t and struct tm */
256 #ifdef HAVE_TIME_H
257 # include <time.h>
258 #endif
259
260 #ifndef HAVE_TIME_T_IN_TIME_H
261 # define time_t long
262 #endif
263
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 *));
267 #endif
268
269 #ifdef HAVE_FLOAT_H
270 # include <float.h>
271 #endif
272
273 /* Some older platforms, namely SunOS 4.x, don't define this. */
274 #ifndef DBL_EPSILON
275 # define DBL_EPSILON     2.2204460492503131E-16
276 #endif
277
278 #ifdef HAVE_LOCALE_H
279 # include <locale.h>
280 #endif
281
282 #ifdef HAVE_MATH_H
283 # include <math.h>
284 #endif
285
286 /* Normally in <math.h> */
287 #ifndef M_PI
288 # define M_PI 3.14159265358979323846
289 #endif
290 #ifndef M_PI_2
291 # define M_PI_2 1.57079632679489661923
292 #endif
293 #ifndef M_LN10
294 #  define M_LN10    2.3025850929940456840e0
295 #endif
296
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)
300 #endif
301 #if defined(DBL_MAX_10_EXP)
302 # define E_MAXEXP (DBL_MAX_10_EXP * M_LN10)
303 #endif
304
305 #ifndef HAVE_STRCASECMP
306 # ifdef HAVE_STRICMP
307 #  define strcasecmp stricmp
308 # else
309 int gp_stricmp __PROTO((const char *, const char *));
310 #  define strcasecmp gp_stricmp
311 # endif
312 #endif
313
314 #ifndef HAVE_STRNCASECMP
315 # ifdef HAVE_STRNICMP
316 #  define strncasecmp strnicmp
317 # else
318 int gp_strnicmp __PROTO((const char *, const char *, size_t));
319 #  define strncasecmp gp_strnicmp
320 # endif
321 #endif
322
323 #ifndef GP_GETCWD
324 # if defined(HAVE_GETCWD)
325 #   if defined(__EMX__)
326 #     define GP_GETCWD(path,len) _getcwd2 (path, len)
327 #   else
328 #     define GP_GETCWD(path,len) getcwd (path, len)
329 #   endif /* __EMX__ */
330 # else
331 #  define GP_GETCWD(path,len) getwd (path)
332 # endif
333 #endif
334
335 #ifdef WIN32
336 # include <windows.h>
337 #endif
338
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))
342 #  ifndef HAVE_SLEEP
343 #    define HAVE_SLEEP
344 #  endif
345 #elif defined(__EMX__)
346 #  define GP_SLEEP(delay) _sleep2((unsigned int) ((delay)*1e3))
347 #  ifndef HAVE_SLEEP
348 #    define HAVE_SLEEP
349 #  endif
350 #elif defined(WIN32)
351 #  define GP_SLEEP(delay) win_sleep((DWORD) 1000*delay)
352 #  ifndef HAVE_SLEEP
353 #    define HAVE_SLEEP
354 #  endif
355 #endif
356
357 #ifndef GP_SLEEP
358 # ifdef __ZTC__
359 #    define GP_SLEEP(delay) usleep ((unsigned long) (delay+0.5))
360 # else
361 #    define GP_SLEEP(delay) sleep ((unsigned int) (delay+0.5))
362 # endif
363 #endif
364
365 #ifdef HAVE_ATEXIT
366 # define GP_ATEXIT(x) atexit((x))
367 #elif defined(HAVE_ON_EXIT)
368 # define GP_ATEXIT(x) on_exit((x),0)
369 #else
370 # define GP_ATEXIT(x) /* you lose */
371 #endif
372
373 /* Misc. defines */
374
375 /* Null character */
376 #define NUL ('\0')
377
378 /* Definitions for debugging */
379 /* #define NDEBUG */
380 #include <assert.h>
381
382 #ifdef DEBUG
383 # define DEBUG_WHERE do { fprintf(stderr,"%s:%d ",__FILE__,__LINE__); } while (0)
384 # define FPRINTF(a) do { DEBUG_WHERE; fprintf a; } while (0)
385 #else
386 # define DEBUG_WHERE     /* nought */
387 # define FPRINTF(a)      /* nought */
388 #endif /* DEBUG */
389
390 #include "syscfg.h"
391
392 #define INT_STR_LEN (3*sizeof(int))
393
394
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 */
398
399 /* There is a bug in the NEXT OS. This is a workaround. Lookout for
400  * an OS correction to cancel the following dinosaur
401  *
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.
408  */
409
410 #if defined ( NEXT ) && NX_CURRENT_COMPILER_RELEASE<310
411 # if defined ( DBL_MAX)
412 #  undef DBL_MAX
413 # endif
414 # define DBL_MAX 1.7976931348623157e+308
415 # undef HUGE
416 # define HUGE    DBL_MAX
417 # undef HUGE_VAL
418 # define HUGE_VAL DBL_MAX
419 #endif /* NEXT && NX_CURRENT_COMPILER_RELEASE<310 */
420
421 /*
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.
427  *
428  * example:
429 #define VERYLARGE 1e37
430  *
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
438  * DBL_MAX).
439  */
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.
442  */
443
444 #ifndef COORDVAL_FLOAT
445 # ifdef DBL_MAX
446 #  define VERYLARGE (DBL_MAX/2-1)
447 # endif
448 #else /* COORDVAL_FLOAT */
449 # ifdef FLT_MAX
450 #  define VERYLARGE (FLT_MAX/2-1)
451 # endif
452 #endif /* COORDVAL_FLOAT */
453
454
455 #ifndef VERYLARGE
456 # ifdef HUGE
457 #  define VERYLARGE (HUGE/2-1)
458 # elif defined(HUGE_VAL)
459 #  define VERYLARGE (HUGE_VAL/2-1)
460 # else
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 */
465 # endif /* HUGE */
466 #endif /* VERYLARGE */
467
468 /* _POSIX_PATH_MAX is too small for practical purposes */
469 #ifdef HAVE_SYS_PARAM_H
470 # include <sys/param.h>
471 #endif
472 #ifndef PATH_MAX
473 # ifndef MAXPATHLEN
474 #  define PATH_MAX 1024
475 # else
476 #  define PATH_MAX MAXPATHLEN
477 # endif
478 #endif
479
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
483  */
484 #define PATH_CONCAT(path,file) \
485  { char *p = path; \
486    p += strlen(path); \
487    if (p!=path) p--; \
488    if (*p && (*p != DIRSEP1) && (*p != DIRSEP2)) { \
489      if (*p) p++; *p++ = DIRSEP1; *p = NUL; \
490    } \
491    strcat (path, file); \
492  }
493
494 #ifndef inrange
495 # define inrange(z,min,max) \
496    (((min)<(max)) ? (((z)>=(min)) && ((z)<=(max))) : \
497                     (((z)>=(max)) && ((z)<=(min))))
498 #endif
499
500 /* HBB 20030117: new macro to simplify clipping operations in the
501  * presence of possibly reverted axes */
502 #ifndef cliptorange
503 # define cliptorange(z,min,max)                 \
504     do {                                        \
505        if ((min) < (max)) {                     \
506            if ((z) > (max))                     \
507                (z) = (max);                     \
508            else if ((z) < (min))                \
509                (z) = (min);                     \
510        } else {                                 \
511            if ((z) > (min))                     \
512                (z) = (min);                     \
513            else if ((z) < (max))                \
514                (z) = (max);                     \
515        }                                        \
516     } while (0)
517 #endif
518
519 /* both min/max and MIN/MAX are defined by some compilers.
520  * we are now on GPMIN / GPMAX
521  */
522 #define GPMAX(a,b) ( (a) > (b) ? (a) : (b) )
523 #define GPMIN(a,b) ( (a) < (b) ? (a) : (b) )
524
525 /* Prototypes from "stdfn.c" */
526
527 char *safe_strncpy __PROTO((char *, const char *, size_t));
528 #ifndef HAVE_SLEEP
529 unsigned int sleep __PROTO((unsigned int));
530 #endif
531
532 double gp_strtod __PROTO((const char *str, char **endptr));
533
534 #endif /* STDFN_H */