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