string last: fix segfault with invalid index
[jimtcl.git] / jimiocompat.h
blob176a5fe0cfafbb30eb7c8298515c381d040d0701
1 #ifndef JIMIOCOMPAT_H
2 #define JIMIOCOMPAT_H
4 /*
5 * Cross-platform compatibility functions and types for I/O.
6 * Currently used by jim-aio.c and jim-exec.c
7 */
9 #include <stdio.h>
10 #include <errno.h>
11 #include <sys/stat.h>
13 #include "jimautoconf.h"
14 #include <jim.h>
15 #include <jim-win32compat.h>
17 /**
18 * Set an error result based on errno and the given message.
20 void Jim_SetResultErrno(Jim_Interp *interp, const char *msg);
22 /**
23 * Opens the file for writing (and appending if append is true).
24 * Returns the file descriptor, or -1 on failure.
26 int Jim_OpenForWrite(const char *filename, int append);
28 /**
29 * Opens the file for reading.
30 * Returns the file descriptor, or -1 on failure.
32 int Jim_OpenForRead(const char *filename);
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>
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
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
56 /**
57 * Unix-compatible errno
59 int Jim_Errno(void);
60 pidtype waitpid(pidtype pid, int *status, int nohang);
62 #define HAVE_PIPE
63 #define pipe(P) _pipe((P), 0, O_NOINHERIT)
65 typedef struct _stat64 jim_stat_t;
66 #define Jim_Stat __stat64
68 #else
69 typedef struct stat jim_stat_t;
70 #define Jim_Stat stat
72 #if defined(HAVE_UNISTD_H)
73 #include <unistd.h>
74 #include <fcntl.h>
75 #include <sys/wait.h>
77 typedef int pidtype;
78 #define Jim_Errno() errno
79 #define JIM_BAD_PID -1
80 #define JIM_NO_PID 0
82 #ifndef HAVE_EXECVPE
83 #define execvpe(ARG0, ARGV, ENV) execvp(ARG0, ARGV)
84 #endif
85 #endif
86 #endif
88 #endif