1 /* Utility and Unix shadow routines for GNU Emacs support programs on NT.
2 Copyright (C) 1994, 2001-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 Geoff Voelker (voelker@cs.washington.edu) 10-8-94
28 #include <sys/types.h>
34 #define MAXPATHLEN _MAX_PATH
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. */
40 sleep (unsigned long seconds
)
42 Sleep (seconds
* 1000);
45 /* Get the current working directory. */
49 if (GetCurrentDirectory (MAXPATHLEN
, dir
) > 0)
54 static HANDLE getppid_parent
;
55 static int getppid_ppid
;
63 ppid
= getenv ("EM_PARENT_PROCESS_ID");
71 getppid_ppid
= atoi (ppid
);
76 getppid_parent
= OpenProcess (SYNCHRONIZE
, FALSE
, atoi (ppid
));
79 printf ("Failed to open handle to parent process: %d\n",
85 result
= WaitForSingleObject (getppid_parent
, 0);
89 /* The parent is still alive. */
92 /* The parent is gone. Return the pid of Unix init (1). */
96 printf ("Checking parent status failed: %d\n", GetLastError ());
104 static char user_name
[256];
105 DWORD length
= sizeof (user_name
);
107 if (GetUserName (user_name
, &length
))
115 char * name
= getlogin ();
117 return strcpy (s
, name
? name
: "");
140 setuid (unsigned uid
)
146 setregid (unsigned rgid
, unsigned gid
)
152 getpwuid (unsigned uid
)
158 getpass (const char * prompt
)
160 static char input
[256];
165 in
= GetStdHandle (STD_INPUT_HANDLE
);
166 err
= GetStdHandle (STD_ERROR_HANDLE
);
168 if (in
== INVALID_HANDLE_VALUE
|| err
== INVALID_HANDLE_VALUE
)
171 if (WriteFile (err
, prompt
, strlen (prompt
), &count
, NULL
))
173 int istty
= (GetFileType (in
) == FILE_TYPE_CHAR
);
179 if (GetConsoleMode (in
, &old_flags
))
180 SetConsoleMode (in
, ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
);
184 rc
= ReadFile (in
, input
, sizeof (input
), &count
, NULL
);
185 if (count
>= 2 && input
[count
- 2] == '\r')
186 input
[count
- 2] = '\0';
190 while (ReadFile (in
, buf
, sizeof (buf
), &count
, NULL
) > 0)
191 if (count
>= 2 && buf
[count
- 2] == '\r')
194 WriteFile (err
, "\r\n", 2, &count
, NULL
);
196 SetConsoleMode (in
, old_flags
);
205 fchown (int fd
, unsigned uid
, unsigned gid
)
210 /* Place a wrapper around the MSVC version of ctime. It returns NULL
211 on network directories, so we handle that case here.
212 (Ulrich Leodolter, 1/11/95). */
214 sys_ctime (const time_t *t
)
216 char *str
= (char *) ctime (t
);
217 return (str
? str
: "Sun Jan 01 00:00:00 1970");
221 sys_fopen (const char * path
, const char * mode
)
223 return fopen (path
, mode
);
227 sys_chdir (const char * path
)
229 return _chdir (path
);
232 static FILETIME utc_base_ft
;
233 static long double utc_base
;
237 convert_time (FILETIME ft
)
241 if (CompareFileTime (&ft
, &utc_base_ft
) < 0)
244 ret
= (long double) ft
.dwHighDateTime
245 * 4096.0L * 1024.0L * 1024.0L + ft
.dwLowDateTime
;
247 return (time_t) (ret
* 1e-7L);
251 is_exec (const char * name
)
253 char * p
= strrchr (name
, '.');
256 && (stricmp (p
, ".exe") == 0 ||
257 stricmp (p
, ".com") == 0 ||
258 stricmp (p
, ".bat") == 0 ||
259 stricmp (p
, ".cmd") == 0));
262 #define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
264 /* We need this because nt/inc/sys/stat.h defines struct stat that is
265 incompatible with the MS run-time libraries. */
267 stat (const char * path
, struct stat
* buf
)
274 char *name
= alloca (FILENAME_MAX
);
278 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
287 st
.wMilliseconds
= 0;
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
;
295 if (path
== NULL
|| buf
== NULL
|| *path
== '\0')
300 if (_mbspbrk (path
, "*?|<>\""))
307 /* Remove trailing directory separator, unless name is the root
308 directory of a drive in which case ensure there is a trailing
311 rootdir
= IS_DIRECTORY_SEP (name
[0])
312 || (len
== 3 && name
[1] == ':' && IS_DIRECTORY_SEP (name
[2]));
315 if (GetDriveType (name
) < 2)
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
);
329 if (IS_DIRECTORY_SEP (name
[len
-1]))
332 fh
= FindFirstFile (name
, &wfd
);
333 if (fh
== INVALID_HANDLE_VALUE
)
340 buf
->st_mode
= (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ?
345 if (name
[0] && name
[1] == ':')
346 buf
->st_dev
= tolower (name
[0]) - 'a' + 1;
348 buf
->st_dev
= _getdrive ();
349 buf
->st_rdev
= buf
->st_dev
;
351 buf
->st_size
= wfd
.nFileSizeLow
;
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
;
360 /* determine rwx permissions */
361 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
)
362 permission
= S_IREAD
;
364 permission
= S_IREAD
| S_IWRITE
;
366 if (wfd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
367 permission
|= S_IEXEC
;
368 else if (is_exec (name
))
369 permission
|= S_IEXEC
;
371 buf
->st_mode
|= permission
| (permission
>> 3) | (permission
>> 6);