1 | /* $Id: env.c,v 1.2 2000-04-17 02:26:04 bird Exp $
|
---|
2 | *
|
---|
3 | * Environment access functions
|
---|
4 | *
|
---|
5 | * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.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 |
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include <os2.h>
|
---|
22 |
|
---|
23 | #include "dev32.h"
|
---|
24 | #include "dev32hlp.h"
|
---|
25 | #include "log.h"
|
---|
26 | #include "ptda.h"
|
---|
27 | #include "OS2Krnl.h"
|
---|
28 | #include <string.h>
|
---|
29 |
|
---|
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 are paired, that means
|
---|
42 | * that the first string is the variable name and the
|
---|
43 | * following string is the value of the variable.
|
---|
44 | * AFAIK a variable can't have an empty value string!
|
---|
45 | * @param pszVar Name of the environment variable to find.
|
---|
46 | */
|
---|
47 | const char *ScanEnv(const char *paszEnv, const char *pszVar)
|
---|
48 | {
|
---|
49 | /*
|
---|
50 | * Loop thru the environment data until an empty string is reached.
|
---|
51 | */
|
---|
52 | while (*paszEnv != '\0')
|
---|
53 | {
|
---|
54 | register int i; /* Variable use to store the compare result. */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Variable name.
|
---|
58 | * Check if it's matching the name we're searching for and skip the variable name.
|
---|
59 | */
|
---|
60 | i = stricmp(paszEnv, pszVar);
|
---|
61 | paszEnv += strlen(paszEnv) + 1;
|
---|
62 | if (i == 0)
|
---|
63 | { /* Variable was found. Return pointer to the value. */
|
---|
64 | return paszEnv;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * !Paranoia!
|
---|
69 | * If no value string we'll quit. This may be an IPE, if not it might
|
---|
70 | * cause one if we continue processing the environment data.
|
---|
71 | */
|
---|
72 | if (*paszEnv == '\0')
|
---|
73 | break;
|
---|
74 |
|
---|
75 | /* Skip value */
|
---|
76 | paszEnv += strlen(paszEnv) + 1;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return NULL;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Get the linear pointer to the environment data.
|
---|
85 | *
|
---|
86 | * @returns Pointer to environment data.
|
---|
87 | * NULL on failure.
|
---|
88 | */
|
---|
89 | const char *GetEnv(void)
|
---|
90 | {
|
---|
91 | /* There is probably two ways of getting the environment data for a the
|
---|
92 | * current process: 1) get it from the PTDA->ptda_environ
|
---|
93 | * 2) Local infosegment (LIS from GetDosVar devhlp)
|
---|
94 | * I am not sure which one of these works best. What I know is that
|
---|
95 | * method 1) is used by the w_GetEnv API worker. This API is called at
|
---|
96 | * Ring-0 from some delete file operation. (Which uses it to get the
|
---|
97 | * DELETEDIR environment variable.) The w_GetEnv API worker is 16-bit
|
---|
98 | * I don't want to thunk around using that. There for I'll implement
|
---|
99 | * my own GetEnv. So, currently I'll write the code for both 1) and
|
---|
100 | * 2), testing will show which one of them are most handy.
|
---|
101 | */
|
---|
102 |
|
---|
103 | #if 1
|
---|
104 | PPTDA pPTDACur; /* Pointer to the current (system context) PTDA */
|
---|
105 | PPTDA pPTDA; /* PTDA in question. */
|
---|
106 | USHORT hobEnviron; /* Object handle of the environ block */
|
---|
107 | APIRET rc; /* Return from VMObjHandleInfo. */
|
---|
108 | USHORT ushPTDA; /* Handle of the context PTDA. (VMObjH..) */
|
---|
109 | ULONG ulAddr = 0; /* Address of the environment data. */
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Use PTDA (1):
|
---|
113 | * Get the current PTDA. (Fail if this call failes.)
|
---|
114 | * IF pPTDAExecChild isn't NULL THEN try get environment for that first.
|
---|
115 | * IF failed or no pPTDAExecChild THEN try get environment from pPTDA.
|
---|
116 | */
|
---|
117 | pPTDACur = ptdaGetCur();
|
---|
118 | if (pPTDA != NULL)
|
---|
119 | {
|
---|
120 | pPTDA = ptdaGet_pPTDAExecChild(pPTDA);
|
---|
121 | if (pPTDA != NULL)
|
---|
122 | {
|
---|
123 | hobEnviron = ptdaGet_ptda_environ(pPTDA);
|
---|
124 | if (hobEnviron != 0)
|
---|
125 | {
|
---|
126 | rc = VMObjHandleInfo(hobEnviron, SSToDS(&ulAddr), SSToDS(&ushPTDA));
|
---|
127 | if (rc == NO_ERROR)
|
---|
128 | return (const char *)ulAddr;
|
---|
129 | kprintf(("GetEnv: VMObjHandleInfo failed with rc=%d for hob=0x%04x\n", rc, hobEnviron));
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | hobEnviron = ptdaGet_ptda_environ(pPTDACur);
|
---|
134 | if (hobEnviron != 0)
|
---|
135 | {
|
---|
136 | rc = VMObjHandleInfo(hobEnviron, SSToDS(&ulAddr), SSToDS(&ushPTDA));
|
---|
137 | if (rc != NO_ERROR)
|
---|
138 | {
|
---|
139 | kprintf(("GetEnv: VMObjHandleInfo failed with rc=%d for hob=0x%04x\n", rc, hobEnviron));
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | kprintf(("GetEnv: Failed to get current PTDA.\n"));
|
---|
146 | }
|
---|
147 |
|
---|
148 | return (const char *)ulAddr;
|
---|
149 |
|
---|
150 | #else
|
---|
151 |
|
---|
152 | struct InfoSegLDT * pLIS; /* Pointer to local infosegment. */
|
---|
153 | PVOID pv; /* Address to return. */
|
---|
154 |
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * Use LocalInfoSegment (2)
|
---|
158 | * Get the LIS using Dh32_GetDosVar
|
---|
159 | */
|
---|
160 | pLIS = (struct InfoSegLDT*)D32Hlp_GetDOSVar(DHGETDOSV_LOCINFOSEG, 0);
|
---|
161 | if (pLIS == NULL)
|
---|
162 | {
|
---|
163 | kprintf(("GetEnv: Failed to get local info segment\n"));
|
---|
164 | return NULL;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (pLIS->LIS_AX <= 3)
|
---|
168 | {
|
---|
169 | kprintf(("GetEnv: environment selector is %d, ie. NULL\n", pLIS->LIS_AX));
|
---|
170 | return NULL;
|
---|
171 | }
|
---|
172 |
|
---|
173 | pv = D32Hlp_VirtToLin2(pLIS->LIS_AX, 0);
|
---|
174 | if (pv != NULL)
|
---|
175 | {
|
---|
176 | kprintf(("GetEnv: VirtToLin2 failed to thunk %04x:0000 to linar address\n", pLIS->LIS_AX));
|
---|
177 | }
|
---|
178 | return (const char *)pv;
|
---|
179 | #endif
|
---|
180 | }
|
---|