|
| 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 */ |
0 commit comments