Merge from emacs-23; up to 2010-06-02T00:10:42Z!yamaoka@jpl.org.
[emacs.git] / lib-src / ntlib.c
blob0ecd4177d2cd9fe5d6dd0c0caf3667f1df35253d
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
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>
32 #include "ntlib.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. */
39 void
40 sleep (unsigned long seconds)
42 Sleep (seconds * 1000);
45 /* Get the current working directory. */
46 char *
47 getwd (char *dir)
49 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
50 return dir;
51 return NULL;
54 static HANDLE getppid_parent;
55 static int getppid_ppid;
57 int
58 getppid (void)
60 char *ppid;
61 DWORD result;
63 ppid = getenv ("EM_PARENT_PROCESS_ID");
64 if (!ppid)
66 printf ("no pid.\n");
67 return 0;
69 else
71 getppid_ppid = atoi (ppid);
74 if (!getppid_parent)
76 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi (ppid));
77 if (!getppid_parent)
79 printf ("Failed to open handle to parent process: %d\n",
80 GetLastError ());
81 exit (1);
85 result = WaitForSingleObject (getppid_parent, 0);
86 switch (result)
88 case WAIT_TIMEOUT:
89 /* The parent is still alive. */
90 return getppid_ppid;
91 case WAIT_OBJECT_0:
92 /* The parent is gone. Return the pid of Unix init (1). */
93 return 1;
94 case WAIT_FAILED:
95 default:
96 printf ("Checking parent status failed: %d\n", GetLastError ());
97 exit (1);
101 char *
102 getlogin (void)
104 static char user_name[256];
105 DWORD length = sizeof (user_name);
107 if (GetUserName (user_name, &length))
108 return user_name;
109 return NULL;
112 char *
113 cuserid (char * s)
115 char * name = getlogin ();
116 if (s)
117 return strcpy (s, name ? name : "");
118 return name;
121 unsigned
122 getuid (void)
124 return 0;
127 unsigned
128 getgid (void)
130 return 0;
133 unsigned
134 getegid (void)
136 return 0;
140 setuid (unsigned uid)
142 return 0;
146 setregid (unsigned rgid, unsigned gid)
148 return 0;
151 struct passwd *
152 getpwuid (unsigned uid)
154 return NULL;
157 char *
158 getpass (const char * prompt)
160 static char input[256];
161 HANDLE in;
162 HANDLE err;
163 DWORD count;
165 in = GetStdHandle (STD_INPUT_HANDLE);
166 err = GetStdHandle (STD_ERROR_HANDLE);
168 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
169 return NULL;
171 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
173 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
174 DWORD old_flags;
175 int rc;
177 if (istty)
179 if (GetConsoleMode (in, &old_flags))
180 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
181 else
182 istty = 0;
184 rc = ReadFile (in, input, sizeof (input), &count, NULL);
185 if (count >= 2 && input[count - 2] == '\r')
186 input[count - 2] = '\0';
187 else
189 char buf[256];
190 while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0)
191 if (count >= 2 && buf[count - 2] == '\r')
192 break;
194 WriteFile (err, "\r\n", 2, &count, NULL);
195 if (istty)
196 SetConsoleMode (in, old_flags);
197 if (rc)
198 return input;
201 return NULL;
205 fchown (int fd, unsigned uid, unsigned gid)
207 return 0;
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). */
213 char *
214 sys_ctime (const time_t *t)
216 char *str = (char *) ctime (t);
217 return (str ? str : "Sun Jan 01 00:00:00 1970");
220 FILE *
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;
234 static int init = 0;
236 static time_t
237 convert_time (FILETIME ft)
239 long double ret;
241 if (CompareFileTime (&ft, &utc_base_ft) < 0)
242 return 0;
244 ret = (long double) ft.dwHighDateTime
245 * 4096.0L * 1024.0L * 1024.0L + ft.dwLowDateTime;
246 ret -= utc_base;
247 return (time_t) (ret * 1e-7L);
250 static int
251 is_exec (const char * name)
253 char * p = strrchr (name, '.');
254 return
255 (p != NULL
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)
269 WIN32_FIND_DATA wfd;
270 HANDLE fh;
271 int permission;
272 int len;
273 int rootdir = FALSE;
274 char *name = alloca (FILENAME_MAX);
276 if (!init)
278 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
279 SYSTEMTIME st;
281 st.wYear = 1970;
282 st.wMonth = 1;
283 st.wDay = 1;
284 st.wHour = 0;
285 st.wMinute = 0;
286 st.wSecond = 0;
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;
292 init = 1;
295 if (path == NULL || buf == NULL || *path == '\0')
297 errno = EFAULT;
298 return -1;
300 if (_mbspbrk (path, "*?|<>\""))
302 errno = ENOENT;
303 return -1;
306 strcpy (name, path);
307 /* Remove trailing directory separator, unless name is the root
308 directory of a drive in which case ensure there is a trailing
309 separator. */
310 len = strlen (name);
311 rootdir = IS_DIRECTORY_SEP (name[0])
312 || (len == 3 && name[1] == ':' && IS_DIRECTORY_SEP (name[2]));
313 if (rootdir)
315 if (GetDriveType (name) < 2)
317 errno = ENOENT;
318 return -1;
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);
327 else
329 if (IS_DIRECTORY_SEP (name[len-1]))
330 name[len - 1] = 0;
332 fh = FindFirstFile (name, &wfd);
333 if (fh == INVALID_HANDLE_VALUE)
335 errno = ENOENT;
336 return -1;
338 FindClose (fh);
340 buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
341 S_IFDIR : S_IFREG;
342 buf->st_nlink = 1;
343 buf->st_ino = 0;
345 if (name[0] && name[1] == ':')
346 buf->st_dev = tolower (name[0]) - 'a' + 1;
347 else
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;
363 else
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);
373 return 0;