* configure.ac (GTK_COMPILES): Check API a bit more carefully.
[emacs.git] / lib-src / ntlib.c
blob41b4e3a0cbce870e6f0aae8d8f9cfbc0febb8778
1 /* Utility and Unix shadow routines for GNU Emacs support programs on NT.
3 Copyright (C) 1994, 2001-2013 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>
33 #include <mbstring.h>
35 #include "ntlib.h"
37 /* MinGW64 defines _TIMEZONE_DEFINED and defines 'struct timespec' in
38 its system headers. */
39 #ifndef _TIMEZONE_DEFINED
40 struct timezone
42 int tz_minuteswest; /* minutes west of Greenwich */
43 int tz_dsttime; /* type of dst correction */
45 #endif
47 #define MAXPATHLEN _MAX_PATH
49 /* Emulate sleep...we could have done this with a define, but that
50 would necessitate including windows.h in the files that used it.
51 This is much easier. */
52 unsigned
53 sleep (unsigned seconds)
55 Sleep (seconds * 1000);
56 return 0;
59 /* Get the current working directory. */
60 char *
61 getwd (char *dir)
63 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
64 return dir;
65 return NULL;
68 static HANDLE getppid_parent;
69 static int getppid_ppid;
71 int
72 getppid (void)
74 char *ppid;
75 DWORD result;
77 ppid = getenv ("EM_PARENT_PROCESS_ID");
78 if (!ppid)
80 printf ("no pid.\n");
81 return 0;
83 else
85 getppid_ppid = atoi (ppid);
88 if (!getppid_parent)
90 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi (ppid));
91 if (!getppid_parent)
93 printf ("Failed to open handle to parent process: %d\n",
94 GetLastError ());
95 exit (1);
99 result = WaitForSingleObject (getppid_parent, 0);
100 switch (result)
102 case WAIT_TIMEOUT:
103 /* The parent is still alive. */
104 return getppid_ppid;
105 case WAIT_OBJECT_0:
106 /* The parent is gone. Return the pid of Unix init (1). */
107 return 1;
108 case WAIT_FAILED:
109 default:
110 printf ("Checking parent status failed: %d\n", GetLastError ());
111 exit (1);
115 char *
116 getlogin (void)
118 static char user_name[256];
119 DWORD length = sizeof (user_name);
121 if (GetUserName (user_name, &length))
122 return user_name;
123 return NULL;
126 char *
127 cuserid (char * s)
129 char * name = getlogin ();
130 if (s)
131 return strcpy (s, name ? name : "");
132 return name;
135 unsigned
136 getuid (void)
138 return 0;
141 unsigned
142 geteuid (void)
144 return getuid ();
147 unsigned
148 getgid (void)
150 return 0;
153 unsigned
154 getegid (void)
156 return 0;
160 setuid (unsigned uid)
162 return 0;
166 setregid (unsigned rgid, unsigned gid)
168 return 0;
171 struct passwd *
172 getpwuid (unsigned uid)
174 return NULL;
177 char *
178 getpass (const char * prompt)
180 static char input[256];
181 HANDLE in;
182 HANDLE err;
183 DWORD count;
185 in = GetStdHandle (STD_INPUT_HANDLE);
186 err = GetStdHandle (STD_ERROR_HANDLE);
188 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
189 return NULL;
191 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
193 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
194 DWORD old_flags;
195 int rc;
197 if (istty)
199 if (GetConsoleMode (in, &old_flags))
200 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
201 else
202 istty = 0;
204 rc = ReadFile (in, input, sizeof (input), &count, NULL);
205 if (count >= 2 && input[count - 2] == '\r')
206 input[count - 2] = '\0';
207 else
209 char buf[256];
210 while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0)
211 if (count >= 2 && buf[count - 2] == '\r')
212 break;
214 WriteFile (err, "\r\n", 2, &count, NULL);
215 if (istty)
216 SetConsoleMode (in, old_flags);
217 if (rc)
218 return input;
221 return NULL;
224 /* This is needed because lib/gettime.c calls gettimeofday, which MSVC
225 doesn't have. Copied from w32.c. */
226 void
227 gettimeofday (struct timeval *tv, struct timezone *tz)
229 struct _timeb tb;
230 _ftime (&tb);
232 tv->tv_sec = tb.time;
233 tv->tv_usec = tb.millitm * 1000L;
234 /* Implementation note: _ftime sometimes doesn't update the dstflag
235 according to the new timezone when the system timezone is
236 changed. We could fix that by using GetSystemTime and
237 GetTimeZoneInformation, but that doesn't seem necessary, since
238 Emacs always calls gettimeofday with the 2nd argument NULL (see
239 current_emacs_time). */
240 if (tz)
242 tz->tz_minuteswest = tb.timezone; /* minutes west of Greenwich */
243 tz->tz_dsttime = tb.dstflag; /* type of dst correction */
248 fchown (int fd, unsigned uid, unsigned gid)
250 return 0;
253 /* Place a wrapper around the MSVC version of ctime. It returns NULL
254 on network directories, so we handle that case here.
255 (Ulrich Leodolter, 1/11/95). */
256 char *
257 sys_ctime (const time_t *t)
259 char *str = (char *) ctime (t);
260 return (str ? str : "Sun Jan 01 00:00:00 1970");
263 FILE *
264 sys_fopen (const char * path, const char * mode)
266 return fopen (path, mode);
270 sys_chdir (const char * path)
272 return _chdir (path);
275 static FILETIME utc_base_ft;
276 static long double utc_base;
277 static int init = 0;
279 static time_t
280 convert_time (FILETIME ft)
282 long double ret;
284 if (CompareFileTime (&ft, &utc_base_ft) < 0)
285 return 0;
287 ret = (long double) ft.dwHighDateTime
288 * 4096.0L * 1024.0L * 1024.0L + ft.dwLowDateTime;
289 ret -= utc_base;
290 return (time_t) (ret * 1e-7L);
293 static int
294 is_exec (const char * name)
296 char * p = strrchr (name, '.');
297 return
298 (p != NULL
299 && (stricmp (p, ".exe") == 0 ||
300 stricmp (p, ".com") == 0 ||
301 stricmp (p, ".bat") == 0 ||
302 stricmp (p, ".cmd") == 0));
305 /* FIXME? This is in config.nt now - is this still needed? */
306 #define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
308 /* We need this because nt/inc/sys/stat.h defines struct stat that is
309 incompatible with the MS run-time libraries. */
311 stat (const char * path, struct stat * buf)
313 WIN32_FIND_DATA wfd;
314 HANDLE fh;
315 int permission;
316 int len;
317 int rootdir = FALSE;
318 char *name = alloca (FILENAME_MAX);
320 if (!init)
322 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
323 SYSTEMTIME st;
325 st.wYear = 1970;
326 st.wMonth = 1;
327 st.wDay = 1;
328 st.wHour = 0;
329 st.wMinute = 0;
330 st.wSecond = 0;
331 st.wMilliseconds = 0;
333 SystemTimeToFileTime (&st, &utc_base_ft);
334 utc_base = (long double) utc_base_ft.dwHighDateTime
335 * 4096.0L * 1024.0L * 1024.0L + utc_base_ft.dwLowDateTime;
336 init = 1;
339 if (path == NULL || buf == NULL || *path == '\0')
341 errno = EFAULT;
342 return -1;
344 if (_mbspbrk (path, "*?|<>\""))
346 errno = ENOENT;
347 return -1;
350 strcpy (name, path);
351 /* Remove trailing directory separator, unless name is the root
352 directory of a drive in which case ensure there is a trailing
353 separator. */
354 len = strlen (name);
355 rootdir = IS_DIRECTORY_SEP (name[0])
356 || (len == 3 && name[1] == ':' && IS_DIRECTORY_SEP (name[2]));
357 if (rootdir)
359 if (GetDriveType (name) < 2)
361 errno = ENOENT;
362 return -1;
364 memset (&wfd, 0, sizeof (wfd));
365 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
366 wfd.ftCreationTime = utc_base_ft;
367 wfd.ftLastAccessTime = utc_base_ft;
368 wfd.ftLastWriteTime = utc_base_ft;
369 strcpy (wfd.cFileName, name);
371 else
373 if (IS_DIRECTORY_SEP (name[len-1]))
374 name[len - 1] = 0;
376 fh = FindFirstFile (name, &wfd);
377 if (fh == INVALID_HANDLE_VALUE)
379 errno = ENOENT;
380 return -1;
382 FindClose (fh);
384 buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
385 S_IFDIR : S_IFREG;
386 buf->st_nlink = 1;
387 buf->st_ino = 0;
389 if (name[0] && name[1] == ':')
390 buf->st_dev = tolower (name[0]) - 'a' + 1;
391 else
392 buf->st_dev = _getdrive ();
393 buf->st_rdev = buf->st_dev;
395 buf->st_size = wfd.nFileSizeLow;
397 /* Convert timestamps to Unix format. */
398 buf->st_mtime = convert_time (wfd.ftLastWriteTime);
399 buf->st_atime = convert_time (wfd.ftLastAccessTime);
400 if (buf->st_atime == 0) buf->st_atime = buf->st_mtime;
401 buf->st_ctime = convert_time (wfd.ftCreationTime);
402 if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime;
404 /* determine rwx permissions */
405 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
406 permission = S_IREAD;
407 else
408 permission = S_IREAD | S_IWRITE;
410 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
411 permission |= S_IEXEC;
412 else if (is_exec (name))
413 permission |= S_IEXEC;
415 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
417 return 0;
421 lstat (const char * path, struct stat * buf)
423 return stat (path, buf);