]> code.delx.au - gnu-emacs/blob - lib-src/ntlib.c
merge trunk
[gnu-emacs] / lib-src / ntlib.c
1 /* Utility and Unix shadow routines for GNU Emacs support programs on NT.
2
3 Copyright (C) 1994, 2001-2012 Free Software Foundation, Inc.
4
5 Author: Geoff Voelker (voelker@cs.washington.edu)
6 Created: 10-8-94
7
8 This file is part of GNU Emacs.
9
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.
14
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.
19
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/>. */
22
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
34 #include "ntlib.h"
35
36 struct timezone
37 {
38 int tz_minuteswest; /* minutes west of Greenwich */
39 int tz_dsttime; /* type of dst correction */
40 };
41
42 #define MAXPATHLEN _MAX_PATH
43
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)
49 {
50 Sleep (seconds * 1000);
51 }
52
53 /* Get the current working directory. */
54 char *
55 getwd (char *dir)
56 {
57 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
58 return dir;
59 return NULL;
60 }
61
62 static HANDLE getppid_parent;
63 static int getppid_ppid;
64
65 int
66 getppid (void)
67 {
68 char *ppid;
69 DWORD result;
70
71 ppid = getenv ("EM_PARENT_PROCESS_ID");
72 if (!ppid)
73 {
74 printf ("no pid.\n");
75 return 0;
76 }
77 else
78 {
79 getppid_ppid = atoi (ppid);
80 }
81
82 if (!getppid_parent)
83 {
84 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi (ppid));
85 if (!getppid_parent)
86 {
87 printf ("Failed to open handle to parent process: %d\n",
88 GetLastError ());
89 exit (1);
90 }
91 }
92
93 result = WaitForSingleObject (getppid_parent, 0);
94 switch (result)
95 {
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);
106 }
107 }
108
109 char *
110 getlogin (void)
111 {
112 static char user_name[256];
113 DWORD length = sizeof (user_name);
114
115 if (GetUserName (user_name, &length))
116 return user_name;
117 return NULL;
118 }
119
120 char *
121 cuserid (char * s)
122 {
123 char * name = getlogin ();
124 if (s)
125 return strcpy (s, name ? name : "");
126 return name;
127 }
128
129 unsigned
130 getuid (void)
131 {
132 return 0;
133 }
134
135 unsigned
136 getgid (void)
137 {
138 return 0;
139 }
140
141 unsigned
142 getegid (void)
143 {
144 return 0;
145 }
146
147 int
148 setuid (unsigned uid)
149 {
150 return 0;
151 }
152
153 int
154 setregid (unsigned rgid, unsigned gid)
155 {
156 return 0;
157 }
158
159 struct passwd *
160 getpwuid (unsigned uid)
161 {
162 return NULL;
163 }
164
165 char *
166 getpass (const char * prompt)
167 {
168 static char input[256];
169 HANDLE in;
170 HANDLE err;
171 DWORD count;
172
173 in = GetStdHandle (STD_INPUT_HANDLE);
174 err = GetStdHandle (STD_ERROR_HANDLE);
175
176 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
177 return NULL;
178
179 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
180 {
181 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
182 DWORD old_flags;
183 int rc;
184
185 if (istty)
186 {
187 if (GetConsoleMode (in, &old_flags))
188 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
189 else
190 istty = 0;
191 }
192 rc = ReadFile (in, input, sizeof (input), &count, NULL);
193 if (count >= 2 && input[count - 2] == '\r')
194 input[count - 2] = '\0';
195 else
196 {
197 char buf[256];
198 while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0)
199 if (count >= 2 && buf[count - 2] == '\r')
200 break;
201 }
202 WriteFile (err, "\r\n", 2, &count, NULL);
203 if (istty)
204 SetConsoleMode (in, old_flags);
205 if (rc)
206 return input;
207 }
208
209 return NULL;
210 }
211
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)
216 {
217 struct _timeb tb;
218 _ftime (&tb);
219
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)
229 {
230 tz->tz_minuteswest = tb.timezone; /* minutes west of Greenwich */
231 tz->tz_dsttime = tb.dstflag; /* type of dst correction */
232 }
233 }
234
235 int
236 fchown (int fd, unsigned uid, unsigned gid)
237 {
238 return 0;
239 }
240
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)
246 {
247 char *str = (char *) ctime (t);
248 return (str ? str : "Sun Jan 01 00:00:00 1970");
249 }
250
251 FILE *
252 sys_fopen (const char * path, const char * mode)
253 {
254 return fopen (path, mode);
255 }
256
257 int
258 sys_chdir (const char * path)
259 {
260 return _chdir (path);
261 }
262
263 static FILETIME utc_base_ft;
264 static long double utc_base;
265 static int init = 0;
266
267 static time_t
268 convert_time (FILETIME ft)
269 {
270 long double ret;
271
272 if (CompareFileTime (&ft, &utc_base_ft) < 0)
273 return 0;
274
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);
279 }
280
281 static int
282 is_exec (const char * name)
283 {
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));
291 }
292
293 /* FIXME? This is in config.nt now - is this still needed? */
294 #define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
295
296 /* We need this because nt/inc/sys/stat.h defines struct stat that is
297 incompatible with the MS run-time libraries. */
298 int
299 stat (const char * path, struct stat * buf)
300 {
301 WIN32_FIND_DATA wfd;
302 HANDLE fh;
303 int permission;
304 int len;
305 int rootdir = FALSE;
306 char *name = alloca (FILENAME_MAX);
307
308 if (!init)
309 {
310 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
311 SYSTEMTIME st;
312
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;
320
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;
325 }
326
327 if (path == NULL || buf == NULL || *path == '\0')
328 {
329 errno = EFAULT;
330 return -1;
331 }
332 if (_mbspbrk (path, "*?|<>\""))
333 {
334 errno = ENOENT;
335 return -1;
336 }
337
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)
346 {
347 if (GetDriveType (name) < 2)
348 {
349 errno = ENOENT;
350 return -1;
351 }
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);
358 }
359 else
360 {
361 if (IS_DIRECTORY_SEP (name[len-1]))
362 name[len - 1] = 0;
363
364 fh = FindFirstFile (name, &wfd);
365 if (fh == INVALID_HANDLE_VALUE)
366 {
367 errno = ENOENT;
368 return -1;
369 }
370 FindClose (fh);
371 }
372 buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
373 S_IFDIR : S_IFREG;
374 buf->st_nlink = 1;
375 buf->st_ino = 0;
376
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;
382
383 buf->st_size = wfd.nFileSizeLow;
384
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;
391
392 /* determine rwx permissions */
393 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
394 permission = S_IREAD;
395 else
396 permission = S_IREAD | S_IWRITE;
397
398 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
399 permission |= S_IEXEC;
400 else if (is_exec (name))
401 permission |= S_IEXEC;
402
403 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
404
405 return 0;
406 }
407
408 int
409 lstat (const char * path, struct stat * buf)
410 {
411 return stat (path, buf);
412 }
413