Add command-line option help-text localization
[cygwin-setup.git] / win32.h
blob02c1d06e25db3f72a2c44d27f3a7d9d1974aebf0
1 /*
2 * Copyright (c) 2000, Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
12 * Written by DJ Delorie <dj@cygnus.com>
13 * and Robert Collins <rbtcollins@hotmail.com>
17 #ifndef SETUP_WIN32_H
18 #define SETUP_WIN32_H
20 #include <sys/types.h>
21 #include <string>
23 /* Needed for some buffers etc., to have a useful replacement for MAX_PATH. */
24 #define CYG_PATH_MAX 4096
26 /* Any include of <windows.h> should be through this file, which wraps it in
27 * various other handling. */
29 /* Basic Windows features only. */
30 #define WIN32_LEAN_AND_MEAN
32 #undef _WIN32_WINNT
33 #define _WIN32_WINNT 0x0a00
35 /* libstdc++-v3 _really_ dislikes min & max defined as macros. */
36 /* As of gcc 3.3.1, it defines NOMINMAX itself, so test first,
37 * to avoid a redefinition error */
38 #ifndef NOMINMAX
39 #define NOMINMAX
40 #endif
42 /* 100ns difference between Windows and UNIX timebase. */
43 #define FACTOR (0x19db1ded53e8000LL)
44 /* # of 100ns intervals per second. */
45 #define NSPERSEC 10000000LL
47 /* Defining WINBASEAPI is required due the overloading definitions of some
48 kernel32 functions in filemanip.cc. */
49 #define WINBASEAPI
51 #include <windows.h>
53 /* FIXME: The use of _access(fname, 0) as an existence check should be
54 * replaced with a more readable idiom, and this fragment removed. */
55 #ifndef _access
56 #define _access access
57 #endif
59 /* When we have to check for a path delimiter, check for both, slash and
60 backslash. */
61 #define isdirsep(ch) \
62 ({ \
63 char __c = (ch); \
64 ((__c) == '/' || (__c) == '\\'); \
67 /* Maximum size of a SID. */
68 #define MAX_SID_LEN 40
70 /* Computes the size of an ACL in relation to the number of ACEs it
71 should contain. */
72 #define TOKEN_ACL_SIZE(cnt) (sizeof (ACL) + \
73 (cnt) * (sizeof (ACCESS_ALLOWED_ACE) + MAX_SID_LEN))
75 struct acl_t {
76 /* Make sure &acl is 4-byte aligned. */
77 ACL acl __attribute__ ((aligned (4)));
78 char aclbuf[TOKEN_ACL_SIZE (7)];
81 class SIDWrapper {
82 public:
83 SIDWrapper () : value (NULL) {}
84 /* Prevent synthetics. If assignment is needed, this should be
85 refcounting. */
86 SIDWrapper (SIDWrapper const &);
87 SIDWrapper& operator= (SIDWrapper const &);
88 ~SIDWrapper () { if (value) FreeSid (value); }
90 /* We could look at doing weird typcast overloads here,
91 but manual access is easier for now. */
92 PSID &theSID () { return value; }
93 PSID const &theSID () const { return value; }
94 private:
95 PSID value;
98 class HANDLEWrapper {
99 public:
100 HANDLEWrapper () : value (NULL) {}
101 /* Prevent synthetics. If assignment is needed, we should duphandles,
102 or refcount. */
103 HANDLEWrapper (HANDLEWrapper const &);
104 HANDLEWrapper& operator= (HANDLEWrapper const &);
105 ~HANDLEWrapper () { if (value) CloseHandle (value); }
106 HANDLE &theHANDLE () { return value; }
107 HANDLE const &theHANDLE () const { return value; }
108 private:
109 HANDLE value;
112 class NTSecurity
114 public:
115 NTSecurity () : nullSID (), everyOneSID (), administratorsSID (), usersSID (),
116 cr_ownerSID (), cr_groupSID (), groupSID (NULL),
117 _wellKnownSIDsinitialized (false), token () {}
118 ~NTSecurity() {}
120 /* prevent synthetics */
121 NTSecurity& operator= (NTSecurity const &);
122 NTSecurity (NTSecurity const &);
124 /* Set POSIX-like permissions on files. The fname is only used for printing
125 log output. The function requires an open HANDLE with sufficient
126 permissions (READ_DAC | WRITE_DAC). */
127 PSECURITY_DESCRIPTOR GetPosixPerms (const char *fname, PSID owner_sid,
128 PSID group_sid, mode_t mode,
129 SECURITY_DESCRIPTOR &out_sd, acl_t &acl);
130 void resetPrimaryGroup();
131 void setAdminGroup ();
132 void initialiseWellKnownSIDs ();
133 void setDefaultSecurity(bool isAdmin);
134 bool isRunAsAdmin ();
135 bool hasSymlinkCreationRights ();
136 private:
137 void NoteFailedAPI (const std::string &);
138 bool wellKnownSIDsinitialized () const { return _wellKnownSIDsinitialized; }
139 void wellKnownSIDsinitialized (bool b) { _wellKnownSIDsinitialized = b; }
140 void setDefaultDACL ();
141 void setBackupPrivileges ();
143 SIDWrapper nullSID, everyOneSID, administratorsSID, usersSID,
144 cr_ownerSID, cr_groupSID;
145 struct {
146 TOKEN_USER user;
147 char buf[MAX_SID_LEN];
148 } ownerSID;
149 PSID groupSID;
150 struct {
151 TOKEN_PRIMARY_GROUP pgrp;
152 char buf[MAX_SID_LEN];
153 } primaryGroupSID;
155 bool _wellKnownSIDsinitialized;
156 HANDLEWrapper token;
157 DWORD size;
160 extern NTSecurity nt_sec;
162 #undef major
163 #undef minor
164 class VersionInfo
166 public:
167 VersionInfo ();
168 DWORD major () const { return v.dwMajorVersion; }
169 DWORD minor () const { return v.dwMinorVersion; }
170 DWORD buildNumber () const { return v.dwBuildNumber; }
171 private:
172 OSVERSIONINFO v;
175 VersionInfo& GetVer ();
177 #define OSMajorVersion() (GetVer ().major ())
178 #define OSMinorVersion() (GetVer ().minor ())
179 #define OSBuildNumber() (GetVer ().buildNumber ())
181 USHORT WowNativeMachine ();
183 static inline void
184 GetDlgItemRect (HWND h, int item, LPRECT r)
186 GetWindowRect (GetDlgItem (h, item), r);
187 MapWindowPoints (HWND_DESKTOP, h, (LPPOINT) r, 2);
190 static inline void
191 SetDlgItemRect (HWND h, int item, LPRECT r)
193 MoveWindow (GetDlgItem (h, item), r->left, r->top,
194 r->right - r->left, r->bottom - r->top, TRUE);
197 const std::string LoadStringUtf8(unsigned int uID);
198 const std::wstring LoadStringW(unsigned int uID);
199 const std::wstring LoadStringWEx(UINT uID, UINT langId);
201 bool is_developer_mode(void);
203 #endif /* SETUP_WIN32_H */