Skip to content

Commit 82342ee

Browse files
authored
Merge pull request #367 from dplanitzer/getprogname_for_windows
implemented getprogname() for Windows
2 parents 12ff819 + 6a135fe commit 82342ee

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ elseif(WIN32)
6363
PRIVATE
6464
shims/generic_sys_queue.h
6565
shims/generic_win_stubs.c
66-
shims/generic_win_stubs.h)
66+
shims/generic_win_stubs.h
67+
shims/getprogname.c)
6768
endif()
6869
if(DISPATCH_USE_INTERNAL_WORKQUEUE)
6970
target_sources(dispatch

src/shims/getprogname.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2009-2010 Mark Heily <mark@heily.com>
3+
* All rights reserved.
4+
*
5+
* @APPLE_APACHE_LICENSE_HEADER_START@
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
* @APPLE_APACHE_LICENSE_HEADER_END@
20+
*/
21+
22+
#include "getprogname.h"
23+
24+
#if !HAVE_GETPROGNAME
25+
26+
#if defined(_WIN32)
27+
#define WIN32_LEAN_AND_MEAN
28+
#ifndef _WIN32_WINNT
29+
#define _WIN32_WINNT 0x0600
30+
#endif /* _WIN32_WINNT */
31+
#include <windows.h>
32+
#include <stdlib.h>
33+
34+
static INIT_ONCE getprogname_init_once = INIT_ONCE_STATIC_INIT;
35+
static TCHAR progname[_MAX_FNAME];
36+
37+
static BOOL CALLBACK
38+
getprogname_init_once_handler(PINIT_ONCE InitOnce, PVOID Parameter,
39+
PVOID *lpContext)
40+
{
41+
TCHAR path[MAX_PATH];
42+
DWORD length = GetModuleFileName(NULL, path, sizeof(path));
43+
44+
if (length < 0) {
45+
progname[0] = '\0';
46+
return TRUE;
47+
} else {
48+
const char *filename;
49+
50+
path[MAX_PATH - 1] = '\0';
51+
filename = strrchr(path, '\\');
52+
if (filename != NULL) {
53+
filename++;
54+
} else {
55+
filename = path;
56+
}
57+
strcpy_s(progname, sizeof(progname), filename);
58+
return TRUE;
59+
}
60+
}
61+
62+
const char *
63+
getprogname(void)
64+
{
65+
(void)InitOnceExecuteOnce(&getprogname_init_once,
66+
getprogname_init_once_handler,
67+
NULL,
68+
NULL);
69+
return progname;
70+
}
71+
#endif /* _WIN32 */
72+
#endif /* HAVE_GETPROGNAME */

src/shims/getprogname.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
extern const char *__progname;
3131
#endif /* __ANDROID */
3232

33+
#if defined(_WIN32)
34+
const char *getprogname(void);
35+
#else
36+
3337
static inline char *
3438
getprogname(void)
3539
{
@@ -41,6 +45,7 @@ getprogname(void)
4145
# error getprogname(3) is not available on this platform
4246
# endif
4347
}
48+
#endif /* _WIN32 */
4449
#endif /* HAVE_GETPROGNAME */
4550

4651
#endif /* __DISPATCH_SHIMS_GETPROGNAME__ */

0 commit comments

Comments
 (0)