/src/FreeRDP/winpr/libwinpr/utils/winpr.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Debugging Utils |
4 | | * |
5 | | * Copyright 2015 Armin Novak <armin.novak@thincast.com> |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * |
8 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | | * you may not use this file except in compliance with the License. |
10 | | * You may obtain a copy of the License at |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | |
21 | | #include <winpr/config.h> |
22 | | |
23 | | #include <winpr/buildflags.h> |
24 | | |
25 | | #include <stdlib.h> |
26 | | #include <stdio.h> |
27 | | #include <winpr/crt.h> |
28 | | #include <winpr/string.h> |
29 | | #include <winpr/winpr.h> |
30 | | #include <winpr/version.h> |
31 | | #include <winpr/wlog.h> |
32 | | #include <winpr/file.h> |
33 | | #include <winpr/build-config.h> |
34 | | |
35 | | #include "../utils.h" |
36 | | |
37 | | #if !defined(WIN32) |
38 | | #include <pthread.h> |
39 | | #endif |
40 | | |
41 | | static INIT_ONCE s_winpr_app_details_once = INIT_ONCE_STATIC_INIT; |
42 | | static char s_winpr_vendor_string[MAX_PATH] = WINPR_C_ARRAY_INIT; |
43 | | static char s_winpr_product_string[MAX_PATH] = WINPR_C_ARRAY_INIT; |
44 | | static SSIZE_T s_winpr_version = -1; |
45 | | static BOOL s_winpr_app_details_are_custom = FALSE; |
46 | | |
47 | | static BOOL CALLBACK init_app_details(WINPR_ATTR_UNUSED PINIT_ONCE once, |
48 | | WINPR_ATTR_UNUSED PVOID param, |
49 | | WINPR_ATTR_UNUSED PVOID* context) |
50 | 1 | { |
51 | 1 | const size_t vlen = sizeof(WINPR_VENDOR_STRING); |
52 | 1 | const size_t plen = sizeof(WINPR_PRODUCT_STRING); |
53 | 1 | if (!strncpy(s_winpr_vendor_string, WINPR_VENDOR_STRING, vlen)) |
54 | 0 | return FALSE; |
55 | | |
56 | 1 | if (!strncpy(s_winpr_product_string, WINPR_PRODUCT_STRING, plen)) |
57 | 0 | return FALSE; |
58 | | |
59 | | #if defined(WITH_RESOURCE_VERSIONING) |
60 | | s_winpr_version = WINPR_VERSION_MAJOR; |
61 | | #else |
62 | 1 | s_winpr_version = -1; |
63 | 1 | #endif |
64 | 1 | return TRUE; |
65 | 1 | } |
66 | | |
67 | | WINPR_ATTR_NODISCARD |
68 | | static BOOL initializeApplicationDetails(void) |
69 | 33.0k | { |
70 | 33.0k | InitOnceExecuteOnce(&s_winpr_app_details_once, init_app_details, NULL, NULL); |
71 | 33.0k | return TRUE; |
72 | 33.0k | } |
73 | | |
74 | | BOOL winpr_setApplicationDetails(const char* vendor, const char* product, SSIZE_T version) |
75 | 0 | { |
76 | 0 | if (!initializeApplicationDetails()) |
77 | 0 | return -1; |
78 | | |
79 | 0 | if (!vendor || !product) |
80 | 0 | return FALSE; |
81 | 0 | const size_t vlen = strnlen(vendor, MAX_PATH); |
82 | 0 | const size_t plen = strnlen(product, MAX_PATH); |
83 | 0 | if ((vlen == MAX_PATH) || (plen == MAX_PATH)) |
84 | 0 | return FALSE; |
85 | | |
86 | 0 | if (!strncpy(s_winpr_vendor_string, vendor, vlen + 1)) |
87 | 0 | return FALSE; |
88 | | |
89 | 0 | if (!strncpy(s_winpr_product_string, product, plen + 1)) |
90 | 0 | return FALSE; |
91 | | |
92 | 0 | s_winpr_version = version; |
93 | 0 | s_winpr_app_details_are_custom = TRUE; |
94 | 0 | return TRUE; |
95 | 0 | } |
96 | | |
97 | | const char* winpr_getApplicationDetailsVendor(void) |
98 | 11.0k | { |
99 | 11.0k | if (!initializeApplicationDetails()) |
100 | 0 | return NULL; |
101 | 11.0k | return s_winpr_vendor_string; |
102 | 11.0k | } |
103 | | |
104 | | const char* winpr_getApplicationDetailsProduct(void) |
105 | 11.0k | { |
106 | 11.0k | if (!initializeApplicationDetails()) |
107 | 0 | return NULL; |
108 | 11.0k | return s_winpr_product_string; |
109 | 11.0k | } |
110 | | |
111 | | char* winpr_getApplicatonDetailsRegKey(const char* fmt) |
112 | 0 | { |
113 | 0 | char* val = winpr_getApplicatonDetailsCombined('\\'); |
114 | 0 | if (!val) |
115 | 0 | return NULL; |
116 | | |
117 | 0 | char* str = NULL; |
118 | 0 | size_t slen = 0; |
119 | 0 | (void)winpr_asprintf(&str, &slen, fmt, val); |
120 | 0 | free(val); |
121 | 0 | return str; |
122 | 0 | } |
123 | | |
124 | | char* winpr_getApplicatonDetailsCombined(char separator) |
125 | 0 | { |
126 | 0 | const SSIZE_T version = winpr_getApplicationDetailsVersion(); |
127 | 0 | const char* vendor = winpr_getApplicationDetailsVendor(); |
128 | 0 | const char* product = winpr_getApplicationDetailsProduct(); |
129 | |
|
130 | 0 | size_t slen = 0; |
131 | 0 | char* str = NULL; |
132 | 0 | if (version < 0) |
133 | 0 | { |
134 | 0 | (void)winpr_asprintf(&str, &slen, "%s%c%s", vendor, separator, product); |
135 | 0 | } |
136 | 0 | else |
137 | 0 | { |
138 | 0 | (void)winpr_asprintf(&str, &slen, "%s%c%s%" PRIdz, vendor, separator, product, version); |
139 | 0 | } |
140 | |
|
141 | 0 | return str; |
142 | 0 | } |
143 | | |
144 | | SSIZE_T winpr_getApplicationDetailsVersion(void) |
145 | 11.0k | { |
146 | 11.0k | if (!initializeApplicationDetails()) |
147 | 0 | return -1; |
148 | 11.0k | return s_winpr_version; |
149 | 11.0k | } |
150 | | |
151 | | BOOL winpr_areApplicationDetailsCustomized(void) |
152 | 0 | { |
153 | 0 | return s_winpr_app_details_are_custom; |
154 | 0 | } |
155 | | |
156 | | void winpr_get_version(int* major, int* minor, int* revision) |
157 | 0 | { |
158 | 0 | if (major) |
159 | 0 | *major = WINPR_VERSION_MAJOR; |
160 | 0 | if (minor) |
161 | 0 | *minor = WINPR_VERSION_MINOR; |
162 | 0 | if (revision) |
163 | 0 | *revision = WINPR_VERSION_REVISION; |
164 | 0 | } |
165 | | |
166 | | const char* winpr_get_version_string(void) |
167 | 0 | { |
168 | 0 | return WINPR_VERSION_FULL; |
169 | 0 | } |
170 | | |
171 | | const char* winpr_get_build_revision(void) |
172 | 0 | { |
173 | 0 | return WINPR_GIT_REVISION; |
174 | 0 | } |
175 | | |
176 | | const char* winpr_get_build_config(void) |
177 | 0 | { |
178 | 0 | static const char build_config[] = |
179 | 0 | "Build configuration: " WINPR_BUILD_CONFIG "\n" |
180 | 0 | "Build type: " WINPR_BUILD_TYPE "\n" |
181 | 0 | "CFLAGS: " WINPR_CFLAGS "\n" |
182 | 0 | "Compiler: " WINPR_COMPILER_ID ", " WINPR_COMPILER_VERSION "\n" |
183 | 0 | "Target architecture: " WINPR_TARGET_ARCH "\n"; |
184 | |
|
185 | 0 | return build_config; |
186 | 0 | } |