1 /* Utility and Unix shadow routines for GNU Emacs support programs on NT.
3 Copyright (C) 1994, 2001-2017 Free Software Foundation, Inc.
5 Author: Geoff Voelker (voelker@cs.washington.edu)
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 (at
13 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 <https://www.gnu.org/licenses/>. */
28 #include <sys/types.h>
32 #include <sys/timeb.h>
37 char *sys_ctime (const time_t *);
38 FILE *sys_fopen (const char *, const char *);
39 int sys_mkdir (const char *, mode_t
);
40 int sys_chdir (const char *);
41 int mkostemp (char *, int);
42 int sys_rename (const char *, const char *);
43 int sys_open (const char *, int, int);
45 /* MinGW64 defines _TIMEZONE_DEFINED and defines 'struct timespec' in
46 its system headers. */
47 #ifndef _TIMEZONE_DEFINED
50 int tz_minuteswest
; /* minutes west of Greenwich */
51 int tz_dsttime
; /* type of dst correction */
55 #define MAXPATHLEN _MAX_PATH
57 /* Emulate sleep...we could have done this with a define, but that
58 would necessitate including windows.h in the files that used it.
59 This is much easier. */
61 sleep (unsigned seconds
)
63 Sleep (seconds
* 1000);
67 /* Get the current working directory. */
71 if (GetCurrentDirectory (MAXPATHLEN
, dir
) > 0)
76 static HANDLE getppid_parent
;
77 static int getppid_ppid
;
85 ppid
= getenv ("EM_PARENT_PROCESS_ID");
93 getppid_ppid
= atoi (ppid
);
98 getppid_parent
= OpenProcess (SYNCHRONIZE
, FALSE
, atoi (ppid
));
101 printf ("Failed to open handle to parent process: %lu\n",
107 result
= WaitForSingleObject (getppid_parent
, 0);
111 /* The parent is still alive. */
114 /* The parent is gone. Return the pid of Unix init (1). */
118 printf ("Checking parent status failed: %lu\n", GetLastError ());
126 static char user_name
[256];
127 DWORD length
= sizeof (user_name
);
129 if (GetUserName (user_name
, &length
))
137 char * name
= getlogin ();
139 return strcpy (s
, name
? name
: "");
168 setuid (unsigned uid
)
174 setregid (unsigned rgid
, unsigned gid
)
180 getpwuid (unsigned uid
)
186 getpass (const char * prompt
)
188 static char input
[256];
193 in
= GetStdHandle (STD_INPUT_HANDLE
);
194 err
= GetStdHandle (STD_ERROR_HANDLE
);
196 if (in
== INVALID_HANDLE_VALUE
|| err
== INVALID_HANDLE_VALUE
)
199 if (WriteFile (err
, prompt
, strlen (prompt
), &count
, NULL
))
201 int istty
= (GetFileType (in
) == FILE_TYPE_CHAR
);
207 if (GetConsoleMode (in
, &old_flags
))
208 SetConsoleMode (in
, ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
);
212 rc
= ReadFile (in
, input
, sizeof (input
), &count
, NULL
);
213 if (count
>= 2 && input
[count
- 2] == '\r')
214 input
[count
- 2] = '\0';
218 while (ReadFile (in
, buf
, sizeof (buf
), &count
, NULL
) > 0)
219 if (count
>= 2 && buf
[count
- 2] == '\r')
222 WriteFile (err
, "\r\n", 2, &count
, NULL
);
224 SetConsoleMode (in
, old_flags
);
233 fchown (int fd
, unsigned uid
, unsigned gid
)
239 sys_fopen (const char * path
, const char * mode
)
241 return fopen (path
, mode
);
245 sys_chdir (const char * path
)
247 return _chdir (path
);
251 sys_mkdir (const char * path
, mode_t mode
)
253 return _mkdir (path
);
256 static FILETIME utc_base_ft
;
257 static long double utc_base
;
261 convert_time (FILETIME ft
)
265 if (CompareFileTime (&ft
, &utc_base_ft
) < 0)
268 ret
= (long double) ft
.dwHighDateTime
269 * 4096.0L * 1024.0L * 1024.0L + ft
.dwLowDateTime
;
271 return (time_t) (ret
* 1e-7L);
275 is_exec (const char * name
)
277 char * p
= strrchr (name
, '.');
280 && (stricmp (p
, ".exe") == 0 ||
281 stricmp (p
, ".com") == 0 ||
282 stricmp (p
, ".bat") == 0 ||
283 stricmp (p
, ".cmd") == 0));
286 /* FIXME? This is in configure.ac now - is this still needed? */
287 #define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
289 /* We need this because nt/inc/sys/stat.h defines struct stat that is
290 incompatible with the MS run-time libraries. */
292 stat (const char * path
, struct stat
* buf
)
299 char *name
= alloca (FILENAME_MAX
);
303 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
312 st
.wMilliseconds
= 0;
314 SystemTimeToFileTime (&st
, &utc_base_ft
);
315 utc_base
= (long double) utc_base_ft
.dwHighDateTime
316 * 4096.0L * 1024.0L * 1024.0L + utc_base_ft
.dwLowDateTime
;
320 if (path
== NULL
|| buf
== NULL
|| *path
== '\0')
325 if (_mbspbrk (path
, "*?|<>\""))
332 /* Remove trailing directory separator, unless name is the root
333 directory of a drive in which case ensure there is a trailing
336 rootdir
= IS_DIRECTORY_SEP (name
[0])
337 || (len
== 3 && name
[1] == ':' && IS_DIRECTORY_SEP (name
[2]));
340 if (GetDriveType (name
) < 2)
345 memset (&wfd
, 0, sizeof (wfd
));
346 wfd
.dwFileAttributes
= FILE_ATTRIBUTE_DIRECTORY
;
347 wfd
.ftCreationTime
= utc_base_ft
;
348 wfd
.ftLastAccessTime
= utc_base_ft
;
349 wfd
.ftLastWriteTime
= utc_base_ft
;
350 strcpy (wfd
.cFileName
, name
);
354 if (IS_DIRECTORY_SEP (name
[len
-1]))
357 fh
= FindFirstFile (name
, &wfd
);
358 if (fh
== INVALID_HANDLE_VALUE
)
365 buf
->st_mode
= (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ?
370 if (name
[0] && name
[1] == ':')
371 buf
->st_dev
= tolower (name
[0]) - 'a' + 1;
373 buf
->st_dev
= _getdrive ();
374 buf
->st_rdev
= buf
->st_dev
;
376 buf
->st_size
= wfd
.nFileSizeLow
;
378 /* Convert timestamps to Unix format. */
379 buf
->st_mtime
= convert_time (wfd
.ftLastWriteTime
);
380 buf
->st_atime
= convert_time (wfd
.ftLastAccessTime
);
381 if (buf
->st_atime
== 0) buf
->st_atime
= buf
->st_mtime
;
382 buf
->st_ctime
= convert_time (wfd
.ftCreationTime
);
383 if (buf
->st_ctime
== 0) buf
->st_ctime
= buf
->st_mtime
;
385 /* determine rwx permissions */
386 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
)
387 permission
= S_IREAD
;
389 permission
= S_IREAD
| S_IWRITE
;
391 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
392 permission
|= S_IEXEC
;
393 else if (is_exec (name
))
394 permission
|= S_IEXEC
;
396 buf
->st_mode
|= permission
| (permission
>> 3) | (permission
>> 6);
402 lstat (const char * path
, struct stat
* buf
)
404 return stat (path
, buf
);
407 /* On Windows, you cannot rename into an existing file. */
409 sys_rename (const char *from
, const char *to
)
411 int retval
= rename (from
, to
);
413 if (retval
< 0 && errno
== EEXIST
)
415 if (unlink (to
) == 0)
416 retval
= rename (from
, to
);
422 sys_open (const char * path
, int oflag
, int mode
)
424 return _open (path
, oflag
, mode
);