c7e29cb331e49f5021b1ac96304e4210c5951709
[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 #ifndef MAXPATHLEN
207 /* in 4.1, param.h fails to define this. */
208 #define MAXPATHLEN 1024
209 #endif /* not MAXPATHLEN */
210
211 #ifndef X_OK
212 # define X_OK 01
213 #endif
214
215 #ifndef FD_CLOEXEC
216 # define FD_CLOEXEC 1
217 #endif
218
219 /* Emacs needs to use its own definitions of certain system calls on
220    some systems (like SunOS 4.1 and USG systems, where the read system
221    call is interruptible but Emacs expects it not to be; and under
222    MULE, where all filenames need to be converted to external format).
223    To do this, we #define read to be sys_read, which is defined in
224    sysdep.c.  We first #undef read, in case some system file defines
225    read as a macro.  sysdep.c doesn't encapsulate read, so the call to
226    read inside of sys_read will do the right thing.
227
228    DONT_ENCAPSULATE is used in files such as sysdep.c that want to
229    call the actual system calls rather than the encapsulated versions.
230    Those files can call sys_read to get the (possibly) encapsulated
231    versions.
232
233    IMPORTANT: the redefinition of the system call must occur *after* the
234    inclusion of any header files that declare or define the system call;
235    otherwise lots of unfriendly things can happen.  This goes for all
236    encapsulated system calls.
237
238    We encapsulate the most common system calls here; we assume their
239    declarations are in one of the standard header files included above.
240    Other encapsulations are declared in the appropriate sys*.h file. */
241
242 #ifdef ENCAPSULATE_READ
243 ssize_t sys_read (int, void *, size_t);
244 #endif
245 #if defined (ENCAPSULATE_READ) && !defined (DONT_ENCAPSULATE)
246 # undef read
247 # define read sys_read
248 #endif
249 #if !defined (ENCAPSULATE_READ) && defined (DONT_ENCAPSULATE)
250 # define sys_read read
251 #endif
252
253 #ifdef ENCAPSULATE_WRITE
254 ssize_t sys_write (int, CONST void *, size_t);
255 #endif
256 #if defined (ENCAPSULATE_WRITE) && !defined (DONT_ENCAPSULATE)
257 # undef write
258 # define write sys_write
259 #endif
260 #if !defined (ENCAPSULATE_WRITE) && defined (DONT_ENCAPSULATE)
261 # define sys_write write
262 #endif
263
264 #ifdef ENCAPSULATE_OPEN
265 int sys_open (CONST char *, int, ...);
266 #endif
267 #if defined (ENCAPSULATE_OPEN) && !defined (DONT_ENCAPSULATE)
268 # undef open
269 # define open sys_open
270 #endif
271 #if !defined (ENCAPSULATE_OPEN) && defined (DONT_ENCAPSULATE)
272 # define sys_open open
273 #endif
274
275 #ifdef ENCAPSULATE_CLOSE
276 int sys_close (int);
277 #endif
278 #if defined (ENCAPSULATE_CLOSE) && !defined (DONT_ENCAPSULATE)
279 # undef close
280 # define close sys_close
281 #endif
282 #if !defined (ENCAPSULATE_CLOSE) && defined (DONT_ENCAPSULATE)
283 # define sys_close close
284 #endif
285
286 /* Now the stdio versions ... */
287
288 #ifdef ENCAPSULATE_FREAD
289 size_t sys_fread (void *, size_t, size_t, FILE *);
290 #endif
291 #if defined (ENCAPSULATE_FREAD) && !defined (DONT_ENCAPSULATE)
292 # undef fread
293 # define fread sys_fread
294 #endif
295 #if !defined (ENCAPSULATE_FREAD) && defined (DONT_ENCAPSULATE)
296 # define sys_fread fread
297 #endif
298
299 #ifdef ENCAPSULATE_FWRITE
300 size_t sys_fwrite (CONST void *, size_t, size_t, FILE *);
301 #endif
302 #if defined (ENCAPSULATE_FWRITE) && !defined (DONT_ENCAPSULATE)
303 # undef fwrite
304 # define fwrite sys_fwrite
305 #endif
306 #if !defined (ENCAPSULATE_FWRITE) && defined (DONT_ENCAPSULATE)
307 # define sys_fwrite fwrite
308 #endif
309
310 #ifdef ENCAPSULATE_FOPEN
311 FILE *sys_fopen (CONST char *, CONST char *);
312 #endif
313 #if defined (ENCAPSULATE_FOPEN) && !defined (DONT_ENCAPSULATE)
314 # undef fopen
315 # define fopen sys_fopen
316 #endif
317 #if !defined (ENCAPSULATE_FOPEN) && defined (DONT_ENCAPSULATE)
318 # define sys_fopen fopen
319 #endif
320
321 #ifdef ENCAPSULATE_FCLOSE
322 int sys_fclose (FILE *);
323 #endif
324 #if defined (ENCAPSULATE_FCLOSE) && !defined (DONT_ENCAPSULATE)
325 # undef fclose
326 # define fclose sys_fclose
327 #endif
328 #if !defined (ENCAPSULATE_FCLOSE) && defined (DONT_ENCAPSULATE)
329 # define sys_fclose fclose
330 #endif
331
332
333 /* encapsulations: file-information calls */
334
335 #ifdef ENCAPSULATE_ACCESS
336 int sys_access (CONST char *path, int mode);
337 #endif
338 #if defined (ENCAPSULATE_ACCESS) && !defined (DONT_ENCAPSULATE)
339 # undef access
340 # define access sys_access
341 #endif
342 #if !defined (ENCAPSULATE_ACCESS) && defined (DONT_ENCAPSULATE)
343 # define sys_access access
344 #endif
345
346 #ifdef ENCAPSULATE_EACCESS
347 int sys_eaccess (CONST char *path, int mode);
348 #endif
349 #if defined (ENCAPSULATE_EACCESS) && !defined (DONT_ENCAPSULATE)
350 # undef eaccess
351 # define eaccess sys_eaccess
352 #endif
353 #if !defined (ENCAPSULATE_EACCESS) && defined (DONT_ENCAPSULATE)
354 # define sys_eaccess eaccess
355 #endif
356
357 #ifdef ENCAPSULATE_LSTAT
358 int sys_lstat (CONST char *path, struct stat *buf);
359 #endif
360 #if defined (ENCAPSULATE_LSTAT) && !defined (DONT_ENCAPSULATE)
361 # undef lstat
362 # define lstat sys_lstat
363 #endif
364 #if !defined (ENCAPSULATE_LSTAT) && defined (DONT_ENCAPSULATE)
365 # define sys_lstat lstat
366 #endif
367
368 #ifdef ENCAPSULATE_READLINK
369 int sys_readlink (CONST char *path, char *buf, size_t bufsiz);
370 #endif
371 #if defined (ENCAPSULATE_READLINK) && !defined (DONT_ENCAPSULATE)
372 # undef readlink
373 # define readlink sys_readlink
374 #endif
375 #if !defined (ENCAPSULATE_READLINK) && defined (DONT_ENCAPSULATE)
376 # define sys_readlink readlink
377 #endif
378
379 #ifdef ENCAPSULATE_FSTAT
380 int sys_fstat (int fd, struct stat *buf);
381 #endif
382 #if defined (ENCAPSULATE_FSTAT) && !defined (DONT_ENCAPSULATE)
383 # undef fstat
384 /* Need to use arguments to avoid messing with struct stat */
385 # define fstat(fd, buf) sys_fstat (fd, buf)
386 #endif
387 #if !defined (ENCAPSULATE_FSTAT) && defined (DONT_ENCAPSULATE)
388 # define sys_fstat fstat
389 #endif
390
391 #ifdef ENCAPSULATE_STAT
392 int sys_stat (CONST char *path, struct stat *buf);
393 #endif
394 #if defined (ENCAPSULATE_STAT) && !defined (DONT_ENCAPSULATE)
395 # undef stat
396 /* Need to use arguments to avoid messing with struct stat */
397 # define stat(path, buf) sys_stat (path, buf)
398 #endif
399 #if !defined (ENCAPSULATE_STAT) && defined (DONT_ENCAPSULATE)
400 # define sys_stat stat
401 #endif
402
403 /* encapsulations: file-manipulation calls */
404
405 #ifdef ENCAPSULATE_CHMOD
406 int sys_chmod (CONST char *path, mode_t mode);
407 #endif
408 #if defined (ENCAPSULATE_CHMOD) && !defined (DONT_ENCAPSULATE)
409 # undef chmod
410 # define chmod sys_chmod
411 #endif
412 #if !defined (ENCAPSULATE_CHMOD) && defined (DONT_ENCAPSULATE)
413 # define sys_chmod chmod
414 #endif
415
416 #ifdef ENCAPSULATE_CREAT
417 int sys_creat (CONST char *path, mode_t mode);
418 #endif
419 #if defined (ENCAPSULATE_CREAT) && !defined (DONT_ENCAPSULATE)
420 # undef creat
421 # define creat sys_creat
422 #endif
423 #if !defined (ENCAPSULATE_CREAT) && defined (DONT_ENCAPSULATE)
424 # define sys_creat creat
425 #endif
426
427 #ifdef ENCAPSULATE_LINK
428 int sys_link (CONST char *existing, CONST char *new);
429 #endif
430 #if defined (ENCAPSULATE_LINK) && !defined (DONT_ENCAPSULATE)
431 # undef link
432 # define link sys_link
433 #endif
434 #if !defined (ENCAPSULATE_LINK) && defined (DONT_ENCAPSULATE)
435 # define sys_link link
436 #endif
437
438 #ifdef ENCAPSULATE_RENAME
439 int sys_rename (CONST char *old, CONST char *new);
440 #endif
441 #if defined (ENCAPSULATE_RENAME) && !defined (DONT_ENCAPSULATE)
442 # undef rename
443 # define rename sys_rename
444 #endif
445 #if !defined (ENCAPSULATE_RENAME) && defined (DONT_ENCAPSULATE)
446 # define sys_rename rename
447 #endif
448
449 #ifdef ENCAPSULATE_SYMLINK
450 int sys_symlink (CONST char *name1, CONST char *name2);
451 #endif
452 #if defined (ENCAPSULATE_SYMLINK) && !defined (DONT_ENCAPSULATE)
453 # undef symlink
454 # define symlink sys_symlink
455 #endif
456 #if !defined (ENCAPSULATE_SYMLINK) && defined (DONT_ENCAPSULATE)
457 # define sys_symlink symlink
458 #endif
459
460 #ifdef ENCAPSULATE_UNLINK
461 int sys_unlink (CONST char *path);
462 #endif
463 #if defined (ENCAPSULATE_UNLINK) && !defined (DONT_ENCAPSULATE)
464 # undef unlink
465 # define unlink sys_unlink
466 #endif
467 #if !defined (ENCAPSULATE_UNLINK) && defined (DONT_ENCAPSULATE)
468 # define sys_unlink unlink
469 #endif
470
471 #ifdef ENCAPSULATE_EXECVP
472 int sys_execvp (CONST char *, char * CONST *);
473 #endif
474 #if defined (ENCAPSULATE_EXECVP) && !defined (DONT_ENCAPSULATE)
475 # undef execvp
476 # define execvp sys_execvp
477 #endif
478 #if !defined (ENCAPSULATE_EXECVP) && defined (DONT_ENCAPSULATE)
479 # define sys_execvp execvp
480 #endif