1 /* $Id$ 2 3 Part of SWI-Prolog 4 5 Author: Jan Wielemaker 6 E-mail: jan@swi.psy.uva.nl 7 WWW: http://www.swi-prolog.org 8 Copyright (C): 1985-2002, University of Amsterdam 9 10 This library is free software; you can redistribute it and/or 11 modify it under the terms of the GNU Lesser General Public 12 License as published by the Free Software Foundation; either 13 version 2.1 of the License, or (at your option) any later version. 14 15 This library is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 Lesser General Public License for more details. 19 20 You should have received a copy of the GNU Lesser General Public 21 License along with this library; if not, write to the Free Software 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 */ 24 25 #if TIME_WITH_SYS_TIME 26 # include <sys/time.h> 27 # include <time.h> 28 #else 29 # if HAVE_SYS_TIME_H 30 # include <sys/time.h> 31 # else 32 # include <time.h> 33 # endif 34 #endif 35 #ifdef HAVE_SYS_PARAM_H /* get MAXPATHLEN */ 36 #include <sys/param.h> 37 #endif 38 39 #ifdef __MINGW32__ 40 __stdcall unsigned long GetTickCount(void); 41 #endif 42 43 44 /******************************** 45 * MEMORY MANAGEMENT * 46 *********************************/ 47 48 extern void *Allocate(intptr_t); 49 50 /******************************** 51 * MISCELLANEOUS * 52 *********************************/ 53 54 extern char *OsError(void); 55 extern bool initOs(void); 56 57 58 /******************************** 59 * FILES * 60 *********************************/ 61 62 #ifndef STREAM_OPEN_BIN_READ 63 #define STREAM_OPEN_BIN_READ "rb" 64 #endif 65 66 #ifndef STREAM_OPEN_BIN_WRITE 67 #define STREAM_OPEN_BIN_WRITE "wb" 68 #endif 69 70 #ifdef HAVE_POPEN 71 #define PIPE 1 72 #define Popen(path, m) Sopen_pipe(OsPath(path), m) 73 #define Pclose(fd) pclose(fd) 74 #endif 75 76 #ifndef MAXPATHLEN 77 #ifdef PATH_MAX 78 #define MAXPATHLEN PATH_MAX 79 #else 80 #ifdef PATHSIZE 81 #define MAXPATHLEN PATHSIZE 82 #endif 83 #endif 84 #endif 85 86 87 #define Fflush(fd) Sflush(fd) 88 #define Fclose(fd) Sclose(fd) 89 #define Open(path, how, mode) open(OsPath(path), how, mode) 90 #define Read(fd, buf, size) read(fd, buf, size) 91 #define Write(fd, buf, size) write(fd, buf, size) 92 #define Getc(fd) Sgetc(fd) 93 #define Putw(w, fd) Sputw((intptr_t)(w), fd) 94 #define Getw(fd) Sgetw(fd) 95 96 /******************************* 97 * PAGE AND TABLE-SIZE * 98 *******************************/ 99 100 #ifdef HAVE_SYSCONF 101 #if defined(_SC_OPEN_MAX) && !defined(HAVE_GETPAGESIZE) 102 #undef getdtablesize 103 #define getdtablesize() sysconf(_SC_OPEN_MAX) 104 #ifndef HAVE_GETDTABLESIZE 105 #define HAVE_GETDTABLESIZE 1 106 #endif 107 #endif 108 #if defined(_SC_PAGESIZE) && !defined(HAVE_GETPAGESIZE) 109 #undef getpagesize 110 #define getpagesize() sysconf(_SC_PAGESIZE) 111 #ifndef HAVE_GETPAGESIZE 112 #define HAVE_GETPAGESIZE 1 113 #endif 114 #endif 115 #endif /*HAVE_SYSCONF*/ 116 117 #ifndef HAVE_GETDTABLESIZE 118 extern int getdtablesize(void); 119 #endif 120 #ifndef HAVE_GETPAGESIZE 121 extern int getpagesize(void); 122 #endif 123 124 /******************************* 125 * FILE ACCESS * 126 *******************************/ 127 128 #define ACCESS_EXIST 0 129 #define ACCESS_EXECUTE 1 130 #define ACCESS_READ 2 131 #define ACCESS_WRITE 4 132 133 134 /******************************** 135 * TIME CONVERSION * 136 *********************************/ 137 138 typedef enum 139 { CPU_USER, 140 CPU_SYSTEM 141 } cputime_kind; 142 143 extern double CpuTime(cputime_kind); 144 extern double WallTime(void); 145 146 147 /******************************* 148 * MEMORY * 149 *******************************/ 150 151 extern uintptr_t UsedMemory(void); 152 extern uintptr_t FreeMemory(void); 153 154 155 /******************************** 156 * IOSTREAM DESCR. SETS * 157 ********************************/ 158 159 #if !defined(FD_ZERO) && !defined(__WINDOWS__) 160 #ifdef HAVE_SYS_SELECT_H 161 #include <sys/select.h> 162 #else 163 #define FD_ZERO(s) { *((uintptr_t *)(s)) = (0L); } 164 #define FD_SET(fd, s) { *((uintptr_t *)(s)) |= ((uintptr_t)L << (fd)); } 165 #define FD_ISSET(fd, s) ( (*((uintptr_t *)(s)) & ((uintptr_t)L << (fd))) != 0 ) 166 #endif 167 #endif 168 169 /******************************** 170 * TERMINAL CONTROL * 171 *********************************/ 172 173 #define TTY_COOKED 1 /* Initial mode: echo */ 174 #define TTY_RAW 2 /* Non-blocking, non-echo */ 175 #define TTY_OUTPUT 3 /* enable post-processing */ 176 #define TTY_SAVE 4 /* just save status */ 177 178 #ifdef HAVE_TCSETATTR 179 #include <termios.h> 180 #include <unistd.h> 181 #define O_HAVE_TERMIO 1 182 #else /*HAVE_TCSETATTR*/ 183 #ifdef HAVE_SYS_TERMIO_H 184 #include <sys/termio.h> 185 #define termios termio 186 #define O_HAVE_TERMIO 1 187 #else 188 #ifdef HAVE_SYS_TERMIOS_H 189 #include <sys/termios.h> 190 #define O_HAVE_TERMIO 1 191 #endif 192 #endif 193 #endif /*HAVE_TCSETATTR*/ 194 195 #ifdef O_HAVE_TERMIO 196 197 typedef struct 198 { struct termios tab; /* saved tty status */ 199 int mode; /* Prolog;'s view on mode */ 200 } ttybuf; 201 202 #else /* !O_HAVE_TERMIO */ 203 204 #ifdef HAVE_SGTTYB 205 #include <sys/ioctl.h> 206 typedef struct 207 { struct sgttyb tab; /* saved tty status */ 208 int mode; /* Prolog;'s view on mode */ 209 } ttybuf; 210 211 #else 212 213 typedef struct 214 { int mode; /* Prolog;'s view on mode */ 215 } ttybuf; 216 217 #endif /*HAVE_SGTTYB*/ 218 #endif /*O_HAVE_TERMIO*/ 219 220 extern ttybuf ttytab; /* saved tty status */ 221 extern int ttymode; /* Current tty mode */ 222 223 #define IsaTty(fd) isatty(fd) 224 225 extern bool PushTty(IOSTREAM *s, ttybuf *, int mode); 226 extern bool PopTty(IOSTREAM *s, ttybuf *); 227 extern void ResetTty(void); 228 229 230 /******************************** 231 * PROCESS CONTROL * 232 *********************************/ 233 234 extern int System(char *command); 235 236 extern char *ExpandOneFile(const char *spec, char *file); 237 extern char *AbsoluteFile(const char *spec, char *path); 238 extern int IsAbsolutePath(const char *spec); 239 extern char *DeRefLink(const char *link, char *buf); 240 extern bool ExistsDirectory(const char *path); 241 extern bool AccessFile(const char *path, int mode); 242 extern bool AccessFile(const char *path, int mode); 243 extern char *OsPath(const char *plpath, char *path); 244 extern char *Getenv(const char *, char *buf, size_t buflen); 245 extern char *BaseName(const char *f); 246 extern time_t LastModifiedFile(const char *f); 247 extern bool ExistsFile(const char *path); 248 extern atom_t TemporaryFile(const char *id, int *fdp); 249 extern atom_t TemporaryFile(const char *id, int *fdp); 250 extern int DeleteTemporaryFile(atom_t name); 251 extern bool ChDir(const char *path); 252 extern char *PrologPath(const char *ospath, char *path, size_t len); 253 254 255 256