123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
-
-
-
- #include <stdio.h>
- #include <dos.h>
- #include <string.h>
- #include <stdlib.h>
-
- typedef struct {
- char fill1[0x0A];
- int *prev_term_handler;
- int *prev_ctrl_c;
- int *prev_crit_error;
- char fill2[0x16];
- int envir_seg;
- } psp;
-
- typedef struct {
- char type;
- int psp_segment;
- int num_segments;
- char fill[11];
- char arena_data;
- } arena;
-
-
- #define NORMAL_ATYPE 0x4D
- #define LAST_ATYPE 0x5A
-
-
- static arena * get_next_arena (arena * ap) {
- return( MK_FP( FP_SEG(ap)+1+ap->num_segments, 0) );
- }
-
-
- static int is_valid_arena (arena * ap) {
- arena * ap1;
- if (ap->type == NORMAL_ATYPE &&
- (ap1=get_next_arena(ap))->type == NORMAL_ATYPE &&
- ( (ap1=get_next_arena(ap1))->type == NORMAL_ATYPE ||
- ap1->type == LAST_ATYPE) )
- return(0);
- return (1);
- }
-
-
- static arena * get_first_arena () {
-
- arena * ap, * ap1;
- int * temp;
- int segment;
-
- for (segment=0; segment<_CS; segment++) {
- ap = MK_FP(segment, 0);
- if ( is_valid_arena (ap) == 0) return (ap);
- }
- return(NULL);
- }
-
-
- static int is_valid_env (char * ad, int num_segs) {
- char * base_ad;
- base_ad = ad;
- while ( (*ad) && (((ad-base_ad)>>4) < num_segs) ) {
- if (strnicmp(ad, "COMSPEC=", 8)==0) return(0);
- ad += strlen(ad) + 1;
- }
- return (1);
- }
-
-
- static arena * get_arena_of_environment () {
-
-
- arena * ap;
- psp * pspp, * pspc;
- unsigned int i, ccseg;
-
-
- pspp = MK_FP(_psp,0);
-
- #ifdef DEBUG
- printf("prog psp=%p\n",pspp);
- #endif
-
-
- ccseg = FP_SEG (pspp->prev_crit_error);
- if ( (i=ccseg-32) < 60) i=60;
-
- while (ccseg>i) {
- pspc = MK_FP (ccseg, 0);
- if ( is_valid_arena((arena *) pspc) == 0) goto L1;
- ccseg--;
- }
- return (NULL);
-
- L1: pspc = MK_FP (++ccseg, 0);
- #ifdef DEBUG
- printf("comm.com=%p\n",pspc);
- #endif
-
-
-
- ap = MK_FP (pspc->envir_seg-1, 0);
- i = ap->num_segments;
-
- if (is_valid_arena (ap) == 0) {
- if (ap->psp_segment != FP_SEG(pspc)) goto L2;
- } else {
- i = 9;
- }
-
- if ( is_valid_env (&ap->arena_data, i) == 0 )
- return (ap);
-
-
-
- L2:
- if ( (ap=get_first_arena()) != NULL ) {
- while (ap->type != LAST_ATYPE) {
- #ifdef DEBUG
- printf("%p\n",ap);
- #endif
- if (ap->psp_segment == FP_SEG(pspc) &&
- is_valid_env (&ap->arena_data, ap->num_segments)==0 )
- return (ap);
-
- ap = get_next_arena(ap);
- }
- } return(NULL);
- }
-
-
-
- int settheenv(char * symbol, char * val) {
- int total_size,
- needed_size=0,
- strlength;
- char * sp, *op, *envir;
- char symb_len=strlen(symbol);
- char found=0;
- arena * ap;
-
- strupr(symbol);
-
-
- if ( (ap=get_arena_of_environment()) == NULL)
- return(1);
-
-
- total_size = 16 * ap->num_segments;
- envir = &ap->arena_data;
- op=sp=envir;
- while (*sp) {
- strlength = strlen(sp)+1;
- if ( *(sp+symb_len)=='=' &&
- strnicmp(sp,symbol,symb_len)==0 )
- found=1;
- else {
- needed_size += strlength;
- if (found) strcpy(op,sp);
- op = &op[strlength];
- }
- sp += strlength;
- }
- *op=0;
- if (strlen(val) > 0) {
- needed_size += 3 + strlen(symbol) + strlen(val);
- if (needed_size > total_size)
- return(1);
-
- strcpy(op, symbol); strcat(op, "="); strcat(op, val);
- op += strlen(op)+1;
- *op = 0;
- }
- return(0);
- }
|