Update PUI to use ffi-curl
[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 realpath_returns_]]ret_t[[() {
52         ]]ret_t[[ r;
53         char p[8] = "/bin/sh\000";
54         char resv[PATH_MAX];
55         int res;
56
57         resv[0] = '\0';
58         r = realpath(p, resv);
59         if (r) {
60                 res = ((]]rettest[[) == 0);
61         } else {
62                 res = 0;
63         }
64         return res;
65 }
66
67 int main()
68 {
69         return realpath_returns_]]ret_t[[();
70 }
71                 ]])],
72                 resvar[=yes],
73                 resvar[=no],
74                 resvar[=no])
75         AC_MSG_RESULT([$]resvar)
76
77         if test "$[]resvar[]" = "yes"; then
78                 resvar_body[=]ret_t
79                 AC_DEFINE_UNQUOTED([REALPATH_RET_T], ret_t,
80                         [return type of realpath()])
81         fi
82
83         popdef([resvar_body])
84         popdef([resvar])
85         popdef([ret_t])
86         popdef([rettest])
87 ])dnl _SXE_CHECK_REALPATH_RETVAL
88
89 AC_DEFUN([_SXE_CHECK_REALPATH_RETVAL_OWNER], [dnl
90         ## defines sxe_func_realpath_retval_owner,
91         ## values are either "sys" or "user"
92         pushdef([resvar], [sxe_func_realpath_retval_owner])
93
94         ## this test is especially critical, because some systems do not
95         ## allocate memory for the user when the return value is "."
96         ## instead they point to a static const char somewhere in their
97         ## guts which, of course, must not be furtherly modified, free'd or
98         ## anything ... took me fucking ages to find out what's going on
99         ## so let's drink to the morons responsible for THAT!
100         AC_MSG_CHECKING([to whom belongs the object returned by realpath])
101         if test "$opsys" = "darwin"; then
102            resvar=user
103         else
104         AC_RUN_IFELSE([AC_LANG_SOURCE([[
105 #ifdef HAVE_STDLIB_H
106 #  include <stdlib.h>
107 #endif
108
109 #ifndef PATH_MAX
110 #define PATH_MAX 4096
111 #endif
112
113 #define FOLLOW_FREE_STRATEGY            1
114 #define FOLLOW_REALLOC_STRATEGY         1
115 #define FOLLOW_DOUBLE_CALL_STRATEGY     1
116
117 int owner_of_object_returned_by_realpath()
118 {
119         void *r;  /* any pointer is just fine */
120         char p[8] = "/bin/sh\000";
121
122         r = (void*)realpath(p, NULL);
123 #if FOLLOW_DOUBLE_CALL_STRATEGY
124         {
125                 char p[8] = "/bin/cp\000";
126
127                 if (r == (void*)realpath(p, NULL)) {
128                         /* System owns when 2 calls yield same pointer */
129                         return 1;
130                 }
131         }
132 #endif
133 #if FOLLOW_REALLOC_STRATEGY
134         /* reallocation would help already */
135         r = realloc(r, 4096);
136 #endif
137 #if FOLLOW_FREE_STRATEGY
138         /* if r was ours we should be able to free it */
139         free(r);
140 #endif
141 #if FOLLOW_FREE_STRATEGY || FOLLOW_REALLOC_STRATEGY
142         return 0;
143 #else
144         return 1;
145 #endif
146 }
147
148 int main()
149 {
150         return owner_of_object_returned_by_realpath();
151 }
152                 ]])],
153                 resvar[=user],
154                 resvar[=sys],
155                 resvar[=sys])
156         fi
157         AC_MSG_RESULT([$]resvar)
158
159         if test "$[]resvar[]" = "user"; then
160                 AC_DEFINE([REALPATH_USER_OWNS_RETVAL], [1],
161                         [Whether the user space owns the retval of realpath()])
162         elif test "$[]resvar[]" = "sys"; then
163                 AC_DEFINE([REALPATH_SYS_OWNS_RETVAL], [1],
164                         [Whether the system owns the retval of realpath()])
165         fi
166
167         popdef([resvar])
168 ])dnl _SXE_CHECK_REALPATH_RETVAL_OWNER
169
170 AC_DEFUN([_SXE_CHECK_REALPATH_ON_PROTECTED_MEMORY], [dnl
171         ## defines sxe_func_realpath_accepts_protmem
172         pushdef([resvar], [sxe_func_realpath_accepts_protmem])
173
174         AC_MSG_CHECKING([whether realpath can operate on protected mem blocks])
175         if test "$opsys" = "darwin"; then
176            resvar=no
177         else
178         AC_RUN_IFELSE([AC_LANG_SOURCE([[
179 #ifdef HAVE_STDLIB_H
180 #  include <stdlib.h>
181 #endif
182
183 #ifndef PATH_MAX
184 #define PATH_MAX 4096
185 #endif
186
187 int realpath_can_operate_on_protected_mem_blocks()
188 {
189         char resv[PATH_MAX];
190         realpath("/bin/sh", NULL);
191         realpath("/bin/sh", resv);
192         return 0;
193 }
194
195 int main()
196 {
197         return realpath_can_operate_on_protected_mem_blocks();
198 }
199                 ]])],
200                 resvar[=yes],
201                 resvar[=no],
202                 resvar[=no])
203         fi
204         AC_MSG_RESULT([$]resvar)
205
206         if test "$[]resvar[]" = "yes"; then
207                 AC_DEFINE([REALPATH_ACCEPTS_PROTMEM], [1],
208                         [Whether realpath() accepts protected memory blocks])
209         fi
210
211         popdef([resvar])
212 ])dnl _SXE_CHECK_REALPATH_ON_PROTECTED_MEMORY
213
214 AC_DEFUN([_SXE_CHECK_REALPATH_SANE_ON_NON_EXISTENT], [dnl
215         ## defines sxe_func_realpath_
216         pushdef([resvar], [sxe_func_realpath_sane_on_non_existent])
217
218         AC_MSG_CHECKING([whether realpath survives if we pass non-existent stuff])
219         AC_RUN_IFELSE([AC_LANG_SOURCE([[
220 #ifdef HAVE_STDLIB_H
221 #  include <stdlib.h>
222 #endif
223
224 #ifndef PATH_MAX
225 #define PATH_MAX 4096
226 #endif
227
228 static char p[24] = "/nobody/has/this/file\000";
229
230 int realpath_survives_non_existent_path()
231 {
232         char *r;
233         char resv[PATH_MAX];
234         r = realpath((char*)p, resv);
235
236         return ((r == NULL) == 0);
237 }
238
239 int main()
240 {
241         return realpath_survives_non_existent_path();
242 }
243                 ]])],
244                 resvar[=yes],
245                 resvar[=no],
246                 resvar[=no])
247         AC_MSG_RESULT([$]resvar)
248
249         if test "$[]resvar[]" = "yes"; then
250                 AC_DEFINE([REALPATH_SANE_ON_NON_EXISTENT], [1],
251                         [Whether realpath() accepts and handles non-existent files])
252         fi
253
254         popdef([resvar])
255 ])dnl _SXE_CHECK_REALPATH_SANE_ON_NON_EXISTENT
256
257 AC_DEFUN([SXE_CHECK_BROKEN_REALPATH], [dnl
258         ## defines 3 vars, look in the sub macros to see which ones
259         _SXE_CHECK_REALPATH_ON_PROTECTED_MEMORY
260         _SXE_CHECK_REALPATH_SANE_ON_NON_EXISTENT
261         _SXE_CHECK_REALPATH_RETVAL
262         _SXE_CHECK_REALPATH_RETVAL_OWNER
263
264         AC_MSG_CHECKING([if realpath is considered broken])
265         if test "$sxe_func_realpath_returns" = "char*" -a \
266                 "$sxe_func_realpath_sane_on_non_existent" = "yes" -a \
267                 "$sxe_func_realpath_retval_owner" = "user"; then
268                 sxe_func_realpath_broken="no"
269         else
270                 sxe_func_realpath_broken="yes"
271         fi
272         AC_MSG_RESULT([$sxe_func_realpath_broken])
273 ])dnl SXE_CHECK_BROKEN_REALPATH
274
275
276 AC_DEFUN([SXE_CHECK_DIRNAME], [dnl
277         ## defines have_dirname
278
279         ## of course posix standards are just rough draughts
280         ## and by no means obliging in any way ...
281         ## and since we all hate working systems we do our best
282         ## to break these so called standards wherever we can
283         ##
284         ## Passage from coreutils:
285         ## In general, we can't use the builtin `dirname' function if available,
286         ## since it has different meanings in different environments. In some
287         ## environments the builtin `dirname' modifies its argument.
288
289         ## for the dirname proto
290         SXE_CHECK_HEADERS([libgen.h])
291         AC_CHECK_DECLS([dirname], [], [], [
292 #ifdef HAVE_LIBGEN_H
293 #  include <libgen.h>
294 #endif
295                 ])
296         AC_CHECK_FUNCS([dirname])       dnl should be part of glibc
297         AC_CHECK_LIB([c], [dirname])
298
299         if test "$ac_cv_func_dirname" = "yes"; then
300                 SXE_CHECK_BROKEN_DIRNAME
301         elif test "$ac_cv_lib_c_dirname" = "yes"; then
302                 LDFLAGS="$LDFLAGS -lc"
303                 SXE_CHECK_BROKEN_DIRNAME
304         else
305                 have_dirname=no
306         fi
307 ])dnl SXE_CHECK_DIRNAME
308
309 AC_DEFUN([_SXE_CHECK_DIRNAME_RETVAL], [dnl
310         ## arg #1 is the return type, char* by default
311         ## arg #2 is the final test,
312         ##        pass something that is expected to be true here
313         ##        the return value is stored in a variable `r'
314         ## defines sxe_func_dirname_returns_<ret_t> and
315         ## sxe_func_dirname_returns=$1 if true
316         pushdef([ret_t], ifelse($1, [], [char*],$1))
317         pushdef([resvar_body], [sxe_func_dirname_returns])
318         pushdef([resvar], resvar_body[_]translit(ret_t, [ -*], [__X]))
319         pushdef([rettest], ifelse($2, [], [[[[r[0] == '.' && r[1] == '\000']]]],$2))
320
321         AC_MSG_CHECKING([whether dirname returns ]ret_t)
322         AC_RUN_IFELSE([AC_LANG_SOURCE([[
323 #ifdef HAVE_LIBGEN_H
324 #  include <libgen.h>
325 #endif
326
327 int main()
328 {
329         ]]ret_t[[ r;
330         char p[11] = "somefile\000";
331         int res;
332
333         r = dirname(p);
334         res = ((]]rettest[[) == 0);
335         return res;
336 }
337                 ]])],
338                 resvar[=yes],
339                 resvar[=no],
340                 resvar[=no])
341         AC_MSG_RESULT([$]resvar)
342
343         if test "$[]resvar[]" = "yes"; then
344                 resvar_body[=]ret_t
345                 AC_DEFINE_UNQUOTED([DIRNAME_RET_T], ret_t,
346                         [return type of dirname()])
347         fi
348
349         popdef([resvar_body])
350         popdef([resvar])
351         popdef([ret_t])
352         popdef([rettest])
353 ])dnl _SXE_CHECK_DIRNAME_RETVAL
354
355 AC_DEFUN([_SXE_CHECK_DIRNAME_ON_C99_RESTRICT_MEMORY], [dnl
356         ## defines sxe_func_dirname_accepts_restrmem
357         pushdef([resvar], [sxe_func_dirname_accepts_restrmem])
358
359         AC_MSG_CHECKING([whether dirname can operate on C99 restrict mem blocks])
360         AC_RUN_IFELSE([AC_LANG_SOURCE([[
361 #ifdef HAVE_STDIO_H
362 #  include <stdio.h>
363 #endif
364 #ifdef HAVE_LIBGEN_H
365 #  include <libgen.h>
366 #endif
367
368 static char f[11] = "./somefile\000";
369
370 int dirname_can_operate_on_c99_restrict()
371 {
372         const char *restrict p = &f;
373         dirname((char*)p);
374         return 0;
375 }
376
377 int main()
378 {
379         return dirname_can_operate_on_c99_restrict();
380 }
381                 ]])],
382                 resvar[=yes],
383                 resvar[=no],
384                 resvar[=no])
385         AC_MSG_RESULT([$]resvar)
386
387         if test "$[]resvar[]" = "yes"; then
388                 AC_DEFINE([DIRNAME_ACCEPTS_RESTRMEM], [1],
389                         [Whether dirname() accepts restricted memory blocks])
390         fi
391
392         popdef([resvar])
393 ])dnl _SXE_CHECK_DIRNAME_ON_C99_RESTRICT_MEMORY
394
395 AC_DEFUN([SXE_CHECK_BROKEN_DIRNAME], [dnl
396         ## defines 3 vars, look in the sub macros to see which ones
397         _SXE_CHECK_DIRNAME_ON_C99_RESTRICT_MEMORY
398         _SXE_CHECK_DIRNAME_RETVAL
399 ])dnl SXE_CHECK_BROKEN_DIRNAME
400
401
402 AC_DEFUN([SXE_CHECK_FILE_LOCK], [dnl
403         ## Determine type of mail locking from configure args and s&m headers
404         AC_MSG_CHECKING([for a type of mail spool file locking])
405         AC_MSG_RESULT([])
406
407         AC_CHECK_FUNCS([lockf flock locking])
408         ## The mail_use_xxx variables are set according to the s&m headers.
409         if test "$with_mail_locking" != "no" -a \
410                 "$mail_use_flock" = "yes"; then
411                 with_mail_locking=flock
412         elif test "$with_mail_locking" != "no" -a \
413                 "$mail_use_lockf" = "yes"; then
414                 with_mail_locking=lockf
415         elif test "$with_mail_locking" != "no" -a \
416                 "$mail_use_locking" = "yes"; then
417                 with_mail_locking=locking
418         fi
419
420         if test "$with_mail_locking" = "lockf"; then
421                 AC_DEFINE([MAIL_LOCK_LOCKF], [1], [Description here!])
422         elif test "$with_mail_locking" = "flock"; then
423                 AC_DEFINE([MAIL_LOCK_FLOCK], [1], [Description here!])
424         elif test "$with_mail_locking" = "locking"; then
425                 AC_DEFINE([MAIL_LOCK_LOCKING], [1], [Description here!])
426         elif test "$with_mail_locking" = "pop"; then
427                 with_pop=yes
428                 with_mail_locking=
429         elif test "$with_mail_locking" = "mmdf"; then
430                 AC_DEFINE([MAIL_LOCK_MMDF], [1], [Description here!])
431         else
432                 with_mail_locking="file"
433                 AC_DEFINE([MAIL_LOCK_DOT], [1], [Description here!])
434         fi
435
436         if test "$with_mail_locking" = "lockf" -a \
437                 "$ac_cv_func_lockf" != "yes"; then
438                 SXE_DIE([lockf mail locking requested but not available.])
439         elif test "$with_mail_locking" = "flock" -a \
440                 "$ac_cv_func_flock" != "yes"; then
441                 SXE_DIE([flock mail locking requested but not available.])
442         elif test "$with_mail_locking" = "locking" -a \
443                 "$ac_cv_func_locking" != "yes"; then
444                 SXE_DIE([locking mail locking requested but not available.])
445         fi
446 ])dnl SXE_CHECK_FILE_LOCK
447
448
449 dnl sxe-fs-funs.m4 ends here