XEmacs 21.2-b1
[chise/xemacs-chise.git.1] / src / sysfile.h
1 /*
2    Copyright (C) 1995 Free Software Foundation, Inc.
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 /* Synched up with: Not really in FSF. */
22
23 #include <errno.h>
24
25 #ifndef WINDOWSNT
26 #include <sys/errno.h>          /* <errno.h> does not always imply this */
27 #endif
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #ifndef INCLUDED_FCNTL
34 # define INCLUDED_FCNTL
35 # include <fcntl.h>
36 #endif /* INCLUDED_FCNTL */
37
38 /* Load sys/types.h if not already loaded.
39    In some systems loading it twice is suicidal.  */
40 #ifndef makedev
41 #include <sys/types.h>          /* some typedefs are used in sys/file.h */
42 #endif
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/param.h>
46
47 #if defined (NeXT) || defined(__CYGWIN32__)
48 /* what is needed from here?  Do others need it too?
49  O_BINARY is in here under cygwin. */
50 # include <sys/fcntl.h>
51 #endif /* NeXT */
52
53 #ifdef WINDOWSNT
54 #include <io.h>
55 #include <direct.h>
56 #endif
57
58 #ifndef O_RDONLY
59 #define O_RDONLY 0
60 #endif
61
62 #ifndef O_WRONLY
63 #define O_WRONLY 1
64 #endif
65
66 #ifndef O_RDWR
67 #define O_RDWR 2
68 #endif
69
70 /* file opening defaults */
71 #ifndef OPEN_BINARY
72 #ifdef O_BINARY
73 #define OPEN_BINARY     O_BINARY
74 #else
75 #define OPEN_BINARY     (0)
76 #endif
77 #endif
78
79 #ifndef OPEN_TEXT
80 #ifdef O_TEXT
81 #define OPEN_TEXT       O_TEXT
82 #else
83 #define OPEN_TEXT       (0)
84 #endif
85 #endif
86
87 #ifndef CREAT_MODE
88 #ifdef WINDOWSNT
89 #define CREAT_MODE      (S_IREAD | S_IWRITE)
90 #else
91 #define CREAT_MODE      (0666)
92 #endif
93 #endif
94
95 #ifndef READ_TEXT
96 #ifdef O_TEXT
97 #define READ_TEXT "rt"
98 #else
99 #define READ_TEXT "r"
100 #endif
101 #endif
102
103 #ifndef READ_BINARY
104 #ifdef O_BINARY
105 #define READ_BINARY "rb"
106 #else
107 #define READ_BINARY "r"
108 #endif
109 #endif
110
111 #ifndef WRITE_BINARY
112 #ifdef O_BINARY
113 #define WRITE_BINARY "wb"
114 #else
115 #define WRITE_BINARY "w"
116 #endif
117 #endif
118
119 /* if system does not have symbolic links, it does not have lstat.
120    In that case, use ordinary stat instead.  */
121
122 #ifndef S_IFLNK
123 #define lstat stat
124 #endif
125
126 #if !S_IRUSR
127 # if S_IREAD
128 #  define S_IRUSR S_IREAD
129 # else
130 #  define S_IRUSR 00400
131 # endif
132 #endif
133
134 #if !S_IWUSR
135 # if S_IWRITE
136 #  define S_IWUSR S_IWRITE
137 # else
138 #  define S_IWUSR 00200
139 # endif
140 #endif
141
142 #if !S_IXUSR
143 # if S_IEXEC
144 #  define S_IXUSR S_IEXEC
145 # else
146 #  define S_IXUSR 00100
147 # endif
148 #endif
149
150 #ifdef STAT_MACROS_BROKEN
151 #undef S_ISBLK
152 #undef S_ISCHR
153 #undef S_ISDIR
154 #undef S_ISFIFO
155 #undef S_ISLNK
156 #undef S_ISMPB
157 #undef S_ISMPC
158 #undef S_ISNWK
159 #undef S_ISREG
160 #undef S_ISSOCK
161 #endif /* STAT_MACROS_BROKEN.  */
162
163 #if !defined(S_ISBLK) && defined(S_IFBLK)
164 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
165 #endif
166 #if !defined(S_ISCHR) && defined(S_IFCHR)
167 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
168 #endif
169 #if !defined(S_ISDIR) && defined(S_IFDIR)
170 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
171 #endif
172 #if !defined(S_ISREG) && defined(S_IFREG)
173 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
174 #endif
175 #if !defined(S_ISFIFO) && defined(S_IFIFO)
176 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
177 #endif
178 #if !defined(S_ISLNK) && defined(S_IFLNK)
179 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
180 #endif
181 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
182 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
183 #endif
184 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
185 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
186 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
187 #endif
188 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
189 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
190 #endif
191
192 #if !defined (USG)  && !defined (WINDOWSNT)
193 # define HAVE_FSYNC
194 #endif
195
196 #ifndef MAXPATHLEN
197 /* in 4.1, param.h fails to define this. */
198 #define MAXPATHLEN 1024
199 #endif /* not MAXPATHLEN */
200
201 #ifndef X_OK
202 # define X_OK 01
203 #endif
204
205 #ifndef FD_CLOEXEC
206 # define FD_CLOEXEC 1
207 #endif
208
209 /* Emacs needs to use its own definitions of certain system calls on
210    some systems (like SunOS 4.1 and USG systems, where the read system
211    call is interruptible but Emacs expects it not to be; and under
212    MULE, where all filenames need to be converted to external format).
213    To do this, we #define read to be sys_read, which is defined in
214    sysdep.c.  We first #undef read, in case some system file defines
215    read as a macro.  sysdep.c doesn't encapsulate read, so the call to
216    read inside of sys_read will do the right thing.
217
218    DONT_ENCAPSULATE is used in files such as sysdep.c that want to
219    call the actual system calls rather than the encapsulated versions.
220    Those files can call sys_read to get the (possibly) encapsulated
221    versions.
222
223    IMPORTANT: the redefinition of the system call must occur *after* the
224    inclusion of any header files that declare or define the system call;
225    otherwise lots of unfriendly things can happen.  This goes for all
226    encapsulated system calls.
227
228    We encapsulate the most common system calls here; we assume their
229    declarations are in one of the standard header files included above.
230    Other encapsulations are declared in the appropriate sys*.h file. */
231
232 #ifdef ENCAPSULATE_READ
233 int sys_read (int, void *, size_t);
234 #endif
235 #if defined (ENCAPSULATE_READ) && !defined (DONT_ENCAPSULATE)
236 # undef read
237 # define read sys_read
238 #endif
239 #if !defined (ENCAPSULATE_READ) && defined (DONT_ENCAPSULATE)
240 # define sys_read read
241 #endif
242
243 #ifdef ENCAPSULATE_WRITE
244 int sys_write (int, CONST void *, size_t);
245 #endif
246 #if defined (ENCAPSULATE_WRITE) && !defined (DONT_ENCAPSULATE)
247 # undef write
248 # define write sys_write
249 #endif
250 #if !defined (ENCAPSULATE_WRITE) && defined (DONT_ENCAPSULATE)
251 # define sys_write write
252 #endif
253
254 #ifdef ENCAPSULATE_OPEN
255 int sys_open (CONST char *, int, ...);
256 #endif
257 #if defined (ENCAPSULATE_OPEN) && !defined (DONT_ENCAPSULATE)
258 # undef open
259 # define open sys_open
260 #endif
261 #if !defined (ENCAPSULATE_OPEN) && defined (DONT_ENCAPSULATE)
262 # define sys_open open
263 #endif
264
265 #ifdef ENCAPSULATE_CLOSE
266 int sys_close (int);
267 #endif
268 #if defined (ENCAPSULATE_CLOSE) && !defined (DONT_ENCAPSULATE)
269 # undef close
270 # define close sys_close
271 #endif
272 #if !defined (ENCAPSULATE_CLOSE) && defined (DONT_ENCAPSULATE)
273 # define sys_close close
274 #endif
275
276 /* Now the stdio versions ... */
277
278 #ifdef ENCAPSULATE_FREAD
279 size_t sys_fread (void *, size_t, size_t, FILE *);
280 #endif
281 #if defined (ENCAPSULATE_FREAD) && !defined (DONT_ENCAPSULATE)
282 # undef fread
283 # define fread sys_fread
284 #endif
285 #if !defined (ENCAPSULATE_FREAD) && defined (DONT_ENCAPSULATE)
286 # define sys_fread fread
287 #endif
288
289 #ifdef ENCAPSULATE_FWRITE
290 size_t sys_fwrite (CONST void *, size_t, size_t, FILE *);
291 #endif
292 #if defined (ENCAPSULATE_FWRITE) && !defined (DONT_ENCAPSULATE)
293 # undef fwrite
294 # define fwrite sys_fwrite
295 #endif
296 #if !defined (ENCAPSULATE_FWRITE) && defined (DONT_ENCAPSULATE)
297 # define sys_fwrite fwrite
298 #endif
299
300 #ifdef ENCAPSULATE_FOPEN
301 FILE *sys_fopen (CONST char *, CONST char *);
302 #endif
303 #if defined (ENCAPSULATE_FOPEN) && !defined (DONT_ENCAPSULATE)
304 # undef fopen
305 # define fopen sys_fopen
306 #endif
307 #if !defined (ENCAPSULATE_FOPEN) && defined (DONT_ENCAPSULATE)
308 # define sys_fopen fopen
309 #endif
310
311 #ifdef ENCAPSULATE_FCLOSE
312 int sys_fclose (FILE *);
313 #endif
314 #if defined (ENCAPSULATE_FCLOSE) && !defined (DONT_ENCAPSULATE)
315 # undef fclose
316 # define fclose sys_fclose
317 #endif
318 #if !defined (ENCAPSULATE_FCLOSE) && defined (DONT_ENCAPSULATE)
319 # define sys_fclose fclose
320 #endif
321
322
323 /* encapsulations: file-information calls */
324
325 #ifdef ENCAPSULATE_ACCESS
326 int sys_access (CONST char *path, int mode);
327 #endif
328 #if defined (ENCAPSULATE_ACCESS) && !defined (DONT_ENCAPSULATE)
329 # undef access
330 # define access sys_access
331 #endif
332 #if !defined (ENCAPSULATE_ACCESS) && defined (DONT_ENCAPSULATE)
333 # define sys_access access
334 #endif
335
336 #ifdef ENCAPSULATE_EACCESS
337 int sys_eaccess (CONST char *path, int mode);
338 #endif
339 #if defined (ENCAPSULATE_EACCESS) && !defined (DONT_ENCAPSULATE)
340 # undef eaccess
341 # define eaccess sys_eaccess
342 #endif
343 #if !defined (ENCAPSULATE_EACCESS) && defined (DONT_ENCAPSULATE)
344 # define sys_eaccess eaccess
345 #endif
346
347 #ifdef ENCAPSULATE_LSTAT
348 int sys_lstat (CONST char *path, struct stat *buf);
349 #endif
350 #if defined (ENCAPSULATE_LSTAT) && !defined (DONT_ENCAPSULATE)
351 # undef lstat
352 # define lstat sys_lstat
353 #endif
354 #if !defined (ENCAPSULATE_LSTAT) && defined (DONT_ENCAPSULATE)
355 # define sys_lstat lstat
356 #endif
357
358 #ifdef ENCAPSULATE_READLINK
359 int sys_readlink (CONST char *path, char *buf, size_t bufsiz);
360 #endif
361 #if defined (ENCAPSULATE_READLINK) && !defined (DONT_ENCAPSULATE)
362 # undef readlink
363 # define readlink sys_readlink
364 #endif
365 #if !defined (ENCAPSULATE_READLINK) && defined (DONT_ENCAPSULATE)
366 # define sys_readlink readlink
367 #endif
368
369 #ifdef ENCAPSULATE_STAT
370 int sys_stat (CONST char *path, struct stat *buf);
371 #endif
372 #if defined (ENCAPSULATE_STAT) && !defined (DONT_ENCAPSULATE)
373 # undef stat
374 /* Need to use arguments to avoid messing with struct stat */
375 # define stat(path, buf) sys_stat (path, buf)
376 #endif
377 #if !defined (ENCAPSULATE_STAT) && defined (DONT_ENCAPSULATE)
378 # define sys_stat stat
379 #endif
380
381 /* encapsulations: file-manipulation calls */
382
383 #ifdef ENCAPSULATE_CHMOD
384 int sys_chmod (CONST char *path, mode_t mode);
385 #endif
386 #if defined (ENCAPSULATE_CHMOD) && !defined (DONT_ENCAPSULATE)
387 # undef chmod
388 # define chmod sys_chmod
389 #endif
390 #if !defined (ENCAPSULATE_CHMOD) && defined (DONT_ENCAPSULATE)
391 # define sys_chmod chmod
392 #endif
393
394 #ifdef ENCAPSULATE_CREAT
395 int sys_creat (CONST char *path, mode_t mode);
396 #endif
397 #if defined (ENCAPSULATE_CREAT) && !defined (DONT_ENCAPSULATE)
398 # undef creat
399 # define creat sys_creat
400 #endif
401 #if !defined (ENCAPSULATE_CREAT) && defined (DONT_ENCAPSULATE)
402 # define sys_creat creat
403 #endif
404
405 #ifdef ENCAPSULATE_LINK
406 int sys_link (CONST char *existing, CONST char *new);
407 #endif
408 #if defined (ENCAPSULATE_LINK) && !defined (DONT_ENCAPSULATE)
409 # undef link
410 # define link sys_link
411 #endif
412 #if !defined (ENCAPSULATE_LINK) && defined (DONT_ENCAPSULATE)
413 # define sys_link link
414 #endif
415
416 #ifdef ENCAPSULATE_RENAME
417 int sys_rename (CONST char *old, CONST char *new);
418 #endif
419 #if defined (ENCAPSULATE_RENAME) && !defined (DONT_ENCAPSULATE)
420 # undef rename
421 # define rename sys_rename
422 #endif
423 #if !defined (ENCAPSULATE_RENAME) && defined (DONT_ENCAPSULATE)
424 # define sys_rename rename
425 #endif
426
427 #ifdef ENCAPSULATE_SYMLINK
428 int sys_symlink (CONST char *name1, CONST char *name2);
429 #endif
430 #if defined (ENCAPSULATE_SYMLINK) && !defined (DONT_ENCAPSULATE)
431 # undef symlink
432 # define symlink sys_symlink
433 #endif
434 #if !defined (ENCAPSULATE_SYMLINK) && defined (DONT_ENCAPSULATE)
435 # define sys_symlink symlink
436 #endif
437
438 #ifdef ENCAPSULATE_UNLINK
439 int sys_unlink (CONST char *path);
440 #endif
441 #if defined (ENCAPSULATE_UNLINK) && !defined (DONT_ENCAPSULATE)
442 # undef unlink
443 # define unlink sys_unlink
444 #endif
445 #if !defined (ENCAPSULATE_UNLINK) && defined (DONT_ENCAPSULATE)
446 # define sys_unlink unlink
447 #endif
448
449 #ifdef ENCAPSULATE_EXECVP
450 int sys_execvp (CONST char *, char * CONST *);
451 #endif
452 #if defined (ENCAPSULATE_EXECVP) && !defined (DONT_ENCAPSULATE)
453 # undef execvp
454 # define execvp sys_execvp
455 #endif
456 #if !defined (ENCAPSULATE_EXECVP) && defined (DONT_ENCAPSULATE)
457 # define sys_execvp execvp
458 #endif