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