Sys: setEnv, for this program and those it starts On Linux the environment becomes Sys's own the first time a variable is added, as C's setenv makes it: an array of pointers that doubles when full, a variable set again keeping its place, and the NAME=values from blocks of 64 KB, never given back, so that what env gave stays good. env, envAt, spawn and exec read it through environment. Over C, setenv. Library growth that breaks no program, met in rwalk's tests.
| @@ -583,23 +583,6 @@ | ||
| 583 | 583 | |
| 584 | 584 | Met in rrem's parser, documenting `(?>x*)`. |
| 585 | 585 | |
| 586 | -## Setting an environment variable | |
| 587 | - | |
| 588 | -`Sys.env` reads a variable; nothing sets one. A test can't give the | |
| 589 | -programs it runs, or the code it tests, an environment of its own: | |
| 590 | -rwalk's tests read the user's real global ignore file, since they | |
| 591 | -can't point `XDG_CONFIG_HOME` at a directory of their own. | |
| 592 | - | |
| 593 | -The proposal, in `Sys`: | |
| 594 | - | |
| 595 | -``` | |
| 596 | -proc setEnv*(name, value: []U8): INT; (* sets name to value, for this program and those it starts; 0, or minus an errno *) | |
| 597 | -``` | |
| 598 | - | |
| 599 | -It breaks no program. | |
| 600 | - | |
| 601 | -Met in rwalk's tests. | |
| 602 | - | |
| 603 | 586 | ## A hint for a hexadecimal name |
| 604 | 587 | |
| 605 | 588 | `FFFDH`, meant as a number, is an identifier, and the error is "FFFDH |
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | | Module | What | |
| 12 | 12 | |---|---| |
| 13 | 13 | | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | |
| 14 | -| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; what an errno means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | | |
| 14 | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `setEnv`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; what an errno means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | | |
| 15 | 15 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 16 | 16 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 17 | 17 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| @@ -254,6 +254,7 @@ | ||
| 254 | 254 | |
| 255 | 255 | proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) |
| 256 | 256 | proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) |
| 257 | +proc setEnv*(name, value: []U8): INT; (* name set to value, for env and the programs started *) | |
| 257 | 258 | proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) |
| 258 | 259 | proc setWorkDir*(path: []U8): INT; |
| 259 | 260 | |
| @@ -424,7 +425,10 @@ | ||
| 424 | 425 | taken away from the mode of every file this program, and those it |
| 425 | 426 | starts, make, and says what they were. `envAt` gives the environment |
| 426 | 427 | one variable at a time, from 0, for a shell to list it and to pass it |
| 427 | -on whole with `onlyEnv`. `getMode` reads a terminal's modes, and | |
| 428 | +on whole with `onlyEnv`. `setEnv` refuses a name that is empty or | |
| 429 | +holds an `=`, and a 0 in either, with -22; as C's `setenv`, it isn't | |
| 430 | +safe while another thread reads the environment, and what `env` gave | |
| 431 | +for a variable may be gone once it sets that variable again. `getMode` reads a terminal's modes, and | |
| 428 | 432 | `setMode` sets them again once what was written is sent, so that a |
| 429 | 433 | shell keeps them for each job: a program stopped by Ctrl-Z with its |
| 430 | 434 | terminal raw gets it back raw, and the shell its own between. |
| @@ -55,6 +55,7 @@ | ||
| 55 | 55 | extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32; |
| 56 | 56 | extern proc getrandom*(buf: ^U8; n: UINT; flags: U32): INT; |
| 57 | 57 | extern proc getenv*(name: ^U8): ^U8; |
| 58 | +extern proc setenv*(name, value: ^U8; overwrite: I32): I32; | |
| 58 | 59 | extern proc strerror*(err: I32): ^U8; |
| 59 | 60 | extern proc getcwd*(buf: ^U8; n: UINT): ^U8; |
| 60 | 61 | extern proc chdir*(path: ^U8): I32; |
| @@ -138,6 +138,12 @@ | ||
| 138 | 138 | sigFd: INT; (* the signalfd that gives them, once caughtSigs isn't empty *) |
| 139 | 139 | noted: [65]INT; (* 1 at sig when a signal handle took has come, until came asks *) |
| 140 | 140 | uts: [390]U8; (* struct utsname, which hostName gives the nodename of *) |
| 141 | + (* the environment setEnv made, nil while it is the one the program | |
| 142 | + started with; its variables, and the pointers it has room for *) | |
| 143 | + envp: ^^U8; | |
| 144 | + envLen, envCap: INT; | |
| 145 | + envText: ^U8; (* where setEnv puts its next NAME=value *) | |
| 146 | + envLeft: INT; (* and the bytes left there *) | |
| 141 | 147 | |
| 142 | 148 | (* Writes s to the file descriptor fd; the number of bytes written, or |
| 143 | 149 | minus an errno. *) |
| @@ -1151,21 +1157,36 @@ | ||
| 1151 | 1157 | return errorTexts[err]; |
| 1152 | 1158 | end; |
| 1153 | 1159 | |
| 1154 | -(* The value of the environment variable name; "" if it isn't set. The | |
| 1155 | - environment follows the arguments and their nil. *) | |
| 1160 | +(* The environment, its NAME=values as C strings and a nil: the one the | |
| 1161 | + program started with, after the arguments and their nil, until | |
| 1162 | + setEnv makes one of its own. *) | |
| 1163 | +proc environment: ^^U8; | |
| 1164 | +begin | |
| 1165 | + if envp # nil then return envp; end; | |
| 1166 | + return ARGV() + (ARGC() + 1); | |
| 1167 | +end; | |
| 1168 | + | |
| 1169 | +(* Whether the NAME=value s, a C string, is name's. *) | |
| 1170 | +proc named(s: ^U8; name: []U8): BOOL; | |
| 1171 | +var n: INT; | |
| 1172 | +begin | |
| 1173 | + n := 0; | |
| 1174 | + while (n < LEN(name)) and (s[n] = name[n]) do INC(n); end; | |
| 1175 | + return (n = LEN(name)) and (s[n] = '='); | |
| 1176 | +end; | |
| 1177 | + | |
| 1178 | +(* The value of the environment variable name; "" if it isn't set. *) | |
| 1156 | 1179 | proc env*(name: []U8): []U8; |
| 1157 | -var e: ^^U8; s: ^U8; i, n, m: INT; | |
| 1180 | +var e: ^^U8; s: ^U8; i, m: INT; | |
| 1158 | 1181 | begin |
| 1159 | - e := ARGV() + (ARGC() + 1); | |
| 1182 | + e := environment(); | |
| 1160 | 1183 | i := 0; |
| 1161 | 1184 | while e[i] # nil do |
| 1162 | - s := e[i]; | |
| 1163 | - n := 0; | |
| 1164 | - while (n < LEN(name)) and (s[n] = name[n]) do INC(n); end; | |
| 1165 | - if (n = LEN(name)) and (s[n] = '=') then | |
| 1166 | - m := n + 1; | |
| 1185 | + if named(e[i], name) then | |
| 1186 | + s := e[i]; | |
| 1187 | + m := LEN(name) + 1; | |
| 1167 | 1188 | while s[m] # 0 do INC(m); end; |
| 1168 | - return s[n + 1..m]; | |
| 1189 | + return s[LEN(name) + 1..m]; | |
| 1169 | 1190 | end; |
| 1170 | 1191 | INC(i); |
| 1171 | 1192 | end; |
| @@ -1178,7 +1199,7 @@ | ||
| 1178 | 1199 | var e: ^^U8; s: ^U8; j, n: INT; |
| 1179 | 1200 | begin |
| 1180 | 1201 | if i < 0 then return "", false; end; |
| 1181 | - e := ARGV() + (ARGC() + 1); | |
| 1202 | + e := environment(); | |
| 1182 | 1203 | for j := 0 to i do |
| 1183 | 1204 | if e[j] = nil then return "", false; end; |
| 1184 | 1205 | end; |
| @@ -1280,6 +1301,71 @@ | ||
| 1280 | 1301 | return p, size; |
| 1281 | 1302 | end; |
| 1282 | 1303 | |
| 1304 | +(* Sets the environment variable name to value, for env and for the | |
| 1305 | + programs this one starts; 0, or minus an errno: -22 if name is empty | |
| 1306 | + or holds an = or a 0, or value a 0, and -12 if there is no memory. | |
| 1307 | + As C's setenv does, it makes the environment the program's own the | |
| 1308 | + first time it adds a variable: an array of pointers that doubles | |
| 1309 | + when it is full, the old one given back. A variable set again keeps | |
| 1310 | + its place. A NAME=value is never given back, so that what env gave | |
| 1311 | + stays good for the rest of the run; they are taken in turn from | |
| 1312 | + blocks of 64 KB. It isn't safe while another thread reads the | |
| 1313 | + environment or starts a program, as C's setenv isn't. *) | |
| 1314 | +proc setEnv*(name, value: []U8): INT; | |
| 1315 | +var e, p: ^^U8; s: ^U8; size, cap, n, i: INT; | |
| 1316 | +begin | |
| 1317 | + if LEN(name) = 0 then return -22; end; (* EINVAL *) | |
| 1318 | + for i := 0 to LEN(name) - 1 do | |
| 1319 | + if (name[i] = '=') or (name[i] = 0) then return -22; end; | |
| 1320 | + end; | |
| 1321 | + for i := 0 to LEN(value) - 1 do | |
| 1322 | + if value[i] = 0 then return -22; end; | |
| 1323 | + end; | |
| 1324 | + (* NAME=value, written where the next one goes, and taken only once | |
| 1325 | + the array has room for it *) | |
| 1326 | + size := LEN(name) + LEN(value) + 2; | |
| 1327 | + if size > envLeft then | |
| 1328 | + n := 10000H; | |
| 1329 | + if size > n then n := size; end; | |
| 1330 | + s := mmap(n); | |
| 1331 | + if s = nil then return -12; end; (* ENOMEM *) | |
| 1332 | + envText := s; | |
| 1333 | + envLeft := n; | |
| 1334 | + end; | |
| 1335 | + s := envText; | |
| 1336 | + COPY(s[0..LEN(name)], name); | |
| 1337 | + s[LEN(name)] := '='; | |
| 1338 | + COPY(s[LEN(name) + 1..size - 1], value); | |
| 1339 | + s[size - 1] := 0; | |
| 1340 | + e := environment(); | |
| 1341 | + if envp = nil then | |
| 1342 | + envLen := 0; | |
| 1343 | + while e[envLen] # nil do INC(envLen); end; | |
| 1344 | + end; | |
| 1345 | + i := 0; | |
| 1346 | + while (i < envLen) and not named(e[i], name) do INC(i); end; | |
| 1347 | + if i = envLen then | |
| 1348 | + (* a new variable: the array grows if it is the first, or full *) | |
| 1349 | + if (envp = nil) or (envLen + 1 = envCap) then | |
| 1350 | + cap := 2 * (envLen + 1); | |
| 1351 | + if cap < 32 then cap := 32; end; | |
| 1352 | + p := ^^U8(mmap(cap * SIZE(^U8))); | |
| 1353 | + if p = nil then return -12; end; | |
| 1354 | + if envLen > 0 then COPY(p[0..envLen], e[0..envLen]); end; | |
| 1355 | + if envp # nil then munmap(^U8(envp), envCap * SIZE(^U8)); end; | |
| 1356 | + envp := p; | |
| 1357 | + envCap := cap; | |
| 1358 | + e := p; | |
| 1359 | + end; | |
| 1360 | + INC(envLen); | |
| 1361 | + e[envLen] := nil; | |
| 1362 | + end; | |
| 1363 | + e[i] := s; | |
| 1364 | + envText := envText + size; | |
| 1365 | + envLeft := envLeft - size; | |
| 1366 | + return 0; | |
| 1367 | +end; | |
| 1368 | + | |
| 1283 | 1369 | (* A pipe: the fd to read and the fd to write, both closed in a program |
| 1284 | 1370 | exec starts; or minus an errno and -1. *) |
| 1285 | 1371 | proc pipe*: (INT, INT); |
| @@ -1553,7 +1639,7 @@ | ||
| 1553 | 1639 | if p.onlyEnv then |
| 1554 | 1640 | argv, size := argsAndEnv(p.args, p.env, ADR(none)); |
| 1555 | 1641 | else |
| 1556 | - argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1)); | |
| 1642 | + argv, size := argsAndEnv(p.args, p.env, environment()); | |
| 1557 | 1643 | end; |
| 1558 | 1644 | if argv = nil then return -12; end; (* ENOMEM *) |
| 1559 | 1645 | search := env("PATH"); (* read here, before the fork *) |
| @@ -1604,7 +1690,7 @@ | ||
| 1604 | 1690 | if p.onlyEnv then |
| 1605 | 1691 | argv, size := argsAndEnv(p.args, p.env, ADR(none)); |
| 1606 | 1692 | else |
| 1607 | - argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1)); | |
| 1693 | + argv, size := argsAndEnv(p.args, p.env, environment()); | |
| 1608 | 1694 | end; |
| 1609 | 1695 | if argv = nil then return -12; end; (* ENOMEM *) |
| 1610 | 1696 | r := takeOn(p, name, dir); |
| @@ -2146,6 +2232,26 @@ | ||
| 2146 | 2232 | if (w # 80) or (h # 24) then exit(2); end; |
| 2147 | 2233 | (* the environment *) |
| 2148 | 2234 | if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; |
| 2235 | + (* the environment set: refused, added, set again, grown past its first array *) | |
| 2236 | + if (setEnv("", "x") # -22) or (setEnv("A=B", "x") # -22) or (setEnv("A", "\x00") # -22) then exit(2); end; | |
| 2237 | + if (setEnv("RAZ_SYS_TEST", "one") # 0) or (env("RAZ_SYS_TEST") # "one") then exit(2); end; | |
| 2238 | + name := env("RAZ_SYS_TEST"); | |
| 2239 | + if (setEnv("RAZ_SYS_TEST", "") # 0) or (env("RAZ_SYS_TEST") # "") or (name # "one") then exit(2); end; | |
| 2240 | + buf := "RAZ_SYS_TEST_.."; | |
| 2241 | + for k := 0 to 99 do | |
| 2242 | + buf[13] := U8('0' + k div 10); | |
| 2243 | + buf[14] := U8('0' + k mod 10); | |
| 2244 | + if setEnv(buf[0..15], buf[13..15]) # 0 then exit(2); end; | |
| 2245 | + end; | |
| 2246 | + if (env("RAZ_SYS_TEST_42") # "42") or (env("RAZ_SYS_TEST") # "") or (LEN(env("PATH")) = 0) then exit(2); end; | |
| 2247 | + n := 0; | |
| 2248 | + k := 0; | |
| 2249 | + repeat | |
| 2250 | + name, ok := envAt(k); | |
| 2251 | + if (LEN(name) > 12) and (name[0..12] = "RAZ_SYS_TEST") then INC(n); end; | |
| 2252 | + INC(k); | |
| 2253 | + until not ok; | |
| 2254 | + if n # 101 then exit(2); end; | |
| 2149 | 2255 | name, err := workDir(); |
| 2150 | 2256 | if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end; |
| 2151 | 2257 | if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end; |
| @@ -1141,6 +1141,33 @@ | ||
| 1141 | 1141 | return p, size; |
| 1142 | 1142 | end; |
| 1143 | 1143 | |
| 1144 | +(* Sets the environment variable name to value, for env and for the | |
| 1145 | + programs this one starts; 0, or minus an errno: -22 if name is empty | |
| 1146 | + or holds an = or a 0, or value a 0, and -12 if there is no memory. | |
| 1147 | + The C library's setenv keeps the environment. It isn't safe while | |
| 1148 | + another thread reads the environment or starts a program. *) | |
| 1149 | +proc setEnv*(name, value: []U8): INT; | |
| 1150 | +var p: ^U8; size, i, r: INT; | |
| 1151 | +begin | |
| 1152 | + if LEN(name) = 0 then return -22; end; (* EINVAL *) | |
| 1153 | + for i := 0 to LEN(name) - 1 do | |
| 1154 | + if (name[i] = '=') or (name[i] = 0) then return -22; end; | |
| 1155 | + end; | |
| 1156 | + for i := 0 to LEN(value) - 1 do | |
| 1157 | + if value[i] = 0 then return -22; end; | |
| 1158 | + end; | |
| 1159 | + (* both as C strings, in memory that comes zeroed *) | |
| 1160 | + size := LEN(name) + LEN(value) + 2; | |
| 1161 | + p := mmap(size); | |
| 1162 | + if p = nil then return -12; end; (* ENOMEM *) | |
| 1163 | + COPY(p[0..LEN(name)], name); | |
| 1164 | + COPY(p[LEN(name) + 1..size - 1], value); | |
| 1165 | + r := 0; | |
| 1166 | + if C.setenv(p, p + (LEN(name) + 1), 1) # 0 then r := failed(); end; | |
| 1167 | + munmap(p, size); | |
| 1168 | + return r; | |
| 1169 | +end; | |
| 1170 | + | |
| 1144 | 1171 | (* A pipe: the fd to read and the fd to write, both closed in a program |
| 1145 | 1172 | exec starts; or minus an errno and -1. *) |
| 1146 | 1173 | proc pipe*: (INT, INT); |
| @@ -2031,6 +2058,26 @@ | ||
| 2031 | 2058 | if (w # 80) or (h # 24) then exit(2); end; |
| 2032 | 2059 | (* the environment *) |
| 2033 | 2060 | if (LEN(env("PATH")) = 0) or (LEN(env("RAZ_NO_SUCH_VARIABLE")) # 0) then exit(2); end; |
| 2061 | + (* the environment set: refused, added, set again, grown past its first array *) | |
| 2062 | + if (setEnv("", "x") # -22) or (setEnv("A=B", "x") # -22) or (setEnv("A", "\x00") # -22) then exit(2); end; | |
| 2063 | + if (setEnv("RAZ_SYS_TEST", "one") # 0) or (env("RAZ_SYS_TEST") # "one") then exit(2); end; | |
| 2064 | + name := env("RAZ_SYS_TEST"); | |
| 2065 | + if (setEnv("RAZ_SYS_TEST", "") # 0) or (env("RAZ_SYS_TEST") # "") or (name # "one") then exit(2); end; | |
| 2066 | + buf := "RAZ_SYS_TEST_.."; | |
| 2067 | + for k := 0 to 99 do | |
| 2068 | + buf[13] := U8('0' + k div 10); | |
| 2069 | + buf[14] := U8('0' + k mod 10); | |
| 2070 | + if setEnv(buf[0..15], buf[13..15]) # 0 then exit(2); end; | |
| 2071 | + end; | |
| 2072 | + if (env("RAZ_SYS_TEST_42") # "42") or (env("RAZ_SYS_TEST") # "") or (LEN(env("PATH")) = 0) then exit(2); end; | |
| 2073 | + n := 0; | |
| 2074 | + k := 0; | |
| 2075 | + repeat | |
| 2076 | + name, ok := envAt(k); | |
| 2077 | + if (LEN(name) > 12) and (name[0..12] = "RAZ_SYS_TEST") then INC(n); end; | |
| 2078 | + INC(k); | |
| 2079 | + until not ok; | |
| 2080 | + if n # 101 then exit(2); end; | |
| 2034 | 2081 | name, err := workDir(); |
| 2035 | 2082 | if (err # 0) or (LEN(name) = 0) or (name[0] # '/') then exit(2); end; |
| 2036 | 2083 | if (setWorkDir("/tmp/raz-sys-dir") # 0) or (removeDir("/tmp/raz-sys-dir") # 0) then exit(2); end; |
| @@ -0,0 +1,4 @@ | ||
| 1 | +second | |
| 2 | +1 | |
| 3 | +given | |
| 4 | +1 |
| @@ -0,0 +1,26 @@ | ||
| 1 | +(* Sys.setEnv over C: a variable set, then set again, reaches a program this | |
| 2 | + one starts, as one variable; one it named in its own env wins. *) | |
| 3 | +module SetEnvC; | |
| 4 | +import Out, Rt, Sys; | |
| 5 | + | |
| 6 | +proc run(env: [][]U8); | |
| 7 | +var p: Sys.Program; pid: INT; | |
| 8 | +begin | |
| 9 | + p.path := "/bin/sh"; | |
| 10 | + p.args := ["sh", "-c", "echo $RAZ_SET_ENV; env | grep -c ^RAZ_SET_ENV="]; | |
| 11 | + p.env := env; | |
| 12 | + Out.flush; | |
| 13 | + pid := Sys.spawn(p); | |
| 14 | + if pid < 0 then Rt.stop("SetEnv: no sh"); end; | |
| 15 | + _, _, _ := Sys.wait(pid, true); | |
| 16 | +end; | |
| 17 | + | |
| 18 | +proc main*; | |
| 19 | +begin | |
| 20 | + if Sys.setEnv("RAZ_SET_ENV", "first") # 0 then Rt.stop("SetEnv: set"); end; | |
| 21 | + if Sys.setEnv("RAZ_SET_ENV", "second") # 0 then Rt.stop("SetEnv: set again"); end; | |
| 22 | + run([]); | |
| 23 | + run(["RAZ_SET_ENV=given"]); | |
| 24 | +end; | |
| 25 | + | |
| 26 | +end SetEnvC. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +second | |
| 2 | +1 | |
| 3 | +given | |
| 4 | +1 |
| @@ -0,0 +1,26 @@ | ||
| 1 | +(* Sys.setEnv: a variable set, then set again, reaches a program this | |
| 2 | + one starts, as one variable; one it named in its own env wins. *) | |
| 3 | +module SetEnv; | |
| 4 | +import Out, Rt, Sys; | |
| 5 | + | |
| 6 | +proc run(env: [][]U8); | |
| 7 | +var p: Sys.Program; pid: INT; | |
| 8 | +begin | |
| 9 | + p.path := "/bin/sh"; | |
| 10 | + p.args := ["sh", "-c", "echo $RAZ_SET_ENV; env | grep -c ^RAZ_SET_ENV="]; | |
| 11 | + p.env := env; | |
| 12 | + Out.flush; | |
| 13 | + pid := Sys.spawn(p); | |
| 14 | + if pid < 0 then Rt.stop("SetEnv: no sh"); end; | |
| 15 | + _, _, _ := Sys.wait(pid, true); | |
| 16 | +end; | |
| 17 | + | |
| 18 | +proc main*; | |
| 19 | +begin | |
| 20 | + if Sys.setEnv("RAZ_SET_ENV", "first") # 0 then Rt.stop("SetEnv: set"); end; | |
| 21 | + if Sys.setEnv("RAZ_SET_ENV", "second") # 0 then Rt.stop("SetEnv: set again"); end; | |
| 22 | + run([]); | |
| 23 | + run(["RAZ_SET_ENV=given"]); | |
| 24 | +end; | |
| 25 | + | |
| 26 | +end SetEnv. |