#include "perl.h"
#define NO_XSLOCKS
+#define PERL_NO_GET_CONTEXT
#include "XSUB.h"
#include "Win32iop.h"
int _CRT_glob = 0;
#endif
+#ifdef __BORLANDC__
+# define _stat stat
+# define _utimbuf utimbuf
+#endif
+
#define EXECF_EXEC 1
#define EXECF_SPAWN 2
#define EXECF_SPAWN_NOWAIT 3
static void get_shell(void);
static long tokenize(char *str, char **dest, char ***destv);
- int do_spawn2(pTHX_ char *cmd, int exectype);
+ int do_spawn2(char *cmd, int exectype);
static BOOL has_shell_metachars(char *ptr);
static long filetime_to_clock(PFILETIME ft);
static BOOL filetime_from_time(PFILETIME ft, time_t t);
-static char * get_emd_part(SV *leading, char *trailing, ...);
+static char * get_emd_part(SV **leading, char *trailing, ...);
static void remove_dead_process(long deceased);
static long find_pid(int pid);
static char * qualified_path(const char *cmd);
return (win32_os_id() == VER_PLATFORM_WIN32_NT);
}
-/* sv (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
+/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
static char*
-get_regstr_from(HKEY hkey, const char *valuename, SV *sv)
+get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
{
/* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
HKEY handle;
DWORD datalen;
retval = RegQueryValueEx(handle, valuename, 0, &type, NULL, &datalen);
if (retval == ERROR_SUCCESS && type == REG_SZ) {
- dPERLOBJ;
- if (!sv)
- sv = sv_2mortal(newSVpvn("",0));
- SvGROW(sv, datalen);
+ dTHXo;
+ if (!*svp)
+ *svp = sv_2mortal(newSVpvn("",0));
+ SvGROW(*svp, datalen);
retval = RegQueryValueEx(handle, valuename, 0, NULL,
- (PBYTE)SvPVX(sv), &datalen);
+ (PBYTE)SvPVX(*svp), &datalen);
if (retval == ERROR_SUCCESS) {
- str = SvPVX(sv);
- SvCUR_set(sv,datalen-1);
+ str = SvPVX(*svp);
+ SvCUR_set(*svp,datalen-1);
}
}
RegCloseKey(handle);
return str;
}
-/* sv (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
+/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
static char*
-get_regstr(const char *valuename, SV *sv)
+get_regstr(const char *valuename, SV **svp)
{
- char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, sv);
+ char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
if (!str)
- str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, sv);
+ str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
return str;
}
-/* prev_path (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
+/* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
static char *
-get_emd_part(SV *prev_path, char *trailing_path, ...)
+get_emd_part(SV **prev_pathp, char *trailing_path, ...)
{
char base[10];
va_list ap;
/* only add directory if it exists */
if (GetFileAttributes(mod_name) != (DWORD) -1) {
/* directory exists */
- dPERLOBJ;
- if (!prev_path)
- prev_path = sv_2mortal(newSVpvn("",0));
- sv_catpvn(prev_path, ";", 1);
- sv_catpv(prev_path, mod_name);
- return SvPVX(prev_path);
+ dTHXo;
+ if (!*prev_pathp)
+ *prev_pathp = sv_2mortal(newSVpvn("",0));
+ sv_catpvn(*prev_pathp, ";", 1);
+ sv_catpv(*prev_pathp, mod_name);
+ return SvPVX(*prev_pathp);
}
return Nullch;
}
char *
-win32_get_privlib(pTHX_ char *pl)
+win32_get_privlib(char *pl)
{
- dPERLOBJ;
+ dTHXo;
char *stdlib = "lib";
char buffer[MAX_PATH+1];
SV *sv = Nullsv;
/* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || ""; */
sprintf(buffer, "%s-%s", stdlib, pl);
- if (!get_regstr(buffer, sv))
- (void)get_regstr(stdlib, sv);
+ if (!get_regstr(buffer, &sv))
+ (void)get_regstr(stdlib, &sv);
/* $stdlib .= ";$EMD/../../lib" */
- return get_emd_part(sv, stdlib, ARCHNAME, "bin", Nullch);
+ return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch);
}
char *
-win32_get_sitelib(pTHX_ char *pl)
+win32_get_sitelib(char *pl)
{
- dPERLOBJ;
+ dTHXo;
char *sitelib = "sitelib";
char regstr[40];
char pathstr[MAX_PATH+1];
/* $HKCU{"sitelib-$]"} || $HKLM{"sitelib-$]"} . ---; */
sprintf(regstr, "%s-%s", sitelib, pl);
- (void)get_regstr(regstr, sv1);
+ (void)get_regstr(regstr, &sv1);
/* $sitelib .=
* ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/$]/lib"; */
sprintf(pathstr, "site/%s/lib", pl);
- (void)get_emd_part(sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
+ (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
if (!sv1 && strlen(pl) == 7) {
/* pl may have been SUBVERSION-specific; try again without
* SUBVERSION */
sprintf(pathstr, "site/%.5s/lib", pl);
- (void)get_emd_part(sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
+ (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
}
/* $HKCU{'sitelib'} || $HKLM{'sitelib'} . ---; */
- (void)get_regstr(sitelib, sv2);
+ (void)get_regstr(sitelib, &sv2);
/* $sitelib .=
* ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/lib"; */
- (void)get_emd_part(sv2, "site/lib", ARCHNAME, "bin", pl, Nullch);
+ (void)get_emd_part(&sv2, "site/lib", ARCHNAME, "bin", pl, Nullch);
if (!sv1 && !sv2)
return Nullch;
char **retvstart = 0;
int items = -1;
if (str) {
- dPERLOBJ;
+ dTHXo;
int slen = strlen(str);
register char *ret;
register char **retv;
static void
get_shell(void)
{
- dPERLOBJ;
+ dTHXo;
if (!w32_perlshell_tokens) {
/* we don't use COMSPEC here for two reasons:
* 1. the same reason perl on UNIX doesn't use SHELL--rampant and
}
int
-do_aspawn(pTHX_ void *vreally, void **vmark, void **vsp)
+do_aspawn(void *vreally, void **vmark, void **vsp)
{
- dPERLOBJ;
+ dTHXo;
SV *really = (SV*)vreally;
SV **mark = (SV**)vmark;
SV **sp = (SV**)vsp;
}
int
-do_spawn2(pTHX_ char *cmd, int exectype)
+do_spawn2(char *cmd, int exectype)
{
- dPERLOBJ;
+ dTHXo;
char **a;
char *s;
char **argv;
}
int
-do_spawn(pTHX_ char *cmd)
+do_spawn(char *cmd)
{
- return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
+ return do_spawn2(cmd, EXECF_SPAWN);
}
int
-do_spawn_nowait(pTHX_ char *cmd)
+do_spawn_nowait(char *cmd)
{
- return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
+ return do_spawn2(cmd, EXECF_SPAWN_NOWAIT);
}
bool
Perl_do_exec(pTHX_ char *cmd)
{
- do_spawn2(aTHX_ cmd, EXECF_EXEC);
+ do_spawn2(cmd, EXECF_EXEC);
return FALSE;
}
* (separated by nulls) and when one of the other dir functions is called
* return the pointer to the current file name.
*/
-DIR *
+DllExport DIR *
win32_opendir(char *filename)
{
- dTHX;
- dPERLOBJ;
+ dTHXo;
DIR *p;
long len;
long idx;
? FindNextFileW(fh, &wFindData)
: FindNextFileA(fh, &aFindData)) {
if (USING_WIDE()) {
- W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer));
+ W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer));
}
/* ptr is set above to the correct area */
len = strlen(ptr);
/* Readdir just returns the current string pointer and bumps the
* string pointer to the nDllExport entry.
*/
-struct direct *
+DllExport struct direct *
win32_readdir(DIR *dirp)
{
int len;
}
/* Telldir returns the current string pointer position */
-long
+DllExport long
win32_telldir(DIR *dirp)
{
return (long) dirp->curr;
/* Seekdir moves the string pointer to a previously saved position
*(Saved by telldir).
*/
-void
+DllExport void
win32_seekdir(DIR *dirp, long loc)
{
dirp->curr = (char *)loc;
}
/* Rewinddir resets the string pointer to the start */
-void
+DllExport void
win32_rewinddir(DIR *dirp)
{
dirp->curr = dirp->start;
}
/* free the memory allocated by opendir */
-int
+DllExport int
win32_closedir(DIR *dirp)
{
- dPERLOBJ;
+ dTHXo;
Safefree(dirp->start);
Safefree(dirp);
return 1;
char *
getlogin(void)
{
- dTHX;
+ dTHXo;
char *buf = getlogin_buffer;
DWORD size = sizeof(getlogin_buffer);
if (GetUserName(buf,&size))
static long
find_pid(int pid)
{
- dPERLOBJ;
+ dTHXo;
long child;
for (child = 0 ; child < w32_num_children ; ++child) {
if (w32_child_pids[child] == pid)
remove_dead_process(long child)
{
if (child >= 0) {
- dPERLOBJ;
+ dTHXo;
CloseHandle(w32_child_handles[child]);
Copy(&w32_child_handles[child+1], &w32_child_handles[child],
(w32_num_children-child-1), HANDLE);
DllExport int
win32_stat(const char *path, struct stat *buffer)
{
- dPERLOBJ;
+ dTHXo;
char t[MAX_PATH+1];
int l = strlen(path);
int res;
}
}
if (USING_WIDE()) {
- dTHX;
A2WHELPER(path, wbuffer, sizeof(wbuffer));
res = _wstat(wbuffer, (struct _stat *)buffer);
}
DllExport char *
win32_getenv(const char *name)
{
- dTHX;
- dPERLOBJ;
+ dTHXo;
WCHAR wBuffer[MAX_PATH];
DWORD needlen;
SV *curitem = Nullsv;
(WCHAR*)SvPVX(curitem),
needlen);
} while (needlen >= SvLEN(curitem)/sizeof(WCHAR));
- SvCUR_set(curitem, needlen*sizeof(WCHAR));
+ SvCUR_set(curitem, (needlen*sizeof(WCHAR))+1);
acuritem = sv_2mortal(newSVsv(curitem));
W2AHELPER((WCHAR*)SvPVX(acuritem), SvPVX(curitem), SvCUR(curitem));
}
/* allow any environment variables that begin with 'PERL'
to be stored in the registry */
if (strncmp(name, "PERL", 4) == 0)
- (void)get_regstr(name, curitem);
+ (void)get_regstr(name, &curitem);
}
if (curitem && SvCUR(curitem))
return SvPVX(curitem);
DllExport int
win32_putenv(const char *name)
{
- dPERLOBJ;
+ dTHXo;
char* curitem;
char* val;
WCHAR* wCuritem;
if (name) {
if (USING_WIDE()) {
- dTHX;
length = strlen(name)+1;
New(1309,wCuritem,length,WCHAR);
- A2WHELPER(name, wCuritem, length*2);
+ A2WHELPER(name, wCuritem, length*sizeof(WCHAR));
wVal = wcschr(wCuritem, '=');
if(wVal) {
*wVal++ = '\0';
DllExport int
win32_utime(const char *filename, struct utimbuf *times)
{
- dPERLOBJ;
+ dTHXo;
HANDLE handle;
FILETIME ftCreate;
FILETIME ftAccess;
int rc;
if (USING_WIDE()) {
- dTHX;
A2WHELPER(filename, wbuffer, sizeof(wbuffer));
rc = _wutime(wbuffer, (struct _utimbuf*)times);
}
DllExport int
win32_waitpid(int pid, int *status, int flags)
{
- dPERLOBJ;
+ dTHXo;
int retval = -1;
if (pid == -1)
return win32_wait(status);
/* XXX this wait emulation only knows about processes
* spawned via win32_spawnvp(P_NOWAIT, ...).
*/
- dPERLOBJ;
+ dTHXo;
int i, retval;
DWORD exitcode, waitcode;
static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
{
- dPERLOBJ;
+ dTHXo;
KillTimer(NULL,timerid);
timerid=0;
sighandler(14);
* Snag is unless something is looking at the message queue
* nothing happens :-(
*/
- dPERLOBJ;
+ dTHXo;
if (sec)
{
timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
DllExport char *
win32_crypt(const char *txt, const char *salt)
{
+ dTHXo;
#ifdef HAVE_DES_FCRYPT
dTHR;
- dPERLOBJ;
return des_fcrypt(txt, salt, crypt_buffer);
#else
die("The crypt() function is unimplemented due to excessive paranoia.");
HANDLE fh;
if (!IsWinNT()) {
- dPERLOBJ;
+ dTHXo;
Perl_croak_nocontext("flock() unimplemented on this platform");
return -1;
}
DWORD source = 0;
if (e < 0 || e > sys_nerr) {
- dTHX;
+ dTHXo;
if (e < 0)
e = GetLastError();
}
DllExport void
-win32_str_os_error(pTHX_ void *sv, DWORD dwErr)
+win32_str_os_error(void *sv, DWORD dwErr)
{
DWORD dwLen;
char *sMsg;
|FORMAT_MESSAGE_IGNORE_INSERTS
|FORMAT_MESSAGE_FROM_SYSTEM, NULL,
dwErr, 0, (char *)&sMsg, 1, NULL);
+ /* strip trailing whitespace and period */
if (0 < dwLen) {
- while (0 < dwLen && isSPACE(sMsg[--dwLen]))
- ;
+ do {
+ --dwLen; /* dwLen doesn't include trailing null */
+ } while (0 < dwLen && isSPACE(sMsg[dwLen]));
if ('.' != sMsg[dwLen])
dwLen++;
- sMsg[dwLen]= '\0';
+ sMsg[dwLen] = '\0';
}
if (0 == dwLen) {
sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
dwErr, GetLastError());
}
if (sMsg) {
- dPERLOBJ;
+ dTHXo;
sv_setpvn((SV*)sv, sMsg, dwLen);
LocalFree(sMsg);
}
DllExport FILE *
win32_fopen(const char *filename, const char *mode)
{
- dPERLOBJ;
+ dTHXo;
WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH];
+
+ if (!*filename)
+ return NULL;
+
if (stricmp(filename, "/dev/null")==0)
filename = "NUL";
if (USING_WIDE()) {
- dTHX;
A2WHELPER(mode, wMode, sizeof(wMode));
A2WHELPER(filename, wBuffer, sizeof(wBuffer));
return _wfopen(wBuffer, wMode);
DllExport FILE *
win32_fdopen(int handle, const char *mode)
{
- dPERLOBJ;
+ dTHXo;
WCHAR wMode[MODE_SIZE];
if (USING_WIDE()) {
- dTHX;
A2WHELPER(mode, wMode, sizeof(wMode));
return _wfdopen(handle, wMode);
}
DllExport FILE *
win32_freopen(const char *path, const char *mode, FILE *stream)
{
- dPERLOBJ;
+ dTHXo;
WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH];
if (stricmp(path, "/dev/null")==0)
path = "NUL";
if (USING_WIDE()) {
- dTHX;
A2WHELPER(mode, wMode, sizeof(wMode));
A2WHELPER(path, wBuffer, sizeof(wBuffer));
return _wfreopen(wBuffer, wMode, stream);
/* start the child */
{
- dTHX;
- dPERLOBJ;
- if ((childpid = do_spawn_nowait(aTHX_ (char*)command)) == -1)
+ dTHXo;
+ if ((childpid = do_spawn_nowait((char*)command)) == -1)
goto cleanup;
/* revert stdfd to whatever it was before */
#ifdef USE_RTL_POPEN
return _pclose(pf);
#else
- dTHX;
- dPERLOBJ;
+ dTHXo;
int childpid, status;
SV *sv;
* it doesn't work under Windows95!
*/
if (IsWinNT()) {
- dPERLOBJ;
+ dTHXo;
if (USING_WIDE()) {
- dTHX;
A2WHELPER(oname, wOldName, sizeof(wOldName));
A2WHELPER(newname, wNewName, sizeof(wNewName));
bResult = MoveFileExW(wOldName,wNewName,
DllExport int
win32_open(const char *path, int flag, ...)
{
- dPERLOBJ;
+ dTHXo;
va_list ap;
int pmode;
WCHAR wBuffer[MAX_PATH];
path = "NUL";
if (USING_WIDE()) {
- dTHX;
A2WHELPER(path, wBuffer, sizeof(wBuffer));
return _wopen(wBuffer, flag, pmode);
}
static char *
create_command_line(const char* command, const char * const *args)
{
- dPERLOBJ;
+ dTHXo;
int index;
char *cmd, *ptr, *arg;
STRLEN len = strlen(command) + 1;
static char *
qualified_path(const char *cmd)
{
- dPERLOBJ;
+ dTHXo;
char *pathstr;
char *fullcmd, *curfullcmd;
STRLEN cmdlen = 0;
return Nullch;
}
+/* The following are just place holders.
+ * Some hosts may provide and environment that the OS is
+ * not tracking, therefore, these host must provide that
+ * environment and the current directory to CreateProcess
+ */
+
+void*
+get_childenv(void)
+{
+ return NULL;
+}
+
+void
+free_childenv(void* d)
+{
+}
+
+char*
+get_childdir(void)
+{
+ return NULL;
+}
+
+void
+free_childdir(char* d)
+{
+}
+
+
/* XXX this needs to be made more compatible with the spawnvp()
* provided by the various RTLs. In particular, searching for
* *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
#ifdef USE_RTL_SPAWNVP
return spawnvp(mode, cmdname, (char * const *)argv);
#else
- dPERLOBJ;
+ dTHXo;
DWORD ret;
+ void* env;
+ char* dir;
STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInformation;
DWORD create = 0;
? &argv[1] : argv);
char *fullcmd = Nullch;
+ env = PerlEnv_get_childenv();
+ dir = PerlEnv_get_childdir();
+
switch(mode) {
case P_NOWAIT: /* asynch + remember result */
if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
create |= CREATE_NEW_CONSOLE;
}
-#ifndef DEBUGGING
- StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
- StartupInfo.wShowWindow = SW_HIDE;
-#endif
-
RETRY:
if (!CreateProcess(cmdname, /* search PATH to find executable */
cmd, /* executable, and its arguments */
NULL, /* thread attributes */
TRUE, /* inherit handles */
create, /* creation flags */
- NULL, /* inherit environment */
- NULL, /* inherit cwd */
+ (LPVOID)env, /* inherit environment */
+ dir, /* inherit cwd */
&StartupInfo,
&ProcessInformation))
{
}
CloseHandle(ProcessInformation.hThread);
+
RETVAL:
+ PerlEnv_free_childenv(env);
+ PerlEnv_free_childdir(dir);
Safefree(cmd);
Safefree(fullcmd);
return (int)ret;
}
DllExport void*
-win32_dynaload(aTHX_ const char*filename)
+win32_dynaload(const char* filename)
{
- dPERLOBJ;
+ dTHXo;
HMODULE hModule;
if (USING_WIDE()) {
WCHAR wfilename[MAX_PATH];
* This must be called before the script is parsed,
* therefore no locking of threads is needed
*/
- dTHX;
- dPERLOBJ;
+ dTHXo;
struct host_link *link;
New(1314, link, 1, struct host_link);
link->host_data = data;
DllExport void *
win32_get_host_data(char *nameId)
{
- dTHX;
- dPERLOBJ;
+ dTHXo;
struct host_link *link = w32_host_link;
while(link) {
if(strEQ(link->nameId, nameId))
}
void
-Perl_init_os_extras(pTHX)
+Perl_init_os_extras(void)
{
- dPERLOBJ;
+ dTHXo;
char *file = __FILE__;
dXSUB_SYS;