Fix pdump on Mac OS Lion from Nelson
[sxemacs] / m4 / sxe-fs-funs.m4
1 dnl sxe-fs-funs.m4 -- Functions for file system access
2
3 AC_DEFUN([SXE_CHECK_REALPATH], [dnl
4         ## defines have_realpath
5
6         ## for the dirname proto
7         AC_CHECK_DECLS([realpath], [], [], [
8 #ifdef HAVE_STDLIB_H
9 #  include <stdlib.h>
10 #endif
11                 ])
12         AC_CHECK_FUNCS([realpath])
13         AC_CHECK_LIB([c], [realpath])
14
15         if test "$ac_cv_func_realpath" = "yes"; then
16                 SXE_CHECK_BROKEN_REALPATH
17         elif test "$ac_cv_lib_c_realpath" = "yes"; then
18                 LDFLAGS="$LDFLAGS -lc"
19                 SXE_CHECK_BROKEN_REALPATH
20         else
21                 have_realpath=no
22         fi
23
24         if test "$sxe_func_realpath_broken" != "no"; then
25                 SXE_ADD_CORE_OBJS([realpath.o])
26         fi
27 ])dnl SXE_CHECK_REALPATH
28
29 AC_DEFUN([_SXE_CHECK_REALPATH_RETVAL], [dnl
30         ## arg #1 is the return type, char* by default
31         ## arg #2 is the final test,
32         ##        pass something that is expected to be true here
33         ##        the return value is stored in a variable `r'
34         ## defines sxe_func_realpath_returns_<ret_t> and
35         ## sxe_func_realpath_returns=$1 if true
36         pushdef([ret_t], ifelse($1, [], [char*],$1))
37         pushdef([resvar_body], [sxe_func_realpath_returns])
38         pushdef([resvar], resvar_body[_]translit(ret_t, [ -*], [__X]))
39         pushdef([rettest], ifelse($2, [], [[[[r[0] == '/']]]],$2))
40
41         AC_MSG_CHECKING([whether realpath returns ]ret_t)
42         AC_RUN_IFELSE([AC_LANG_SOURCE([[
43 #ifdef HAVE_STDLIB_H
44 #  include <stdlib.h>
45 #endif
46
47 #ifndef PATH_MAX
48 #define PATH_MAX 4096
49 #endif
50
51 int main()
52 {
53         ]]ret_t[[ r;
54         char p[8] = "/bin/sh\000";
55         char resv[PATH_MAX];
56         int res;
57
58         resv[0] = '\0';
59         r = realpath(p, resv);
60         if (r) {
61                 res = ((]]rettest[[) == 0);
62         } else {
63                 res = 0;
64         }
65         return res;
66 }
67                 ]])],
68                 resvar[=yes],
69                 resvar[=no],
70                 resvar[=no])
71         AC_MSG_RESULT([$]resvar)
72
73         if test "$[]resvar[]" = "yes"; then
74                 resvar_body[=]ret_t
75                 AC_DEFINE_UNQUOTED([REALPATH_RET_T], ret_t,
76                         [return type of realpath()])
77         fi
78
79         popdef([resvar_body])
80         popdef([resvar])
81         popdef([ret_t])
82         popdef([rettest])
83 ])dnl _SXE_CHECK_REALPATH_RETVAL
84
85 AC_DEFUN([_SXE_CHECK_REALPATH_RETVAL_OWNER], [dnl
86         ## defines sxe_func_realpath_retval_owner,
87         ## values are either "sys" or "user"
88         pushdef([resvar], [sxe_func_realpath_retval_owner])
89
90         ## this test is especially critical, because some systems do not
91         ## allocate memory for the user when the return value is "."
92         ## instead they point to a static const char somewhere in their
93         ## guts which, of course, must not be furtherly modified, free'd or
94         ## anything ... took me fucking ages to find out what's going on
95         ## so let's drink to the morons responsible for THAT!
96         AC_MSG_CHECKING([to whom belongs the object returned by realpath])
97         if test "$opsys" = "darwin"; then
98            resvar=user
99         else
100         AC_RUN_IFELSE([AC_LANG_SOURCE([[
101 #ifdef HAVE_STDLIB_H
102 #  include <stdlib.h>
103 #endif
104
105 #ifndef PATH_MAX
106 #define PATH_MAX 4096
107 #endif
108
109 #define FOLLOW_FREE_STRATEGY            1
110 #define FOLLOW_REALLOC_STRATEGY         1
111
112 int main()
113 {
114         void *r;  /* any pointer is just fine */
115         char p[8] = "/bin/sh\000";
116
117         r = (void*)realpath(p, NULL);
118 #if FOLLOW_REALLOC_STRATEGY
119         /* reallocation would help already */
120         r = realloc(r, 4096);
121 #endif
122 #if FOLLOW_FREE_STRATEGY
123         /* if r was ours we should be able to free it */
124         free(r);
125 #endif
126 #if FOLLOW_FREE_STRATEGY || FOLLOW_REALLOC_STRATEGY
127         return 0;
128 #else
129         return 1;
130 #endif
131 }
132                 ]])],
133                 resvar[=user],
134                 resvar[=sys],
135                 resvar[=sys])
136         fi
137         AC_MSG_RESULT([$]resvar)
138
139         if test "$[]resvar[]" = "user"; then
140                 AC_DEFINE([REALPATH_USER_OWNS_RETVAL], [1],
141                         [Whether the user space owns the retval of realpath()])
142         elif test "$[]resvar[]" = "sys"; then
143                 AC_DEFINE([REALPATH_SYS_OWNS_RETVAL], [1],
144                         [Whether the system owns the retval of realpath()])
145         fi
146
147         popdef([resvar])
148 ])dnl _SXE_CHECK_REALPATH_RETVAL_OWNER
149
150 AC_DEFUN([_SXE_CHECK_REALPATH_ON_PROTECTED_MEMORY], [dnl
151         ## defines sxe_func_realpath_accepts_protmem
152         pushdef([resvar], [sxe_func_realpath_accepts_protmem])
153
154         AC_MSG_CHECKING([whether realpath can operate on protected mem blocks])
155         if test "$opsys" = "darwin"; then
156            resvar=no
157         else
158         AC_RUN_IFELSE([AC_LANG_SOURCE([[
159 #ifdef HAVE_STDLIB_H
160 #  include <stdlib.h>
161 #endif
162
163 #ifndef PATH_MAX
164 #define PATH_MAX 4096
165 #endif
166
167 int main()
168 {
169         char resv[PATH_MAX];
170         realpath("/bin/sh", NULL);
171         realpath("/bin/sh", resv);
172         return 0;
173 }
174                 ]])],
175                 resvar[=yes],
176                 resvar[=no],
177                 resvar[=no])
178         fi
179         AC_MSG_RESULT([$]resvar)
180
181         if test "$[]resvar[]" = "yes"; then
182                 AC_DEFINE([REALPATH_ACCEPTS_PROTMEM], [1],
183                         [Whether realpath() accepts protected memory blocks])
184         fi
185
186         popdef([resvar])
187 ])dnl _SXE_CHECK_REALPATH_ON_PROTECTED_MEMORY
188
189 AC_DEFUN([_SXE_CHECK_REALPATH_SANE_ON_NON_EXISTENT], [dnl
190         ## defines sxe_func_realpath_
191         pushdef([resvar], [sxe_func_realpath_sane_on_non_existent])
192
193         AC_MSG_CHECKING([whether realpath survives if we pass non-existent stuff])
194         AC_RUN_IFELSE([AC_LANG_SOURCE([[
195 #ifdef HAVE_STDLIB_H
196 #  include <stdlib.h>
197 #endif
198
199 #ifndef PATH_MAX
200 #define PATH_MAX 4096
201 #endif
202
203 static char p[24] = "/nobody/has/this/file\000";
204
205 int main()
206 {
207         char *r;
208         char resv[PATH_MAX];
209         r = realpath((char*)p, resv);
210
211         return ((r == NULL) == 0);
212 }
213                 ]])],
214                 resvar[=yes],
215                 resvar[=no],
216                 resvar[=no])
217         AC_MSG_RESULT([$]resvar)
218
219         if test "$[]resvar[]" = "yes"; then
220                 AC_DEFINE([REALPATH_SANE_ON_NON_EXISTENT], [1],
221                         [Whether realpath() accepts and handles non-existent files])
222         fi
223
224         popdef([resvar])
225 ])dnl _SXE_CHECK_REALPATH_SANE_ON_NON_EXISTENT
226
227 AC_DEFUN([SXE_CHECK_BROKEN_REALPATH], [dnl
228         ## defines 3 vars, look in the sub macros to see which ones
229         _SXE_CHECK_REALPATH_ON_PROTECTED_MEMORY
230         _SXE_CHECK_REALPATH_SANE_ON_NON_EXISTENT
231         _SXE_CHECK_REALPATH_RETVAL
232         _SXE_CHECK_REALPATH_RETVAL_OWNER
233
234         AC_MSG_CHECKING([if realpath is considered broken])
235         if test "$sxe_func_realpath_returns" = "char*" -a \
236                 "$sxe_func_realpath_sane_on_non_existent" = "yes" -a \
237                 "$sxe_func_realpath_retval_owner" = "user"; then
238                 sxe_func_realpath_broken="no"
239         else
240                 sxe_func_realpath_broken="yes"
241         fi
242         AC_MSG_RESULT([$sxe_func_realpath_broken])
243 ])dnl SXE_CHECK_BROKEN_REALPATH
244
245
246 AC_DEFUN([SXE_CHECK_DIRNAME], [dnl
247         ## defines have_dirname
248
249         ## of course posix standards are just rough draughts
250         ## and by no means obliging in any way ...
251         ## and since we all hate working systems we do our best
252         ## to break these so called standards wherever we can
253         ##
254         ## Passage from coreutils: 
255         ## In general, we can't use the builtin `dirname' function if available,
256         ## since it has different meanings in different environments. In some
257         ## environments the builtin `dirname' modifies its argument.
258
259         ## for the dirname proto
260         SXE_CHECK_HEADERS([libgen.h])
261         AC_CHECK_DECLS([dirname], [], [], [
262 #ifdef HAVE_LIBGEN_H
263 #  include <libgen.h>
264 #endif
265                 ])
266         AC_CHECK_FUNCS([dirname])       dnl should be part of glibc
267         AC_CHECK_LIB([c], [dirname])
268
269         if test "$ac_cv_func_dirname" = "yes"; then
270                 SXE_CHECK_BROKEN_DIRNAME
271         elif test "$ac_cv_lib_c_dirname" = "yes"; then
272                 LDFLAGS="$LDFLAGS -lc"
273                 SXE_CHECK_BROKEN_DIRNAME
274         else
275                 have_dirname=no
276         fi
277 ])dnl SXE_CHECK_DIRNAME
278
279 AC_DEFUN([_SXE_CHECK_DIRNAME_SIDE_EFFECT], [dnl
280         ## defines sxe_func_dirname_side_effect
281         ## and DIRNAME_SIDE_EFFECT
282         pushdef([resvar], [sxe_func_dirname_side_effect])
283
284         AC_MSG_CHECKING([whether dirname modifies its argument by side-effect])
285         AC_RUN_IFELSE([AC_LANG_SOURCE([[
286 #ifdef HAVE_LIBGEN_H
287 #  include <libgen.h>
288 #endif
289
290 int main()
291 {
292         char p[11] = "somefile\000";
293         dirname(p);
294         return ((p[0] == '.' && p[1] == '\0') == 0);
295 }
296                 ]])],
297                 resvar[=yes],
298                 resvar[=no],
299                 resvar[=no])
300         AC_MSG_RESULT([$]resvar)
301
302         if test "$[]resvar[]" = "yes"; then
303                 AC_DEFINE([DIRNAME_SIDE_EFFECT], [1],
304                         [Whether dirname() operates by side effect])
305         fi
306
307         popdef([resvar])
308 ])dnl _SXE_CHECK_DIRNAME_SIDE_EFFECT
309
310 AC_DEFUN([_SXE_CHECK_DIRNAME_RETVAL], [dnl
311         ## arg #1 is the return type, char* by default
312         ## arg #2 is the final test,
313         ##        pass something that is expected to be true here
314         ##        the return value is stored in a variable `r'
315         ## defines sxe_func_dirname_returns_<ret_t> and
316         ## sxe_func_dirname_returns=$1 if true
317         pushdef([ret_t], ifelse($1, [], [char*],$1))
318         pushdef([resvar_body], [sxe_func_dirname_returns])
319         pushdef([resvar], resvar_body[_]translit(ret_t, [ -*], [__X]))
320         pushdef([rettest], ifelse($2, [], [[[[r[0] == '.' && r[1] == '\000']]]],$2))
321
322         AC_MSG_CHECKING([whether dirname returns ]ret_t)
323         AC_RUN_IFELSE([AC_LANG_SOURCE([[
324 #ifdef HAVE_LIBGEN_H
325 #  include <libgen.h>
326 #endif
327
328 int main()
329 {
330         ]]ret_t[[ r;
331         char p[11] = "somefile\000";
332         int res;
333
334         r = dirname(p);
335         res = ((]]rettest[[) == 0);
336         return res;
337 }
338                 ]])],
339                 resvar[=yes],
340                 resvar[=no],
341                 resvar[=no])
342         AC_MSG_RESULT([$]resvar)
343
344         if test "$[]resvar[]" = "yes"; then
345                 resvar_body[=]ret_t
346                 AC_DEFINE_UNQUOTED([DIRNAME_RET_T], ret_t,
347                         [return type of dirname()])
348         fi
349
350         popdef([resvar_body])
351         popdef([resvar])
352         popdef([ret_t])
353         popdef([rettest])
354 ])dnl _SXE_CHECK_DIRNAME_RETVAL
355
356 AC_DEFUN([_SXE_CHECK_DIRNAME_RETVAL_OWNER], [dnl
357         ## defines sxe_func_dirname_retval_owner,
358         ## values are either "sys" or "user"
359         pushdef([resvar], [sxe_func_dirname_retval_owner])
360
361         malloc_check=${MALLOC_CHECK_}
362         ## Turn off the stupid glibc 2.5 stack trace check. We *know* we may
363         ## do something bad here :-) 
364         MALLOC_CHECK_=0
365         export MALLOC_CHECK_
366         ## this test is especially critical, because some systems do not
367         ## allocate memory for the user when the return value is "."
368         ## instead they point to a static const char somewhere in their
369         ## guts which, of course, must not be furtherly modified, free'd or
370         ## anything ... took me fucking ages to find out what's going on
371         ## so let's drink to the morons responsible for THAT!
372         AC_MSG_CHECKING([to whom belongs the object returned by dirname])
373         AC_RUN_IFELSE([AC_LANG_SOURCE([[
374 #ifdef HAVE_LIBGEN_H
375 #  include <libgen.h>
376 #endif
377
378 #define FOLLOW_FREE_STRATEGY            1
379 #define FOLLOW_REALLOC_STRATEGY         1
380
381 int main()
382 {
383         void *r;  /* any pointer is just fine */
384         char p[11] = "./somefile\000";
385
386         r = (void*)dirname(p);
387 #if FOLLOW_REALLOC_STRATEGY
388         /* reallocation would help already */
389         r = realloc(r, 4096);
390 #endif
391 #if FOLLOW_FREE_STRATEGY
392         /* if r was ours we should be able to free it */
393         free(r);
394 #endif
395 #if FOLLOW_FREE_STRATEGY || FOLLOW_REALLOC_STRATEGY
396         return 0;
397 #else
398         return 1;
399 #endif
400 }
401                 ]])],
402                 resvar[=user],
403                 resvar[=sys],
404                 resvar[=sys])
405         if test "${malloc_check}" = "" ; then
406                 unset MALLOC_CHECK_
407         else 
408                 MALLOC_CHECK_=${malloc_check}
409         fi
410         AC_MSG_RESULT([$]resvar)
411
412         if test "$[]resvar[]" = "user"; then
413                 AC_DEFINE([DIRNAME_USER_OWNS_RETVAL], [1],
414                         [Whether the user space owns the retval of dirname()])
415         elif test "$[]resvar[]" = "sys"; then
416                 AC_DEFINE([DIRNAME_SYS_OWNS_RETVAL], [1],
417                         [Whether the system owns the retval of dirname()])
418         fi
419
420         popdef([resvar])
421 ])dnl _SXE_CHECK_DIRNAME_RETVAL_OWNER
422
423 AC_DEFUN([_SXE_CHECK_DIRNAME_ON_PROTECTED_MEMORY], [dnl
424         ## defines sxe_func_dirname_accepts_protmem
425         pushdef([resvar], [sxe_func_dirname_accepts_protmem])
426
427         AC_MSG_CHECKING([whether dirname can operate on protected mem blocks])
428         AC_RUN_IFELSE([AC_LANG_SOURCE([[
429 #ifdef HAVE_LIBGEN_H
430 #  include <libgen.h>
431 #endif
432
433 int main()
434 {
435         dirname("./somefile");
436         return 0;
437 }
438                 ]])],
439                 resvar[=yes],
440                 resvar[=no],
441                 resvar[=no])
442         AC_MSG_RESULT([$]resvar)
443
444         if test "$[]resvar[]" = "yes"; then
445                 AC_DEFINE([DIRNAME_ACCEPTS_PROTMEM], [1],
446                         [Whether dirname() accepts protected memory blocks])
447         fi
448
449         popdef([resvar])
450 ])dnl _SXE_CHECK_DIRNAME_ON_PROTECTED_MEMORY
451
452 AC_DEFUN([_SXE_CHECK_DIRNAME_ON_C99_RESTRICT_MEMORY], [dnl
453         ## defines sxe_func_dirname_accepts_restrmem
454         pushdef([resvar], [sxe_func_dirname_accepts_restrmem])
455
456         AC_MSG_CHECKING([whether dirname can operate on C99 restrict mem blocks])
457         AC_RUN_IFELSE([AC_LANG_SOURCE([[
458 #ifdef HAVE_STDIO_H
459 #  inlcude <stdio.h>
460 #endif
461 #ifdef HAVE_LIBGEN_H
462 #  include <libgen.h>
463 #endif
464
465 static char f[11] = "./somefile\000";
466
467 int main()
468 {
469         const char *restrict p = &f;
470         dirname((char*)p);
471         return 0;
472 }
473                 ]])],
474                 resvar[=yes],
475                 resvar[=no],
476                 resvar[=no])
477         AC_MSG_RESULT([$]resvar)
478
479         if test "$[]resvar[]" = "yes"; then
480                 AC_DEFINE([DIRNAME_ACCEPTS_RESTRMEM], [1],
481                         [Whether dirname() accepts restricted memory blocks])
482         fi
483
484         popdef([resvar])
485 ])dnl _SXE_CHECK_DIRNAME_ON_C99_RESTRICT_MEMORY
486
487 AC_DEFUN([SXE_CHECK_BROKEN_DIRNAME], [dnl
488         ## defines 3 vars, look in the sub macros to see which ones
489         _SXE_CHECK_DIRNAME_SIDE_EFFECT
490         _SXE_CHECK_DIRNAME_ON_PROTECTED_MEMORY
491         _SXE_CHECK_DIRNAME_ON_C99_RESTRICT_MEMORY
492         _SXE_CHECK_DIRNAME_RETVAL
493         _SXE_CHECK_DIRNAME_RETVAL_OWNER
494 ])dnl SXE_CHECK_BROKEN_DIRNAME
495
496
497 AC_DEFUN([SXE_CHECK_FILE_LOCK], [dnl
498         ## Determine type of mail locking from configure args and s&m headers
499         AC_MSG_CHECKING([for a type of mail spool file locking])
500         AC_MSG_RESULT([])
501
502         AC_CHECK_FUNCS([lockf flock locking])
503         ## The mail_use_xxx variables are set according to the s&m headers.
504         if test "$with_mail_locking" != "no" -a \
505                 "$mail_use_flock" = "yes"; then
506                 with_mail_locking=flock
507         elif test "$with_mail_locking" != "no" -a \
508                 "$mail_use_lockf" = "yes"; then
509                 with_mail_locking=lockf
510         elif test "$with_mail_locking" != "no" -a \
511                 "$mail_use_locking" = "yes"; then
512                 with_mail_locking=locking
513         fi
514
515         if test "$with_mail_locking" = "lockf"; then
516                 AC_DEFINE([MAIL_LOCK_LOCKF], [1], [Description here!])
517         elif test "$with_mail_locking" = "flock"; then
518                 AC_DEFINE([MAIL_LOCK_FLOCK], [1], [Description here!])
519         elif test "$with_mail_locking" = "locking"; then
520                 AC_DEFINE([MAIL_LOCK_LOCKING], [1], [Description here!])
521         elif test "$with_mail_locking" = "pop"; then
522                 with_pop=yes
523                 with_mail_locking=
524         elif test "$with_mail_locking" = "mmdf"; then
525                 AC_DEFINE([MAIL_LOCK_MMDF], [1], [Description here!])
526         else
527                 with_mail_locking="file"
528                 AC_DEFINE([MAIL_LOCK_DOT], [1], [Description here!])
529         fi
530
531         if test "$with_mail_locking" = "lockf" -a \
532                 "$ac_cv_func_lockf" != "yes"; then
533                 SXE_DIE([lockf mail locking requested but not available.])
534         elif test "$with_mail_locking" = "flock" -a \
535                 "$ac_cv_func_flock" != "yes"; then
536                 SXE_DIE([flock mail locking requested but not available.])
537         elif test "$with_mail_locking" = "locking" -a \
538                 "$ac_cv_func_locking" != "yes"; then
539                 SXE_DIE([locking mail locking requested but not available.])
540         fi
541 ])dnl SXE_CHECK_FILE_LOCK
542
543
544 dnl sxe-fs-funs.m4 ends here