Tweak regexp in help-mode-finish
[bpt/emacs.git] / lib-src / ntlib.c
CommitLineData
95ed0025 1/* Utility and Unix shadow routines for GNU Emacs support programs on NT.
73b0cd50 2 Copyright (C) 1994, 2001-2011 Free Software Foundation, Inc.
95ed0025 3
3b7ad313
EN
4This file is part of GNU Emacs.
5
294981c7 6GNU Emacs is free software: you can redistribute it and/or modify
3b7ad313 7it under the terms of the GNU General Public License as published by
294981c7
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
3b7ad313
EN
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
294981c7
GM
17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
18
95ed0025
RS
19
20 Geoff Voelker (voelker@cs.washington.edu) 10-8-94
21*/
22
23#include <windows.h>
24#include <stdlib.h>
25#include <stdio.h>
14f29224
GV
26#include <time.h>
27#include <direct.h>
10fea9c4
EZ
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <errno.h>
14f29224
GV
31
32#include "ntlib.h"
95ed0025
RS
33
34#define MAXPATHLEN _MAX_PATH
35
36/* Emulate sleep...we could have done this with a define, but that
37 would necessitate including windows.h in the files that used it.
38 This is much easier. */
39void
7c3320d8 40sleep (unsigned long seconds)
95ed0025
RS
41{
42 Sleep (seconds * 1000);
43}
44
45/* Get the current working directory. */
133319ab 46char *
95ed0025
RS
47getwd (char *dir)
48{
14f29224
GV
49 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
50 return dir;
51 return NULL;
95ed0025
RS
52}
53
54static HANDLE getppid_parent;
55static int getppid_ppid;
56
57int
7c3320d8 58getppid (void)
95ed0025
RS
59{
60 char *ppid;
61 DWORD result;
62
97e3c91b 63 ppid = getenv ("EM_PARENT_PROCESS_ID");
177c0ea7 64 if (!ppid)
95ed0025 65 {
7c3320d8 66 printf ("no pid.\n");
95ed0025 67 return 0;
177c0ea7
JB
68 }
69 else
95ed0025
RS
70 {
71 getppid_ppid = atoi (ppid);
72 }
73
177c0ea7 74 if (!getppid_parent)
95ed0025 75 {
7c3320d8 76 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi (ppid));
177c0ea7 77 if (!getppid_parent)
95ed0025
RS
78 {
79 printf ("Failed to open handle to parent process: %d\n",
7c3320d8 80 GetLastError ());
95ed0025
RS
81 exit (1);
82 }
83 }
84
85 result = WaitForSingleObject (getppid_parent, 0);
177c0ea7 86 switch (result)
95ed0025
RS
87 {
88 case WAIT_TIMEOUT:
89 /* The parent is still alive. */
90 return getppid_ppid;
91 case WAIT_OBJECT_0:
92 /* The parent is gone. Return the pid of Unix init (1). */
93 return 1;
94 case WAIT_FAILED:
95 default:
7c3320d8 96 printf ("Checking parent status failed: %d\n", GetLastError ());
95ed0025
RS
97 exit (1);
98 }
99}
14f29224
GV
100
101char *
7c3320d8 102getlogin (void)
14f29224
GV
103{
104 static char user_name[256];
105 DWORD length = sizeof (user_name);
106
107 if (GetUserName (user_name, &length))
108 return user_name;
109 return NULL;
110}
111
112char *
113cuserid (char * s)
114{
115 char * name = getlogin ();
116 if (s)
117 return strcpy (s, name ? name : "");
118 return name;
119}
120
22749e9a 121unsigned
7c3320d8 122getuid (void)
14f29224
GV
123{
124 return 0;
125}
126
b372fceb 127unsigned
7c3320d8 128getgid (void)
b372fceb
JB
129{
130 return 0;
131}
132
133unsigned
7c3320d8 134getegid (void)
b372fceb
JB
135{
136 return 0;
137}
138
14f29224 139int
22749e9a 140setuid (unsigned uid)
14f29224
GV
141{
142 return 0;
143}
144
b372fceb 145int
f915f0f7 146setregid (unsigned rgid, unsigned gid)
b372fceb
JB
147{
148 return 0;
149}
150
14f29224 151struct passwd *
22749e9a 152getpwuid (unsigned uid)
14f29224
GV
153{
154 return NULL;
155}
156
157char *
158getpass (const char * prompt)
159{
160 static char input[256];
161 HANDLE in;
162 HANDLE err;
163 DWORD count;
164
165 in = GetStdHandle (STD_INPUT_HANDLE);
166 err = GetStdHandle (STD_ERROR_HANDLE);
167
168 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
169 return NULL;
170
171 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
172 {
173 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
174 DWORD old_flags;
175 int rc;
176
177 if (istty)
178 {
179 if (GetConsoleMode (in, &old_flags))
180 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
181 else
182 istty = 0;
183 }
184 rc = ReadFile (in, input, sizeof (input), &count, NULL);
185 if (count >= 2 && input[count - 2] == '\r')
186 input[count - 2] = '\0';
187 else
188 {
189 char buf[256];
190 while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0)
191 if (count >= 2 && buf[count - 2] == '\r')
192 break;
193 }
194 WriteFile (err, "\r\n", 2, &count, NULL);
195 if (istty)
196 SetConsoleMode (in, old_flags);
197 if (rc)
198 return input;
199 }
200
201 return NULL;
202}
203
204int
22749e9a 205fchown (int fd, unsigned uid, unsigned gid)
14f29224
GV
206{
207 return 0;
208}
209
210/* Place a wrapper around the MSVC version of ctime. It returns NULL
177c0ea7 211 on network directories, so we handle that case here.
14f29224
GV
212 (Ulrich Leodolter, 1/11/95). */
213char *
214sys_ctime (const time_t *t)
215{
216 char *str = (char *) ctime (t);
217 return (str ? str : "Sun Jan 01 00:00:00 1970");
218}
219
220FILE *
7c3320d8 221sys_fopen (const char * path, const char * mode)
14f29224
GV
222{
223 return fopen (path, mode);
224}
225
226int
227sys_chdir (const char * path)
228{
229 return _chdir (path);
230}
ab5796a9 231
10fea9c4
EZ
232static FILETIME utc_base_ft;
233static long double utc_base;
234static int init = 0;
235
236static time_t
237convert_time (FILETIME ft)
238{
239 long double ret;
240
241 if (CompareFileTime (&ft, &utc_base_ft) < 0)
242 return 0;
243
244 ret = (long double) ft.dwHighDateTime
245 * 4096.0L * 1024.0L * 1024.0L + ft.dwLowDateTime;
246 ret -= utc_base;
247 return (time_t) (ret * 1e-7L);
248}
249
250static int
251is_exec (const char * name)
252{
253 char * p = strrchr (name, '.');
254 return
255 (p != NULL
256 && (stricmp (p, ".exe") == 0 ||
257 stricmp (p, ".com") == 0 ||
258 stricmp (p, ".bat") == 0 ||
259 stricmp (p, ".cmd") == 0));
260}
261
262#define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
263
264/* We need this because nt/inc/sys/stat.h defines struct stat that is
265 incompatible with the MS run-time libraries. */
266int
267stat (const char * path, struct stat * buf)
268{
269 WIN32_FIND_DATA wfd;
270 HANDLE fh;
271 int permission;
272 int len;
273 int rootdir = FALSE;
274 char *name = alloca (FILENAME_MAX);
275
276 if (!init)
277 {
278 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
279 SYSTEMTIME st;
280
281 st.wYear = 1970;
282 st.wMonth = 1;
283 st.wDay = 1;
284 st.wHour = 0;
285 st.wMinute = 0;
286 st.wSecond = 0;
287 st.wMilliseconds = 0;
288
289 SystemTimeToFileTime (&st, &utc_base_ft);
290 utc_base = (long double) utc_base_ft.dwHighDateTime
291 * 4096.0L * 1024.0L * 1024.0L + utc_base_ft.dwLowDateTime;
292 init = 1;
293 }
294
295 if (path == NULL || buf == NULL || *path == '\0')
296 {
297 errno = EFAULT;
298 return -1;
299 }
300 if (_mbspbrk (path, "*?|<>\""))
301 {
302 errno = ENOENT;
303 return -1;
304 }
305
306 strcpy (name, path);
307 /* Remove trailing directory separator, unless name is the root
308 directory of a drive in which case ensure there is a trailing
309 separator. */
310 len = strlen (name);
311 rootdir = IS_DIRECTORY_SEP (name[0])
312 || (len == 3 && name[1] == ':' && IS_DIRECTORY_SEP (name[2]));
313 if (rootdir)
314 {
315 if (GetDriveType (name) < 2)
316 {
317 errno = ENOENT;
318 return -1;
319 }
320 memset (&wfd, 0, sizeof (wfd));
321 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
322 wfd.ftCreationTime = utc_base_ft;
323 wfd.ftLastAccessTime = utc_base_ft;
324 wfd.ftLastWriteTime = utc_base_ft;
325 strcpy (wfd.cFileName, name);
326 }
327 else
328 {
329 if (IS_DIRECTORY_SEP (name[len-1]))
330 name[len - 1] = 0;
331
332 fh = FindFirstFile (name, &wfd);
333 if (fh == INVALID_HANDLE_VALUE)
334 {
335 errno = ENOENT;
336 return -1;
337 }
338 FindClose (fh);
339 }
340 buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
341 S_IFDIR : S_IFREG;
342 buf->st_nlink = 1;
343 buf->st_ino = 0;
344
345 if (name[0] && name[1] == ':')
346 buf->st_dev = tolower (name[0]) - 'a' + 1;
347 else
348 buf->st_dev = _getdrive ();
349 buf->st_rdev = buf->st_dev;
350
351 buf->st_size = wfd.nFileSizeLow;
352
353 /* Convert timestamps to Unix format. */
354 buf->st_mtime = convert_time (wfd.ftLastWriteTime);
355 buf->st_atime = convert_time (wfd.ftLastAccessTime);
356 if (buf->st_atime == 0) buf->st_atime = buf->st_mtime;
357 buf->st_ctime = convert_time (wfd.ftCreationTime);
358 if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime;
359
360 /* determine rwx permissions */
361 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
362 permission = S_IREAD;
363 else
364 permission = S_IREAD | S_IWRITE;
365
366 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
367 permission |= S_IEXEC;
368 else if (is_exec (name))
369 permission |= S_IEXEC;
370
371 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
372
373 return 0;
374}
375