| 1 | /* $Id: env.c,v 1.5 2001-02-10 11:11:47 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * Environment access functions
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
|
|---|
| 6 | *
|
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | /*******************************************************************************
|
|---|
| 12 | * Defined Constants And Macros *
|
|---|
| 13 | *******************************************************************************/
|
|---|
| 14 | #define INCL_DOSERRORS /* Error codes */
|
|---|
| 15 | #define INCL_OS2KRNL_VM /* OS2KRNL: Virtual Memory Management */
|
|---|
| 16 | #define INCL_OS2KRNL_PTDA /* OS2KRNL: (per)ProcessTaskDataArea */
|
|---|
| 17 |
|
|---|
| 18 | /*******************************************************************************
|
|---|
| 19 | * Header Files *
|
|---|
| 20 | *******************************************************************************/
|
|---|
| 21 | #include <os2.h>
|
|---|
| 22 |
|
|---|
| 23 | #include "devSegDf.h" /* Win32k segment definitions. */
|
|---|
| 24 | #include "dev32.h"
|
|---|
| 25 | #include "dev32hlp.h"
|
|---|
| 26 | #include "log.h"
|
|---|
| 27 | #include "OS2Krnl.h"
|
|---|
| 28 | #include <string.h>
|
|---|
| 29 | #include "macros.h"
|
|---|
| 30 | #include "env.h"
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Scans the given environment data for a given environment variable and returns
|
|---|
| 35 | * its value if found.
|
|---|
| 36 | * @returns Pointer to environment variable value for the variable given in
|
|---|
| 37 | * pszVar.
|
|---|
| 38 | * If the variable wasn't found NULL is returned.
|
|---|
| 39 | * @param paszEnv Pointer to the environment data to search.
|
|---|
| 40 | * The environment data is a list of zero-strings terminated
|
|---|
| 41 | * by an empty string. The strings consists of two parts which
|
|---|
| 42 | * are separated by a euqal char ('='). The first part is the
|
|---|
| 43 | * variable name. The second part is the value of the variable.
|
|---|
| 44 | *
|
|---|
| 45 | * IF this is NULL we'll simply return NULL.
|
|---|
| 46 | * @param pszVar Name of the environment variable to find. (NULL not allowed.)
|
|---|
| 47 | */
|
|---|
| 48 | const char *ScanEnv(const char *paszEnv, const char *pszVar)
|
|---|
| 49 | {
|
|---|
| 50 | int cchVar;
|
|---|
| 51 | /*
|
|---|
| 52 | * Return if environment not found.
|
|---|
| 53 | */
|
|---|
| 54 | #ifdef DEBUG
|
|---|
| 55 | if (pszVar < (const char *)0x10000 || *pszVar == '\0')
|
|---|
| 56 | kprintf(("ScanEnv: Invalid parameter pszVar (%p)\n", pszVar));
|
|---|
| 57 | #endif
|
|---|
| 58 | if (paszEnv == NULL)
|
|---|
| 59 | return NULL;
|
|---|
| 60 | #ifdef DEBUG
|
|---|
| 61 | if (paszEnv < (const char *)0x10000)
|
|---|
| 62 | kprintf(("ScanEnv: Invalid parameter paszEnv (%p)\n", paszEnv));
|
|---|
| 63 | #endif
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | * Loop thru the environment data until an empty string is reached.
|
|---|
| 67 | */
|
|---|
| 68 | cchVar = strlen(pszVar);
|
|---|
| 69 | while (*paszEnv != '\0')
|
|---|
| 70 | {
|
|---|
| 71 | /*
|
|---|
| 72 | * Check if the variable name is it's matching the one we're searching for.
|
|---|
| 73 | */
|
|---|
| 74 | const char *pszEqual = strchr(paszEnv, '=');
|
|---|
| 75 | if (pszEqual != NULL && (pszEqual - paszEnv) == cchVar
|
|---|
| 76 | && memcmp(paszEnv, pszVar, cchVar) == 0
|
|---|
| 77 | )
|
|---|
| 78 | {
|
|---|
| 79 | /*
|
|---|
| 80 | * Variable was found. Return pointer to the value.
|
|---|
| 81 | */
|
|---|
| 82 | return pszEqual + 1;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /*
|
|---|
| 86 | * Skip this variable. (Don't use pszEqual since it might be NULL)
|
|---|
| 87 | */
|
|---|
| 88 | paszEnv += strlen(paszEnv) + 1;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | return NULL;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Get the linear pointer to the environment data for the current
|
|---|
| 97 | * process or the process being started (EXECed).
|
|---|
| 98 | *
|
|---|
| 99 | * @param fExecChild TRUE: Get exec child environment.
|
|---|
| 100 | * (Not supported by method 2)
|
|---|
| 101 | * FALSE: Get current process environment.
|
|---|
| 102 | * @returns Pointer to environment data.
|
|---|
| 103 | * NULL on failure.
|
|---|
| 104 | */
|
|---|
| 105 | const char *GetEnv(BOOL fExecChild)
|
|---|
| 106 | {
|
|---|
| 107 | /* There are probably two ways of getting the environment data for the
|
|---|
| 108 | * current process: 1) get it from the PTDA->ptda_environ
|
|---|
| 109 | * 2) Local infosegment (LIS from GetDosVar devhlp)
|
|---|
| 110 | * I am not sure which one of these which works best. What I know is that
|
|---|
| 111 | * method 1) is used by the w_GetEnv API worker. This API is called at
|
|---|
| 112 | * Ring-0 from some delete file operation. (Which uses it to get the
|
|---|
| 113 | * DELETEDIR environment variable.) The w_GetEnv API worker is 16-bit
|
|---|
| 114 | * I don't want to thunk around using that. There for I'll implement
|
|---|
| 115 | * my own GetEnv. So, currently I'll write the code for both 1) and
|
|---|
| 116 | * 2), testing will show which one of them are the better.
|
|---|
| 117 | */
|
|---|
| 118 |
|
|---|
| 119 | #if 1
|
|---|
| 120 | PPTDA pPTDACur; /* Pointer to the current (system context) PTDA */
|
|---|
| 121 | PPTDA pPTDA; /* PTDA in question. */
|
|---|
| 122 | USHORT hobEnviron; /* Object handle of the environ block */
|
|---|
| 123 | APIRET rc; /* Return from VMObjHandleInfo. */
|
|---|
| 124 | USHORT ushPTDA; /* Handle of the context PTDA. (VMObjH..) */
|
|---|
| 125 | ULONG ulAddr = 0; /* Address of the environment data. */
|
|---|
| 126 |
|
|---|
| 127 | /*
|
|---|
| 128 | * Use PTDA (1):
|
|---|
| 129 | * Get the current PTDA. (Fail if this call failes.)
|
|---|
| 130 | * IF pPTDAExecChild isn't NULL THEN try get environment for that first.
|
|---|
| 131 | * IF failed or no pPTDAExecChild THEN try get environment from pPTDA.
|
|---|
| 132 | */
|
|---|
| 133 | pPTDACur = ptdaGetCur();
|
|---|
| 134 | if (pPTDACur != NULL)
|
|---|
| 135 | {
|
|---|
| 136 | pPTDA = ptdaGet_pPTDAExecChild(pPTDACur);
|
|---|
| 137 | if (pPTDA != NULL && fExecChild)
|
|---|
| 138 | {
|
|---|
| 139 | hobEnviron = ptdaGet_ptda_environ(pPTDA);
|
|---|
| 140 | if (hobEnviron != 0)
|
|---|
| 141 | {
|
|---|
| 142 | rc = VMObjHandleInfo(hobEnviron, SSToDS(&ulAddr), SSToDS(&ushPTDA));
|
|---|
| 143 | if (rc == NO_ERROR)
|
|---|
| 144 | return (const char *)ulAddr;
|
|---|
| 145 | kprintf(("GetEnv: (1) VMObjHandleInfo failed with rc=%d for hob=0x%04x \n", rc, hobEnviron));
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | hobEnviron = ptdaGet_ptda_environ(pPTDACur);
|
|---|
| 150 | if (hobEnviron != 0)
|
|---|
| 151 | {
|
|---|
| 152 | rc = VMObjHandleInfo(hobEnviron, SSToDS(&ulAddr), SSToDS(&ushPTDA));
|
|---|
| 153 | if (rc == NO_ERROR)
|
|---|
| 154 | return (const char *)ulAddr;
|
|---|
| 155 | kprintf(("GetEnv: (2) VMObjHandleInfo failed with rc=%d for hob=0x%04x\n", rc, hobEnviron));
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | else
|
|---|
| 159 | { /* Not called at task time? No current task! */
|
|---|
| 160 | kprintf(("GetEnv: Failed to get current PTDA.\n"));
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | return NULL;
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | #else
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | struct InfoSegLDT * pLIS; /* Pointer to local infosegment. */
|
|---|
| 170 | PVOID pv; /* Address to return. */
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 | /*
|
|---|
| 174 | * Use LocalInfoSegment (2)
|
|---|
| 175 | * Get the LIS using Dh32_GetDosVar
|
|---|
| 176 | */
|
|---|
| 177 | pLIS = (struct InfoSegLDT*)D32Hlp_GetDOSVar(DHGETDOSV_LOCINFOSEG, 0);
|
|---|
| 178 | if (pLIS == NULL)
|
|---|
| 179 | {
|
|---|
| 180 | kprintf(("GetEnv: Failed to get local info segment\n"));
|
|---|
| 181 | NOREF(fExecChild);
|
|---|
| 182 | return NULL;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | if (pLIS->LIS_AX <= 3)
|
|---|
| 186 | {
|
|---|
| 187 | kprintf(("GetEnv: environment selector is %d, ie. NULL\n", pLIS->LIS_AX));
|
|---|
| 188 | return NULL;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | pv = D32Hlp_VirtToLin2(pLIS->LIS_AX, 0);
|
|---|
| 192 | if (pv != NULL)
|
|---|
| 193 | {
|
|---|
| 194 | kprintf(("GetEnv: VirtToLin2 failed to thunk %04x:0000 to linar address\n", pLIS->LIS_AX));
|
|---|
| 195 | }
|
|---|
| 196 | return (const char *)pv;
|
|---|
| 197 | #endif
|
|---|
| 198 | }
|
|---|