Win32: keep the environment sorted
commitb6c976d8da991d16ba31d3b0d9eda10c4ae475a9
authorKarsten Blees <blees@dcon.de>
Sun, 15 Jan 2012 01:30:02 +0000 (15 02:30 +0100)
committerJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 30 Mar 2012 18:44:01 +0000 (30 13:44 -0500)
tree9e09865757c1de0b5c85a315e7776c53e391d0ee
parentf84912d2e39d37f199f4a25a535ae246d88ec85d
Win32: keep the environment sorted

The Windows environment is sorted, keep it that way for O(log n)
environment access.

Change compareenv to compare only the keys, so that it can be used to
find an entry irrespective of the value.

Change lookupenv to binary seach for an entry. Return one's complement of
the insert position if not found (libc's bsearch returns NULL).

Replace MSVCRT's getenv with a minimal do_getenv based on the binary search
function.

Change do_putenv to insert new entries at the correct position. Simplify
the function by swapping if conditions and using memmove instead of for
loops.

Move qsort from make_environment_block to mingw_startup. We still need to
sort on startup to make sure that the environment is sorted according to
our compareenv function (while Win32 / CreateProcess requires the
environment block to be sorted case-insensitively, CreateProcess currently
doesn't enforce this, and some applications such as bash just don't care).

Note that environment functions are _not_ thread-safe and are not required
to be so by POSIX, the application is responsible for synchronizing access
to the environment. MSVCRT's getenv and our new getenv implementation are
better than that in that they are thread-safe with respect to other getenv
calls as long as the environment is not modified. Git's indiscriminate use
of getenv in background threads currently requires this property.

Signed-off-by: Karsten Blees <blees@dcon.de>
compat/mingw.c