| ... | ... | @@ -1908,15 +1908,16 @@ pub fn execvpeZ( |
| 1908 | 1908 | |
| 1909 | 1909 | /// Get an environment variable. |
| 1910 | 1910 | /// See also `getenvZ`. |
| 1911 | | pub fn getenv(key: []const u8) ?[]const u8 { |
| 1911 | pub fn getenv(key: []const u8) ?[:0]const u8 { |
| 1912 | 1912 | if (builtin.link_libc) { |
| 1913 | // Append null byte to the key to use with cstd getenv |
| 1913 | 1914 | var small_key_buf: [64]u8 = undefined; |
| 1914 | 1915 | if (key.len < small_key_buf.len) { |
| 1915 | 1916 | @memcpy(small_key_buf[0..key.len], key); |
| 1916 | 1917 | small_key_buf[key.len] = 0; |
| 1917 | | const key0 = small_key_buf[0..key.len :0]; |
| 1918 | | return getenvZ(key0); |
| 1918 | return getenvZ(small_key_buf[0..key.len :0]); |
| 1919 | 1919 | } |
| 1920 | |
| 1920 | 1921 | // Search the entire `environ` because we don't have a null terminated pointer. |
| 1921 | 1922 | var ptr = std.c.environ; |
| 1922 | 1923 | while (ptr[0]) |line| : (ptr += 1) { |
| ... | ... | @@ -1926,11 +1927,7 @@ pub fn getenv(key: []const u8) ?[]const u8 { |
| 1926 | 1927 | |
| 1927 | 1928 | if (!mem.eql(u8, this_key, key)) continue; |
| 1928 | 1929 | |
| 1929 | | var end_i: usize = line_i; |
| 1930 | | while (line[end_i] != 0) : (end_i += 1) {} |
| 1931 | | const value = line[line_i + 1 .. end_i]; |
| 1932 | | |
| 1933 | | return value; |
| 1930 | return mem.sliceTo(line + line_i + 1, 0); |
| 1934 | 1931 | } |
| 1935 | 1932 | return null; |
| 1936 | 1933 | } |
| ... | ... | @@ -1946,18 +1943,14 @@ pub fn getenv(key: []const u8) ?[]const u8 { |
| 1946 | 1943 | const this_key = ptr[0..line_i]; |
| 1947 | 1944 | if (!mem.eql(u8, key, this_key)) continue; |
| 1948 | 1945 | |
| 1949 | | var end_i: usize = line_i; |
| 1950 | | while (ptr[end_i] != 0) : (end_i += 1) {} |
| 1951 | | const this_value = ptr[line_i + 1 .. end_i]; |
| 1952 | | |
| 1953 | | return this_value; |
| 1946 | return mem.sliceTo(ptr + line_i + 1, 0); |
| 1954 | 1947 | } |
| 1955 | 1948 | return null; |
| 1956 | 1949 | } |
| 1957 | 1950 | |
| 1958 | 1951 | /// Get an environment variable with a null-terminated name. |
| 1959 | 1952 | /// See also `getenv`. |
| 1960 | | pub fn getenvZ(key: [*:0]const u8) ?[]const u8 { |
| 1953 | pub fn getenvZ(key: [*:0]const u8) ?[:0]const u8 { |
| 1961 | 1954 | if (builtin.link_libc) { |
| 1962 | 1955 | const value = system.getenv(key) orelse return null; |
| 1963 | 1956 | return mem.sliceTo(value, 0); |