keyboard.c, keymap.c: Use bool for booleans.
[emacs.git] / lib-src / ntlib.c
blobfeaad1c1bb7b591a509755ef53c4136565daca25
1 /* Utility and Unix shadow routines for GNU Emacs support programs on NT.
3 Copyright (C) 1994, 2001-2012 Free Software Foundation, Inc.
5 Author: Geoff Voelker (voelker@cs.washington.edu)
6 Created: 10-8-94
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <windows.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <direct.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <sys/timeb.h>
34 #include "ntlib.h"
36 struct timezone
38 int tz_minuteswest; /* minutes west of Greenwich */
39 int tz_dsttime; /* type of dst correction */
42 #define MAXPATHLEN _MAX_PATH
44 /* Emulate sleep...we could have done this with a define, but that
45 would necessitate including windows.h in the files that used it.
46 This is much easier. */
47 void
48 sleep (unsigned long seconds)
50 Sleep (seconds * 1000);
53 /* Get the current working directory. */
54 char *
55 getwd (char *dir)
57 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
58 return dir;
59 return NULL;
62 static HANDLE getppid_parent;
63 static int getppid_ppid;
65 int
66 getppid (void)
68 char *ppid;
69 DWORD result;
71 ppid = getenv ("EM_PARENT_PROCESS_ID");
72 if (!ppid)
74 printf ("no pid.\n");
75 return 0;
77 else
79 getppid_ppid = atoi (ppid);
82 if (!getppid_parent)
84 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi (ppid));
85 if (!getppid_parent)
87 printf ("Failed to open handle to parent process: %d\n",
88 GetLastError ());
89 exit (1);
93 result = WaitForSingleObject (getppid_parent, 0);
94 switch (result)
96 case WAIT_TIMEOUT:
97 /* The parent is still alive. */
98 return getppid_ppid;
99 case WAIT_OBJECT_0:
100 /* The parent is gone. Return the pid of Unix init (1). */
101 return 1;
102 case WAIT_FAILED:
103 default:
104 printf ("Checking parent status failed: %d\n", GetLastError ());
105 exit (1);
109 char *
110 getlogin (void)
112 static char user_name[256];
113 DWORD length = sizeof (user_name);
115 if (GetUserName (user_name, &length))
116 return user_name;
117 return NULL;
120 char *
121 cuserid (char * s)
123 char * name = getlogin ();
124 if (s)
125 return strcpy (s, name ? name : "");
126 return name;
129 unsigned
130 getuid (void)
132 return 0;
135 unsigned
136 getgid (void)
138 return 0;
141 unsigned
142 getegid (void)
144 return 0;
148 setuid (unsigned uid)
150 return 0;
154 setregid (unsigned rgid, unsigned gid)
156 return 0;
159 struct passwd *
160 getpwuid (unsigned uid)
162 return NULL;
165 char *
166 getpass (const char * prompt)
168 static char input[256];
169 HANDLE in;
170 HANDLE err;
171 DWORD count;
173 in = GetStdHandle (STD_INPUT_HANDLE);
174 err = GetStdHandle (STD_ERROR_HANDLE);
176 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
177 return NULL;
179 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
181 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
182 DWORD old_flags;
183 int rc;
185 if (istty)
187 if (GetConsoleMode (in, &old_flags))
188 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
189 else
190 istty = 0;
192 rc = ReadFile (in, input, sizeof (input), &count, NULL);
193 if (count >= 2 && input[count - 2] == '\r')
194 input[count - 2] = '\0';
195 else
197 char buf[256];
198 while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0)
199 if (count >= 2 && buf[count - 2] == '\r')
200 break;
202 WriteFile (err, "\r\n", 2, &count, NULL);
203 if (istty)
204 SetConsoleMode (in, old_flags);
205 if (rc)
206 return input;
209 return NULL;
212 /* This is needed because lib/gettime.c calls gettimeofday, which MSVC
213 doesn't have. Copied from w32.c. */
214 void
215 gettimeofday (struct timeval *tv, struct timezone *tz)
217 struct _timeb tb;
218 _ftime (&tb);
220 tv->tv_sec = tb.time;
221 tv->tv_usec = tb.millitm * 1000L;
222 /* Implementation note: _ftime sometimes doesn't update the dstflag
223 according to the new timezone when the system timezone is
224 changed. We could fix that by using GetSystemTime and
225 GetTimeZoneInformation, but that doesn't seem necessary, since
226 Emacs always calls gettimeofday with the 2nd argument NULL (see
227 current_emacs_time). */
228 if (tz)
230 tz->tz_minuteswest = tb.timezone; /* minutes west of Greenwich */
231 tz->tz_dsttime = tb.dstflag; /* type of dst correction */
236 fchown (int fd, unsigned uid, unsigned gid)
238 return 0;
241 /* Place a wrapper around the MSVC version of ctime. It returns NULL
242 on network directories, so we handle that case here.
243 (Ulrich Leodolter, 1/11/95). */
244 char *
245 sys_ctime (const time_t *t)
247 char *str = (char *) ctime (t);
248 return (str ? str : "Sun Jan 01 00:00:00 1970");
251 FILE *
252 sys_fopen (const char * path, const char * mode)
254 return fopen (path, mode);
258 sys_chdir (const char * path)
260 return _chdir (path);
263 static FILETIME utc_base_ft;
264 static long double utc_base;
265 static int init = 0;
267 static time_t
268 convert_time (FILETIME ft)
270 long double ret;
272 if (CompareFileTime (&ft, &utc_base_ft) < 0)
273 return 0;
275 ret = (long double) ft.dwHighDateTime
276 * 4096.0L * 1024.0L * 1024.0L + ft.dwLowDateTime;
277 ret -= utc_base;
278 return (time_t) (ret * 1e-7L);
281 static int
282 is_exec (const char * name)
284 char * p = strrchr (name, '.');
285 return
286 (p != NULL
287 && (stricmp (p, ".exe") == 0 ||
288 stricmp (p, ".com") == 0 ||
289 stricmp (p, ".bat") == 0 ||
290 stricmp (p, ".cmd") == 0));
293 /* FIXME? This is in config.nt now - is this still needed? */
294 #define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
296 /* We need this because nt/inc/sys/stat.h defines struct stat that is
297 incompatible with the MS run-time libraries. */
299 stat (const char * path, struct stat * buf)
301 WIN32_FIND_DATA wfd;
302 HANDLE fh;
303 int permission;
304 int len;
305 int rootdir = FALSE;
306 char *name = alloca (FILENAME_MAX);
308 if (!init)
310 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
311 SYSTEMTIME st;
313 st.wYear = 1970;
314 st.wMonth = 1;
315 st.wDay = 1;
316 st.wHour = 0;
317 st.wMinute = 0;
318 st.wSecond = 0;
319 st.wMilliseconds = 0;
321 SystemTimeToFileTime (&st, &utc_base_ft);
322 utc_base = (long double) utc_base_ft.dwHighDateTime
323 * 4096.0L * 1024.0L * 1024.0L + utc_base_ft.dwLowDateTime;
324 init = 1;
327 if (path == NULL || buf == NULL || *path == '\0')
329 errno = EFAULT;
330 return -1;
332 if (_mbspbrk (path, "*?|<>\""))
334 errno = ENOENT;
335 return -1;
338 strcpy (name, path);
339 /* Remove trailing directory separator, unless name is the root
340 directory of a drive in which case ensure there is a trailing
341 separator. */
342 len = strlen (name);
343 rootdir = IS_DIRECTORY_SEP (name[0])
344 || (len == 3 && name[1] == ':' && IS_DIRECTORY_SEP (name[2]));
345 if (rootdir)
347 if (GetDriveType (name) < 2)
349 errno = ENOENT;
350 return -1;
352 memset (&wfd, 0, sizeof (wfd));
353 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
354 wfd.ftCreationTime = utc_base_ft;
355 wfd.ftLastAccessTime = utc_base_ft;
356 wfd.ftLastWriteTime = utc_base_ft;
357 strcpy (wfd.cFileName, name);
359 else
361 if (IS_DIRECTORY_SEP (name[len-1]))
362 name[len - 1] = 0;
364 fh = FindFirstFile (name, &wfd);
365 if (fh == INVALID_HANDLE_VALUE)
367 errno = ENOENT;
368 return -1;
370 FindClose (fh);
372 buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
373 S_IFDIR : S_IFREG;
374 buf->st_nlink = 1;
375 buf->st_ino = 0;
377 if (name[0] && name[1] == ':')
378 buf->st_dev = tolower (name[0]) - 'a' + 1;
379 else
380 buf->st_dev = _getdrive ();
381 buf->st_rdev = buf->st_dev;
383 buf->st_size = wfd.nFileSizeLow;
385 /* Convert timestamps to Unix format. */
386 buf->st_mtime = convert_time (wfd.ftLastWriteTime);
387 buf->st_atime = convert_time (wfd.ftLastAccessTime);
388 if (buf->st_atime == 0) buf->st_atime = buf->st_mtime;
389 buf->st_ctime = convert_time (wfd.ftCreationTime);
390 if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime;
392 /* determine rwx permissions */
393 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
394 permission = S_IREAD;
395 else
396 permission = S_IREAD | S_IWRITE;
398 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
399 permission |= S_IEXEC;
400 else if (is_exec (name))
401 permission |= S_IEXEC;
403 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
405 return 0;
409 lstat (const char * path, struct stat * buf)
411 return stat (path, buf);