-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathODSpawn.c
More file actions
1101 lines (968 loc) · 29.2 KB
/
Copy pathODSpawn.c
File metadata and controls
1101 lines (968 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* OpenDoors Online Software Programming Toolkit
* (C) Copyright 1991 - 1999 by Brian Pirie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* File: ODSpawn.c
*
* Description: Implements the od_spawn...() functions for suspending this
* program and executing a sub-program. Can be called by the
* user explicitly, or invoked for sysop OS shell.
*
* Revisions: Date Ver Who Change
* ---------------------------------------------------------------
* Oct 13, 1994 6.00 BP New file header format.
* Oct 21, 1994 6.00 BP Further isolated com routines.
* Dec 09, 1994 6.00 BP Use new directory access functions.
* Dec 13, 1994 6.00 BP Standardized coding style.
* Dec 31, 1994 6.00 BP Remove #ifndef USEINLINE DOS code.
* Jan 01, 1995 6.00 BP _waitdrain -> ODWaitDrain()
* Aug 19, 1995 6.00 BP 32-bit portability.
* Nov 11, 1995 6.00 BP Removed register keyword.
* Nov 14, 1995 6.00 BP Added include of odscrn.h.
* Nov 16, 1995 6.00 BP Removed oddoor.h, added odcore.h.
* Nov 17, 1995 6.00 BP Use new input queue mechanism.
* Nov 21, 1995 6.00 BP Ported to Win32.
* Dec 30, 1995 6.00 BP Added ODCALL for calling convention.
* Jan 23, 1996 6.00 BP Finished port to Win32.
* Feb 19, 1996 6.00 BP Changed version number to 6.00.
* Feb 23, 1996 6.00 BP Enable and test under Win32.
* Feb 27, 1996 6.00 BP Store screen info in our own struct.
* Mar 03, 1996 6.10 BP Begin version 6.10.
* Mar 19, 1996 6.10 BP MSVC15 source-level compatibility.
* Aug 10, 2003 6.23 SH *nix support - some functions not supported (Yet)
*/
#define BUILDING_OPENDOORS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include "OpenDoor.h"
#ifdef ODPLAT_NIX
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
#include "ODCore.h"
#include "ODGen.h"
#include "ODCom.h"
#include "ODPlat.h"
#include "ODScrn.h"
#include "ODInQue.h"
#include "ODInEx.h"
#include "ODUtil.h"
#include "ODKrnl.h"
#include "ODSwap.h"
#ifdef ODPLAT_WIN32
#include "ODFrame.h"
#endif /* ODPLAT_WIN32 */
#if defined(ODPLAT_WIN32) && defined(_MSC_VER)
#undef P_WAIT
#undef P_NOWAIT
#include <process.h>
#endif /* ODPLAT_WIN32 && _MSC_VER */
#ifdef ODPLAT_DOS
/* Local and global variables for memory swapping spawn routines. */
int _swap = 0; /* if 0, do swap */
char *_swappath = NULL; /* swap path */
int _useems = 0; /* if 0, use EMS */
int _required = 0; /* child memory requirement in K */
static long swapsize; /* swap size requirement in bytes */
static int ems = 2; /* if 0, EMS is available */
static int mapsize; /* size of page map information */
static unsigned int tempno = 1; /* tempfile number */
static char errtab[] = /* error table */
{
0,
EINVAL,
ENOENT,
ENOENT,
EMFILE,
EACCES,
EBADF,
ENOMEM,
ENOMEM,
ENOMEM,
E2BIG,
ENOEXEC,
EINVAL,
EINVAL,
-1,
EXDEV,
EACCES,
EXDEV,
ENOENT,
-1
};
static VECTOR vectab1[]=
{
0, 1, 0, 0,
1, 1, 0, 0,
2, 1, 0, 0,
3, 1, 0, 0,
0x1B, 1, 0, 0,
0x23, 1, 0, 0,
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 2, 0, 0, /* free record */
0, 3, 0, 0 /* end record */
};
static VECTOR vectab2[(sizeof vectab1)/(sizeof vectab1[0])];
/* Location function prototypes. */
int _spawnvpe(int nModeFlag, char *const pszPath, const char *const papszArgs[],
const char *const papszEnviron[]);
int _spawnve(int nModeFlag, char *pszPath, char *papszArgs[],
char * papszEnviron[]);
static void savevect(void);
#endif /* ODPLAT_DOS */
#ifdef ODPLAT_NIX
/* Location function prototypes. */
int _spawnvpe(int nModeFlag, char *const pszPath, const char *const papszArgs[],
const char *const papszEnviron[]);
#endif /* ODPLAT_NIX */
/* ----------------------------------------------------------------------------
* od_spawn()
*
* Executes the specified command line, suspending OpenDoors operations while
* the spawned-to program is running.
*
* Parameters: pszCommandLine - Command to execute along with any parameters.
*
* Return: TRUE on success, or FALSE on failure.
*/
ODAPIDEF BOOL ODCALL od_spawn(const char *pszCommandLine)
{
#ifdef ODPLAT_DOS
char *const apszArgs[4];
INT16 nReturnCode;
/* Log function entry if running in trace mode. */
TRACE(TRACE_API, "od_spawn()");
*apszArgs=getenv("COMSPEC");
apszArgs[1] = "/c";
apszArgs[2] = pszCommandLine;
apszArgs[3] = NULL;
if(*apszArgs != NULL)
{
if((nReturnCode = od_spawnvpe(P_WAIT, *apszArgs, apszArgs, NULL)) != -1
|| errno != ENOENT)
{
return(nReturnCode != -1);
}
}
*apszArgs = "command.com";
return(od_spawnvpe(P_WAIT, *apszArgs, apszArgs, NULL) != -1);
#endif /* ODPLAT_DOS */
#ifdef ODPLAT_WIN32
char *pch;
const char *apszArgs[3];
char szProgName[80];
/* Build command and arguments list. */
/* Build program name. */
ODStringCopy(szProgName, pszCommandLine, sizeof(szProgName));
pch = strchr(szProgName, ' ');
if(pch != NULL) *pch = '\0';
apszArgs[0] = szProgName;
/* Build arguments. */
pch = strchr(pszCommandLine, ' ');
if(pch == NULL)
{
apszArgs[1] = NULL;
}
else
{
apszArgs[1] = pch + 1;
apszArgs[2] = NULL;
}
/* Now, call od_spawnvpe(). */
return(od_spawnvpe(P_WAIT, szProgName, apszArgs, NULL) != -1);
#endif /* ODPLAT_WIN32 */
#ifdef ODPLAT_NIX
sigset_t block;
int retval;
/* Suspend kernel */
sigemptyset(&block);
sigaddset(&block,SIGALRM);
sigprocmask(SIG_BLOCK,&block,NULL);
retval=system(pszCommandLine);
/* Restore kernel */
sigemptyset(&block);
sigaddset(&block,SIGALRM);
sigprocmask(SIG_UNBLOCK,&block,NULL);
return(retval!=-1 && retval != 127);
#endif
}
/* ----------------------------------------------------------------------------
* od_spawnvpe()
*
* Executes the specified program, using the specified arguments and
* environment variables, optionally suspending OpenDoors operations while
* the spawned-to program is running.
*
* Parameters: nModeFlag - P_WAIT to for OpenDoors operations to be suspended
* while the spawned-to program is running, or
* P_NOWAIT if the calling program should continue to
* run while the spawned-to program is running. In
* non-multitasking environments, the only valid value
* of this parameters is P_WAIT.
*
* pszPath - Complete path and filename of the program to
* exectute.
*
* papszArg - Array of string pointers to command line arguments.
*
* papszEnv - Array of string pointers to environment variables.
*
* Return: -1 on failure or the spawned-to program's return value on
* success.
*/
ODAPIDEF INT16 ODCALL od_spawnvpe(INT16 nModeFlag, char *const pszPath,
const char *const papszArg[], const char *const papszEnv[])
{
INT16 nToReturn;
time_t nStartUnixTime;
DWORD dwQuotient;
#ifdef ODPLAT_WIN32
void *pWindow;
#endif /* ODPLAT_WIN32 */
#ifdef ODPLAT_DOS
char *pszDir;
BYTE *abtScreenBuffer;
INT nDrive;
tODScrnTextInfo TextInfo;
#endif /* ODPLAT_DOS */
/* Log function entry if running in trace mode. */
TRACE(TRACE_API, "od_spawnvpe()");
/* Initialize OpenDoors if it hasn't already been done. */
if(!bODInitialized) od_init();
#ifdef ODPLAT_DOS
/* Ensure the nModeFlag is P_WAIT, which is the only valid value for */
/* the MS-DOS version of OpenDoors. */
if(nModeFlag != P_WAIT)
{
od_control.od_error = ERR_PARAMETER;
return(-1);
}
/* Store current screen contents. */
if((abtScreenBuffer = malloc(4000)) == NULL)
{
od_control.od_error = ERR_MEMORY;
return(-1);
}
if((pszDir = malloc(256)) == NULL)
{
od_control.od_error = ERR_MEMORY;
free(abtScreenBuffer);
return(-1);
}
/* Store current display settings. */
ODScrnGetTextInfo(&TextInfo);
/* Set current output area to the full screen. */
ODScrnSetBoundary(1,1,80,25);
/* Store contents of entire screen. */
ODScrnGetText(1, 1, 80, 25, (char *)abtScreenBuffer);
/* Set the current display colour to grey on black. */
ODScrnSetAttribute(0x07);
/* Clear the screen if required. Otherwise, move the cursor to the */
/* upper left corner of the screen. */
if(od_control.od_clear_on_exit)
{
ODScrnClear();
}
else
{
ODScrnSetCursorPos(1, 1);
}
/* Store current directory. */
strcpy(pszDir, "X:\\");
pszDir[0] = 'A' + (nDrive = _getdrv());
_getcd(0, (char *)pszDir + 3);
#endif /* ODPLAT_DOS */
/* Remember when spawned to program was executed. */
nStartUnixTime = time(NULL);
if(nModeFlag == P_WAIT)
{
/* Display the spawn message box under Win32. */
#ifdef ODPLAT_WIN32
pWindow = ODScrnShowMessage("Running sub-program...", 0);
#endif /* ODPLAT_WIN32 */
/* Wait for up to ten seconds for outbound buffer to drain. */
ODWaitDrain(10000);
#ifdef OD_MULTITHREADED
/* Mutlithreaded versions of OpenDoors must shutdown the kernel */
/* before closing the serial port. */
ODKrnlShutdown();
#endif /* OD_MULTITHREADED */
/* Close serial port. */
if(od_control.baud != 0)
{
#ifdef ODPLAT_WIN32
/* Disable DTR response by the modem before closing the serial */
/* port, if this is required. */
ODInExDisableDTR();
#endif /* ODPLAT_WIN32 */
ODComClose(hSerialPort);
}
}
/* Execute specified program with the specified arguments. */
nToReturn = _spawnvpe(nModeFlag, pszPath, papszArg, papszEnv);
if(nModeFlag == P_WAIT)
{
/* Re-open serial port. */
if(od_control.baud != 0)
{
ODComOpen(hSerialPort);
}
#ifdef OD_MULTITHREADED
/* Mutlithreaded versions of OpenDoors must shutdown the kernel */
/* before closing the serial port, so reinitialize the kernel now. */
ODKrnlInitialize();
#endif /* OD_MULTITHREADED */
if(!(bIsShell || od_control.od_spawn_freeze_time))
{
ODDWordDivide(&dwQuotient, NULL, time(NULL) - nStartUnixTime, 60L);
od_control.user_timelimit -= (int)dwQuotient;
}
else
{
nNextTimeDeductTime += time(NULL) - nStartUnixTime;
}
/* Reset the time of the last input activity to the current time. */
/* This will prevent an immediate inactity timeout, regardless of */
/* how long the spawned-to program was active. */
ODInQueueResetLastActivity(hODInputQueue);
/* Clear inbound buffer. */
od_clear_keybuffer();
/* Remove the spawn message box under Win32. */
#ifdef ODPLAT_WIN32
ODScrnRemoveMessage(pWindow);
#endif /* ODPLAT_WIN32 */
}
#ifdef ODPLAT_DOS
/* Redisplay the door screen. */
ODScrnPutText(1, 1, 80, 25, (char *)abtScreenBuffer);
/* Restore cursor to old position. */
ODScrnSetBoundary(TextInfo.winleft, TextInfo.wintop,
TextInfo.winright, TextInfo.winbottom);
ODScrnSetAttribute(TextInfo.attribute);
ODScrnSetCursorPos(TextInfo.curx, TextInfo.cury);
_setdrvcd(nDrive, pszDir);
/* Free allocated space. */
free(abtScreenBuffer);
free(pszDir);
#endif /* ODPLAT_DOS */
/* Return appropriate value. */
return(nToReturn);
}
#ifdef ODPLAT_DOS
/* ----------------------------------------------------------------------------
* _spawnvpe() *** PRIVATE FUNCTION ***
*
* Executes a child program in the MS-DOS environment, swapping the calling
* program out of memory if enabled.
*
* Parameters: nModeFlag - Must be P_WAIT.
*
* pszPath - Name of program to execute.
*
* papszArgs - Array of command-line arguments.
*
* papszEnviron - Array of environment variables.
*
* Return: -1 on failure or the spawned-to program's return value on
* success.
*/
int _spawnvpe(int nModeFlag, char *const pszPath, const char *const papszArgs[],
const char *const papszEnviron[])
{
char *e;
char *p;
char buf[80];
int nReturnCode;
_swappath = (char *)(strlen(od_control.od_swapping_path) == 0 ? NULL
: (char *)od_control.od_swapping_path);
_useems = od_control.od_swapping_noems;
_swap = od_control.od_swapping_disable;
if((nReturnCode=_spawnve(nModeFlag, pszPath, papszArgs, papszEnviron))!=-1
|| errno!=ENOENT || *pszPath=='\\' || *pszPath=='/'
|| *pszPath && *(pszPath+1)==':' || (e=getenv("PATH"))==NULL)
{
return(nReturnCode);
}
for (;;e++)
{
if((p=strchr(e,';'))!=NULL)
{
if(p-e > 66)
{
e=p;
continue;
}
}
else if(strlen(e)>66)
{
return( -1 );
}
p=buf;
while(*e && *e!=';') *p++=*e++;
if(p>buf)
{
if(*(p-1)!='\\' && *(p-1)!='/') *p++ = '\\';
strcpy(p,pszPath);
if((nReturnCode=_spawnve(nModeFlag,buf,papszArgs,papszEnviron))!=-1 || errno!=ENOENT)
{
return(nReturnCode);
}
}
if(*e=='\0') return(-1);
}
}
/* ----------------------------------------------------------------------------
* addvect() *** PRIVATE FUNCTION ***
*
* Adds a vector to the vector table.
*
* Parameters: number - The vector number.
*
* opcode - Vector flags.
*
* Return: -1 on failure, or 0 on success.
*/
int addvect(int number, int opcode)
{
VECTOR *vect = vectab1;
if ( number < 0 || number > 0xFF ||
( opcode != IRET && opcode != CURRENT ))
{
errno = EINVAL;
return( -1 );
}
/* see if number is already in table */
while ( vect->flag != 3 && ( vect->flag == 2 ||
vect->number != ( char )number ))
{
vect++;
}
if ( vect->flag == 3 )
{
/* look for a free record */
vect = vectab1;
while ( vect->flag == CURRENT || vect->flag == IRET )
vect++;
}
if ( vect->flag != 3 )
{
vect->number = ( char )number;
vect->flag = ( char )opcode;
if ( opcode == CURRENT )
_getvect( number, &vect->vseg, &vect->voff );
return( 0 );
}
errno = ENOMEM;
return( -1 );
}
/* ----------------------------------------------------------------------------
* savevect() *** PRIVATE FUNCTION ***
*
* Saves current vector in vector table.
*
* Parameters: none
*
* Return: void
*/
static void savevect(void)
{
VECTOR *vect1 = vectab1;
VECTOR *vect2 = vectab2;
while ( vect1->flag != 3 )
{
if ( vect1->flag != 2 )
{
vect2->number = vect1->number;
vect2->flag = CURRENT;
_getvect( vect1->number, &vect2->vseg, &vect2->voff );
}
else
vect2->flag = 2; /* free */
vect1++;
vect2++;
}
vect2->flag = 3; /* end */
}
/* ----------------------------------------------------------------------------
* testfile() *** PRIVATE FUNCTION ***
*
* Tests swap file.
*
* Parameters: p - Path.
*
* file - File name.
*
* handle - File handle.
*
* Return: 1 on failure.
*/
static int testfile(char *p, char *file, int *handle)
{
unsigned int startno = tempno;
int nDrive = ( *file | 32 ) - 96; /* a = 1, b = 2, etc. */
int root;
unsigned int bytes; /* bytes per cluster */
unsigned int clusters; /* free clusters */
int need; /* clusters needed for swap file */
int nReturnCode; /* return code */
unsigned long dwQuotient;
unsigned long remainder;
if ( file + 2 == p )
{
*p++ = '\\';
if ( _getcd( nDrive, p )) /* get current directory */
return( 1 ); /* invalid drive */
p = file + strlen( file );
}
else
{
*p = '\0';
if ( ODFileAccessMode( file, 0 ))
return( 1 ); /* path does not exist */
}
if ( *( p - 1 ) != '\\' && *( p - 1 ) != '/' )
*p++ = '\\';
if ( p - file == 3 )
root = 1; /* is root directory */
else
root = 0; /* is not root directory */
strcpy( p, "swp" );
p += 3;
if ( _dskspace( nDrive, &bytes, &clusters ) != 0 )
return( 1 ); /* invalid drive */
ODDWordDivide(&dwQuotient, &remainder, swapsize, bytes);
need = (int)dwQuotient;
if ( remainder )
need++;
if ( root == 0 ) /* if subdirectory */
need++; /* in case the directory needs space */
if ( clusters < ( unsigned int )need )
return( 1 ); /* insufficient free disk space */
do
{
again: tempno = ( ++tempno ) ? tempno : 1;
if ( tempno == startno )
return( 1 ); /* extremely unlikely */
ltoa(( long )tempno, p, 10 );
}
while ( !ODFileAccessMode( file, 0 ));
/*
* The return code from _create will equal 80 if the user is running DOS 3.0
* or above and the file was created by another program between the access
* call and the _create call.
*/
if (( nReturnCode = _create( file, handle )) == 80 )
goto again;
return( nReturnCode );
}
/* ----------------------------------------------------------------------------
* tempfile() *** PRIVATE FUNCTION ***
*
* Creates a temporary swap file.
*
* Parameters: file - Filename
*
* handle - Handle to file.
*
* Return: 0 on success, or 1 on failure.
*/
static int tempfile(char *file, int *handle)
{
char *s = _swappath;
char *p = file;
if ( s )
{
for ( ;; s++ )
{
while ( *s && *s != ';' )
*p++ = *s++;
if ( p > file )
{
if ( p == file + 1 || file[ 1 ] != ':' )
{
memmove( file + 2, file, ( int )( p - file ));
*file = ( char )( _getdrv() + 'a' );
file[ 1 ] = ':';
p += 2;
}
if ( testfile( p, file, handle ) == 0 )
return( 0 );
p = file;
}
if ( *s == '\0' )
break;
}
}
else /* try the current directory */
{
*p++ = ( char )( _getdrv() + 'a' );
*p++ = ':';
if ( testfile( p, file, handle ) == 0 )
return( 0 );
}
errno = EACCES;
return( 1 );
}
/* ----------------------------------------------------------------------------
* cmdenv() *** PRIVATE FUNCTION ***
*
* Constructs environment.
*
* Parameters: papszArgs - Array of arguments.
*
* papszEnviron - Array of environment variables to add.
*
* command - The command specified.
*
* env - Pointer to environment.
*
* memory - Allocated memory.
*
* Return: Environment length.
*/
static int cmdenv(char **papszArgs, char **papszEnviron, char *command,
char **env, char **memory)
{
char **vp;
unsigned int elen = 0; /* environment length */
char *p;
int cnt;
int len;
/* construct environment */
if ( papszEnviron == NULL )
{
char far *parent_env;
char far *env_ptr;
int nul_count;
ASM mov ah, 0x62
ASM int 0x21
ASM push es
ASM mov es, bx
ASM mov ax, es:[0x2c]
ASM pop es
ASM mov word ptr parent_env, 0
ASM mov word ptr parent_env + 2, ax
env_ptr = parent_env;
nul_count = 0;
while(nul_count < 2)
{
if(*env_ptr)
{
nul_count = 0;
}
else
{
++nul_count;
}
++env_ptr;
++elen;
}
if ( elen > 32766 ) /* 32K - 2 */
{
errno = E2BIG;
return( -1 );
}
if (( p = malloc(elen + 15 )) == NULL )
{
errno = ENOMEM;
return( -1 );
}
*memory = p;
*( unsigned int * )&p = *( unsigned int * )&p + 15 & ~15;
*env = p;
len = elen;
while(len--)
{
*p++ = *parent_env++;
}
}
else
{
for ( vp = papszEnviron; *vp; vp++ )
{
elen += strlen( *vp ) + 1;
if ( elen > 32766 ) /* 32K - 2 */
{
errno = E2BIG;
return( -1 );
}
}
if (( p = malloc( ++elen + 15 )) == NULL )
{
errno = ENOMEM;
return( -1 );
}
*memory = p;
*( unsigned int * )&p = *( unsigned int * )&p + 15 & ~15;
*env = p;
for ( vp = papszEnviron; *vp; vp++ )
p = strchr( strcpy( p, *vp ), '\0' ) + 1;
*p = '\0'; /* final element */
}
/* construct command-line */
vp = papszArgs;
p = command + 1;
cnt = 0;
if (vp!=NULL && *vp )
{
while ( *++vp )
{
*p++ = ' ';
cnt++;
len = strlen( *vp );
if ( cnt + len > 125 )
{
errno = E2BIG;
free( *memory );
return( -1 );
}
strcpy( p, *vp );
p += len;
cnt += len;
}
}
*p = '\r';
*command = ( char )cnt;
return(( int )elen ); /* return environment length */
}
/* ----------------------------------------------------------------------------
* doxspawn() *** PRIVATE FUNCTION ***
*
* Performs spawn using memory swapping.
*
* Parameters: pszPath - Path to command to exectute.
*
* papszArg - Array of arugments.
*
* papszEnviron - Pointer to the environment.
*
* Return: 0 on success, or -1 on failure.
*/
static int doxspawn(char *pszPath, char *papszArgs[], char *papszEnviron[])
{
int nReturnCode = 0; /* assume do xspawn */
int doswap = 0; /* assume do swap */
int elen; /* environment length */
char *memory;
char *env; /* environment */
char command[ 128 ]; /* command-line */
long totalsize; /* parent and free memory in bytes */
int handle;
int pages;
char file[ 79 ];
char *mapbuf = NULL; /* buffer for map information */
/* construct the command-line and the environment */
if (( elen = cmdenv( papszArgs, papszEnviron, command, &env, &memory )) == -1 )
return( -1 );
if ( _swap == 0 )
{
if ( _useems == 0 )
{
if ( ems == 2 )
ems = _chkems( "EMMXXXX0", &mapsize );
if ( ems == 0 && ( mapbuf = malloc( mapsize )) == NULL )
{
errno = ENOMEM;
free( memory );
return( -1 );
}
}
if (( nReturnCode = _xsize( _psp, &swapsize, &totalsize )) == 0 )
{
if ( _required == 0 || totalsize - swapsize - 272
< (long)ODDWordShiftLeft(( long )_required , 10 ))
{
if ( ems == 0 && _useems == 0 )
{
pages = ( int )ODDWordShiftRight( swapsize , 14);
if ((long)ODDWordShiftLeft(( long )pages , 14 ) < swapsize )
pages++;
if ( _savemap( mapbuf ) == 0 &&
_getems( pages, &handle ) == 0 )
*file = '\0'; /* use EMS */
else if ( tempfile( file, &handle ) != 0 )
nReturnCode = -1; /* don't do xspawn */
}
else if ( tempfile( file, &handle ) != 0 )
nReturnCode = -1; /* don't do xspawn */
}
else
doswap = 1; /* don't do swap */
}
else
{
errno = errtab[ nReturnCode ];
nReturnCode = -1; /* don't do xspawn */
}
}
else
doswap = 1; /* don't do swap */
if ( nReturnCode == 0 )
{
savevect(); /* save current vectors */
nReturnCode = _xspawn( pszPath, command, env, vectab1, doswap, elen, file,
handle );
_setvect( vectab2 ); /* restore saved vectors */
if ( nReturnCode == 0 )
nReturnCode = _getrc(); /* get child return code */
else
{
errno = errtab[ nReturnCode ];
nReturnCode = -1;
}
/*
* If EMS was used, restore the page-mapping state of the expanded
* memory hardware.
*/
if ( doswap == 0 && *file == '\0' && _restmap( mapbuf ) != 0 )
{
errno = EACCES;
nReturnCode = -1;
}
}
if ( mapbuf )
free( mapbuf );
free( memory );
return( nReturnCode );
}
/* ----------------------------------------------------------------------------
* _spawnve() *** PRIVATE FUNCTION ***
*
* Performs a spawn.
*
* Parameters: nModeFlag - Must be P_WAIT
*
* pszPath - Command to execute.
*
* papszArgs - Command line arguments.
*
* papszEnviron - Pointer to environment.
*
* Return: void
*/
int _spawnve(int nModeFlag, char *pszPath, char *papszArgs[],
char * papszEnviron[])
{