1 #ifndef JIMIOCOMPAT_H
2 #define JIMIOCOMPAT_H
3 
4 /*
5  * Cross-platform compatibility functions and types for I/O.
6  * Currently used by jim-aio.c and jim-exec.c
7  */
8 
9 #include <stdio.h>
10 #include <errno.h>
11 #include <sys/stat.h>
12 
13 #include "jimautoconf.h"
14 #include <jim.h>
15 #include <jim-win32compat.h>
16 
17 /**
18  * Set an error result based on errno and the given message.
19  */
20 void Jim_SetResultErrno(Jim_Interp *interp, const char *msg);
21 
22 /**
23  * Opens the file for writing (and appending if append is true).
24  * Returns the file descriptor, or -1 on failure.
25  */
26 int Jim_OpenForWrite(const char *filename, int append);
27 
28 /**
29  * Opens the file for reading.
30  * Returns the file descriptor, or -1 on failure.
31  */
32 int Jim_OpenForRead(const char *filename);
33 
34 #if defined(__MINGW32__)
35     #ifndef STRICT
36     #define STRICT
37     #endif
38     #define WIN32_LEAN_AND_MEAN
39     #include <windows.h>
40     #include <fcntl.h>
41     #include <io.h>
42     #include <process.h>
43 
44     typedef HANDLE pidtype;
45     #define JIM_BAD_PID INVALID_HANDLE_VALUE
46     /* Note that this isn't a separate value on Windows since we don't have os.fork */
47     #define JIM_NO_PID INVALID_HANDLE_VALUE
48 
49     /* These seem to accord with the conventions used by msys/mingw32 */
50     #define WIFEXITED(STATUS) (((STATUS) & 0xff00) == 0)
51     #define WEXITSTATUS(STATUS) ((STATUS) & 0x00ff)
52     #define WIFSIGNALED(STATUS) (((STATUS) & 0xff00) != 0)
53     #define WTERMSIG(STATUS) (((STATUS) >> 8) & 0xff)
54     #define WNOHANG 1
55 
56     /**
57      * Unix-compatible errno
58      */
59     int Jim_Errno(void);
60     pidtype waitpid(pidtype pid, int *status, int nohang);
61 
62     #define HAVE_PIPE
63     #define pipe(P) _pipe((P), 0, O_NOINHERIT)
64 
65     typedef struct _stat64 jim_stat_t;
66     #define Jim_Stat __stat64
67 
68 #else
69     typedef struct stat jim_stat_t;
70     #define Jim_Stat stat
71 
72     #if defined(HAVE_UNISTD_H)
73         #include <unistd.h>
74         #include <fcntl.h>
75         #include <sys/wait.h>
76 
77         typedef int pidtype;
78         #define Jim_Errno() errno
79         #define JIM_BAD_PID -1
80         #define JIM_NO_PID 0
81 
82         #ifndef HAVE_EXECVPE
83             #define execvpe(ARG0, ARGV, ENV) execvp(ARG0, ARGV)
84         #endif
85     #endif
86 #endif
87 
88 #endif
89