Skip to content

implemented getprogname() for Windows #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ elseif(WIN32)
target_sources(dispatch
PRIVATE
shims/generic_win_stubs.c
shims/generic_win_stubs.h)
shims/generic_win_stubs.h
shims/getprogname.c)
endif()
if(DISPATCH_USE_INTERNAL_WORKQUEUE)
target_sources(dispatch
Expand Down
72 changes: 72 additions & 0 deletions src/shims/getprogname.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2009-2010 Mark Heily <[email protected]>
* All rights reserved.
*
* @APPLE_APACHE_LICENSE_HEADER_START@
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @APPLE_APACHE_LICENSE_HEADER_END@
*/

#include "getprogname.h"

#if !HAVE_GETPROGNAME

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif /* _WIN32_WINNT */
#include <windows.h>
#include <stdlib.h>

static INIT_ONCE getprogname_init_once = INIT_ONCE_STATIC_INIT;
static TCHAR progname[_MAX_FNAME];

static BOOL CALLBACK
getprogname_init_once_handler(PINIT_ONCE InitOnce, PVOID Parameter,
PVOID *lpContext)
{
TCHAR path[MAX_PATH];
DWORD length = GetModuleFileName(NULL, path, sizeof(path));

if (length < 0) {
progname[0] = '\0';
return TRUE;
} else {
const char *filename;

path[MAX_PATH - 1] = '\0';
filename = strrchr(path, '\\');
if (filename != NULL) {
filename++;
} else {
filename = path;
}
strcpy_s(progname, sizeof(progname), filename);
return TRUE;
}
}

const char *
getprogname(void)
{
(void)InitOnceExecuteOnce(&getprogname_init_once,
getprogname_init_once_handler,
NULL,
NULL);
return progname;
}
#endif /* _WIN32 */
#endif /* HAVE_GETPROGNAME */
5 changes: 5 additions & 0 deletions src/shims/getprogname.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
extern const char *__progname;
#endif /* __ANDROID */

#if defined(_WIN32)
const char *getprogname(void);
#else

static inline char *
getprogname(void)
{
Expand All @@ -41,6 +45,7 @@ getprogname(void)
# error getprogname(3) is not available on this platform
# endif
}
#endif /* _WIN32 */
#endif /* HAVE_GETPROGNAME */

#endif /* __DISPATCH_SHIMS_GETPROGNAME__ */