diff options
author | Ron Yorston <rmy@pobox.com> | 2024-07-07 12:51:51 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2024-07-07 13:04:40 +0100 |
commit | a694cb601a92a1e4eded89f4c13793c9d12e33b6 (patch) | |
tree | f7cda76d1343e5c966b7ad80d3ab53441322b6e5 /win32 | |
parent | f4fc4fb51fcba40a5db81861629fff27d73bed85 (diff) | |
download | busybox-w32-a694cb601a92a1e4eded89f4c13793c9d12e33b6.tar.gz busybox-w32-a694cb601a92a1e4eded89f4c13793c9d12e33b6.tar.bz2 busybox-w32-a694cb601a92a1e4eded89f4c13793c9d12e33b6.zip |
win32: code shrink system drive handling
A previous commit (e3bfe3695) revised the use of getsysdir() to
obtain the system directory, and hence the system drive. See the
commit message for the history to that point.
Further improvements are possible:
- Remove getsysdir() and push the calls to GetSystemDirectory()
down into get_system_drive() and get_proc_addr().
- Check the return value of GetSystemDirectory(). It's unlikely
to fail, but better safe than sorry.
- Instead of making all callers of get_system_drive() check for a
NULL return value always return a non-NULL pointer. If the drive
can't be found an empty string is returned instead (which is what
the callers were using anyway).
- The function need_system_drive() was only used in one place (in
httpd). Move the code there and remove the function.
- Use concat_path_file() where possible.
Saves 76-144 bytes.
Diffstat (limited to 'win32')
-rw-r--r-- | win32/mingw.c | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index 7e42e4434..6544d11ad 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -1146,17 +1146,6 @@ static char *gethomedir(void) | |||
1146 | return buf; | 1146 | return buf; |
1147 | } | 1147 | } |
1148 | 1148 | ||
1149 | static char *getsysdir(void) | ||
1150 | { | ||
1151 | static char *buf = NULL; | ||
1152 | |||
1153 | if (!buf) { | ||
1154 | buf = xzalloc(PATH_MAX); | ||
1155 | GetSystemDirectory(buf, PATH_MAX); | ||
1156 | } | ||
1157 | return buf; | ||
1158 | } | ||
1159 | |||
1160 | #define NAME_LEN 100 | 1149 | #define NAME_LEN 100 |
1161 | char *get_user_name(void) | 1150 | char *get_user_name(void) |
1162 | { | 1151 | { |
@@ -2297,27 +2286,21 @@ int root_len(const char *path) | |||
2297 | 2286 | ||
2298 | const char *get_system_drive(void) | 2287 | const char *get_system_drive(void) |
2299 | { | 2288 | { |
2300 | static char *drive = NULL; | 2289 | static const char *drive = NULL; |
2290 | char sysdir[PATH_MAX]; | ||
2301 | int len; | 2291 | int len; |
2302 | 2292 | ||
2303 | if (drive == NULL) { | 2293 | if (drive == NULL) { |
2304 | const char *sysdir = getsysdir(); | 2294 | UINT ret = GetSystemDirectory(sysdir, PATH_MAX); |
2305 | if ((len=root_len(sysdir))) { | 2295 | if ((ret != 0 && ret < PATH_MAX) && (len=root_len(sysdir))) |
2306 | drive = xstrndup(sysdir, len); | 2296 | drive = xstrndup(sysdir, len); |
2307 | } | 2297 | else |
2298 | drive = ""; | ||
2308 | } | 2299 | } |
2309 | 2300 | ||
2310 | return getenv(BB_SYSTEMROOT) ?: drive; | 2301 | return getenv(BB_SYSTEMROOT) ?: drive; |
2311 | } | 2302 | } |
2312 | 2303 | ||
2313 | /* Return pointer to system drive if path is of form '/file', else NULL */ | ||
2314 | const char *need_system_drive(const char *path) | ||
2315 | { | ||
2316 | if (root_len(path) == 0 && (path[0] == '/' || path[0] == '\\')) | ||
2317 | return get_system_drive(); | ||
2318 | return NULL; | ||
2319 | } | ||
2320 | |||
2321 | int chdir_system_drive(void) | 2304 | int chdir_system_drive(void) |
2322 | { | 2305 | { |
2323 | const char *sd = get_system_drive(); | 2306 | const char *sd = get_system_drive(); |
@@ -2409,10 +2392,14 @@ void *get_proc_addr(const char *dll, const char *function, | |||
2409 | * on Windows 7. If it does, retry using LoadLibrary with an | 2392 | * on Windows 7. If it does, retry using LoadLibrary with an |
2410 | * explicit, backslash-separated path. */ | 2393 | * explicit, backslash-separated path. */ |
2411 | if (!hnd) { | 2394 | if (!hnd) { |
2412 | char *path = concat_path_file(getsysdir(), dll); | 2395 | char buf[PATH_MAX]; |
2413 | slash_to_bs(path); | 2396 | UINT ret = GetSystemDirectory(buf, PATH_MAX); |
2414 | hnd = LoadLibrary(path); | 2397 | if (ret != 0 && ret < PATH_MAX) { |
2415 | free(path); | 2398 | char *path = concat_path_file(buf, dll); |
2399 | slash_to_bs(path); | ||
2400 | hnd = LoadLibrary(path); | ||
2401 | free(path); | ||
2402 | } | ||
2416 | } | 2403 | } |
2417 | 2404 | ||
2418 | if (hnd) | 2405 | if (hnd) |