2001-05-21 21:08:10 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
2008-07-11 18:02:10 +01:00
|
|
|
* $Id: sys.c,v 1.36 2008-07-11 17:02:09 vsc Exp $ *
|
2003-01-27 15:55:40 +00:00
|
|
|
* mods: $Log: not supported by cvs2svn $
|
2008-07-11 18:02:10 +01:00
|
|
|
* mods: Revision 1.35 2008/05/23 13:16:13 vsc
|
|
|
|
* mods: fix sys.c for win32
|
|
|
|
* mods:
|
2008-05-23 14:16:13 +01:00
|
|
|
* mods: Revision 1.34 2008/05/22 23:25:21 vsc
|
|
|
|
* mods: add tmp_file/2
|
|
|
|
* mods:
|
2008-05-23 00:25:21 +01:00
|
|
|
* mods: Revision 1.33 2007/10/05 18:24:30 vsc
|
|
|
|
* mods: fix garbage collector and fix LeaveGoal
|
|
|
|
* mods:
|
2007-10-05 19:24:30 +01:00
|
|
|
* mods: Revision 1.32 2007/05/07 12:11:39 vsc
|
|
|
|
* mods: fix mktime fix
|
|
|
|
* mods:
|
2007-05-07 13:11:39 +01:00
|
|
|
* mods: Revision 1.31 2007/05/07 11:21:29 vsc
|
|
|
|
* mods: mktime needs to know if daylight time savings are on
|
|
|
|
* mods: (obs from Bernd Gutmann).
|
|
|
|
* mods:
|
2007-05-07 12:21:29 +01:00
|
|
|
* mods: Revision 1.30 2007/05/02 11:16:43 vsc
|
|
|
|
* mods: small fixes to sys.c
|
|
|
|
* mods:
|
2007-05-02 12:16:43 +01:00
|
|
|
* mods: Revision 1.29 2006/10/10 14:08:17 vsc
|
|
|
|
* mods: small fixes on threaded implementation.
|
|
|
|
* mods:
|
2006-10-10 15:08:17 +01:00
|
|
|
* mods: Revision 1.28 2006/05/25 16:28:28 vsc
|
|
|
|
* mods: include thread_sleep functionality.
|
|
|
|
* mods:
|
2006-05-25 17:28:28 +01:00
|
|
|
* mods: Revision 1.27 2006/05/17 18:38:11 vsc
|
|
|
|
* mods: make system library use true file name
|
|
|
|
* mods:
|
2006-05-17 19:38:11 +01:00
|
|
|
* mods: Revision 1.26 2006/04/25 03:23:40 vsc
|
|
|
|
* mods: fix ! in debugger (execute_clause)
|
|
|
|
* mods: improve system/1 and execute/1
|
|
|
|
* mods:
|
2006-04-25 04:23:40 +01:00
|
|
|
* mods: Revision 1.25 2006/01/17 14:10:42 vsc
|
|
|
|
* mods: YENV may be an HW register (breaks some tabling code)
|
|
|
|
* mods: All YAAM instructions are now brackedted, so Op introduced an { and EndOp introduces an }. This is because Ricardo assumes that.
|
|
|
|
* mods: Fix attvars when COROUTING is undefined.
|
|
|
|
* mods:
|
2006-01-17 14:10:42 +00:00
|
|
|
* mods: Revision 1.24 2006/01/08 23:01:48 vsc
|
|
|
|
* mods: *** empty log message ***
|
|
|
|
* mods:
|
2006-01-08 23:04:41 +00:00
|
|
|
* mods: Revision 1.23 2005/10/21 16:09:03 vsc
|
|
|
|
* mods: SWI compatible module only operators
|
|
|
|
* mods:
|
2005-10-21 17:09:03 +01:00
|
|
|
* mods: Revision 1.22 2005/03/10 18:04:01 rslopes
|
|
|
|
* mods: update YAP_Error arguments
|
|
|
|
* mods: to be able to compile on Windows...
|
|
|
|
* mods:
|
2005-03-10 18:04:01 +00:00
|
|
|
* mods: Revision 1.21 2004/08/11 16:14:54 vsc
|
|
|
|
* mods: whole lot of fixes:
|
|
|
|
* mods: - memory leak in indexing
|
|
|
|
* mods: - memory management in WIN32 now supports holes
|
|
|
|
* mods: - extend Yap interface, more support for SWI-Interface
|
|
|
|
* mods: - new predicate mktime in system
|
|
|
|
* mods: - buffer console I/O in WIN32
|
|
|
|
* mods:
|
2004-08-11 17:14:55 +01:00
|
|
|
* mods: Revision 1.20 2004/07/23 19:02:09 vsc
|
|
|
|
* mods: misc fixes
|
|
|
|
* mods:
|
2004-07-23 20:02:09 +01:00
|
|
|
* mods: Revision 1.19 2004/07/23 03:37:17 vsc
|
|
|
|
* mods: fix heap overflow in YAP_LookupAtom
|
|
|
|
* mods:
|
2004-07-23 04:37:17 +01:00
|
|
|
* mods: Revision 1.18 2004/01/26 12:51:33 vsc
|
|
|
|
* mods: should be datime/1 not date/1
|
|
|
|
* mods:
|
2004-01-26 12:51:33 +00:00
|
|
|
* mods: Revision 1.17 2004/01/26 12:41:06 vsc
|
|
|
|
* mods: bug fixes
|
|
|
|
* mods:
|
2004-01-26 12:41:06 +00:00
|
|
|
* mods: Revision 1.16 2003/01/27 15:55:40 vsc
|
|
|
|
* mods: use CVS Id
|
|
|
|
* mods:
|
2003-01-27 15:55:40 +00:00
|
|
|
* mods: Revision 1.15 2003/01/27 15:54:10 vsc
|
|
|
|
* mods: fix header
|
|
|
|
* mods: *
|
2001-05-21 21:08:10 +01:00
|
|
|
* comments: regular expression interpreter *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
2002-09-09 18:40:12 +01:00
|
|
|
#include "YapInterface.h"
|
2001-05-21 21:08:10 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
#if HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2006-10-10 15:08:17 +01:00
|
|
|
#if HAVE_MATH_H
|
|
|
|
#include <math.h>
|
|
|
|
#endif
|
2001-05-21 21:08:10 +01:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_WAIT_H
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_DIRENT_H
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
2001-05-28 20:54:53 +01:00
|
|
|
#if HAVE_DIRECT_H
|
|
|
|
#include <direct.h>
|
|
|
|
#endif
|
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
#include <windows.h>
|
|
|
|
#include <process.h>
|
|
|
|
#endif
|
2001-05-21 21:08:10 +01:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#ifdef HAVE_ENVIRON
|
|
|
|
#undef HAVE_ENVIRON
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-06-19 15:09:44 +01:00
|
|
|
#if __ANDROID__
|
|
|
|
#include <android/asset_manager.h>
|
|
|
|
#include <android/asset_manager_jni.h>
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
2001-05-21 21:08:10 +01:00
|
|
|
|
2014-05-25 10:21:28 +01:00
|
|
|
void init_sys(void);
|
2001-05-21 21:08:10 +01:00
|
|
|
|
2001-06-07 18:54:29 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
2002-09-09 18:40:12 +01:00
|
|
|
static YAP_Term
|
2001-06-07 18:54:29 +01:00
|
|
|
WinError(void)
|
|
|
|
{
|
|
|
|
char msg[256];
|
|
|
|
/* Error, we could not read time */
|
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msg, 256,
|
|
|
|
NULL);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_MkAtomTerm(YAP_LookupAtom(msg)));
|
2001-06-07 18:54:29 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-08-11 17:14:55 +01:00
|
|
|
/* Return time in a structure */
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2004-08-11 17:14:55 +01:00
|
|
|
sysmktime(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
SYSTEMTIME stime, stime0;
|
|
|
|
FILETIME ftime, ftime0;
|
|
|
|
|
|
|
|
stime.wYear = YAP_IntOfTerm(YAP_ARG1);
|
|
|
|
stime.wMonth = YAP_IntOfTerm(YAP_ARG2);
|
|
|
|
stime.wDay = YAP_IntOfTerm(YAP_ARG3);
|
|
|
|
stime.wHour = YAP_IntOfTerm(YAP_ARG4);
|
|
|
|
stime.wMinute = YAP_IntOfTerm(YAP_ARG5);
|
|
|
|
stime.wSecond = YAP_IntOfTerm(YAP_ARG6);
|
|
|
|
stime.wMilliseconds = 0;
|
|
|
|
stime0.wYear = 1970;
|
|
|
|
stime0.wMonth = 1;
|
|
|
|
stime0.wDay = 1;
|
|
|
|
stime0.wHour = 12;
|
|
|
|
stime0.wMinute = 0;
|
|
|
|
stime0.wSecond = 0;
|
|
|
|
stime0.wMilliseconds = 0;
|
|
|
|
if (!SystemTimeToFileTime(&stime,&ftime)) {
|
|
|
|
return YAP_Unify(YAP_ARG8, YAP_MkIntTerm(errno));
|
|
|
|
}
|
|
|
|
if (!SystemTimeToFileTime(&stime0,&ftime0)) {
|
|
|
|
return YAP_Unify(YAP_ARG8, YAP_MkIntTerm(errno));
|
|
|
|
}
|
|
|
|
#if __GNUC__
|
|
|
|
{
|
|
|
|
unsigned long long f1 = (((unsigned long long)ftime.dwHighDateTime)<<32)+(unsigned long long)ftime.dwLowDateTime;
|
|
|
|
unsigned long long f0 = (((unsigned long long)ftime0.dwHighDateTime)<<32)+(unsigned long long)ftime0.dwLowDateTime;
|
2006-01-08 23:04:41 +00:00
|
|
|
return YAP_Unify(YAP_ARG7,YAP_MkIntTerm((long int)((f1-f0)/10000000)));
|
2004-08-11 17:14:55 +01:00
|
|
|
}
|
|
|
|
#else
|
2005-10-21 17:09:03 +01:00
|
|
|
return FALSE;
|
2004-08-11 17:14:55 +01:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_MKTIME
|
|
|
|
struct tm loc;
|
|
|
|
time_t tim;
|
|
|
|
|
|
|
|
loc.tm_year = YAP_IntOfTerm(YAP_ARG1)-1900;
|
|
|
|
loc.tm_mon = YAP_IntOfTerm(YAP_ARG2)-1;
|
|
|
|
loc.tm_mday = YAP_IntOfTerm(YAP_ARG3);
|
|
|
|
loc.tm_hour = YAP_IntOfTerm(YAP_ARG4);
|
|
|
|
loc.tm_min = YAP_IntOfTerm(YAP_ARG5);
|
|
|
|
loc.tm_sec = YAP_IntOfTerm(YAP_ARG6);
|
2010-02-21 00:59:38 +00:00
|
|
|
loc.tm_isdst = -1;
|
2004-08-11 17:14:55 +01:00
|
|
|
|
2009-06-01 22:42:17 +01:00
|
|
|
if ((tim = mktime(&loc)) == (time_t)-1) {
|
2004-08-11 17:14:55 +01:00
|
|
|
return YAP_Unify(YAP_ARG8, YAP_MkIntTerm(errno));
|
|
|
|
}
|
|
|
|
return YAP_Unify(YAP_ARG7,YAP_MkIntTerm(tim));
|
|
|
|
#else
|
|
|
|
oops
|
|
|
|
#endif /* HAVE_MKTIME */
|
|
|
|
#endif /* WINDOWS */
|
|
|
|
}
|
|
|
|
|
2001-05-21 21:08:10 +01:00
|
|
|
/* Return time in a structure */
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
datime(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_Term tf, out[6];
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
SYSTEMTIME stime;
|
|
|
|
GetLocalTime(&stime);
|
2002-09-09 18:40:12 +01:00
|
|
|
out[0] = YAP_MkIntTerm(stime.wYear);
|
|
|
|
out[1] = YAP_MkIntTerm(stime.wMonth);
|
|
|
|
out[2] = YAP_MkIntTerm(stime.wDay);
|
|
|
|
out[3] = YAP_MkIntTerm(stime.wHour);
|
|
|
|
out[4] = YAP_MkIntTerm(stime.wMinute);
|
|
|
|
out[5] = YAP_MkIntTerm(stime.wSecond);
|
2001-05-28 20:54:53 +01:00
|
|
|
#elif HAVE_TIME
|
2001-05-21 21:08:10 +01:00
|
|
|
time_t tp;
|
|
|
|
|
|
|
|
if ((tp = time(NULL)) == -1) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
#ifdef HAVE_LOCALTIME
|
|
|
|
{
|
|
|
|
struct tm *loc = localtime(&tp);
|
|
|
|
if (loc == NULL) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
2002-09-09 18:40:12 +01:00
|
|
|
out[0] = YAP_MkIntTerm(1900+loc->tm_year);
|
|
|
|
out[1] = YAP_MkIntTerm(1+loc->tm_mon);
|
|
|
|
out[2] = YAP_MkIntTerm(loc->tm_mday);
|
|
|
|
out[3] = YAP_MkIntTerm(loc->tm_hour);
|
|
|
|
out[4] = YAP_MkIntTerm(loc->tm_min);
|
|
|
|
out[5] = YAP_MkIntTerm(loc->tm_sec);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
oops
|
|
|
|
#endif /* HAVE_LOCALTIME */
|
|
|
|
#else
|
|
|
|
oops
|
|
|
|
#endif /* HAVE_TIME */
|
2004-01-26 12:51:33 +00:00
|
|
|
tf = YAP_MkApplTerm(YAP_MkFunctor(YAP_LookupAtom("datime"),6), 6, out);
|
2005-10-21 17:09:03 +01:00
|
|
|
return YAP_Unify(YAP_ARG1, tf);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define BUF_SIZE 1024
|
|
|
|
|
|
|
|
/* Return a list of files for a directory */
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
list_directory(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_Term tf = YAP_MkAtomTerm(YAP_LookupAtom("[]"));
|
2004-07-23 04:37:17 +01:00
|
|
|
long sl = YAP_InitSlot(tf);
|
2001-05-21 21:08:10 +01:00
|
|
|
|
2006-01-17 14:10:42 +00:00
|
|
|
char *buf = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
struct _finddata_t c_file;
|
|
|
|
char bs[BUF_SIZE];
|
|
|
|
long hFile;
|
|
|
|
|
|
|
|
bs[0] = '\0';
|
|
|
|
#if HAVE_STRNCPY
|
|
|
|
strncpy(bs, buf, BUF_SIZE);
|
|
|
|
#else
|
|
|
|
strcpy(bs, buf);
|
|
|
|
#endif
|
|
|
|
#if HAVE_STRNCAT
|
|
|
|
strncat(bs, "/*", BUF_SIZE);
|
|
|
|
#else
|
2007-05-02 12:16:43 +01:00
|
|
|
strcat(bs, "/*");
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif
|
|
|
|
if ((hFile = _findfirst(bs, &c_file)) == -1L) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2,tf));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
2004-07-23 04:37:17 +01:00
|
|
|
YAP_PutInSlot(sl, YAP_MkPairTerm(YAP_MkAtomTerm(YAP_LookupAtom(c_file.name)), YAP_GetFromSlot(sl)));
|
2001-05-28 20:54:53 +01:00
|
|
|
while (_findnext( hFile, &c_file) == 0) {
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_Term ti = YAP_MkAtomTerm(YAP_LookupAtom(c_file.name));
|
2004-07-23 04:37:17 +01:00
|
|
|
YAP_PutInSlot(sl,YAP_MkPairTerm(ti, YAP_GetFromSlot(sl)));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
_findclose( hFile );
|
2014-06-19 15:09:44 +01:00
|
|
|
#else
|
|
|
|
#if __ANDROID__
|
2014-06-11 19:36:50 +01:00
|
|
|
{
|
|
|
|
extern AAssetManager *assetManager;
|
|
|
|
const char *dirName = buf+strlen("/assets/");
|
|
|
|
AAssetManager* mgr = assetManager;
|
|
|
|
AAssetDir *de;
|
|
|
|
const char* dp;
|
|
|
|
|
|
|
|
if ((de = AAssetManager_openDir(mgr, dirName)) == NULL) {
|
|
|
|
return(YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
|
|
|
|
}
|
|
|
|
while (( dp = AAssetDir_getNextFileName(de))) {
|
|
|
|
YAP_Term ti = YAP_MkAtomTerm(YAP_LookupAtom(dp));
|
|
|
|
YAP_PutInSlot(sl,YAP_MkPairTerm(ti, YAP_GetFromSlot(sl)));
|
|
|
|
}
|
|
|
|
AAssetDir_close(de);
|
|
|
|
}
|
2014-06-19 15:09:44 +01:00
|
|
|
#endif
|
|
|
|
#if HAVE_OPENDIR
|
2001-05-21 21:08:10 +01:00
|
|
|
{
|
|
|
|
DIR *de;
|
|
|
|
struct dirent *dp;
|
|
|
|
|
|
|
|
if ((de = opendir(buf)) == NULL) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
while ((dp = readdir(de))) {
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_Term ti = YAP_MkAtomTerm(YAP_LookupAtom(dp->d_name));
|
2004-07-23 04:37:17 +01:00
|
|
|
YAP_PutInSlot(sl,YAP_MkPairTerm(ti, YAP_GetFromSlot(sl)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
closedir(de);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_OPENDIR */
|
2014-06-19 15:09:44 +01:00
|
|
|
#endif
|
2004-07-23 04:37:17 +01:00
|
|
|
tf = YAP_GetFromSlot(sl);
|
|
|
|
return YAP_Unify(YAP_ARG2, tf);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
p_unlink(void)
|
|
|
|
{
|
2006-01-17 14:10:42 +00:00
|
|
|
char *fd = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
if (_unlink(fd) == -1)
|
|
|
|
#else
|
|
|
|
if (unlink(fd) == -1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(errno)));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
2001-05-21 21:08:10 +01:00
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
p_mkdir(void)
|
|
|
|
{
|
2006-01-17 14:10:42 +00:00
|
|
|
char *fd = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
if (_mkdir(fd) == -1) {
|
|
|
|
#else
|
2001-05-21 21:08:10 +01:00
|
|
|
if (mkdir(fd, 0777) == -1) {
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif
|
2001-05-21 21:08:10 +01:00
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
p_rmdir(void)
|
|
|
|
{
|
2006-01-17 14:10:42 +00:00
|
|
|
char *fd = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
if (_rmdir(fd) == -1) {
|
|
|
|
#else
|
2001-05-21 21:08:10 +01:00
|
|
|
if (rmdir(fd) == -1) {
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif
|
2001-05-21 21:08:10 +01:00
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
rename_file(void)
|
|
|
|
{
|
2006-01-17 14:10:42 +00:00
|
|
|
char *s1 = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
|
|
|
char *s2 = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2));
|
2001-05-21 21:08:10 +01:00
|
|
|
#if HAVE_RENAME
|
|
|
|
if (rename(s1, s2) == -1) {
|
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
dir_separator(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom("/"))));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
file_property(void)
|
|
|
|
{
|
2006-05-17 19:38:11 +01:00
|
|
|
const char *fd;
|
2001-05-28 20:54:53 +01:00
|
|
|
#if HAVE_LSTAT
|
2001-05-21 21:08:10 +01:00
|
|
|
struct stat buf;
|
|
|
|
|
2006-01-17 14:10:42 +00:00
|
|
|
fd = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2001-05-21 21:08:10 +01:00
|
|
|
if (lstat(fd, &buf) == -1) {
|
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG7, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
2002-06-11 00:38:59 +01:00
|
|
|
if (S_ISREG(buf.st_mode)) {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!(YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("regular"))) &&
|
|
|
|
YAP_Unify(YAP_ARG6, YAP_MkIntTerm(0))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
|
|
|
} else if (S_ISDIR(buf.st_mode)) {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!(YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("directory"))) &&
|
|
|
|
YAP_Unify(YAP_ARG6, YAP_MkIntTerm(0))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
|
|
|
} else if (S_ISFIFO(buf.st_mode)) {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!(YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("fifo"))) &&
|
|
|
|
YAP_Unify(YAP_ARG6, YAP_MkIntTerm(0))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
|
|
|
} else if (S_ISLNK(buf.st_mode)) {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("symlink"))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
2002-06-11 06:30:05 +01:00
|
|
|
#if HAVE_READLINK
|
|
|
|
{
|
|
|
|
char tmp[256];
|
2002-06-11 06:43:01 +01:00
|
|
|
int n;
|
|
|
|
if ((n = readlink(fd,tmp,256)) == -1) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG7, YAP_MkIntTerm(errno)));
|
2002-06-11 06:30:05 +01:00
|
|
|
}
|
2002-06-11 06:43:01 +01:00
|
|
|
tmp[n] = '\0';
|
2002-09-09 18:40:12 +01:00
|
|
|
if(!YAP_Unify(YAP_ARG6,YAP_MkAtomTerm(YAP_LookupAtom(tmp)))) {
|
2002-06-11 06:30:05 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!YAP_Unify(YAP_ARG6, YAP_MkIntTerm(0)))
|
2002-06-11 06:30:05 +01:00
|
|
|
return(FALSE);
|
|
|
|
#endif
|
2002-06-11 00:38:59 +01:00
|
|
|
} else if (S_ISSOCK(buf.st_mode)) {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!(YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("socket"))) &&
|
|
|
|
YAP_Unify(YAP_ARG6, YAP_MkIntTerm(0))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
|
|
|
} else {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!(YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("unknown"))) &&
|
|
|
|
YAP_Unify(YAP_ARG6, YAP_MkIntTerm(0))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2001-05-28 20:54:53 +01:00
|
|
|
#elif defined(__MINGW32__) || _MSC_VER
|
|
|
|
/* for some weird reason _stat did not work with mingw32 */
|
2002-10-07 15:50:00 +01:00
|
|
|
struct _stat buf;
|
2001-05-28 20:54:53 +01:00
|
|
|
|
2002-09-09 18:40:12 +01:00
|
|
|
fd = YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2002-10-07 15:50:00 +01:00
|
|
|
if (_stat(fd, &buf) != 0) {
|
2001-05-28 20:54:53 +01:00
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG7, YAP_MkIntTerm(errno)));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
2002-06-11 00:38:59 +01:00
|
|
|
if (buf.st_mode & S_IFREG) {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("regular"))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
|
|
|
} else if (buf.st_mode & S_IFDIR) {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("directory"))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
|
|
|
} else {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("unknown"))))
|
2002-06-11 00:38:59 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif
|
2002-06-11 00:38:59 +01:00
|
|
|
return (
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_Unify(YAP_ARG3, YAP_MkIntTerm(buf.st_size)) &&
|
|
|
|
YAP_Unify(YAP_ARG4, YAP_MkIntTerm(buf.st_mtime)) &&
|
|
|
|
YAP_Unify(YAP_ARG5, YAP_MkIntTerm(buf.st_mode))
|
2002-06-11 00:38:59 +01:00
|
|
|
);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* temporary files */
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
p_mktemp(void)
|
|
|
|
{
|
2014-06-14 10:27:39 +01:00
|
|
|
#if HAVE_MKSTEMP || HAVE_MKTEMP || defined(__MINGW32__) || _MSC_VER
|
2002-06-18 06:58:12 +01:00
|
|
|
char *s, tmp[BUF_SIZE];
|
2006-01-17 14:10:42 +00:00
|
|
|
s = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2001-05-21 21:08:10 +01:00
|
|
|
#if HAVE_STRNCPY
|
2002-06-18 06:58:12 +01:00
|
|
|
strncpy(tmp, s, BUF_SIZE);
|
2001-05-21 21:08:10 +01:00
|
|
|
#else
|
|
|
|
strcpy(tmp, s);
|
|
|
|
#endif
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
if ((s = _mktemp(tmp)) == NULL) {
|
2004-07-23 20:02:09 +01:00
|
|
|
/* return an error number */
|
|
|
|
return(YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
|
|
|
|
}
|
|
|
|
return(YAP_Unify(YAP_ARG2,YAP_MkAtomTerm(YAP_LookupAtom(s))));
|
2014-06-14 10:27:39 +01:00
|
|
|
#elif HAVE_MKSTEMP
|
2014-06-19 15:09:44 +01:00
|
|
|
strcpy(tmp, "/tmp/YAP_tmpXXXXXXXX");
|
2014-06-14 10:27:39 +01:00
|
|
|
if(mkstemp(tmp) == -1) {
|
|
|
|
/* return an error number */
|
|
|
|
return(YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
|
|
|
|
}
|
|
|
|
return YAP_Unify(YAP_ARG2,YAP_MkAtomTerm(YAP_LookupAtom(tmp)));
|
2001-05-28 20:54:53 +01:00
|
|
|
#else
|
2001-05-21 21:08:10 +01:00
|
|
|
if ((s = mktemp(tmp)) == NULL) {
|
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
2004-07-23 20:02:09 +01:00
|
|
|
return YAP_Unify(YAP_ARG2,YAP_MkAtomTerm(YAP_LookupAtom(s)));
|
|
|
|
#endif
|
2001-05-21 21:08:10 +01:00
|
|
|
#else
|
2004-07-23 20:02:09 +01:00
|
|
|
return FALSE;
|
2001-05-21 21:08:10 +01:00
|
|
|
#endif
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2008-05-23 00:25:21 +01:00
|
|
|
p_tmpnam(void)
|
2001-05-21 21:08:10 +01:00
|
|
|
{
|
2014-06-14 10:27:39 +01:00
|
|
|
#if HAVE_MKSTEMP
|
2014-06-19 15:09:44 +01:00
|
|
|
char s[21];
|
|
|
|
strcpy(s, "/tmp/YAP_tmpXXXXXXXX");
|
2014-06-14 10:27:39 +01:00
|
|
|
if(mkstemp(s) == -1)
|
|
|
|
return FALSE;
|
|
|
|
return YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom(s)));
|
|
|
|
#elif HAVE_MKTEMP
|
2014-05-25 10:21:28 +01:00
|
|
|
char *s;
|
|
|
|
if (!(s = mktemp("/tmp/YAP_tmpXXXXXXXX")))
|
|
|
|
return FALSE;
|
|
|
|
return YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom(s)));
|
|
|
|
#elif HAVE_TMPNAM
|
2008-05-23 00:25:21 +01:00
|
|
|
char buf[L_tmpnam], *s;
|
|
|
|
if (!(s = tmpnam(buf)))
|
|
|
|
return FALSE;
|
|
|
|
return YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom(s)));
|
2001-05-21 21:08:10 +01:00
|
|
|
#else
|
2004-07-23 20:02:09 +01:00
|
|
|
return FALSE;
|
2001-05-21 21:08:10 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2008-05-23 00:25:21 +01:00
|
|
|
p_tmpdir(void)
|
|
|
|
{
|
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
char buf[512];
|
|
|
|
DWORD out = GetTempPath(512, buf);
|
|
|
|
if (!out) {
|
|
|
|
return(YAP_Unify(YAP_ARG2, WinError()));
|
|
|
|
}
|
|
|
|
if (out > 511) {
|
2008-05-23 14:16:13 +01:00
|
|
|
char *nbuf = malloc(out+1);
|
2008-05-23 00:25:21 +01:00
|
|
|
if (!nbuf)
|
|
|
|
return YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom("no malloc memory")));
|
|
|
|
out = GetTempPath(512, nbuf);
|
|
|
|
if (!out) {
|
2008-05-23 14:16:13 +01:00
|
|
|
return YAP_Unify(YAP_ARG2, WinError());
|
2008-05-23 00:25:21 +01:00
|
|
|
}
|
|
|
|
return YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom(nbuf)));
|
|
|
|
}
|
|
|
|
return YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom(buf)));
|
|
|
|
#else
|
2008-05-23 14:16:13 +01:00
|
|
|
char *s;
|
2008-05-23 00:25:21 +01:00
|
|
|
if ((s = getenv("TMPDIR")))
|
|
|
|
return YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom(s)));
|
|
|
|
#ifdef P_tmpdir
|
|
|
|
return YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom(P_tmpdir)));
|
|
|
|
#endif
|
|
|
|
return YAP_Unify(YAP_ARG1,YAP_MkAtomTerm(YAP_LookupAtom("/tmp")));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-05-21 21:08:10 +01:00
|
|
|
/* return YAP's environment */
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
p_environ(void)
|
|
|
|
{
|
2010-06-18 10:32:57 +01:00
|
|
|
#if HAVE_ENVIRON && 0
|
|
|
|
#if HAVE__NSGETENVIRON
|
|
|
|
char ** ptr = _NSGetEnviron();
|
|
|
|
#elif defined(__MINGW32__) || _MSC_VER
|
2001-05-28 20:54:53 +01:00
|
|
|
extern char **_environ;
|
2010-06-18 10:32:57 +01:00
|
|
|
char ** ptr = _environ;
|
2001-05-28 20:54:53 +01:00
|
|
|
#else
|
2001-05-21 21:08:10 +01:00
|
|
|
extern char **environ;
|
2010-06-18 10:32:57 +01:00
|
|
|
char ** ptr = environ;
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_Term t1 = YAP_ARG1;
|
|
|
|
long int i;
|
2001-05-21 21:08:10 +01:00
|
|
|
|
2002-09-09 18:40:12 +01:00
|
|
|
i = YAP_IntOfTerm(t1);
|
2010-06-18 10:32:57 +01:00
|
|
|
if (ptr[i] == NULL)
|
2001-05-21 21:08:10 +01:00
|
|
|
return(FALSE);
|
|
|
|
else {
|
2010-06-18 10:32:57 +01:00
|
|
|
YAP_Term t = YAP_BufferToString(ptr[i]);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(t, YAP_ARG2));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
#else
|
2010-06-18 10:32:57 +01:00
|
|
|
YAP_Error(0, 0L, "environ not available in this configuration" );
|
2001-05-21 21:08:10 +01:00
|
|
|
return(FALSE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
2001-06-29 20:30:25 +01:00
|
|
|
static HANDLE
|
2002-09-09 18:40:12 +01:00
|
|
|
get_handle(YAP_Term ti, DWORD fd)
|
2001-05-28 20:54:53 +01:00
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
if (YAP_IsAtomTerm(ti)) {
|
2001-06-29 20:30:25 +01:00
|
|
|
HANDLE out;
|
|
|
|
SECURITY_ATTRIBUTES satt;
|
|
|
|
|
|
|
|
satt.nLength = sizeof(satt);
|
|
|
|
satt.lpSecurityDescriptor = NULL;
|
|
|
|
satt.bInheritHandle = TRUE;
|
|
|
|
out = CreateFile("NUL",
|
|
|
|
GENERIC_READ|GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE,
|
|
|
|
&satt,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
return(out);
|
2001-05-28 20:54:53 +01:00
|
|
|
} else {
|
2002-09-09 18:40:12 +01:00
|
|
|
if (YAP_IsIntTerm(ti)) {
|
2001-06-29 20:30:25 +01:00
|
|
|
return(GetStdHandle(fd));
|
|
|
|
} else
|
2002-09-09 18:40:12 +01:00
|
|
|
return((HANDLE)YAP_StreamToFileNo(ti));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-09-09 18:40:12 +01:00
|
|
|
close_handle(YAP_Term ti, HANDLE h)
|
2001-05-28 20:54:53 +01:00
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
if (YAP_IsAtomTerm(ti)) {
|
2001-06-29 20:30:25 +01:00
|
|
|
CloseHandle(h);
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
2001-06-29 20:30:25 +01:00
|
|
|
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif
|
|
|
|
|
2001-05-21 21:08:10 +01:00
|
|
|
/* execute a command as a detached process */
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
execute_command(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_Term ti = YAP_ARG2, to = YAP_ARG3, te = YAP_ARG4;
|
2001-05-21 21:08:10 +01:00
|
|
|
int res;
|
2011-07-10 12:49:27 +01:00
|
|
|
YAP_Term AtomNull = YAP_MkAtomTerm(YAP_LookupAtom("null"));
|
|
|
|
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
2001-06-29 20:30:25 +01:00
|
|
|
HANDLE inpf, outf, errf;
|
2001-05-28 20:54:53 +01:00
|
|
|
DWORD CreationFlags = 0;
|
|
|
|
STARTUPINFO StartupInfo;
|
|
|
|
PROCESS_INFORMATION ProcessInformation;
|
2001-06-29 20:30:25 +01:00
|
|
|
inpf = get_handle(ti, STD_INPUT_HANDLE);
|
|
|
|
if (inpf == INVALID_HANDLE_VALUE) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG6, WinError()));
|
2001-06-29 20:30:25 +01:00
|
|
|
}
|
|
|
|
outf = get_handle(to, STD_OUTPUT_HANDLE);
|
|
|
|
if (outf == INVALID_HANDLE_VALUE) {
|
|
|
|
close_handle(ti, inpf);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG6, WinError()));
|
2001-06-29 20:30:25 +01:00
|
|
|
}
|
|
|
|
errf = get_handle(te, STD_OUTPUT_HANDLE);
|
|
|
|
if (errf == INVALID_HANDLE_VALUE) {
|
|
|
|
close_handle(ti, inpf);
|
|
|
|
close_handle(to, outf);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG6, WinError()));
|
2001-06-29 20:30:25 +01:00
|
|
|
}
|
2002-09-09 18:40:12 +01:00
|
|
|
if (!YAP_IsIntTerm(ti) && !YAP_IsIntTerm(to) && !YAP_IsIntTerm(te)) {
|
2001-06-29 20:30:25 +01:00
|
|
|
/* we do not keep a current stream */
|
2001-05-28 20:54:53 +01:00
|
|
|
CreationFlags = DETACHED_PROCESS;
|
|
|
|
}
|
|
|
|
StartupInfo.cb = sizeof(STARTUPINFO);
|
|
|
|
StartupInfo.lpReserved = NULL;
|
|
|
|
StartupInfo.lpDesktop = NULL; /* inherit */
|
|
|
|
StartupInfo.lpTitle = NULL; /* we do not create a new console window */
|
|
|
|
StartupInfo.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
StartupInfo.cbReserved2 = 0;
|
|
|
|
StartupInfo.lpReserved2 = NULL;
|
2001-06-29 20:30:25 +01:00
|
|
|
StartupInfo.hStdInput = inpf;
|
|
|
|
StartupInfo.hStdOutput = outf;
|
|
|
|
StartupInfo.hStdError = errf;
|
2001-05-28 20:54:53 +01:00
|
|
|
/* got stdin, stdout and error as I like it */
|
|
|
|
if (CreateProcess(NULL,
|
2007-05-02 12:16:43 +01:00
|
|
|
(char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)),
|
2001-05-28 20:54:53 +01:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
CreationFlags,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&StartupInfo,
|
|
|
|
&ProcessInformation) == FALSE) {
|
2001-06-29 20:30:25 +01:00
|
|
|
close_handle(ti, inpf);
|
|
|
|
close_handle(to, outf);
|
|
|
|
close_handle(te, errf);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG6, WinError()));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
2001-06-29 20:30:25 +01:00
|
|
|
close_handle(ti, inpf);
|
|
|
|
close_handle(to, outf);
|
|
|
|
close_handle(te, errf);
|
2001-05-28 20:54:53 +01:00
|
|
|
res = ProcessInformation.dwProcessId;
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG5,YAP_MkIntTerm(res)));
|
2001-05-28 20:54:53 +01:00
|
|
|
#else /* UNIX CODE */
|
2001-06-29 20:30:25 +01:00
|
|
|
int inpf, outf, errf;
|
2001-05-21 21:08:10 +01:00
|
|
|
/* process input first */
|
2011-07-10 12:49:27 +01:00
|
|
|
if (ti == AtomNull) {
|
2001-05-21 21:08:10 +01:00
|
|
|
inpf = open("/dev/null", O_RDONLY);
|
|
|
|
} else {
|
2001-06-29 20:30:25 +01:00
|
|
|
int sd;
|
2002-09-09 18:40:12 +01:00
|
|
|
if (YAP_IsIntTerm(ti))
|
2015-04-22 21:19:14 +01:00
|
|
|
sd = YAP_IntOfTerm(ti);
|
2001-06-29 20:30:25 +01:00
|
|
|
else
|
2002-09-09 18:40:12 +01:00
|
|
|
sd = YAP_StreamToFileNo(ti);
|
2015-04-22 21:19:14 +01:00
|
|
|
if (sd == 0)
|
|
|
|
inpf = 0;
|
|
|
|
else
|
|
|
|
inpf = dup(sd);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
if (inpf < 0) {
|
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG6, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
/* then output stream */
|
2011-07-10 12:49:27 +01:00
|
|
|
if (to == AtomNull) {
|
2001-05-21 21:08:10 +01:00
|
|
|
outf = open("/dev/zero", O_WRONLY);
|
|
|
|
} else {
|
2001-06-29 20:30:25 +01:00
|
|
|
int sd;
|
2002-09-09 18:40:12 +01:00
|
|
|
if (YAP_IsIntTerm(to))
|
2015-04-22 21:19:14 +01:00
|
|
|
sd = YAP_IntOfTerm(to);
|
2001-06-29 20:30:25 +01:00
|
|
|
else
|
2002-09-09 18:40:12 +01:00
|
|
|
sd = YAP_StreamToFileNo(to);
|
2015-04-22 21:19:14 +01:00
|
|
|
if (sd == 1)
|
|
|
|
outf = 1;
|
|
|
|
else
|
|
|
|
outf = dup(sd);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
if (outf < 0) {
|
|
|
|
/* return an error number */
|
2015-04-22 21:19:14 +01:00
|
|
|
if (inpf != 0) close(inpf);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG6, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
/* then error stream */
|
2011-07-10 12:49:27 +01:00
|
|
|
if (te == AtomNull) {
|
2001-05-21 21:08:10 +01:00
|
|
|
errf = open("/dev/zero", O_WRONLY);
|
|
|
|
} else {
|
2001-06-29 20:30:25 +01:00
|
|
|
int sd;
|
2002-09-09 18:40:12 +01:00
|
|
|
if (YAP_IsIntTerm(te))
|
2015-04-22 21:19:14 +01:00
|
|
|
sd = YAP_IntOfTerm(te);
|
2001-06-29 20:30:25 +01:00
|
|
|
else
|
2002-09-09 18:40:12 +01:00
|
|
|
sd = YAP_StreamToFileNo(te);
|
2015-04-22 21:19:14 +01:00
|
|
|
if (sd == 2)
|
|
|
|
errf = 2;
|
|
|
|
else
|
|
|
|
errf = dup(sd);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
if (errf < 0) {
|
|
|
|
/* return an error number */
|
2015-04-22 21:19:14 +01:00
|
|
|
if (inpf != 0) close(inpf);
|
|
|
|
if (outf != 1) close(outf);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG6, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
2008-07-11 18:02:10 +01:00
|
|
|
YAP_FlushAllStreams();
|
2001-05-21 21:08:10 +01:00
|
|
|
/* we are now ready to fork */
|
|
|
|
if ((res = fork()) < 0) {
|
|
|
|
/* close streams we don't need */
|
2015-04-22 21:19:14 +01:00
|
|
|
if (inpf != 0) close(inpf);
|
|
|
|
if (outf != 1) close(outf);
|
|
|
|
if (errf != 2) close(errf);
|
2001-05-21 21:08:10 +01:00
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG6, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
} else if (res == 0) {
|
|
|
|
char *argv[4];
|
|
|
|
|
|
|
|
/* child */
|
|
|
|
/* close current streams, but not std streams */
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_CloseAllOpenStreams();
|
2015-04-22 21:19:14 +01:00
|
|
|
if (inpf != 0) {
|
|
|
|
close(0);
|
|
|
|
if (dup(inpf) != 0)
|
|
|
|
exit(1);
|
|
|
|
close(inpf);
|
|
|
|
}
|
|
|
|
if (outf != 1) {
|
|
|
|
close(1);
|
|
|
|
if (dup(outf) != 1)
|
2011-07-10 12:49:27 +01:00
|
|
|
exit(1);
|
2015-04-22 21:19:14 +01:00
|
|
|
close(outf);
|
|
|
|
}
|
|
|
|
if (errf != 2) {
|
|
|
|
close(2);
|
|
|
|
if (dup(errf) != 2)
|
|
|
|
exit(2);
|
|
|
|
close(errf);
|
|
|
|
}
|
2001-05-21 21:08:10 +01:00
|
|
|
argv[0] = "sh";
|
|
|
|
argv[1] = "-c";
|
2006-01-17 14:10:42 +00:00
|
|
|
argv[2] = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2001-05-21 21:08:10 +01:00
|
|
|
argv[3] = NULL;
|
|
|
|
execv("/bin/sh", argv);
|
|
|
|
exit(127);
|
|
|
|
/* we have the streams where we want them, just want to execute now */
|
|
|
|
} else {
|
2015-04-22 21:19:14 +01:00
|
|
|
if (inpf != 0) close(inpf);
|
|
|
|
if (outf != 1) close(outf);
|
|
|
|
if (errf != 2) close(errf);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG5,YAP_MkIntTerm(res)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif /* UNIX code */
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* execute a command as a detached process */
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-22 21:41:52 +01:00
|
|
|
do_system(void)
|
2001-05-21 21:08:10 +01:00
|
|
|
{
|
2006-01-17 14:10:42 +00:00
|
|
|
char *command = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
2001-05-21 21:08:10 +01:00
|
|
|
#if HAVE_SYSTEM
|
2002-06-18 06:58:12 +01:00
|
|
|
int sys = system(command);
|
2001-06-29 20:30:25 +01:00
|
|
|
if (sys < 0) {
|
2006-04-25 04:23:40 +01:00
|
|
|
return YAP_Unify(YAP_ARG3,YAP_MkIntTerm(errno));
|
2001-06-29 20:30:25 +01:00
|
|
|
}
|
2006-04-25 04:23:40 +01:00
|
|
|
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(sys));
|
2002-06-18 06:58:12 +01:00
|
|
|
#else
|
2007-05-02 12:16:43 +01:00
|
|
|
YAP_Error(0,0L,"system not available in this configuration, trying %s", command);
|
2006-04-25 04:23:40 +01:00
|
|
|
return FALSE;
|
2002-06-18 06:58:12 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* execute a command as a detached process */
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2002-06-18 06:58:12 +01:00
|
|
|
do_shell(void)
|
|
|
|
{
|
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
2006-04-25 04:23:40 +01:00
|
|
|
YAP_Error(0,0L,"system not available in this configuration");
|
|
|
|
return(FALSE);
|
|
|
|
#elif HAVE_SYSTEM
|
2002-09-09 18:40:12 +01:00
|
|
|
char *buf = YAP_AllocSpaceFromYap(BUF_SIZE);
|
2002-06-18 06:58:12 +01:00
|
|
|
int sys;
|
|
|
|
|
|
|
|
if (buf == NULL) {
|
2005-03-10 18:04:01 +00:00
|
|
|
YAP_Error(0,0L,"No Temporary Space for Shell");
|
2002-06-18 06:58:12 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
#if HAVE_STRNCPY
|
2006-04-25 04:23:40 +01:00
|
|
|
strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)), BUF_SIZE);
|
|
|
|
strncpy(buf, " ", BUF_SIZE);
|
|
|
|
strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)), BUF_SIZE);
|
|
|
|
strncpy(buf, " ", BUF_SIZE);
|
|
|
|
strncpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)), BUF_SIZE);
|
2002-06-18 06:58:12 +01:00
|
|
|
#else
|
2006-04-25 04:23:40 +01:00
|
|
|
strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1)));
|
|
|
|
strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2)));
|
|
|
|
strcpy(buf, " ");
|
2007-10-05 19:24:30 +01:00
|
|
|
strcpy(buf, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)));
|
2002-06-18 06:58:12 +01:00
|
|
|
#endif
|
|
|
|
sys = system(buf);
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_FreeSpaceFromYap(buf);
|
2002-06-18 06:58:12 +01:00
|
|
|
if (sys < 0) {
|
2006-04-25 04:23:40 +01:00
|
|
|
return YAP_Unify(YAP_ARG5,YAP_MkIntTerm(errno));
|
2002-06-18 06:58:12 +01:00
|
|
|
}
|
2006-04-25 04:23:40 +01:00
|
|
|
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(sys));
|
2002-06-18 06:58:12 +01:00
|
|
|
#else
|
|
|
|
char *cptr[4];
|
|
|
|
int t;
|
|
|
|
int sys;
|
|
|
|
|
2006-01-17 14:10:42 +00:00
|
|
|
cptr[0]= (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
|
|
|
cptr[1]= (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG2));
|
|
|
|
cptr[2]= (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3));
|
2002-06-18 06:58:12 +01:00
|
|
|
cptr[3]= NULL;
|
|
|
|
t = fork();
|
|
|
|
if (t < 0) {
|
2006-04-25 04:23:40 +01:00
|
|
|
return YAP_Unify(YAP_ARG5,YAP_MkIntTerm(errno));
|
2002-06-18 06:58:12 +01:00
|
|
|
} else if (t == 0) {
|
2006-04-25 04:23:40 +01:00
|
|
|
t = execvp(cptr[0],cptr);
|
|
|
|
return t;
|
2002-06-18 06:58:12 +01:00
|
|
|
} else {
|
|
|
|
t = wait(&sys);
|
|
|
|
if (t < 0) {
|
2006-04-25 04:23:40 +01:00
|
|
|
return YAP_Unify(YAP_ARG5,YAP_MkIntTerm(errno));
|
2002-06-18 06:58:12 +01:00
|
|
|
}
|
|
|
|
}
|
2006-04-25 04:23:40 +01:00
|
|
|
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(sys));
|
2001-05-21 21:08:10 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* execute a command as a detached process */
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2015-04-22 21:19:14 +01:00
|
|
|
plwait(void)
|
2001-05-21 21:08:10 +01:00
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
long int pid = YAP_IntOfTerm(YAP_ARG1);
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
HANDLE proc = OpenProcess(STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE, FALSE, pid);
|
|
|
|
DWORD ExitCode;
|
|
|
|
if (proc == NULL) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG3, WinError()));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
if (WaitForSingleObject(proc, INFINITE) == WAIT_FAILED) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG3, WinError()));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
if (GetExitCodeProcess(proc, &ExitCode) == 0) {
|
2015-04-22 21:19:14 +01:00
|
|
|
return(YAP_Unify(YAP_ARG4, WinError()));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
CloseHandle(proc);
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(ExitCode)));
|
2001-05-28 20:54:53 +01:00
|
|
|
#else
|
2001-05-21 21:08:10 +01:00
|
|
|
do {
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* check for interruptions */
|
|
|
|
if (waitpid(pid, &status, 0) == -1) {
|
2015-04-22 21:19:14 +01:00
|
|
|
if (errno) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return YAP_Unify( YAP_ARG3, YAP_MkIntTerm(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (WIFEXITED( status ) ) {
|
|
|
|
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(WEXITSTATUS(status)) );
|
|
|
|
} else if (WIFSIGNALED( status )) {
|
|
|
|
return YAP_Unify(YAP_ARG3, YAP_MkAtomTerm(YAP_LookupAtom("signal")) ) &&
|
|
|
|
YAP_Unify(YAP_ARG4, YAP_MkIntTerm( WTERMSIG(status)) );
|
|
|
|
} else /* WIFSTOPPED(status) */ {
|
|
|
|
return YAP_Unify(YAP_ARG3, YAP_MkAtomTerm(YAP_LookupAtom("stopped")) ) &&
|
|
|
|
YAP_Unify(YAP_ARG4, YAP_MkIntTerm( WSTOPSIG(status) ) );
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
} while(TRUE);
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
p_sleep(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_Term ts = YAP_ARG1;
|
2014-06-16 17:00:09 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
{
|
|
|
|
unsigned long int secs = 0, usecs = 0, msecs, out;
|
|
|
|
if (YAP_IsIntTerm(ts)) {
|
|
|
|
secs = YAP_IntOfTerm(ts);
|
|
|
|
} else if (YAP_IsFloatTerm(ts)) {
|
|
|
|
double tfl = YAP_FloatOfTerm(ts);
|
2001-05-21 21:08:10 +01:00
|
|
|
if (tfl > 1.0)
|
|
|
|
secs = tfl;
|
|
|
|
else
|
2006-05-25 17:28:28 +01:00
|
|
|
usecs = tfl*1000000;
|
2014-06-16 17:00:09 +01:00
|
|
|
}
|
|
|
|
msecs = secs*1000 + usecs/1000;
|
|
|
|
Sleep(msecs);
|
|
|
|
/* no errors possible */
|
|
|
|
out = 0;
|
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(out)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
2006-05-25 17:28:28 +01:00
|
|
|
#elif HAVE_NANOSLEEP
|
|
|
|
{
|
|
|
|
struct timespec req;
|
2014-06-16 17:00:09 +01:00
|
|
|
int out;
|
|
|
|
|
2006-05-25 17:28:28 +01:00
|
|
|
if (YAP_IsFloatTerm(ts)) {
|
|
|
|
double tfl = YAP_FloatOfTerm(ts);
|
|
|
|
|
|
|
|
req.tv_nsec = (tfl-floor(tfl))*1000000000;
|
|
|
|
req.tv_sec = rint(tfl);
|
|
|
|
} else {
|
|
|
|
req.tv_nsec = 0;
|
2014-06-16 17:00:09 +01:00
|
|
|
req.tv_sec = YAP_IntOfTerm(ts);
|
2006-05-25 17:28:28 +01:00
|
|
|
}
|
|
|
|
out = nanosleep(&req, NULL);
|
2014-06-16 17:00:09 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(out)));
|
2006-05-25 17:28:28 +01:00
|
|
|
}
|
|
|
|
#elif HAVE_USLEEP
|
2014-06-16 17:00:09 +01:00
|
|
|
{
|
|
|
|
useconds_t usecs;
|
|
|
|
if (YAP_IsFloatTerm(ts)) {
|
|
|
|
double tfl = YAP_FloatOfTerm(ts);
|
|
|
|
|
|
|
|
usecs = rint(tfl*1000000);
|
|
|
|
} else {
|
|
|
|
usecs = YAP_IntOfTerm(ts)*1000000;
|
|
|
|
}
|
2006-05-25 17:28:28 +01:00
|
|
|
out = usleep(usecs);
|
2014-06-16 17:00:09 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(out)));
|
|
|
|
}
|
2006-05-25 17:28:28 +01:00
|
|
|
#elif HAVE_SLEEP
|
2014-06-16 17:00:09 +01:00
|
|
|
{
|
|
|
|
unsigned int secs, out;
|
|
|
|
if (YAP_IsFloatTerm(ts)) {
|
|
|
|
secs = rint(YAP_FloatOfTerm(ts));
|
|
|
|
} else {
|
|
|
|
secs = YAP_IntOfTerm(ts);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
2014-06-16 17:00:09 +01:00
|
|
|
out = sleep(secs);
|
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(out)));
|
|
|
|
}
|
2006-05-25 17:28:28 +01:00
|
|
|
#else
|
|
|
|
YAP_Error(0,0L,"sleep not available in this configuration");
|
|
|
|
return FALSE:
|
2001-05-21 21:08:10 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* host info */
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
host_name(void)
|
|
|
|
{
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
char name[MAX_COMPUTERNAME_LENGTH+1];
|
|
|
|
DWORD nSize = MAX_COMPUTERNAME_LENGTH+1;
|
|
|
|
if (GetComputerName(name, &nSize) == 0) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, WinError()));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
#else
|
2001-05-21 21:08:10 +01:00
|
|
|
#if HAVE_GETHOSTNAME
|
2001-05-28 20:54:53 +01:00
|
|
|
char name[256];
|
2001-05-21 21:08:10 +01:00
|
|
|
if (gethostname(name, 256) == -1) {
|
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG2, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif /* defined(__MINGW32__) || _MSC_VER */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG1, YAP_MkAtomTerm(YAP_LookupAtom(name))));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
host_id(void)
|
|
|
|
{
|
|
|
|
#if HAVE_GETHOSTID
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG1, YAP_MkIntTerm(gethostid())));
|
2001-05-28 20:54:53 +01:00
|
|
|
#else
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG1, YAP_MkIntTerm(0)));
|
2001-05-21 21:08:10 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
pid(void)
|
|
|
|
{
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG1, YAP_MkIntTerm(_getpid())));
|
2001-05-28 20:54:53 +01:00
|
|
|
#else
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG1, YAP_MkIntTerm(getpid())));
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-06-08 15:52:54 +01:00
|
|
|
win(void)
|
|
|
|
{
|
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
return(TRUE);
|
|
|
|
#else
|
|
|
|
return(FALSE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
p_kill(void)
|
|
|
|
{
|
2001-05-28 20:54:53 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
/* Windows does not support cross-process signals, so we shall do the
|
|
|
|
SICStus thing and assume that a signal to a process will
|
|
|
|
always kill it */
|
2002-09-09 18:40:12 +01:00
|
|
|
HANDLE proc = OpenProcess(STANDARD_RIGHTS_REQUIRED|PROCESS_TERMINATE, FALSE, YAP_IntOfTerm(YAP_ARG1));
|
2001-05-28 20:54:53 +01:00
|
|
|
if (proc == NULL) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG3, WinError()));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
if (TerminateProcess(proc, -1) == 0) {
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG3, WinError()));
|
2001-05-28 20:54:53 +01:00
|
|
|
}
|
|
|
|
CloseHandle(proc);
|
|
|
|
#else
|
2002-09-09 18:40:12 +01:00
|
|
|
if (kill(YAP_IntOfTerm(YAP_ARG1), YAP_IntOfTerm(YAP_ARG2)) < 0) {
|
2001-05-21 21:08:10 +01:00
|
|
|
/* return an error number */
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
2001-05-28 20:54:53 +01:00
|
|
|
#endif /* defined(__MINGW32__) || _MSC_VER */
|
2001-05-21 21:08:10 +01:00
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2015-04-22 21:19:14 +01:00
|
|
|
#if HAVE_OPENSSL_RIPEMD_H
|
|
|
|
#include <openssl/ripemd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** md5( +Text, -Key, -Remaining keyq
|
|
|
|
* encode text using OpenSSL
|
|
|
|
*
|
|
|
|
* arg Text as List of codes
|
|
|
|
* arg2 and 3: difference list with character codes.
|
|
|
|
*
|
|
|
|
* @return whether ARG1's md5 unifies with the difference liat.
|
|
|
|
*/
|
|
|
|
static YAP_Bool
|
|
|
|
md5(void)
|
|
|
|
{
|
|
|
|
#if HAVE_OPENSSL_RIPEMD_H
|
|
|
|
char buf[21];
|
|
|
|
|
|
|
|
char *s = (char *) YAP_AllocSpaceFromYap(YAP_ListLength(YAP_ARG1)+1);
|
|
|
|
YAP_StringToBuffer( YAP_ARG1 , s, 20 ) ;
|
|
|
|
RIPEMD160((const unsigned char *)s, strlen(s), (unsigned char *)buf);
|
|
|
|
YAP_FreeSpaceFromYap(s);
|
|
|
|
return YAP_Unify( YAP_ARG2, YAP_BufferToDiffList( buf , YAP_ARG3 ) );
|
|
|
|
#endif /* defined(__MINGW32__) || _MSC_VER */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-05-21 21:08:10 +01:00
|
|
|
error_message(void)
|
|
|
|
{
|
|
|
|
#if HAVE_STRERROR
|
2007-05-02 12:16:43 +01:00
|
|
|
return YAP_Unify(YAP_ARG2,YAP_MkAtomTerm(YAP_LookupAtom(strerror(YAP_IntOfTerm(YAP_ARG1)))));
|
2001-05-21 21:08:10 +01:00
|
|
|
#else
|
2007-05-02 12:16:43 +01:00
|
|
|
return YAP_Unify(YAP_ARG2,YAP_ARG1);
|
2001-05-21 21:08:10 +01:00
|
|
|
#endif
|
|
|
|
}
|
2015-04-22 21:19:14 +01:00
|
|
|
void
|
2001-05-21 21:08:10 +01:00
|
|
|
init_sys(void)
|
|
|
|
{
|
2007-05-07 13:11:39 +01:00
|
|
|
#if HAVE_MKTIME
|
|
|
|
tzset();
|
|
|
|
#endif
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_UserCPredicate("datime", datime, 2);
|
2004-08-11 17:14:55 +01:00
|
|
|
YAP_UserCPredicate("mktime", sysmktime, 8);
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_UserCPredicate("list_directory", list_directory, 3);
|
|
|
|
YAP_UserCPredicate("file_property", file_property, 7);
|
|
|
|
YAP_UserCPredicate("unlink", p_unlink, 2);
|
|
|
|
YAP_UserCPredicate("mkdir", p_mkdir, 2);
|
|
|
|
YAP_UserCPredicate("rmdir", p_rmdir, 2);
|
|
|
|
YAP_UserCPredicate("dir_separator", dir_separator, 1);
|
|
|
|
YAP_UserCPredicate("p_environ", p_environ, 2);
|
|
|
|
YAP_UserCPredicate("exec_command", execute_command, 6);
|
|
|
|
YAP_UserCPredicate("do_shell", do_shell, 5);
|
|
|
|
YAP_UserCPredicate("do_system", do_system, 3);
|
2015-04-22 21:19:14 +01:00
|
|
|
YAP_UserCPredicate("plwait", plwait, 4);
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_UserCPredicate("host_name", host_name, 2);
|
|
|
|
YAP_UserCPredicate("host_id", host_id, 2);
|
|
|
|
YAP_UserCPredicate("pid", pid, 2);
|
|
|
|
YAP_UserCPredicate("kill", p_kill, 3);
|
|
|
|
YAP_UserCPredicate("mktemp", p_mktemp, 3);
|
2008-05-23 00:25:21 +01:00
|
|
|
YAP_UserCPredicate("tmpnam", p_tmpnam, 2);
|
|
|
|
YAP_UserCPredicate("tmpdir", p_tmpdir, 2);
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_UserCPredicate("rename_file", rename_file, 3);
|
|
|
|
YAP_UserCPredicate("sleep", p_sleep, 2);
|
|
|
|
YAP_UserCPredicate("error_message", error_message, 2);
|
|
|
|
YAP_UserCPredicate("win", win, 0);
|
2015-04-22 21:19:14 +01:00
|
|
|
YAP_UserCPredicate("md5", md5, 3);
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
2014-09-16 19:15:56 +01:00
|
|
|
int WINAPI win_sys(HANDLE, DWORD, LPVOID);
|
2001-05-21 21:08:10 +01:00
|
|
|
|
2001-05-28 20:54:53 +01:00
|
|
|
int WINAPI win_sys(HANDLE hinst, DWORD reason, LPVOID reserved)
|
2001-05-21 21:08:10 +01:00
|
|
|
{
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
break;
|
|
|
|
}
|
2001-05-28 20:54:53 +01:00
|
|
|
return 1;
|
2001-05-21 21:08:10 +01:00
|
|
|
}
|
|
|
|
#endif
|