Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2370 lines
52 KiB

  1. /* perlhost.h
  2. *
  3. * (c) 1999 Microsoft Corporation. All rights reserved.
  4. * Portions (c) 1999 ActiveState Tool Corp, http://www.ActiveState.com/
  5. *
  6. * You may distribute under the terms of either the GNU General Public
  7. * License or the Artistic License, as specified in the README file.
  8. */
  9. #ifndef ___PerlHost_H___
  10. #define ___PerlHost_H___
  11. #include <signal.h>
  12. #include "iperlsys.h"
  13. #include "vmem.h"
  14. #include "vdir.h"
  15. #if !defined(PERL_OBJECT)
  16. START_EXTERN_C
  17. #endif
  18. extern char * g_win32_get_privlib(const char *pl);
  19. extern char * g_win32_get_sitelib(const char *pl);
  20. extern char * g_win32_get_vendorlib(const char *pl);
  21. extern char * g_getlogin(void);
  22. #if !defined(PERL_OBJECT)
  23. END_EXTERN_C
  24. #endif
  25. class CPerlHost
  26. {
  27. public:
  28. CPerlHost(void);
  29. CPerlHost(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
  30. struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
  31. struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
  32. struct IPerlDir** ppDir, struct IPerlSock** ppSock,
  33. struct IPerlProc** ppProc);
  34. CPerlHost(CPerlHost& host);
  35. ~CPerlHost(void);
  36. static CPerlHost* IPerlMem2Host(struct IPerlMem* piPerl);
  37. static CPerlHost* IPerlMemShared2Host(struct IPerlMem* piPerl);
  38. static CPerlHost* IPerlMemParse2Host(struct IPerlMem* piPerl);
  39. static CPerlHost* IPerlEnv2Host(struct IPerlEnv* piPerl);
  40. static CPerlHost* IPerlStdIO2Host(struct IPerlStdIO* piPerl);
  41. static CPerlHost* IPerlLIO2Host(struct IPerlLIO* piPerl);
  42. static CPerlHost* IPerlDir2Host(struct IPerlDir* piPerl);
  43. static CPerlHost* IPerlSock2Host(struct IPerlSock* piPerl);
  44. static CPerlHost* IPerlProc2Host(struct IPerlProc* piPerl);
  45. BOOL PerlCreate(void);
  46. int PerlParse(int argc, char** argv, char** env);
  47. int PerlRun(void);
  48. void PerlDestroy(void);
  49. /* IPerlMem */
  50. inline void* Malloc(size_t size) { return m_pVMem->Malloc(size); };
  51. inline void* Realloc(void* ptr, size_t size) { return m_pVMem->Realloc(ptr, size); };
  52. inline void Free(void* ptr) { m_pVMem->Free(ptr); };
  53. inline void* Calloc(size_t num, size_t size)
  54. {
  55. size_t count = num*size;
  56. void* lpVoid = Malloc(count);
  57. if (lpVoid)
  58. ZeroMemory(lpVoid, count);
  59. return lpVoid;
  60. };
  61. inline void GetLock(void) { m_pVMem->GetLock(); };
  62. inline void FreeLock(void) { m_pVMem->FreeLock(); };
  63. inline int IsLocked(void) { return m_pVMem->IsLocked(); };
  64. /* IPerlMemShared */
  65. inline void* MallocShared(size_t size)
  66. {
  67. return m_pVMemShared->Malloc(size);
  68. };
  69. inline void* ReallocShared(void* ptr, size_t size) { return m_pVMemShared->Realloc(ptr, size); };
  70. inline void FreeShared(void* ptr) { m_pVMemShared->Free(ptr); };
  71. inline void* CallocShared(size_t num, size_t size)
  72. {
  73. size_t count = num*size;
  74. void* lpVoid = MallocShared(count);
  75. if (lpVoid)
  76. ZeroMemory(lpVoid, count);
  77. return lpVoid;
  78. };
  79. inline void GetLockShared(void) { m_pVMem->GetLock(); };
  80. inline void FreeLockShared(void) { m_pVMem->FreeLock(); };
  81. inline int IsLockedShared(void) { return m_pVMem->IsLocked(); };
  82. /* IPerlMemParse */
  83. inline void* MallocParse(size_t size) { return m_pVMemParse->Malloc(size); };
  84. inline void* ReallocParse(void* ptr, size_t size) { return m_pVMemParse->Realloc(ptr, size); };
  85. inline void FreeParse(void* ptr) { m_pVMemParse->Free(ptr); };
  86. inline void* CallocParse(size_t num, size_t size)
  87. {
  88. size_t count = num*size;
  89. void* lpVoid = MallocParse(count);
  90. if (lpVoid)
  91. ZeroMemory(lpVoid, count);
  92. return lpVoid;
  93. };
  94. inline void GetLockParse(void) { m_pVMem->GetLock(); };
  95. inline void FreeLockParse(void) { m_pVMem->FreeLock(); };
  96. inline int IsLockedParse(void) { return m_pVMem->IsLocked(); };
  97. /* IPerlEnv */
  98. char *Getenv(const char *varname);
  99. int Putenv(const char *envstring);
  100. inline char *Getenv(const char *varname, unsigned long *len)
  101. {
  102. *len = 0;
  103. char *e = Getenv(varname);
  104. if (e)
  105. *len = strlen(e);
  106. return e;
  107. }
  108. void* CreateChildEnv(void) { return CreateLocalEnvironmentStrings(*m_pvDir); };
  109. void FreeChildEnv(void* pStr) { FreeLocalEnvironmentStrings((char*)pStr); };
  110. char* GetChildDir(void);
  111. void FreeChildDir(char* pStr);
  112. void Reset(void);
  113. void Clearenv(void);
  114. inline LPSTR GetIndex(DWORD &dwIndex)
  115. {
  116. if(dwIndex < m_dwEnvCount)
  117. {
  118. ++dwIndex;
  119. return m_lppEnvList[dwIndex-1];
  120. }
  121. return NULL;
  122. };
  123. protected:
  124. LPSTR Find(LPCSTR lpStr);
  125. void Add(LPCSTR lpStr);
  126. LPSTR CreateLocalEnvironmentStrings(VDir &vDir);
  127. void FreeLocalEnvironmentStrings(LPSTR lpStr);
  128. LPSTR* Lookup(LPCSTR lpStr);
  129. DWORD CalculateEnvironmentSpace(void);
  130. public:
  131. /* IPerlDIR */
  132. virtual int Chdir(const char *dirname);
  133. /* IPerllProc */
  134. void Abort(void);
  135. void Exit(int status);
  136. void _Exit(int status);
  137. int Execl(const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3);
  138. int Execv(const char *cmdname, const char *const *argv);
  139. int Execvp(const char *cmdname, const char *const *argv);
  140. inline VMem* GetMemShared(void) { m_pVMemShared->AddRef(); return m_pVMemShared; };
  141. inline VMem* GetMemParse(void) { m_pVMemParse->AddRef(); return m_pVMemParse; };
  142. inline VDir* GetDir(void) { return m_pvDir; };
  143. public:
  144. struct IPerlMem m_hostperlMem;
  145. struct IPerlMem m_hostperlMemShared;
  146. struct IPerlMem m_hostperlMemParse;
  147. struct IPerlEnv m_hostperlEnv;
  148. struct IPerlStdIO m_hostperlStdIO;
  149. struct IPerlLIO m_hostperlLIO;
  150. struct IPerlDir m_hostperlDir;
  151. struct IPerlSock m_hostperlSock;
  152. struct IPerlProc m_hostperlProc;
  153. struct IPerlMem* m_pHostperlMem;
  154. struct IPerlMem* m_pHostperlMemShared;
  155. struct IPerlMem* m_pHostperlMemParse;
  156. struct IPerlEnv* m_pHostperlEnv;
  157. struct IPerlStdIO* m_pHostperlStdIO;
  158. struct IPerlLIO* m_pHostperlLIO;
  159. struct IPerlDir* m_pHostperlDir;
  160. struct IPerlSock* m_pHostperlSock;
  161. struct IPerlProc* m_pHostperlProc;
  162. inline char* MapPathA(const char *pInName) { return m_pvDir->MapPathA(pInName); };
  163. inline WCHAR* MapPathW(const WCHAR *pInName) { return m_pvDir->MapPathW(pInName); };
  164. protected:
  165. VDir* m_pvDir;
  166. VMem* m_pVMem;
  167. VMem* m_pVMemShared;
  168. VMem* m_pVMemParse;
  169. DWORD m_dwEnvCount;
  170. LPSTR* m_lppEnvList;
  171. };
  172. #define STRUCT2PTR(x, y) (CPerlHost*)(((LPBYTE)x)-offsetof(CPerlHost, y))
  173. inline CPerlHost* IPerlMem2Host(struct IPerlMem* piPerl)
  174. {
  175. return STRUCT2PTR(piPerl, m_hostperlMem);
  176. }
  177. inline CPerlHost* IPerlMemShared2Host(struct IPerlMem* piPerl)
  178. {
  179. return STRUCT2PTR(piPerl, m_hostperlMemShared);
  180. }
  181. inline CPerlHost* IPerlMemParse2Host(struct IPerlMem* piPerl)
  182. {
  183. return STRUCT2PTR(piPerl, m_hostperlMemParse);
  184. }
  185. inline CPerlHost* IPerlEnv2Host(struct IPerlEnv* piPerl)
  186. {
  187. return STRUCT2PTR(piPerl, m_hostperlEnv);
  188. }
  189. inline CPerlHost* IPerlStdIO2Host(struct IPerlStdIO* piPerl)
  190. {
  191. return STRUCT2PTR(piPerl, m_hostperlStdIO);
  192. }
  193. inline CPerlHost* IPerlLIO2Host(struct IPerlLIO* piPerl)
  194. {
  195. return STRUCT2PTR(piPerl, m_hostperlLIO);
  196. }
  197. inline CPerlHost* IPerlDir2Host(struct IPerlDir* piPerl)
  198. {
  199. return STRUCT2PTR(piPerl, m_hostperlDir);
  200. }
  201. inline CPerlHost* IPerlSock2Host(struct IPerlSock* piPerl)
  202. {
  203. return STRUCT2PTR(piPerl, m_hostperlSock);
  204. }
  205. inline CPerlHost* IPerlProc2Host(struct IPerlProc* piPerl)
  206. {
  207. return STRUCT2PTR(piPerl, m_hostperlProc);
  208. }
  209. #undef IPERL2HOST
  210. #define IPERL2HOST(x) IPerlMem2Host(x)
  211. /* IPerlMem */
  212. void*
  213. PerlMemMalloc(struct IPerlMem* piPerl, size_t size)
  214. {
  215. return IPERL2HOST(piPerl)->Malloc(size);
  216. }
  217. void*
  218. PerlMemRealloc(struct IPerlMem* piPerl, void* ptr, size_t size)
  219. {
  220. return IPERL2HOST(piPerl)->Realloc(ptr, size);
  221. }
  222. void
  223. PerlMemFree(struct IPerlMem* piPerl, void* ptr)
  224. {
  225. IPERL2HOST(piPerl)->Free(ptr);
  226. }
  227. void*
  228. PerlMemCalloc(struct IPerlMem* piPerl, size_t num, size_t size)
  229. {
  230. return IPERL2HOST(piPerl)->Calloc(num, size);
  231. }
  232. void
  233. PerlMemGetLock(struct IPerlMem* piPerl)
  234. {
  235. IPERL2HOST(piPerl)->GetLock();
  236. }
  237. void
  238. PerlMemFreeLock(struct IPerlMem* piPerl)
  239. {
  240. IPERL2HOST(piPerl)->FreeLock();
  241. }
  242. int
  243. PerlMemIsLocked(struct IPerlMem* piPerl)
  244. {
  245. return IPERL2HOST(piPerl)->IsLocked();
  246. }
  247. struct IPerlMem perlMem =
  248. {
  249. PerlMemMalloc,
  250. PerlMemRealloc,
  251. PerlMemFree,
  252. PerlMemCalloc,
  253. PerlMemGetLock,
  254. PerlMemFreeLock,
  255. PerlMemIsLocked,
  256. };
  257. #undef IPERL2HOST
  258. #define IPERL2HOST(x) IPerlMemShared2Host(x)
  259. /* IPerlMemShared */
  260. void*
  261. PerlMemSharedMalloc(struct IPerlMem* piPerl, size_t size)
  262. {
  263. return IPERL2HOST(piPerl)->MallocShared(size);
  264. }
  265. void*
  266. PerlMemSharedRealloc(struct IPerlMem* piPerl, void* ptr, size_t size)
  267. {
  268. return IPERL2HOST(piPerl)->ReallocShared(ptr, size);
  269. }
  270. void
  271. PerlMemSharedFree(struct IPerlMem* piPerl, void* ptr)
  272. {
  273. IPERL2HOST(piPerl)->FreeShared(ptr);
  274. }
  275. void*
  276. PerlMemSharedCalloc(struct IPerlMem* piPerl, size_t num, size_t size)
  277. {
  278. return IPERL2HOST(piPerl)->CallocShared(num, size);
  279. }
  280. void
  281. PerlMemSharedGetLock(struct IPerlMem* piPerl)
  282. {
  283. IPERL2HOST(piPerl)->GetLockShared();
  284. }
  285. void
  286. PerlMemSharedFreeLock(struct IPerlMem* piPerl)
  287. {
  288. IPERL2HOST(piPerl)->FreeLockShared();
  289. }
  290. int
  291. PerlMemSharedIsLocked(struct IPerlMem* piPerl)
  292. {
  293. return IPERL2HOST(piPerl)->IsLockedShared();
  294. }
  295. struct IPerlMem perlMemShared =
  296. {
  297. PerlMemSharedMalloc,
  298. PerlMemSharedRealloc,
  299. PerlMemSharedFree,
  300. PerlMemSharedCalloc,
  301. PerlMemSharedGetLock,
  302. PerlMemSharedFreeLock,
  303. PerlMemSharedIsLocked,
  304. };
  305. #undef IPERL2HOST
  306. #define IPERL2HOST(x) IPerlMemParse2Host(x)
  307. /* IPerlMemParse */
  308. void*
  309. PerlMemParseMalloc(struct IPerlMem* piPerl, size_t size)
  310. {
  311. return IPERL2HOST(piPerl)->MallocParse(size);
  312. }
  313. void*
  314. PerlMemParseRealloc(struct IPerlMem* piPerl, void* ptr, size_t size)
  315. {
  316. return IPERL2HOST(piPerl)->ReallocParse(ptr, size);
  317. }
  318. void
  319. PerlMemParseFree(struct IPerlMem* piPerl, void* ptr)
  320. {
  321. IPERL2HOST(piPerl)->FreeParse(ptr);
  322. }
  323. void*
  324. PerlMemParseCalloc(struct IPerlMem* piPerl, size_t num, size_t size)
  325. {
  326. return IPERL2HOST(piPerl)->CallocParse(num, size);
  327. }
  328. void
  329. PerlMemParseGetLock(struct IPerlMem* piPerl)
  330. {
  331. IPERL2HOST(piPerl)->GetLockParse();
  332. }
  333. void
  334. PerlMemParseFreeLock(struct IPerlMem* piPerl)
  335. {
  336. IPERL2HOST(piPerl)->FreeLockParse();
  337. }
  338. int
  339. PerlMemParseIsLocked(struct IPerlMem* piPerl)
  340. {
  341. return IPERL2HOST(piPerl)->IsLockedParse();
  342. }
  343. struct IPerlMem perlMemParse =
  344. {
  345. PerlMemParseMalloc,
  346. PerlMemParseRealloc,
  347. PerlMemParseFree,
  348. PerlMemParseCalloc,
  349. PerlMemParseGetLock,
  350. PerlMemParseFreeLock,
  351. PerlMemParseIsLocked,
  352. };
  353. #undef IPERL2HOST
  354. #define IPERL2HOST(x) IPerlEnv2Host(x)
  355. /* IPerlEnv */
  356. char*
  357. PerlEnvGetenv(struct IPerlEnv* piPerl, const char *varname)
  358. {
  359. return IPERL2HOST(piPerl)->Getenv(varname);
  360. };
  361. int
  362. PerlEnvPutenv(struct IPerlEnv* piPerl, const char *envstring)
  363. {
  364. return IPERL2HOST(piPerl)->Putenv(envstring);
  365. };
  366. char*
  367. PerlEnvGetenv_len(struct IPerlEnv* piPerl, const char* varname, unsigned long* len)
  368. {
  369. return IPERL2HOST(piPerl)->Getenv(varname, len);
  370. }
  371. int
  372. PerlEnvUname(struct IPerlEnv* piPerl, struct utsname *name)
  373. {
  374. return win32_uname(name);
  375. }
  376. void
  377. PerlEnvClearenv(struct IPerlEnv* piPerl)
  378. {
  379. IPERL2HOST(piPerl)->Clearenv();
  380. }
  381. void*
  382. PerlEnvGetChildenv(struct IPerlEnv* piPerl)
  383. {
  384. return IPERL2HOST(piPerl)->CreateChildEnv();
  385. }
  386. void
  387. PerlEnvFreeChildenv(struct IPerlEnv* piPerl, void* childEnv)
  388. {
  389. IPERL2HOST(piPerl)->FreeChildEnv(childEnv);
  390. }
  391. char*
  392. PerlEnvGetChilddir(struct IPerlEnv* piPerl)
  393. {
  394. return IPERL2HOST(piPerl)->GetChildDir();
  395. }
  396. void
  397. PerlEnvFreeChilddir(struct IPerlEnv* piPerl, char* childDir)
  398. {
  399. IPERL2HOST(piPerl)->FreeChildDir(childDir);
  400. }
  401. unsigned long
  402. PerlEnvOsId(struct IPerlEnv* piPerl)
  403. {
  404. return win32_os_id();
  405. }
  406. char*
  407. PerlEnvLibPath(struct IPerlEnv* piPerl, const char *pl)
  408. {
  409. return g_win32_get_privlib(pl);
  410. }
  411. char*
  412. PerlEnvSiteLibPath(struct IPerlEnv* piPerl, const char *pl)
  413. {
  414. return g_win32_get_sitelib(pl);
  415. }
  416. char*
  417. PerlEnvVendorLibPath(struct IPerlEnv* piPerl, const char *pl)
  418. {
  419. return g_win32_get_vendorlib(pl);
  420. }
  421. void
  422. PerlEnvGetChildIO(struct IPerlEnv* piPerl, child_IO_table* ptr)
  423. {
  424. win32_get_child_IO(ptr);
  425. }
  426. struct IPerlEnv perlEnv =
  427. {
  428. PerlEnvGetenv,
  429. PerlEnvPutenv,
  430. PerlEnvGetenv_len,
  431. PerlEnvUname,
  432. PerlEnvClearenv,
  433. PerlEnvGetChildenv,
  434. PerlEnvFreeChildenv,
  435. PerlEnvGetChilddir,
  436. PerlEnvFreeChilddir,
  437. PerlEnvOsId,
  438. PerlEnvLibPath,
  439. PerlEnvSiteLibPath,
  440. PerlEnvVendorLibPath,
  441. PerlEnvGetChildIO,
  442. };
  443. #undef IPERL2HOST
  444. #define IPERL2HOST(x) IPerlStdIO2Host(x)
  445. /* PerlStdIO */
  446. PerlIO*
  447. PerlStdIOStdin(struct IPerlStdIO* piPerl)
  448. {
  449. return (PerlIO*)win32_stdin();
  450. }
  451. PerlIO*
  452. PerlStdIOStdout(struct IPerlStdIO* piPerl)
  453. {
  454. return (PerlIO*)win32_stdout();
  455. }
  456. PerlIO*
  457. PerlStdIOStderr(struct IPerlStdIO* piPerl)
  458. {
  459. return (PerlIO*)win32_stderr();
  460. }
  461. PerlIO*
  462. PerlStdIOOpen(struct IPerlStdIO* piPerl, const char *path, const char *mode)
  463. {
  464. return (PerlIO*)win32_fopen(path, mode);
  465. }
  466. int
  467. PerlStdIOClose(struct IPerlStdIO* piPerl, PerlIO* pf)
  468. {
  469. return win32_fclose(((FILE*)pf));
  470. }
  471. int
  472. PerlStdIOEof(struct IPerlStdIO* piPerl, PerlIO* pf)
  473. {
  474. return win32_feof((FILE*)pf);
  475. }
  476. int
  477. PerlStdIOError(struct IPerlStdIO* piPerl, PerlIO* pf)
  478. {
  479. return win32_ferror((FILE*)pf);
  480. }
  481. void
  482. PerlStdIOClearerr(struct IPerlStdIO* piPerl, PerlIO* pf)
  483. {
  484. win32_clearerr((FILE*)pf);
  485. }
  486. int
  487. PerlStdIOGetc(struct IPerlStdIO* piPerl, PerlIO* pf)
  488. {
  489. return win32_getc((FILE*)pf);
  490. }
  491. char*
  492. PerlStdIOGetBase(struct IPerlStdIO* piPerl, PerlIO* pf)
  493. {
  494. #ifdef FILE_base
  495. FILE *f = (FILE*)pf;
  496. return FILE_base(f);
  497. #else
  498. return Nullch;
  499. #endif
  500. }
  501. int
  502. PerlStdIOGetBufsiz(struct IPerlStdIO* piPerl, PerlIO* pf)
  503. {
  504. #ifdef FILE_bufsiz
  505. FILE *f = (FILE*)pf;
  506. return FILE_bufsiz(f);
  507. #else
  508. return (-1);
  509. #endif
  510. }
  511. int
  512. PerlStdIOGetCnt(struct IPerlStdIO* piPerl, PerlIO* pf)
  513. {
  514. #ifdef USE_STDIO_PTR
  515. FILE *f = (FILE*)pf;
  516. return FILE_cnt(f);
  517. #else
  518. return (-1);
  519. #endif
  520. }
  521. char*
  522. PerlStdIOGetPtr(struct IPerlStdIO* piPerl, PerlIO* pf)
  523. {
  524. #ifdef USE_STDIO_PTR
  525. FILE *f = (FILE*)pf;
  526. return FILE_ptr(f);
  527. #else
  528. return Nullch;
  529. #endif
  530. }
  531. char*
  532. PerlStdIOGets(struct IPerlStdIO* piPerl, PerlIO* pf, char* s, int n)
  533. {
  534. return win32_fgets(s, n, (FILE*)pf);
  535. }
  536. int
  537. PerlStdIOPutc(struct IPerlStdIO* piPerl, PerlIO* pf, int c)
  538. {
  539. return win32_fputc(c, (FILE*)pf);
  540. }
  541. int
  542. PerlStdIOPuts(struct IPerlStdIO* piPerl, PerlIO* pf, const char *s)
  543. {
  544. return win32_fputs(s, (FILE*)pf);
  545. }
  546. int
  547. PerlStdIOFlush(struct IPerlStdIO* piPerl, PerlIO* pf)
  548. {
  549. return win32_fflush((FILE*)pf);
  550. }
  551. int
  552. PerlStdIOUngetc(struct IPerlStdIO* piPerl, PerlIO* pf,int c)
  553. {
  554. return win32_ungetc(c, (FILE*)pf);
  555. }
  556. int
  557. PerlStdIOFileno(struct IPerlStdIO* piPerl, PerlIO* pf)
  558. {
  559. return win32_fileno((FILE*)pf);
  560. }
  561. PerlIO*
  562. PerlStdIOFdopen(struct IPerlStdIO* piPerl, int fd, const char *mode)
  563. {
  564. return (PerlIO*)win32_fdopen(fd, mode);
  565. }
  566. PerlIO*
  567. PerlStdIOReopen(struct IPerlStdIO* piPerl, const char*path, const char*mode, PerlIO* pf)
  568. {
  569. return (PerlIO*)win32_freopen(path, mode, (FILE*)pf);
  570. }
  571. SSize_t
  572. PerlStdIORead(struct IPerlStdIO* piPerl, PerlIO* pf, void *buffer, Size_t size)
  573. {
  574. return win32_fread(buffer, 1, size, (FILE*)pf);
  575. }
  576. SSize_t
  577. PerlStdIOWrite(struct IPerlStdIO* piPerl, PerlIO* pf, const void *buffer, Size_t size)
  578. {
  579. return win32_fwrite(buffer, 1, size, (FILE*)pf);
  580. }
  581. void
  582. PerlStdIOSetBuf(struct IPerlStdIO* piPerl, PerlIO* pf, char* buffer)
  583. {
  584. win32_setbuf((FILE*)pf, buffer);
  585. }
  586. int
  587. PerlStdIOSetVBuf(struct IPerlStdIO* piPerl, PerlIO* pf, char* buffer, int type, Size_t size)
  588. {
  589. return win32_setvbuf((FILE*)pf, buffer, type, size);
  590. }
  591. void
  592. PerlStdIOSetCnt(struct IPerlStdIO* piPerl, PerlIO* pf, int n)
  593. {
  594. #ifdef STDIO_CNT_LVALUE
  595. FILE *f = (FILE*)pf;
  596. FILE_cnt(f) = n;
  597. #endif
  598. }
  599. void
  600. PerlStdIOSetPtrCnt(struct IPerlStdIO* piPerl, PerlIO* pf, char * ptr, int n)
  601. {
  602. #ifdef STDIO_PTR_LVALUE
  603. FILE *f = (FILE*)pf;
  604. FILE_ptr(f) = ptr;
  605. FILE_cnt(f) = n;
  606. #endif
  607. }
  608. void
  609. PerlStdIOSetlinebuf(struct IPerlStdIO* piPerl, PerlIO* pf)
  610. {
  611. win32_setvbuf((FILE*)pf, NULL, _IOLBF, 0);
  612. }
  613. int
  614. PerlStdIOPrintf(struct IPerlStdIO* piPerl, PerlIO* pf, const char *format,...)
  615. {
  616. va_list(arglist);
  617. va_start(arglist, format);
  618. return win32_vfprintf((FILE*)pf, format, arglist);
  619. }
  620. int
  621. PerlStdIOVprintf(struct IPerlStdIO* piPerl, PerlIO* pf, const char *format, va_list arglist)
  622. {
  623. return win32_vfprintf((FILE*)pf, format, arglist);
  624. }
  625. long
  626. PerlStdIOTell(struct IPerlStdIO* piPerl, PerlIO* pf)
  627. {
  628. return win32_ftell((FILE*)pf);
  629. }
  630. int
  631. PerlStdIOSeek(struct IPerlStdIO* piPerl, PerlIO* pf, off_t offset, int origin)
  632. {
  633. return win32_fseek((FILE*)pf, offset, origin);
  634. }
  635. void
  636. PerlStdIORewind(struct IPerlStdIO* piPerl, PerlIO* pf)
  637. {
  638. win32_rewind((FILE*)pf);
  639. }
  640. PerlIO*
  641. PerlStdIOTmpfile(struct IPerlStdIO* piPerl)
  642. {
  643. return (PerlIO*)win32_tmpfile();
  644. }
  645. int
  646. PerlStdIOGetpos(struct IPerlStdIO* piPerl, PerlIO* pf, Fpos_t *p)
  647. {
  648. return win32_fgetpos((FILE*)pf, p);
  649. }
  650. int
  651. PerlStdIOSetpos(struct IPerlStdIO* piPerl, PerlIO* pf, const Fpos_t *p)
  652. {
  653. return win32_fsetpos((FILE*)pf, p);
  654. }
  655. void
  656. PerlStdIOInit(struct IPerlStdIO* piPerl)
  657. {
  658. }
  659. void
  660. PerlStdIOInitOSExtras(struct IPerlStdIO* piPerl)
  661. {
  662. Perl_init_os_extras();
  663. }
  664. int
  665. PerlStdIOOpenOSfhandle(struct IPerlStdIO* piPerl, long osfhandle, int flags)
  666. {
  667. return win32_open_osfhandle(osfhandle, flags);
  668. }
  669. int
  670. PerlStdIOGetOSfhandle(struct IPerlStdIO* piPerl, int filenum)
  671. {
  672. return win32_get_osfhandle(filenum);
  673. }
  674. PerlIO*
  675. PerlStdIOFdupopen(struct IPerlStdIO* piPerl, PerlIO* pf)
  676. {
  677. PerlIO* pfdup;
  678. fpos_t pos;
  679. char mode[3];
  680. int fileno = win32_dup(win32_fileno((FILE*)pf));
  681. /* open the file in the same mode */
  682. #ifdef __BORLANDC__
  683. if(((FILE*)pf)->flags & _F_READ) {
  684. mode[0] = 'r';
  685. mode[1] = 0;
  686. }
  687. else if(((FILE*)pf)->flags & _F_WRIT) {
  688. mode[0] = 'a';
  689. mode[1] = 0;
  690. }
  691. else if(((FILE*)pf)->flags & _F_RDWR) {
  692. mode[0] = 'r';
  693. mode[1] = '+';
  694. mode[2] = 0;
  695. }
  696. #else
  697. if(((FILE*)pf)->_flag & _IOREAD) {
  698. mode[0] = 'r';
  699. mode[1] = 0;
  700. }
  701. else if(((FILE*)pf)->_flag & _IOWRT) {
  702. mode[0] = 'a';
  703. mode[1] = 0;
  704. }
  705. else if(((FILE*)pf)->_flag & _IORW) {
  706. mode[0] = 'r';
  707. mode[1] = '+';
  708. mode[2] = 0;
  709. }
  710. #endif
  711. /* it appears that the binmode is attached to the
  712. * file descriptor so binmode files will be handled
  713. * correctly
  714. */
  715. pfdup = (PerlIO*)win32_fdopen(fileno, mode);
  716. /* move the file pointer to the same position */
  717. if (!fgetpos((FILE*)pf, &pos)) {
  718. fsetpos((FILE*)pfdup, &pos);
  719. }
  720. return pfdup;
  721. }
  722. struct IPerlStdIO perlStdIO =
  723. {
  724. PerlStdIOStdin,
  725. PerlStdIOStdout,
  726. PerlStdIOStderr,
  727. PerlStdIOOpen,
  728. PerlStdIOClose,
  729. PerlStdIOEof,
  730. PerlStdIOError,
  731. PerlStdIOClearerr,
  732. PerlStdIOGetc,
  733. PerlStdIOGetBase,
  734. PerlStdIOGetBufsiz,
  735. PerlStdIOGetCnt,
  736. PerlStdIOGetPtr,
  737. PerlStdIOGets,
  738. PerlStdIOPutc,
  739. PerlStdIOPuts,
  740. PerlStdIOFlush,
  741. PerlStdIOUngetc,
  742. PerlStdIOFileno,
  743. PerlStdIOFdopen,
  744. PerlStdIOReopen,
  745. PerlStdIORead,
  746. PerlStdIOWrite,
  747. PerlStdIOSetBuf,
  748. PerlStdIOSetVBuf,
  749. PerlStdIOSetCnt,
  750. PerlStdIOSetPtrCnt,
  751. PerlStdIOSetlinebuf,
  752. PerlStdIOPrintf,
  753. PerlStdIOVprintf,
  754. PerlStdIOTell,
  755. PerlStdIOSeek,
  756. PerlStdIORewind,
  757. PerlStdIOTmpfile,
  758. PerlStdIOGetpos,
  759. PerlStdIOSetpos,
  760. PerlStdIOInit,
  761. PerlStdIOInitOSExtras,
  762. PerlStdIOFdupopen,
  763. };
  764. #undef IPERL2HOST
  765. #define IPERL2HOST(x) IPerlLIO2Host(x)
  766. /* IPerlLIO */
  767. int
  768. PerlLIOAccess(struct IPerlLIO* piPerl, const char *path, int mode)
  769. {
  770. return win32_access(path, mode);
  771. }
  772. int
  773. PerlLIOChmod(struct IPerlLIO* piPerl, const char *filename, int pmode)
  774. {
  775. return win32_chmod(filename, pmode);
  776. }
  777. int
  778. PerlLIOChown(struct IPerlLIO* piPerl, const char *filename, uid_t owner, gid_t group)
  779. {
  780. return chown(filename, owner, group);
  781. }
  782. int
  783. PerlLIOChsize(struct IPerlLIO* piPerl, int handle, long size)
  784. {
  785. return chsize(handle, size);
  786. }
  787. int
  788. PerlLIOClose(struct IPerlLIO* piPerl, int handle)
  789. {
  790. return win32_close(handle);
  791. }
  792. int
  793. PerlLIODup(struct IPerlLIO* piPerl, int handle)
  794. {
  795. return win32_dup(handle);
  796. }
  797. int
  798. PerlLIODup2(struct IPerlLIO* piPerl, int handle1, int handle2)
  799. {
  800. return win32_dup2(handle1, handle2);
  801. }
  802. int
  803. PerlLIOFlock(struct IPerlLIO* piPerl, int fd, int oper)
  804. {
  805. return win32_flock(fd, oper);
  806. }
  807. int
  808. PerlLIOFileStat(struct IPerlLIO* piPerl, int handle, struct stat *buffer)
  809. {
  810. return win32_fstat(handle, buffer);
  811. }
  812. int
  813. PerlLIOIOCtl(struct IPerlLIO* piPerl, int i, unsigned int u, char *data)
  814. {
  815. return win32_ioctlsocket((SOCKET)i, (long)u, (u_long*)data);
  816. }
  817. int
  818. PerlLIOIsatty(struct IPerlLIO* piPerl, int fd)
  819. {
  820. return isatty(fd);
  821. }
  822. int
  823. PerlLIOLink(struct IPerlLIO* piPerl, const char*oldname, const char *newname)
  824. {
  825. return win32_link(oldname, newname);
  826. }
  827. long
  828. PerlLIOLseek(struct IPerlLIO* piPerl, int handle, long offset, int origin)
  829. {
  830. return win32_lseek(handle, offset, origin);
  831. }
  832. int
  833. PerlLIOLstat(struct IPerlLIO* piPerl, const char *path, struct stat *buffer)
  834. {
  835. return win32_stat(path, buffer);
  836. }
  837. char*
  838. PerlLIOMktemp(struct IPerlLIO* piPerl, char *Template)
  839. {
  840. return mktemp(Template);
  841. }
  842. int
  843. PerlLIOOpen(struct IPerlLIO* piPerl, const char *filename, int oflag)
  844. {
  845. return win32_open(filename, oflag);
  846. }
  847. int
  848. PerlLIOOpen3(struct IPerlLIO* piPerl, const char *filename, int oflag, int pmode)
  849. {
  850. return win32_open(filename, oflag, pmode);
  851. }
  852. int
  853. PerlLIORead(struct IPerlLIO* piPerl, int handle, void *buffer, unsigned int count)
  854. {
  855. return win32_read(handle, buffer, count);
  856. }
  857. int
  858. PerlLIORename(struct IPerlLIO* piPerl, const char *OldFileName, const char *newname)
  859. {
  860. return win32_rename(OldFileName, newname);
  861. }
  862. int
  863. PerlLIOSetmode(struct IPerlLIO* piPerl, int handle, int mode)
  864. {
  865. return win32_setmode(handle, mode);
  866. }
  867. int
  868. PerlLIONameStat(struct IPerlLIO* piPerl, const char *path, struct stat *buffer)
  869. {
  870. return win32_stat(path, buffer);
  871. }
  872. char*
  873. PerlLIOTmpnam(struct IPerlLIO* piPerl, char *string)
  874. {
  875. return tmpnam(string);
  876. }
  877. int
  878. PerlLIOUmask(struct IPerlLIO* piPerl, int pmode)
  879. {
  880. return umask(pmode);
  881. }
  882. int
  883. PerlLIOUnlink(struct IPerlLIO* piPerl, const char *filename)
  884. {
  885. return win32_unlink(filename);
  886. }
  887. int
  888. PerlLIOUtime(struct IPerlLIO* piPerl, char *filename, struct utimbuf *times)
  889. {
  890. return win32_utime(filename, times);
  891. }
  892. int
  893. PerlLIOWrite(struct IPerlLIO* piPerl, int handle, const void *buffer, unsigned int count)
  894. {
  895. return win32_write(handle, buffer, count);
  896. }
  897. struct IPerlLIO perlLIO =
  898. {
  899. PerlLIOAccess,
  900. PerlLIOChmod,
  901. PerlLIOChown,
  902. PerlLIOChsize,
  903. PerlLIOClose,
  904. PerlLIODup,
  905. PerlLIODup2,
  906. PerlLIOFlock,
  907. PerlLIOFileStat,
  908. PerlLIOIOCtl,
  909. PerlLIOIsatty,
  910. PerlLIOLink,
  911. PerlLIOLseek,
  912. PerlLIOLstat,
  913. PerlLIOMktemp,
  914. PerlLIOOpen,
  915. PerlLIOOpen3,
  916. PerlLIORead,
  917. PerlLIORename,
  918. PerlLIOSetmode,
  919. PerlLIONameStat,
  920. PerlLIOTmpnam,
  921. PerlLIOUmask,
  922. PerlLIOUnlink,
  923. PerlLIOUtime,
  924. PerlLIOWrite,
  925. };
  926. #undef IPERL2HOST
  927. #define IPERL2HOST(x) IPerlDir2Host(x)
  928. /* IPerlDIR */
  929. int
  930. PerlDirMakedir(struct IPerlDir* piPerl, const char *dirname, int mode)
  931. {
  932. return win32_mkdir(dirname, mode);
  933. }
  934. int
  935. PerlDirChdir(struct IPerlDir* piPerl, const char *dirname)
  936. {
  937. return IPERL2HOST(piPerl)->Chdir(dirname);
  938. }
  939. int
  940. PerlDirRmdir(struct IPerlDir* piPerl, const char *dirname)
  941. {
  942. return win32_rmdir(dirname);
  943. }
  944. int
  945. PerlDirClose(struct IPerlDir* piPerl, DIR *dirp)
  946. {
  947. return win32_closedir(dirp);
  948. }
  949. DIR*
  950. PerlDirOpen(struct IPerlDir* piPerl, char *filename)
  951. {
  952. return win32_opendir(filename);
  953. }
  954. struct direct *
  955. PerlDirRead(struct IPerlDir* piPerl, DIR *dirp)
  956. {
  957. return win32_readdir(dirp);
  958. }
  959. void
  960. PerlDirRewind(struct IPerlDir* piPerl, DIR *dirp)
  961. {
  962. win32_rewinddir(dirp);
  963. }
  964. void
  965. PerlDirSeek(struct IPerlDir* piPerl, DIR *dirp, long loc)
  966. {
  967. win32_seekdir(dirp, loc);
  968. }
  969. long
  970. PerlDirTell(struct IPerlDir* piPerl, DIR *dirp)
  971. {
  972. return win32_telldir(dirp);
  973. }
  974. char*
  975. PerlDirMapPathA(struct IPerlDir* piPerl, const char* path)
  976. {
  977. return IPERL2HOST(piPerl)->MapPathA(path);
  978. }
  979. WCHAR*
  980. PerlDirMapPathW(struct IPerlDir* piPerl, const WCHAR* path)
  981. {
  982. return IPERL2HOST(piPerl)->MapPathW(path);
  983. }
  984. struct IPerlDir perlDir =
  985. {
  986. PerlDirMakedir,
  987. PerlDirChdir,
  988. PerlDirRmdir,
  989. PerlDirClose,
  990. PerlDirOpen,
  991. PerlDirRead,
  992. PerlDirRewind,
  993. PerlDirSeek,
  994. PerlDirTell,
  995. PerlDirMapPathA,
  996. PerlDirMapPathW,
  997. };
  998. /* IPerlSock */
  999. u_long
  1000. PerlSockHtonl(struct IPerlSock* piPerl, u_long hostlong)
  1001. {
  1002. return win32_htonl(hostlong);
  1003. }
  1004. u_short
  1005. PerlSockHtons(struct IPerlSock* piPerl, u_short hostshort)
  1006. {
  1007. return win32_htons(hostshort);
  1008. }
  1009. u_long
  1010. PerlSockNtohl(struct IPerlSock* piPerl, u_long netlong)
  1011. {
  1012. return win32_ntohl(netlong);
  1013. }
  1014. u_short
  1015. PerlSockNtohs(struct IPerlSock* piPerl, u_short netshort)
  1016. {
  1017. return win32_ntohs(netshort);
  1018. }
  1019. SOCKET PerlSockAccept(struct IPerlSock* piPerl, SOCKET s, struct sockaddr* addr, int* addrlen)
  1020. {
  1021. return win32_accept(s, addr, addrlen);
  1022. }
  1023. int
  1024. PerlSockBind(struct IPerlSock* piPerl, SOCKET s, const struct sockaddr* name, int namelen)
  1025. {
  1026. return win32_bind(s, name, namelen);
  1027. }
  1028. int
  1029. PerlSockConnect(struct IPerlSock* piPerl, SOCKET s, const struct sockaddr* name, int namelen)
  1030. {
  1031. return win32_connect(s, name, namelen);
  1032. }
  1033. void
  1034. PerlSockEndhostent(struct IPerlSock* piPerl)
  1035. {
  1036. win32_endhostent();
  1037. }
  1038. void
  1039. PerlSockEndnetent(struct IPerlSock* piPerl)
  1040. {
  1041. win32_endnetent();
  1042. }
  1043. void
  1044. PerlSockEndprotoent(struct IPerlSock* piPerl)
  1045. {
  1046. win32_endprotoent();
  1047. }
  1048. void
  1049. PerlSockEndservent(struct IPerlSock* piPerl)
  1050. {
  1051. win32_endservent();
  1052. }
  1053. struct hostent*
  1054. PerlSockGethostbyaddr(struct IPerlSock* piPerl, const char* addr, int len, int type)
  1055. {
  1056. return win32_gethostbyaddr(addr, len, type);
  1057. }
  1058. struct hostent*
  1059. PerlSockGethostbyname(struct IPerlSock* piPerl, const char* name)
  1060. {
  1061. return win32_gethostbyname(name);
  1062. }
  1063. struct hostent*
  1064. PerlSockGethostent(struct IPerlSock* piPerl)
  1065. {
  1066. dTHXo;
  1067. Perl_croak(aTHX_ "gethostent not implemented!\n");
  1068. return NULL;
  1069. }
  1070. int
  1071. PerlSockGethostname(struct IPerlSock* piPerl, char* name, int namelen)
  1072. {
  1073. return win32_gethostname(name, namelen);
  1074. }
  1075. struct netent *
  1076. PerlSockGetnetbyaddr(struct IPerlSock* piPerl, long net, int type)
  1077. {
  1078. return win32_getnetbyaddr(net, type);
  1079. }
  1080. struct netent *
  1081. PerlSockGetnetbyname(struct IPerlSock* piPerl, const char *name)
  1082. {
  1083. return win32_getnetbyname((char*)name);
  1084. }
  1085. struct netent *
  1086. PerlSockGetnetent(struct IPerlSock* piPerl)
  1087. {
  1088. return win32_getnetent();
  1089. }
  1090. int PerlSockGetpeername(struct IPerlSock* piPerl, SOCKET s, struct sockaddr* name, int* namelen)
  1091. {
  1092. return win32_getpeername(s, name, namelen);
  1093. }
  1094. struct protoent*
  1095. PerlSockGetprotobyname(struct IPerlSock* piPerl, const char* name)
  1096. {
  1097. return win32_getprotobyname(name);
  1098. }
  1099. struct protoent*
  1100. PerlSockGetprotobynumber(struct IPerlSock* piPerl, int number)
  1101. {
  1102. return win32_getprotobynumber(number);
  1103. }
  1104. struct protoent*
  1105. PerlSockGetprotoent(struct IPerlSock* piPerl)
  1106. {
  1107. return win32_getprotoent();
  1108. }
  1109. struct servent*
  1110. PerlSockGetservbyname(struct IPerlSock* piPerl, const char* name, const char* proto)
  1111. {
  1112. return win32_getservbyname(name, proto);
  1113. }
  1114. struct servent*
  1115. PerlSockGetservbyport(struct IPerlSock* piPerl, int port, const char* proto)
  1116. {
  1117. return win32_getservbyport(port, proto);
  1118. }
  1119. struct servent*
  1120. PerlSockGetservent(struct IPerlSock* piPerl)
  1121. {
  1122. return win32_getservent();
  1123. }
  1124. int
  1125. PerlSockGetsockname(struct IPerlSock* piPerl, SOCKET s, struct sockaddr* name, int* namelen)
  1126. {
  1127. return win32_getsockname(s, name, namelen);
  1128. }
  1129. int
  1130. PerlSockGetsockopt(struct IPerlSock* piPerl, SOCKET s, int level, int optname, char* optval, int* optlen)
  1131. {
  1132. return win32_getsockopt(s, level, optname, optval, optlen);
  1133. }
  1134. unsigned long
  1135. PerlSockInetAddr(struct IPerlSock* piPerl, const char* cp)
  1136. {
  1137. return win32_inet_addr(cp);
  1138. }
  1139. char*
  1140. PerlSockInetNtoa(struct IPerlSock* piPerl, struct in_addr in)
  1141. {
  1142. return win32_inet_ntoa(in);
  1143. }
  1144. int
  1145. PerlSockListen(struct IPerlSock* piPerl, SOCKET s, int backlog)
  1146. {
  1147. return win32_listen(s, backlog);
  1148. }
  1149. int
  1150. PerlSockRecv(struct IPerlSock* piPerl, SOCKET s, char* buffer, int len, int flags)
  1151. {
  1152. return win32_recv(s, buffer, len, flags);
  1153. }
  1154. int
  1155. PerlSockRecvfrom(struct IPerlSock* piPerl, SOCKET s, char* buffer, int len, int flags, struct sockaddr* from, int* fromlen)
  1156. {
  1157. return win32_recvfrom(s, buffer, len, flags, from, fromlen);
  1158. }
  1159. int
  1160. PerlSockSelect(struct IPerlSock* piPerl, int nfds, char* readfds, char* writefds, char* exceptfds, const struct timeval* timeout)
  1161. {
  1162. return win32_select(nfds, (Perl_fd_set*)readfds, (Perl_fd_set*)writefds, (Perl_fd_set*)exceptfds, timeout);
  1163. }
  1164. int
  1165. PerlSockSend(struct IPerlSock* piPerl, SOCKET s, const char* buffer, int len, int flags)
  1166. {
  1167. return win32_send(s, buffer, len, flags);
  1168. }
  1169. int
  1170. PerlSockSendto(struct IPerlSock* piPerl, SOCKET s, const char* buffer, int len, int flags, const struct sockaddr* to, int tolen)
  1171. {
  1172. return win32_sendto(s, buffer, len, flags, to, tolen);
  1173. }
  1174. void
  1175. PerlSockSethostent(struct IPerlSock* piPerl, int stayopen)
  1176. {
  1177. win32_sethostent(stayopen);
  1178. }
  1179. void
  1180. PerlSockSetnetent(struct IPerlSock* piPerl, int stayopen)
  1181. {
  1182. win32_setnetent(stayopen);
  1183. }
  1184. void
  1185. PerlSockSetprotoent(struct IPerlSock* piPerl, int stayopen)
  1186. {
  1187. win32_setprotoent(stayopen);
  1188. }
  1189. void
  1190. PerlSockSetservent(struct IPerlSock* piPerl, int stayopen)
  1191. {
  1192. win32_setservent(stayopen);
  1193. }
  1194. int
  1195. PerlSockSetsockopt(struct IPerlSock* piPerl, SOCKET s, int level, int optname, const char* optval, int optlen)
  1196. {
  1197. return win32_setsockopt(s, level, optname, optval, optlen);
  1198. }
  1199. int
  1200. PerlSockShutdown(struct IPerlSock* piPerl, SOCKET s, int how)
  1201. {
  1202. return win32_shutdown(s, how);
  1203. }
  1204. SOCKET
  1205. PerlSockSocket(struct IPerlSock* piPerl, int af, int type, int protocol)
  1206. {
  1207. return win32_socket(af, type, protocol);
  1208. }
  1209. int
  1210. PerlSockSocketpair(struct IPerlSock* piPerl, int domain, int type, int protocol, int* fds)
  1211. {
  1212. dTHXo;
  1213. Perl_croak(aTHX_ "socketpair not implemented!\n");
  1214. return 0;
  1215. }
  1216. int
  1217. PerlSockClosesocket(struct IPerlSock* piPerl, SOCKET s)
  1218. {
  1219. return win32_closesocket(s);
  1220. }
  1221. int
  1222. PerlSockIoctlsocket(struct IPerlSock* piPerl, SOCKET s, long cmd, u_long *argp)
  1223. {
  1224. return win32_ioctlsocket(s, cmd, argp);
  1225. }
  1226. struct IPerlSock perlSock =
  1227. {
  1228. PerlSockHtonl,
  1229. PerlSockHtons,
  1230. PerlSockNtohl,
  1231. PerlSockNtohs,
  1232. PerlSockAccept,
  1233. PerlSockBind,
  1234. PerlSockConnect,
  1235. PerlSockEndhostent,
  1236. PerlSockEndnetent,
  1237. PerlSockEndprotoent,
  1238. PerlSockEndservent,
  1239. PerlSockGethostname,
  1240. PerlSockGetpeername,
  1241. PerlSockGethostbyaddr,
  1242. PerlSockGethostbyname,
  1243. PerlSockGethostent,
  1244. PerlSockGetnetbyaddr,
  1245. PerlSockGetnetbyname,
  1246. PerlSockGetnetent,
  1247. PerlSockGetprotobyname,
  1248. PerlSockGetprotobynumber,
  1249. PerlSockGetprotoent,
  1250. PerlSockGetservbyname,
  1251. PerlSockGetservbyport,
  1252. PerlSockGetservent,
  1253. PerlSockGetsockname,
  1254. PerlSockGetsockopt,
  1255. PerlSockInetAddr,
  1256. PerlSockInetNtoa,
  1257. PerlSockListen,
  1258. PerlSockRecv,
  1259. PerlSockRecvfrom,
  1260. PerlSockSelect,
  1261. PerlSockSend,
  1262. PerlSockSendto,
  1263. PerlSockSethostent,
  1264. PerlSockSetnetent,
  1265. PerlSockSetprotoent,
  1266. PerlSockSetservent,
  1267. PerlSockSetsockopt,
  1268. PerlSockShutdown,
  1269. PerlSockSocket,
  1270. PerlSockSocketpair,
  1271. PerlSockClosesocket,
  1272. };
  1273. /* IPerlProc */
  1274. #define EXECF_EXEC 1
  1275. #define EXECF_SPAWN 2
  1276. void
  1277. PerlProcAbort(struct IPerlProc* piPerl)
  1278. {
  1279. win32_abort();
  1280. }
  1281. char *
  1282. PerlProcCrypt(struct IPerlProc* piPerl, const char* clear, const char* salt)
  1283. {
  1284. return win32_crypt(clear, salt);
  1285. }
  1286. void
  1287. PerlProcExit(struct IPerlProc* piPerl, int status)
  1288. {
  1289. exit(status);
  1290. }
  1291. void
  1292. PerlProc_Exit(struct IPerlProc* piPerl, int status)
  1293. {
  1294. _exit(status);
  1295. }
  1296. int
  1297. PerlProcExecl(struct IPerlProc* piPerl, const char *cmdname, const char *arg0, const char *arg1, const char *arg2, const char *arg3)
  1298. {
  1299. return execl(cmdname, arg0, arg1, arg2, arg3);
  1300. }
  1301. int
  1302. PerlProcExecv(struct IPerlProc* piPerl, const char *cmdname, const char *const *argv)
  1303. {
  1304. return win32_execvp(cmdname, argv);
  1305. }
  1306. int
  1307. PerlProcExecvp(struct IPerlProc* piPerl, const char *cmdname, const char *const *argv)
  1308. {
  1309. return win32_execvp(cmdname, argv);
  1310. }
  1311. uid_t
  1312. PerlProcGetuid(struct IPerlProc* piPerl)
  1313. {
  1314. return getuid();
  1315. }
  1316. uid_t
  1317. PerlProcGeteuid(struct IPerlProc* piPerl)
  1318. {
  1319. return geteuid();
  1320. }
  1321. gid_t
  1322. PerlProcGetgid(struct IPerlProc* piPerl)
  1323. {
  1324. return getgid();
  1325. }
  1326. gid_t
  1327. PerlProcGetegid(struct IPerlProc* piPerl)
  1328. {
  1329. return getegid();
  1330. }
  1331. char *
  1332. PerlProcGetlogin(struct IPerlProc* piPerl)
  1333. {
  1334. return g_getlogin();
  1335. }
  1336. int
  1337. PerlProcKill(struct IPerlProc* piPerl, int pid, int sig)
  1338. {
  1339. return win32_kill(pid, sig);
  1340. }
  1341. int
  1342. PerlProcKillpg(struct IPerlProc* piPerl, int pid, int sig)
  1343. {
  1344. dTHXo;
  1345. Perl_croak(aTHX_ "killpg not implemented!\n");
  1346. return 0;
  1347. }
  1348. int
  1349. PerlProcPauseProc(struct IPerlProc* piPerl)
  1350. {
  1351. return win32_sleep((32767L << 16) + 32767);
  1352. }
  1353. PerlIO*
  1354. PerlProcPopen(struct IPerlProc* piPerl, const char *command, const char *mode)
  1355. {
  1356. dTHXo;
  1357. PERL_FLUSHALL_FOR_CHILD;
  1358. return (PerlIO*)win32_popen(command, mode);
  1359. }
  1360. int
  1361. PerlProcPclose(struct IPerlProc* piPerl, PerlIO *stream)
  1362. {
  1363. return win32_pclose((FILE*)stream);
  1364. }
  1365. int
  1366. PerlProcPipe(struct IPerlProc* piPerl, int *phandles)
  1367. {
  1368. return win32_pipe(phandles, 512, O_BINARY);
  1369. }
  1370. int
  1371. PerlProcSetuid(struct IPerlProc* piPerl, uid_t u)
  1372. {
  1373. return setuid(u);
  1374. }
  1375. int
  1376. PerlProcSetgid(struct IPerlProc* piPerl, gid_t g)
  1377. {
  1378. return setgid(g);
  1379. }
  1380. int
  1381. PerlProcSleep(struct IPerlProc* piPerl, unsigned int s)
  1382. {
  1383. return win32_sleep(s);
  1384. }
  1385. int
  1386. PerlProcTimes(struct IPerlProc* piPerl, struct tms *timebuf)
  1387. {
  1388. return win32_times(timebuf);
  1389. }
  1390. int
  1391. PerlProcWait(struct IPerlProc* piPerl, int *status)
  1392. {
  1393. return win32_wait(status);
  1394. }
  1395. int
  1396. PerlProcWaitpid(struct IPerlProc* piPerl, int pid, int *status, int flags)
  1397. {
  1398. return win32_waitpid(pid, status, flags);
  1399. }
  1400. Sighandler_t
  1401. PerlProcSignal(struct IPerlProc* piPerl, int sig, Sighandler_t subcode)
  1402. {
  1403. return signal(sig, subcode);
  1404. }
  1405. #ifdef USE_ITHREADS
  1406. static THREAD_RET_TYPE
  1407. win32_start_child(LPVOID arg)
  1408. {
  1409. PerlInterpreter *my_perl = (PerlInterpreter*)arg;
  1410. GV *tmpgv;
  1411. int status;
  1412. #ifdef PERL_OBJECT
  1413. CPerlObj *pPerl = (CPerlObj*)my_perl;
  1414. #endif
  1415. #ifdef PERL_SYNC_FORK
  1416. static long sync_fork_id = 0;
  1417. long id = ++sync_fork_id;
  1418. #endif
  1419. PERL_SET_THX(my_perl);
  1420. /* set $$ to pseudo id */
  1421. #ifdef PERL_SYNC_FORK
  1422. w32_pseudo_id = id;
  1423. #else
  1424. w32_pseudo_id = GetCurrentThreadId();
  1425. if (IsWin95()) {
  1426. int pid = (int)w32_pseudo_id;
  1427. if (pid < 0)
  1428. w32_pseudo_id = -pid;
  1429. }
  1430. #endif
  1431. if (tmpgv = gv_fetchpv("$", TRUE, SVt_PV))
  1432. sv_setiv(GvSV(tmpgv), -(IV)w32_pseudo_id);
  1433. hv_clear(PL_pidstatus);
  1434. /* push a zero on the stack (we are the child) */
  1435. {
  1436. dSP;
  1437. dTARGET;
  1438. PUSHi(0);
  1439. PUTBACK;
  1440. }
  1441. /* continue from next op */
  1442. PL_op = PL_op->op_next;
  1443. {
  1444. dJMPENV;
  1445. volatile int oldscope = PL_scopestack_ix;
  1446. restart:
  1447. JMPENV_PUSH(status);
  1448. switch (status) {
  1449. case 0:
  1450. CALLRUNOPS(aTHX);
  1451. status = 0;
  1452. break;
  1453. case 2:
  1454. while (PL_scopestack_ix > oldscope)
  1455. LEAVE;
  1456. FREETMPS;
  1457. PL_curstash = PL_defstash;
  1458. if (PL_endav && !PL_minus_c)
  1459. call_list(oldscope, PL_endav);
  1460. status = STATUS_NATIVE_EXPORT;
  1461. break;
  1462. case 3:
  1463. if (PL_restartop) {
  1464. POPSTACK_TO(PL_mainstack);
  1465. PL_op = PL_restartop;
  1466. PL_restartop = Nullop;
  1467. goto restart;
  1468. }
  1469. PerlIO_printf(Perl_error_log, "panic: restartop\n");
  1470. FREETMPS;
  1471. status = 1;
  1472. break;
  1473. }
  1474. JMPENV_POP;
  1475. /* XXX hack to avoid perl_destruct() freeing optree */
  1476. PL_main_root = Nullop;
  1477. }
  1478. /* close the std handles to avoid fd leaks */
  1479. {
  1480. do_close(gv_fetchpv("STDIN", TRUE, SVt_PVIO), FALSE);
  1481. do_close(gv_fetchpv("STDOUT", TRUE, SVt_PVIO), FALSE);
  1482. do_close(gv_fetchpv("STDERR", TRUE, SVt_PVIO), FALSE);
  1483. }
  1484. /* destroy everything (waits for any pseudo-forked children) */
  1485. perl_destruct(my_perl);
  1486. perl_free(my_perl);
  1487. #ifdef PERL_SYNC_FORK
  1488. return id;
  1489. #else
  1490. return (DWORD)status;
  1491. #endif
  1492. }
  1493. #endif /* USE_ITHREADS */
  1494. int
  1495. PerlProcFork(struct IPerlProc* piPerl)
  1496. {
  1497. dTHXo;
  1498. #ifdef USE_ITHREADS
  1499. DWORD id;
  1500. HANDLE handle;
  1501. CPerlHost *h;
  1502. if (w32_num_pseudo_children >= MAXIMUM_WAIT_OBJECTS) {
  1503. errno = EAGAIN;
  1504. return -1;
  1505. }
  1506. h = new CPerlHost(*(CPerlHost*)w32_internal_host);
  1507. PerlInterpreter *new_perl = perl_clone_using((PerlInterpreter*)aTHXo, 1,
  1508. h->m_pHostperlMem,
  1509. h->m_pHostperlMemShared,
  1510. h->m_pHostperlMemParse,
  1511. h->m_pHostperlEnv,
  1512. h->m_pHostperlStdIO,
  1513. h->m_pHostperlLIO,
  1514. h->m_pHostperlDir,
  1515. h->m_pHostperlSock,
  1516. h->m_pHostperlProc
  1517. );
  1518. new_perl->Isys_intern.internal_host = h;
  1519. # ifdef PERL_SYNC_FORK
  1520. id = win32_start_child((LPVOID)new_perl);
  1521. PERL_SET_THX(aTHXo);
  1522. # else
  1523. # ifdef USE_RTL_THREAD_API
  1524. handle = (HANDLE)_beginthreadex((void*)NULL, 0, win32_start_child,
  1525. (void*)new_perl, 0, (unsigned*)&id);
  1526. # else
  1527. handle = CreateThread(NULL, 0, win32_start_child,
  1528. (LPVOID)new_perl, 0, &id);
  1529. # endif
  1530. PERL_SET_THX(aTHXo); /* XXX perl_clone*() set TLS */
  1531. if (!handle) {
  1532. errno = EAGAIN;
  1533. return -1;
  1534. }
  1535. if (IsWin95()) {
  1536. int pid = (int)id;
  1537. if (pid < 0)
  1538. id = -pid;
  1539. }
  1540. w32_pseudo_child_handles[w32_num_pseudo_children] = handle;
  1541. w32_pseudo_child_pids[w32_num_pseudo_children] = id;
  1542. ++w32_num_pseudo_children;
  1543. # endif
  1544. return -(int)id;
  1545. #else
  1546. Perl_croak(aTHX_ "fork() not implemented!\n");
  1547. return -1;
  1548. #endif /* USE_ITHREADS */
  1549. }
  1550. int
  1551. PerlProcGetpid(struct IPerlProc* piPerl)
  1552. {
  1553. return win32_getpid();
  1554. }
  1555. void*
  1556. PerlProcDynaLoader(struct IPerlProc* piPerl, const char* filename)
  1557. {
  1558. return win32_dynaload(filename);
  1559. }
  1560. void
  1561. PerlProcGetOSError(struct IPerlProc* piPerl, SV* sv, DWORD dwErr)
  1562. {
  1563. win32_str_os_error(sv, dwErr);
  1564. }
  1565. int
  1566. PerlProcSpawnvp(struct IPerlProc* piPerl, int mode, const char *cmdname, const char *const *argv)
  1567. {
  1568. return win32_spawnvp(mode, cmdname, argv);
  1569. }
  1570. /* XXX these three are only here for binary compatibility */
  1571. BOOL
  1572. PerlProcDoCmd(struct IPerlProc* piPerl, char *cmd)
  1573. {
  1574. dTHXo;
  1575. return do_exec(cmd);
  1576. }
  1577. int
  1578. PerlProcSpawn(struct IPerlProc* piPerl, char* cmds)
  1579. {
  1580. dTHXo;
  1581. return do_spawn(cmds);
  1582. }
  1583. int
  1584. PerlProcASpawn(struct IPerlProc* piPerl, SV *really, SV **mark, SV **sp)
  1585. {
  1586. dTHXo;
  1587. return do_aspawn(really, mark, sp);
  1588. }
  1589. /* XXX above three are only here for binary compatibility */
  1590. struct IPerlProc perlProc =
  1591. {
  1592. PerlProcAbort,
  1593. PerlProcCrypt,
  1594. PerlProcExit,
  1595. PerlProc_Exit,
  1596. PerlProcExecl,
  1597. PerlProcExecv,
  1598. PerlProcExecvp,
  1599. PerlProcGetuid,
  1600. PerlProcGeteuid,
  1601. PerlProcGetgid,
  1602. PerlProcGetegid,
  1603. PerlProcGetlogin,
  1604. PerlProcKill,
  1605. PerlProcKillpg,
  1606. PerlProcPauseProc,
  1607. PerlProcPopen,
  1608. PerlProcPclose,
  1609. PerlProcPipe,
  1610. PerlProcSetuid,
  1611. PerlProcSetgid,
  1612. PerlProcSleep,
  1613. PerlProcTimes,
  1614. PerlProcWait,
  1615. PerlProcWaitpid,
  1616. PerlProcSignal,
  1617. PerlProcFork,
  1618. PerlProcGetpid,
  1619. PerlProcDynaLoader,
  1620. PerlProcGetOSError,
  1621. PerlProcDoCmd,
  1622. PerlProcSpawn,
  1623. PerlProcSpawnvp,
  1624. PerlProcASpawn,
  1625. };
  1626. /*
  1627. * CPerlHost
  1628. */
  1629. CPerlHost::CPerlHost(void)
  1630. {
  1631. m_pvDir = new VDir();
  1632. m_pVMem = new VMem();
  1633. m_pVMemShared = new VMem();
  1634. m_pVMemParse = new VMem();
  1635. m_pvDir->Init(NULL, m_pVMem);
  1636. m_dwEnvCount = 0;
  1637. m_lppEnvList = NULL;
  1638. CopyMemory(&m_hostperlMem, &perlMem, sizeof(perlMem));
  1639. CopyMemory(&m_hostperlMemShared, &perlMemShared, sizeof(perlMemShared));
  1640. CopyMemory(&m_hostperlMemParse, &perlMemParse, sizeof(perlMemParse));
  1641. CopyMemory(&m_hostperlEnv, &perlEnv, sizeof(perlEnv));
  1642. CopyMemory(&m_hostperlStdIO, &perlStdIO, sizeof(perlStdIO));
  1643. CopyMemory(&m_hostperlLIO, &perlLIO, sizeof(perlLIO));
  1644. CopyMemory(&m_hostperlDir, &perlDir, sizeof(perlDir));
  1645. CopyMemory(&m_hostperlSock, &perlSock, sizeof(perlSock));
  1646. CopyMemory(&m_hostperlProc, &perlProc, sizeof(perlProc));
  1647. m_pHostperlMem = &m_hostperlMem;
  1648. m_pHostperlMemShared = &m_hostperlMemShared;
  1649. m_pHostperlMemParse = &m_hostperlMemParse;
  1650. m_pHostperlEnv = &m_hostperlEnv;
  1651. m_pHostperlStdIO = &m_hostperlStdIO;
  1652. m_pHostperlLIO = &m_hostperlLIO;
  1653. m_pHostperlDir = &m_hostperlDir;
  1654. m_pHostperlSock = &m_hostperlSock;
  1655. m_pHostperlProc = &m_hostperlProc;
  1656. }
  1657. #define SETUPEXCHANGE(xptr, iptr, table) \
  1658. STMT_START { \
  1659. if (xptr) { \
  1660. iptr = *xptr; \
  1661. *xptr = &table; \
  1662. } \
  1663. else { \
  1664. iptr = &table; \
  1665. } \
  1666. } STMT_END
  1667. CPerlHost::CPerlHost(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
  1668. struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
  1669. struct IPerlStdIO** ppStdIO, struct IPerlLIO** ppLIO,
  1670. struct IPerlDir** ppDir, struct IPerlSock** ppSock,
  1671. struct IPerlProc** ppProc)
  1672. {
  1673. m_pvDir = new VDir(0);
  1674. m_pVMem = new VMem();
  1675. m_pVMemShared = new VMem();
  1676. m_pVMemParse = new VMem();
  1677. m_pvDir->Init(NULL, m_pVMem);
  1678. m_dwEnvCount = 0;
  1679. m_lppEnvList = NULL;
  1680. CopyMemory(&m_hostperlMem, &perlMem, sizeof(perlMem));
  1681. CopyMemory(&m_hostperlMemShared, &perlMemShared, sizeof(perlMemShared));
  1682. CopyMemory(&m_hostperlMemParse, &perlMemParse, sizeof(perlMemParse));
  1683. CopyMemory(&m_hostperlEnv, &perlEnv, sizeof(perlEnv));
  1684. CopyMemory(&m_hostperlStdIO, &perlStdIO, sizeof(perlStdIO));
  1685. CopyMemory(&m_hostperlLIO, &perlLIO, sizeof(perlLIO));
  1686. CopyMemory(&m_hostperlDir, &perlDir, sizeof(perlDir));
  1687. CopyMemory(&m_hostperlSock, &perlSock, sizeof(perlSock));
  1688. CopyMemory(&m_hostperlProc, &perlProc, sizeof(perlProc));
  1689. SETUPEXCHANGE(ppMem, m_pHostperlMem, m_hostperlMem);
  1690. SETUPEXCHANGE(ppMemShared, m_pHostperlMemShared, m_hostperlMemShared);
  1691. SETUPEXCHANGE(ppMemParse, m_pHostperlMemParse, m_hostperlMemParse);
  1692. SETUPEXCHANGE(ppEnv, m_pHostperlEnv, m_hostperlEnv);
  1693. SETUPEXCHANGE(ppStdIO, m_pHostperlStdIO, m_hostperlStdIO);
  1694. SETUPEXCHANGE(ppLIO, m_pHostperlLIO, m_hostperlLIO);
  1695. SETUPEXCHANGE(ppDir, m_pHostperlDir, m_hostperlDir);
  1696. SETUPEXCHANGE(ppSock, m_pHostperlSock, m_hostperlSock);
  1697. SETUPEXCHANGE(ppProc, m_pHostperlProc, m_hostperlProc);
  1698. }
  1699. #undef SETUPEXCHANGE
  1700. CPerlHost::CPerlHost(CPerlHost& host)
  1701. {
  1702. m_pVMem = new VMem();
  1703. m_pVMemShared = host.GetMemShared();
  1704. m_pVMemParse = host.GetMemParse();
  1705. /* duplicate directory info */
  1706. m_pvDir = new VDir(0);
  1707. m_pvDir->Init(host.GetDir(), m_pVMem);
  1708. CopyMemory(&m_hostperlMem, &perlMem, sizeof(perlMem));
  1709. CopyMemory(&m_hostperlMemShared, &perlMemShared, sizeof(perlMemShared));
  1710. CopyMemory(&m_hostperlMemParse, &perlMemParse, sizeof(perlMemParse));
  1711. CopyMemory(&m_hostperlEnv, &perlEnv, sizeof(perlEnv));
  1712. CopyMemory(&m_hostperlStdIO, &perlStdIO, sizeof(perlStdIO));
  1713. CopyMemory(&m_hostperlLIO, &perlLIO, sizeof(perlLIO));
  1714. CopyMemory(&m_hostperlDir, &perlDir, sizeof(perlDir));
  1715. CopyMemory(&m_hostperlSock, &perlSock, sizeof(perlSock));
  1716. CopyMemory(&m_hostperlProc, &perlProc, sizeof(perlProc));
  1717. m_pHostperlMem = &m_hostperlMem;
  1718. m_pHostperlMemShared = &m_hostperlMemShared;
  1719. m_pHostperlMemParse = &m_hostperlMemParse;
  1720. m_pHostperlEnv = &m_hostperlEnv;
  1721. m_pHostperlStdIO = &m_hostperlStdIO;
  1722. m_pHostperlLIO = &m_hostperlLIO;
  1723. m_pHostperlDir = &m_hostperlDir;
  1724. m_pHostperlSock = &m_hostperlSock;
  1725. m_pHostperlProc = &m_hostperlProc;
  1726. m_dwEnvCount = 0;
  1727. m_lppEnvList = NULL;
  1728. /* duplicate environment info */
  1729. LPSTR lpPtr;
  1730. DWORD dwIndex = 0;
  1731. while(lpPtr = host.GetIndex(dwIndex))
  1732. Add(lpPtr);
  1733. }
  1734. CPerlHost::~CPerlHost(void)
  1735. {
  1736. // Reset();
  1737. delete m_pvDir;
  1738. m_pVMemParse->Release();
  1739. m_pVMemShared->Release();
  1740. m_pVMem->Release();
  1741. }
  1742. LPSTR
  1743. CPerlHost::Find(LPCSTR lpStr)
  1744. {
  1745. LPSTR lpPtr;
  1746. LPSTR* lppPtr = Lookup(lpStr);
  1747. if(lppPtr != NULL) {
  1748. for(lpPtr = *lppPtr; *lpPtr != '\0' && *lpPtr != '='; ++lpPtr)
  1749. ;
  1750. if(*lpPtr == '=')
  1751. ++lpPtr;
  1752. return lpPtr;
  1753. }
  1754. return NULL;
  1755. }
  1756. int
  1757. lookup(const void *arg1, const void *arg2)
  1758. { // Compare strings
  1759. char*ptr1, *ptr2;
  1760. char c1,c2;
  1761. ptr1 = *(char**)arg1;
  1762. ptr2 = *(char**)arg2;
  1763. for(;;) {
  1764. c1 = *ptr1++;
  1765. c2 = *ptr2++;
  1766. if(c1 == '\0' || c1 == '=') {
  1767. if(c2 == '\0' || c2 == '=')
  1768. break;
  1769. return -1; // string 1 < string 2
  1770. }
  1771. else if(c2 == '\0' || c2 == '=')
  1772. return 1; // string 1 > string 2
  1773. else if(c1 != c2) {
  1774. c1 = toupper(c1);
  1775. c2 = toupper(c2);
  1776. if(c1 != c2) {
  1777. if(c1 < c2)
  1778. return -1; // string 1 < string 2
  1779. return 1; // string 1 > string 2
  1780. }
  1781. }
  1782. }
  1783. return 0;
  1784. }
  1785. LPSTR*
  1786. CPerlHost::Lookup(LPCSTR lpStr)
  1787. {
  1788. return (LPSTR*)bsearch(&lpStr, m_lppEnvList, m_dwEnvCount, sizeof(LPSTR), lookup);
  1789. }
  1790. int
  1791. compare(const void *arg1, const void *arg2)
  1792. { // Compare strings
  1793. char*ptr1, *ptr2;
  1794. char c1,c2;
  1795. ptr1 = *(char**)arg1;
  1796. ptr2 = *(char**)arg2;
  1797. for(;;) {
  1798. c1 = *ptr1++;
  1799. c2 = *ptr2++;
  1800. if(c1 == '\0' || c1 == '=') {
  1801. if(c1 == c2)
  1802. break;
  1803. return -1; // string 1 < string 2
  1804. }
  1805. else if(c2 == '\0' || c2 == '=')
  1806. return 1; // string 1 > string 2
  1807. else if(c1 != c2) {
  1808. c1 = toupper(c1);
  1809. c2 = toupper(c2);
  1810. if(c1 != c2) {
  1811. if(c1 < c2)
  1812. return -1; // string 1 < string 2
  1813. return 1; // string 1 > string 2
  1814. }
  1815. }
  1816. }
  1817. return 0;
  1818. }
  1819. void
  1820. CPerlHost::Add(LPCSTR lpStr)
  1821. {
  1822. dTHXo;
  1823. char szBuffer[1024];
  1824. LPSTR *lpPtr;
  1825. int index, length = strlen(lpStr)+1;
  1826. for(index = 0; lpStr[index] != '\0' && lpStr[index] != '='; ++index)
  1827. szBuffer[index] = lpStr[index];
  1828. szBuffer[index] = '\0';
  1829. // replacing ?
  1830. lpPtr = Lookup(szBuffer);
  1831. if(lpPtr != NULL) {
  1832. Renew(*lpPtr, length, char);
  1833. strcpy(*lpPtr, lpStr);
  1834. }
  1835. else {
  1836. ++m_dwEnvCount;
  1837. Renew(m_lppEnvList, m_dwEnvCount, LPSTR);
  1838. New(1, m_lppEnvList[m_dwEnvCount-1], length, char);
  1839. if(m_lppEnvList[m_dwEnvCount-1] != NULL) {
  1840. strcpy(m_lppEnvList[m_dwEnvCount-1], lpStr);
  1841. qsort(m_lppEnvList, m_dwEnvCount, sizeof(LPSTR), compare);
  1842. }
  1843. else
  1844. --m_dwEnvCount;
  1845. }
  1846. }
  1847. DWORD
  1848. CPerlHost::CalculateEnvironmentSpace(void)
  1849. {
  1850. DWORD index;
  1851. DWORD dwSize = 0;
  1852. for(index = 0; index < m_dwEnvCount; ++index)
  1853. dwSize += strlen(m_lppEnvList[index]) + 1;
  1854. return dwSize;
  1855. }
  1856. void
  1857. CPerlHost::FreeLocalEnvironmentStrings(LPSTR lpStr)
  1858. {
  1859. dTHXo;
  1860. Safefree(lpStr);
  1861. }
  1862. char*
  1863. CPerlHost::GetChildDir(void)
  1864. {
  1865. dTHXo;
  1866. int length;
  1867. char* ptr;
  1868. New(0, ptr, MAX_PATH+1, char);
  1869. if(ptr) {
  1870. m_pvDir->GetCurrentDirectoryA(MAX_PATH+1, ptr);
  1871. length = strlen(ptr);
  1872. if (length > 3) {
  1873. if ((ptr[length-1] == '\\') || (ptr[length-1] == '/'))
  1874. ptr[length-1] = 0;
  1875. }
  1876. }
  1877. return ptr;
  1878. }
  1879. void
  1880. CPerlHost::FreeChildDir(char* pStr)
  1881. {
  1882. dTHXo;
  1883. Safefree(pStr);
  1884. }
  1885. LPSTR
  1886. CPerlHost::CreateLocalEnvironmentStrings(VDir &vDir)
  1887. {
  1888. dTHXo;
  1889. LPSTR lpStr, lpPtr, lpEnvPtr, lpTmp, lpLocalEnv, lpAllocPtr;
  1890. DWORD dwSize, dwEnvIndex;
  1891. int nLength, compVal;
  1892. // get the process environment strings
  1893. lpAllocPtr = lpTmp = (LPSTR)GetEnvironmentStrings();
  1894. // step over current directory stuff
  1895. while(*lpTmp == '=')
  1896. lpTmp += strlen(lpTmp) + 1;
  1897. // save the start of the environment strings
  1898. lpEnvPtr = lpTmp;
  1899. for(dwSize = 1; *lpTmp != '\0'; lpTmp += strlen(lpTmp) + 1) {
  1900. // calculate the size of the environment strings
  1901. dwSize += strlen(lpTmp) + 1;
  1902. }
  1903. // add the size of current directories
  1904. dwSize += vDir.CalculateEnvironmentSpace();
  1905. // add the additional space used by changes made to the environment
  1906. dwSize += CalculateEnvironmentSpace();
  1907. New(1, lpStr, dwSize, char);
  1908. lpPtr = lpStr;
  1909. if(lpStr != NULL) {
  1910. // build the local environment
  1911. lpStr = vDir.BuildEnvironmentSpace(lpStr);
  1912. dwEnvIndex = 0;
  1913. lpLocalEnv = GetIndex(dwEnvIndex);
  1914. while(*lpEnvPtr != '\0') {
  1915. if(!lpLocalEnv) {
  1916. // all environment overrides have been added
  1917. // so copy string into place
  1918. strcpy(lpStr, lpEnvPtr);
  1919. nLength = strlen(lpEnvPtr) + 1;
  1920. lpStr += nLength;
  1921. lpEnvPtr += nLength;
  1922. }
  1923. else {
  1924. // determine which string to copy next
  1925. compVal = compare(&lpEnvPtr, &lpLocalEnv);
  1926. if(compVal < 0) {
  1927. strcpy(lpStr, lpEnvPtr);
  1928. nLength = strlen(lpEnvPtr) + 1;
  1929. lpStr += nLength;
  1930. lpEnvPtr += nLength;
  1931. }
  1932. else {
  1933. char *ptr = strchr(lpLocalEnv, '=');
  1934. if(ptr && ptr[1]) {
  1935. strcpy(lpStr, lpLocalEnv);
  1936. lpStr += strlen(lpLocalEnv) + 1;
  1937. }
  1938. lpLocalEnv = GetIndex(dwEnvIndex);
  1939. if(compVal == 0) {
  1940. // this string was replaced
  1941. lpEnvPtr += strlen(lpEnvPtr) + 1;
  1942. }
  1943. }
  1944. }
  1945. }
  1946. while(lpLocalEnv) {
  1947. // still have environment overrides to add
  1948. // so copy the strings into place if not an override
  1949. char *ptr = strchr(lpLocalEnv, '=');
  1950. if(ptr && ptr[1]) {
  1951. strcpy(lpStr, lpLocalEnv);
  1952. lpStr += strlen(lpLocalEnv) + 1;
  1953. }
  1954. lpLocalEnv = GetIndex(dwEnvIndex);
  1955. }
  1956. // add final NULL
  1957. *lpStr = '\0';
  1958. }
  1959. // release the process environment strings
  1960. FreeEnvironmentStrings(lpAllocPtr);
  1961. return lpPtr;
  1962. }
  1963. void
  1964. CPerlHost::Reset(void)
  1965. {
  1966. dTHXo;
  1967. if(m_lppEnvList != NULL) {
  1968. for(DWORD index = 0; index < m_dwEnvCount; ++index) {
  1969. Safefree(m_lppEnvList[index]);
  1970. m_lppEnvList[index] = NULL;
  1971. }
  1972. }
  1973. m_dwEnvCount = 0;
  1974. }
  1975. void
  1976. CPerlHost::Clearenv(void)
  1977. {
  1978. dTHXo;
  1979. char ch;
  1980. LPSTR lpPtr, lpStr, lpEnvPtr;
  1981. if (m_lppEnvList != NULL) {
  1982. /* set every entry to an empty string */
  1983. for(DWORD index = 0; index < m_dwEnvCount; ++index) {
  1984. char* ptr = strchr(m_lppEnvList[index], '=');
  1985. if(ptr) {
  1986. *++ptr = 0;
  1987. }
  1988. }
  1989. }
  1990. /* get the process environment strings */
  1991. lpStr = lpEnvPtr = (LPSTR)GetEnvironmentStrings();
  1992. /* step over current directory stuff */
  1993. while(*lpStr == '=')
  1994. lpStr += strlen(lpStr) + 1;
  1995. while(*lpStr) {
  1996. lpPtr = strchr(lpStr, '=');
  1997. if(lpPtr) {
  1998. ch = *++lpPtr;
  1999. *lpPtr = 0;
  2000. Add(lpStr);
  2001. if (!w32_pseudo_id)
  2002. (void)win32_putenv(lpStr);
  2003. *lpPtr = ch;
  2004. }
  2005. lpStr += strlen(lpStr) + 1;
  2006. }
  2007. FreeEnvironmentStrings(lpEnvPtr);
  2008. }
  2009. char*
  2010. CPerlHost::Getenv(const char *varname)
  2011. {
  2012. dTHXo;
  2013. if (w32_pseudo_id) {
  2014. char *pEnv = Find(varname);
  2015. if (pEnv && *pEnv)
  2016. return pEnv;
  2017. }
  2018. return win32_getenv(varname);
  2019. }
  2020. int
  2021. CPerlHost::Putenv(const char *envstring)
  2022. {
  2023. dTHXo;
  2024. Add(envstring);
  2025. if (!w32_pseudo_id)
  2026. return win32_putenv(envstring);
  2027. return 0;
  2028. }
  2029. int
  2030. CPerlHost::Chdir(const char *dirname)
  2031. {
  2032. dTHXo;
  2033. int ret;
  2034. if (!dirname) {
  2035. errno = ENOENT;
  2036. return -1;
  2037. }
  2038. if (USING_WIDE()) {
  2039. WCHAR wBuffer[MAX_PATH];
  2040. A2WHELPER(dirname, wBuffer, sizeof(wBuffer));
  2041. ret = m_pvDir->SetCurrentDirectoryW(wBuffer);
  2042. }
  2043. else
  2044. ret = m_pvDir->SetCurrentDirectoryA((char*)dirname);
  2045. if(ret < 0) {
  2046. errno = ENOENT;
  2047. }
  2048. return ret;
  2049. }
  2050. #endif /* ___PerlHost_H___ */