]> code.delx.au - gnu-emacs/blob - lib-src/ntlib.c
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lib-src / ntlib.c
1 /* Utility and Unix shadow routines for GNU Emacs support programs on NT.
2 Copyright (C) 1994, 2001-2011 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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/>.
18
19
20 Geoff Voelker (voelker@cs.washington.edu) 10-8-94
21 */
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
32 #include "ntlib.h"
33
34 #define MAXPATHLEN _MAX_PATH
35
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)
41 {
42 Sleep (seconds * 1000);
43 }
44
45 /* Get the current working directory. */
46 char *
47 getwd (char *dir)
48 {
49 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
50 return dir;
51 return NULL;
52 }
53
54 static HANDLE getppid_parent;
55 static int getppid_ppid;
56
57 int
58 getppid (void)
59 {
60 char *ppid;
61 DWORD result;
62
63 ppid = getenv ("EM_PARENT_PROCESS_ID");
64 if (!ppid)
65 {
66 printf ("no pid.\n");
67 return 0;
68 }
69 else
70 {
71 getppid_ppid = atoi (ppid);
72 }
73
74 if (!getppid_parent)
75 {
76 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi (ppid));
77 if (!getppid_parent)
78 {
79 printf ("Failed to open handle to parent process: %d\n",
80 GetLastError ());
81 exit (1);
82 }
83 }
84
85 result = WaitForSingleObject (getppid_parent, 0);
86 switch (result)
87 {
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);
98 }
99 }
100
101 char *
102 getlogin (void)
103 {
104 static char user_name[256];
105 DWORD length = sizeof (user_name);
106
107 if (GetUserName (user_name, &length))
108 return user_name;
109 return NULL;
110 }
111
112 char *
113 cuserid (char * s)
114 {
115 char * name = getlogin ();
116 if (s)
117 return strcpy (s, name ? name : "");
118 return name;
119 }
120
121 unsigned
122 getuid (void)
123 {
124 return 0;
125 }
126
127 unsigned
128 getgid (void)
129 {
130 return 0;
131 }
132
133 unsigned
134 getegid (void)
135 {
136 return 0;
137 }
138
139 int
140 setuid (unsigned uid)
141 {
142 return 0;
143 }
144
145 int
146 setegid (unsigned gid)
147 {
148 return 0;
149 }
150
151 struct passwd *
152 getpwuid (unsigned uid)
153 {
154 return NULL;
155 }
156
157 char *
158 getpass (const char * prompt)
159 {
160 static char input[256];
161 HANDLE in;
162 HANDLE err;
163 DWORD count;
164
165 in = GetStdHandle (STD_INPUT_HANDLE);
166 err = GetStdHandle (STD_ERROR_HANDLE);
167
168 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
169 return NULL;
170
171 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
172 {
173 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
174 DWORD old_flags;
175 int rc;
176
177 if (istty)
178 {
179 if (GetConsoleMode (in, &old_flags))
180 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
181 else
182 istty = 0;
183 }
184 rc = ReadFile (in, input, sizeof (input), &count, NULL);
185 if (count >= 2 && input[count - 2] == '\r')
186 input[count - 2] = '\0';
187 else
188 {
189 char buf[256];
190 while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0)
191 if (count >= 2 && buf[count - 2] == '\r')
192 break;
193 }
194 WriteFile (err, "\r\n", 2, &count, NULL);
195 if (istty)
196 SetConsoleMode (in, old_flags);
197 if (rc)
198 return input;
199 }
200
201 return NULL;
202 }
203
204 int
205 fchown (int fd, unsigned uid, unsigned gid)
206 {
207 return 0;
208 }
209
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)
215 {
216 char *str = (char *) ctime (t);
217 return (str ? str : "Sun Jan 01 00:00:00 1970");
218 }
219
220 FILE *
221 sys_fopen (const char * path, const char * mode)
222 {
223 return fopen (path, mode);
224 }
225
226 int
227 sys_chdir (const char * path)
228 {
229 return _chdir (path);
230 }
231
232 static FILETIME utc_base_ft;
233 static long double utc_base;
234 static int init = 0;
235
236 static time_t
237 convert_time (FILETIME ft)
238 {
239 long double ret;
240
241 if (CompareFileTime (&ft, &utc_base_ft) < 0)
242 return 0;
243
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);
248 }
249
250 static int
251 is_exec (const char * name)
252 {
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));
260 }
261
262 #define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
263
264 /* We need this because nt/inc/sys/stat.h defines struct stat that is
265 incompatible with the MS run-time libraries. */
266 int
267 stat (const char * path, struct stat * buf)
268 {
269 WIN32_FIND_DATA wfd;
270 HANDLE fh;
271 int permission;
272 int len;
273 int rootdir = FALSE;
274 char *name = alloca (FILENAME_MAX);
275
276 if (!init)
277 {
278 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
279 SYSTEMTIME st;
280
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;
288
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;
293 }
294
295 if (path == NULL || buf == NULL || *path == '\0')
296 {
297 errno = EFAULT;
298 return -1;
299 }
300 if (_mbspbrk (path, "*?|<>\""))
301 {
302 errno = ENOENT;
303 return -1;
304 }
305
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)
314 {
315 if (GetDriveType (name) < 2)
316 {
317 errno = ENOENT;
318 return -1;
319 }
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);
326 }
327 else
328 {
329 if (IS_DIRECTORY_SEP (name[len-1]))
330 name[len - 1] = 0;
331
332 fh = FindFirstFile (name, &wfd);
333 if (fh == INVALID_HANDLE_VALUE)
334 {
335 errno = ENOENT;
336 return -1;
337 }
338 FindClose (fh);
339 }
340 buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
341 S_IFDIR : S_IFREG;
342 buf->st_nlink = 1;
343 buf->st_ino = 0;
344
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;
350
351 buf->st_size = wfd.nFileSizeLow;
352
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;
359
360 /* determine rwx permissions */
361 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
362 permission = S_IREAD;
363 else
364 permission = S_IREAD | S_IWRITE;
365
366 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
367 permission |= S_IEXEC;
368 else if (is_exec (name))
369 permission |= S_IEXEC;
370
371 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
372
373 return 0;
374 }
375