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