e421d79fe73d5ad8ef1d542879d03efc493968ab
[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 STDERR_FILENO
59 #define STDIN_FILENO    0
60 #define STDOUT_FILENO   1
61 #define STDERR_FILENO   2
62 #endif
63
64 #ifndef O_RDONLY
65 #define O_RDONLY 0
66 #endif
67
68 #ifndef O_WRONLY
69 #define O_WRONLY 1
70 #endif
71
72 #ifndef O_RDWR
73 #define O_RDWR 2
74 #endif
75
76 /* file opening defaults */
77 #ifndef OPEN_BINARY
78 #ifdef O_BINARY
79 #define OPEN_BINARY     O_BINARY
80 #else
81 #define OPEN_BINARY     (0)
82 #endif
83 #endif
84
85 #ifndef OPEN_TEXT
86 #ifdef O_TEXT
87 #define OPEN_TEXT       O_TEXT
88 #else
89 #define OPEN_TEXT       (0)
90 #endif
91 #endif
92
93 #ifndef CREAT_MODE
94 #ifdef WINDOWSNT
95 #define CREAT_MODE      (S_IREAD | S_IWRITE)
96 #else
97 #define CREAT_MODE      (0666)
98 #endif
99 #endif
100
101 #ifndef READ_TEXT
102 #ifdef O_TEXT
103 #define READ_TEXT "rt"
104 #else
105 #define READ_TEXT "r"
106 #endif
107 #endif
108
109 #ifndef READ_BINARY
110 #ifdef O_BINARY
111 #define READ_BINARY "rb"
112 #else
113 #define READ_BINARY "r"
114 #endif
115 #endif
116
117 #ifndef WRITE_BINARY
118 #ifdef O_BINARY
119 #define WRITE_BINARY "wb"
120 #else
121 #define WRITE_BINARY "w"
122 #endif
123 #endif
124
125 #ifndef O_NONBLOCK
126 #ifdef O_NDELAY
127 #define O_NONBLOCK O_NDELAY
128 #else
129 #define O_NONBLOCK 04000
130 #endif
131 #endif
132
133 /* if system does not have symbolic links, it does not have lstat.
134    In that case, use ordinary stat instead.  */
135
136 #ifndef S_IFLNK
137 #define lstat stat
138 #endif
139
140 #if !S_IRUSR
141 # if S_IREAD
142 #  define S_IRUSR S_IREAD
143 # else
144 #  define S_IRUSR 00400
145 # endif
146 #endif
147
148 #if !S_IWUSR
149 # if S_IWRITE
150 #  define S_IWUSR S_IWRITE
151 # else
152 #  define S_IWUSR 00200
153 # endif
154 #endif
155
156 #if !S_IXUSR
157 # if S_IEXEC
158 #  define S_IXUSR S_IEXEC
159 # else
160 #  define S_IXUSR 00100
161 # endif
162 #endif
163
164 #ifdef STAT_MACROS_BROKEN
165 #undef S_ISBLK
166 #undef S_ISCHR
167 #undef S_ISDIR
168 #undef S_ISFIFO
169 #undef S_ISLNK
170 #undef S_ISMPB
171 #undef S_ISMPC
172 #undef S_ISNWK
173 #undef S_ISREG
174 #undef S_ISSOCK
175 #endif /* STAT_MACROS_BROKEN.  */
176
177 #if !defined(S_ISBLK) && defined(S_IFBLK)
178 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
179 #endif
180 #if !defined(S_ISCHR) && defined(S_IFCHR)
181 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
182 #endif
183 #if !defined(S_ISDIR) && defined(S_IFDIR)
184 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
185 #endif
186 #if !defined(S_ISREG) && defined(S_IFREG)
187 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
188 #endif
189 #if !defined(S_ISFIFO) && defined(S_IFIFO)
190 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
191 #endif
192 #if !defined(S_ISLNK) && defined(S_IFLNK)
193 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
194 #endif
195 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
196 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
197 #endif
198 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
199 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
200 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
201 #endif
202 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
203 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
204 #endif
205
206 #if !defined (USG)  && !defined (WINDOWSNT)
207 # define HAVE_FSYNC
208 #endif
209
210 #ifndef MAXPATHLEN
211 /* in 4.1, param.h fails to define this. */
212 #define MAXPATHLEN 1024
213 #endif /* not MAXPATHLEN */
214
215 #ifndef X_OK
216 # define X_OK 01
217 #endif
218
219 #ifndef FD_CLOEXEC
220 # define FD_CLOEXEC 1
221 #endif
222
223 /* Emacs needs to use its own definitions of certain system calls on
224    some systems (like SunOS 4.1 and USG systems, where the read system
225    call is interruptible but Emacs expects it not to be; and under
226    MULE, where all filenames need to be converted to external format).
227    To do this, we #define read to be sys_read, which is defined in
228    sysdep.c.  We first #undef read, in case some system file defines
229    read as a macro.  sysdep.c doesn't encapsulate read, so the call to
230    read inside of sys_read will do the right thing.
231
232    DONT_ENCAPSULATE is used in files such as sysdep.c that want to
233    call the actual system calls rather than the encapsulated versions.
234    Those files can call sys_read to get the (possibly) encapsulated
235    versions.
236
237    IMPORTANT: the redefinition of the system call must occur *after* the
238    inclusion of any header files that declare or define the system call;
239    otherwise lots of unfriendly things can happen.  This goes for all
240    encapsulated system calls.
241
242    We encapsulate the most common system calls here; we assume their
243    declarations are in one of the standard header files included above.
244    Other encapsulations are declared in the appropriate sys*.h file. */
245
246 #ifdef ENCAPSULATE_READ
247 ssize_t sys_read (int, void *, size_t);
248 #endif
249 #if defined (ENCAPSULATE_READ) && !defined (DONT_ENCAPSULATE)
250 # undef read
251 # define read sys_read
252 #endif
253 #if !defined (ENCAPSULATE_READ) && defined (DONT_ENCAPSULATE)
254 # define sys_read read
255 #endif
256
257 #ifdef ENCAPSULATE_WRITE
258 ssize_t sys_write (int, CONST void *, size_t);
259 #endif
260 #if defined (ENCAPSULATE_WRITE) && !defined (DONT_ENCAPSULATE)
261 # undef write
262 # define write sys_write
263 #endif
264 #if !defined (ENCAPSULATE_WRITE) && defined (DONT_ENCAPSULATE)
265 # define sys_write write
266 #endif
267
268 #ifdef ENCAPSULATE_OPEN
269 int sys_open (CONST char *, int, ...);
270 #endif
271 #if defined (ENCAPSULATE_OPEN) && !defined (DONT_ENCAPSULATE)
272 # undef open
273 # define open sys_open
274 #endif
275 #if !defined (ENCAPSULATE_OPEN) && defined (DONT_ENCAPSULATE)
276 # define sys_open open
277 #endif
278
279 #ifdef ENCAPSULATE_CLOSE
280 int sys_close (int);
281 #endif
282 #if defined (ENCAPSULATE_CLOSE) && !defined (DONT_ENCAPSULATE)
283 # undef close
284 # define close sys_close
285 #endif
286 #if !defined (ENCAPSULATE_CLOSE) && defined (DONT_ENCAPSULATE)
287 # define sys_close close
288 #endif
289
290 /* Now the stdio versions ... */
291
292 #ifdef ENCAPSULATE_FREAD
293 size_t sys_fread (void *, size_t, size_t, FILE *);
294 #endif
295 #if defined (ENCAPSULATE_FREAD) && !defined (DONT_ENCAPSULATE)
296 # undef fread
297 # define fread sys_fread
298 #endif
299 #if !defined (ENCAPSULATE_FREAD) && defined (DONT_ENCAPSULATE)
300 # define sys_fread fread
301 #endif
302
303 #ifdef ENCAPSULATE_FWRITE
304 size_t sys_fwrite (CONST void *, size_t, size_t, FILE *);
305 #endif
306 #if defined (ENCAPSULATE_FWRITE) && !defined (DONT_ENCAPSULATE)
307 # undef fwrite
308 # define fwrite sys_fwrite
309 #endif
310 #if !defined (ENCAPSULATE_FWRITE) && defined (DONT_ENCAPSULATE)
311 # define sys_fwrite fwrite
312 #endif
313
314 #ifdef ENCAPSULATE_FOPEN
315 FILE *sys_fopen (CONST char *, CONST char *);
316 #endif
317 #if defined (ENCAPSULATE_FOPEN) && !defined (DONT_ENCAPSULATE)
318 # undef fopen
319 # define fopen sys_fopen
320 #endif
321 #if !defined (ENCAPSULATE_FOPEN) && defined (DONT_ENCAPSULATE)
322 # define sys_fopen fopen
323 #endif
324
325 #ifdef ENCAPSULATE_FCLOSE
326 int sys_fclose (FILE *);
327 #endif
328 #if defined (ENCAPSULATE_FCLOSE) && !defined (DONT_ENCAPSULATE)
329 # undef fclose
330 # define fclose sys_fclose
331 #endif
332 #if !defined (ENCAPSULATE_FCLOSE) && defined (DONT_ENCAPSULATE)
333 # define sys_fclose fclose
334 #endif
335
336
337 /* encapsulations: file-information calls */
338
339 #ifdef ENCAPSULATE_ACCESS
340 int sys_access (CONST char *path, int mode);
341 #endif
342 #if defined (ENCAPSULATE_ACCESS) && !defined (DONT_ENCAPSULATE)
343 # undef access
344 # define access sys_access
345 #endif
346 #if !defined (ENCAPSULATE_ACCESS) && defined (DONT_ENCAPSULATE)
347 # define sys_access access
348 #endif
349
350 #ifdef ENCAPSULATE_EACCESS
351 int sys_eaccess (CONST char *path, int mode);
352 #endif
353 #if defined (ENCAPSULATE_EACCESS) && !defined (DONT_ENCAPSULATE)
354 # undef eaccess
355 # define eaccess sys_eaccess
356 #endif
357 #if !defined (ENCAPSULATE_EACCESS) && defined (DONT_ENCAPSULATE)
358 # define sys_eaccess eaccess
359 #endif
360
361 #ifdef ENCAPSULATE_LSTAT
362 int sys_lstat (CONST char *path, struct stat *buf);
363 #endif
364 #if defined (ENCAPSULATE_LSTAT) && !defined (DONT_ENCAPSULATE)
365 # undef lstat
366 # define lstat sys_lstat
367 #endif
368 #if !defined (ENCAPSULATE_LSTAT) && defined (DONT_ENCAPSULATE)
369 # define sys_lstat lstat
370 #endif
371
372 #ifdef ENCAPSULATE_READLINK
373 int sys_readlink (CONST char *path, char *buf, size_t bufsiz);
374 #endif
375 #if defined (ENCAPSULATE_READLINK) && !defined (DONT_ENCAPSULATE)
376 # undef readlink
377 # define readlink sys_readlink
378 #endif
379 #if !defined (ENCAPSULATE_READLINK) && defined (DONT_ENCAPSULATE)
380 # define sys_readlink readlink
381 #endif
382
383 #ifdef ENCAPSULATE_STAT
384 int sys_stat (CONST char *path, struct stat *buf);
385 #endif
386 #if defined (ENCAPSULATE_STAT) && !defined (DONT_ENCAPSULATE)
387 # undef stat
388 /* Need to use arguments to avoid messing with struct stat */
389 # define stat(path, buf) sys_stat (path, buf)
390 #endif
391 #if !defined (ENCAPSULATE_STAT) && defined (DONT_ENCAPSULATE)
392 # define sys_stat stat
393 #endif
394
395 /* encapsulations: file-manipulation calls */
396
397 #ifdef ENCAPSULATE_CHMOD
398 int sys_chmod (CONST char *path, mode_t mode);
399 #endif
400 #if defined (ENCAPSULATE_CHMOD) && !defined (DONT_ENCAPSULATE)
401 # undef chmod
402 # define chmod sys_chmod
403 #endif
404 #if !defined (ENCAPSULATE_CHMOD) && defined (DONT_ENCAPSULATE)
405 # define sys_chmod chmod
406 #endif
407
408 #ifdef ENCAPSULATE_CREAT
409 int sys_creat (CONST char *path, mode_t mode);
410 #endif
411 #if defined (ENCAPSULATE_CREAT) && !defined (DONT_ENCAPSULATE)
412 # undef creat
413 # define creat sys_creat
414 #endif
415 #if !defined (ENCAPSULATE_CREAT) && defined (DONT_ENCAPSULATE)
416 # define sys_creat creat
417 #endif
418
419 #ifdef ENCAPSULATE_LINK
420 int sys_link (CONST char *existing, CONST char *new);
421 #endif
422 #if defined (ENCAPSULATE_LINK) && !defined (DONT_ENCAPSULATE)
423 # undef link
424 # define link sys_link
425 #endif
426 #if !defined (ENCAPSULATE_LINK) && defined (DONT_ENCAPSULATE)
427 # define sys_link link
428 #endif
429
430 #ifdef ENCAPSULATE_RENAME
431 int sys_rename (CONST char *old, CONST char *new);
432 #endif
433 #if defined (ENCAPSULATE_RENAME) && !defined (DONT_ENCAPSULATE)
434 # undef rename
435 # define rename sys_rename
436 #endif
437 #if !defined (ENCAPSULATE_RENAME) && defined (DONT_ENCAPSULATE)
438 # define sys_rename rename
439 #endif
440
441 #ifdef ENCAPSULATE_SYMLINK
442 int sys_symlink (CONST char *name1, CONST char *name2);
443 #endif
444 #if defined (ENCAPSULATE_SYMLINK) && !defined (DONT_ENCAPSULATE)
445 # undef symlink
446 # define symlink sys_symlink
447 #endif
448 #if !defined (ENCAPSULATE_SYMLINK) && defined (DONT_ENCAPSULATE)
449 # define sys_symlink symlink
450 #endif
451
452 #ifdef ENCAPSULATE_UNLINK
453 int sys_unlink (CONST char *path);
454 #endif
455 #if defined (ENCAPSULATE_UNLINK) && !defined (DONT_ENCAPSULATE)
456 # undef unlink
457 # define unlink sys_unlink
458 #endif
459 #if !defined (ENCAPSULATE_UNLINK) && defined (DONT_ENCAPSULATE)
460 # define sys_unlink unlink
461 #endif
462
463 #ifdef ENCAPSULATE_EXECVP
464 int sys_execvp (CONST char *, char * CONST *);
465 #endif
466 #if defined (ENCAPSULATE_EXECVP) && !defined (DONT_ENCAPSULATE)
467 # undef execvp
468 # define execvp sys_execvp
469 #endif
470 #if !defined (ENCAPSULATE_EXECVP) && defined (DONT_ENCAPSULATE)
471 # define sys_execvp execvp
472 #endif