Include sys/types.h, sys/stat.h, and errno.h.
[bpt/emacs.git] / lib-src / ntlib.c
CommitLineData
95ed0025 1/* Utility and Unix shadow routines for GNU Emacs support programs on NT.
b3d90e46 2 Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005,
a5b68355 3 2006, 2007, 2008 Free Software Foundation, Inc.
95ed0025 4
3b7ad313
EN
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4a9f99bd 9the Free Software Foundation; either version 3, or (at your option)
3b7ad313
EN
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
364c38d3
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA.
95ed0025
RS
21
22 Geoff Voelker (voelker@cs.washington.edu) 10-8-94
23*/
24
25#include <windows.h>
26#include <stdlib.h>
27#include <stdio.h>
14f29224
GV
28#include <time.h>
29#include <direct.h>
10fea9c4
EZ
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <errno.h>
14f29224
GV
33
34#include "ntlib.h"
95ed0025
RS
35
36#define MAXPATHLEN _MAX_PATH
37
38/* Emulate sleep...we could have done this with a define, but that
39 would necessitate including windows.h in the files that used it.
40 This is much easier. */
41void
c09c2c99 42sleep(unsigned long seconds)
95ed0025
RS
43{
44 Sleep (seconds * 1000);
45}
46
47/* Get the current working directory. */
133319ab 48char *
95ed0025
RS
49getwd (char *dir)
50{
14f29224
GV
51 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
52 return dir;
53 return NULL;
95ed0025
RS
54}
55
56static HANDLE getppid_parent;
57static int getppid_ppid;
58
59int
60getppid(void)
61{
62 char *ppid;
63 DWORD result;
64
97e3c91b 65 ppid = getenv ("EM_PARENT_PROCESS_ID");
177c0ea7 66 if (!ppid)
95ed0025
RS
67 {
68 printf("no pid.\n");
69 return 0;
177c0ea7
JB
70 }
71 else
95ed0025
RS
72 {
73 getppid_ppid = atoi (ppid);
74 }
75
177c0ea7 76 if (!getppid_parent)
95ed0025
RS
77 {
78 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi(ppid));
177c0ea7 79 if (!getppid_parent)
95ed0025
RS
80 {
81 printf ("Failed to open handle to parent process: %d\n",
82 GetLastError());
83 exit (1);
84 }
85 }
86
87 result = WaitForSingleObject (getppid_parent, 0);
177c0ea7 88 switch (result)
95ed0025
RS
89 {
90 case WAIT_TIMEOUT:
91 /* The parent is still alive. */
92 return getppid_ppid;
93 case WAIT_OBJECT_0:
94 /* The parent is gone. Return the pid of Unix init (1). */
95 return 1;
96 case WAIT_FAILED:
97 default:
98 printf ("Checking parent status failed: %d\n", GetLastError());
99 exit (1);
100 }
101}
14f29224
GV
102
103char *
104getlogin ()
105{
106 static char user_name[256];
107 DWORD length = sizeof (user_name);
108
109 if (GetUserName (user_name, &length))
110 return user_name;
111 return NULL;
112}
113
114char *
115cuserid (char * s)
116{
117 char * name = getlogin ();
118 if (s)
119 return strcpy (s, name ? name : "");
120 return name;
121}
122
123int
124getuid ()
125{
126 return 0;
127}
128
129int
130setuid (int uid)
131{
132 return 0;
133}
134
135struct passwd *
136getpwuid (int uid)
137{
138 return NULL;
139}
140
141char *
142getpass (const char * prompt)
143{
144 static char input[256];
145 HANDLE in;
146 HANDLE err;
147 DWORD count;
148
149 in = GetStdHandle (STD_INPUT_HANDLE);
150 err = GetStdHandle (STD_ERROR_HANDLE);
151
152 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
153 return NULL;
154
155 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
156 {
157 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
158 DWORD old_flags;
159 int rc;
160
161 if (istty)
162 {
163 if (GetConsoleMode (in, &old_flags))
164 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
165 else
166 istty = 0;
167 }
168 rc = ReadFile (in, input, sizeof (input), &count, NULL);
169 if (count >= 2 && input[count - 2] == '\r')
170 input[count - 2] = '\0';
171 else
172 {
173 char buf[256];
174 while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0)
175 if (count >= 2 && buf[count - 2] == '\r')
176 break;
177 }
178 WriteFile (err, "\r\n", 2, &count, NULL);
179 if (istty)
180 SetConsoleMode (in, old_flags);
181 if (rc)
182 return input;
183 }
184
185 return NULL;
186}
187
188int
189fchown (int fd, int uid, int gid)
190{
191 return 0;
192}
193
194/* Place a wrapper around the MSVC version of ctime. It returns NULL
177c0ea7 195 on network directories, so we handle that case here.
14f29224
GV
196 (Ulrich Leodolter, 1/11/95). */
197char *
198sys_ctime (const time_t *t)
199{
200 char *str = (char *) ctime (t);
201 return (str ? str : "Sun Jan 01 00:00:00 1970");
202}
203
204FILE *
205sys_fopen(const char * path, const char * mode)
206{
207 return fopen (path, mode);
208}
209
210int
211sys_chdir (const char * path)
212{
213 return _chdir (path);
214}
ab5796a9 215
10fea9c4
EZ
216static FILETIME utc_base_ft;
217static long double utc_base;
218static int init = 0;
219
220static time_t
221convert_time (FILETIME ft)
222{
223 long double ret;
224
225 if (CompareFileTime (&ft, &utc_base_ft) < 0)
226 return 0;
227
228 ret = (long double) ft.dwHighDateTime
229 * 4096.0L * 1024.0L * 1024.0L + ft.dwLowDateTime;
230 ret -= utc_base;
231 return (time_t) (ret * 1e-7L);
232}
233
234static int
235is_exec (const char * name)
236{
237 char * p = strrchr (name, '.');
238 return
239 (p != NULL
240 && (stricmp (p, ".exe") == 0 ||
241 stricmp (p, ".com") == 0 ||
242 stricmp (p, ".bat") == 0 ||
243 stricmp (p, ".cmd") == 0));
244}
245
246#define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\')
247
248/* We need this because nt/inc/sys/stat.h defines struct stat that is
249 incompatible with the MS run-time libraries. */
250int
251stat (const char * path, struct stat * buf)
252{
253 WIN32_FIND_DATA wfd;
254 HANDLE fh;
255 int permission;
256 int len;
257 int rootdir = FALSE;
258 char *name = alloca (FILENAME_MAX);
259
260 if (!init)
261 {
262 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
263 SYSTEMTIME st;
264
265 st.wYear = 1970;
266 st.wMonth = 1;
267 st.wDay = 1;
268 st.wHour = 0;
269 st.wMinute = 0;
270 st.wSecond = 0;
271 st.wMilliseconds = 0;
272
273 SystemTimeToFileTime (&st, &utc_base_ft);
274 utc_base = (long double) utc_base_ft.dwHighDateTime
275 * 4096.0L * 1024.0L * 1024.0L + utc_base_ft.dwLowDateTime;
276 init = 1;
277 }
278
279 if (path == NULL || buf == NULL || *path == '\0')
280 {
281 errno = EFAULT;
282 return -1;
283 }
284 if (_mbspbrk (path, "*?|<>\""))
285 {
286 errno = ENOENT;
287 return -1;
288 }
289
290 strcpy (name, path);
291 /* Remove trailing directory separator, unless name is the root
292 directory of a drive in which case ensure there is a trailing
293 separator. */
294 len = strlen (name);
295 rootdir = IS_DIRECTORY_SEP (name[0])
296 || (len == 3 && name[1] == ':' && IS_DIRECTORY_SEP (name[2]));
297 if (rootdir)
298 {
299 if (GetDriveType (name) < 2)
300 {
301 errno = ENOENT;
302 return -1;
303 }
304 memset (&wfd, 0, sizeof (wfd));
305 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
306 wfd.ftCreationTime = utc_base_ft;
307 wfd.ftLastAccessTime = utc_base_ft;
308 wfd.ftLastWriteTime = utc_base_ft;
309 strcpy (wfd.cFileName, name);
310 }
311 else
312 {
313 if (IS_DIRECTORY_SEP (name[len-1]))
314 name[len - 1] = 0;
315
316 fh = FindFirstFile (name, &wfd);
317 if (fh == INVALID_HANDLE_VALUE)
318 {
319 errno = ENOENT;
320 return -1;
321 }
322 FindClose (fh);
323 }
324 buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
325 S_IFDIR : S_IFREG;
326 buf->st_nlink = 1;
327 buf->st_ino = 0;
328
329 if (name[0] && name[1] == ':')
330 buf->st_dev = tolower (name[0]) - 'a' + 1;
331 else
332 buf->st_dev = _getdrive ();
333 buf->st_rdev = buf->st_dev;
334
335 buf->st_size = wfd.nFileSizeLow;
336
337 /* Convert timestamps to Unix format. */
338 buf->st_mtime = convert_time (wfd.ftLastWriteTime);
339 buf->st_atime = convert_time (wfd.ftLastAccessTime);
340 if (buf->st_atime == 0) buf->st_atime = buf->st_mtime;
341 buf->st_ctime = convert_time (wfd.ftCreationTime);
342 if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime;
343
344 /* determine rwx permissions */
345 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
346 permission = S_IREAD;
347 else
348 permission = S_IREAD | S_IWRITE;
349
350 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
351 permission |= S_IEXEC;
352 else if (is_exec (name))
353 permission |= S_IEXEC;
354
355 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
356
357 return 0;
358}
359
ab5796a9
MB
360/* arch-tag: 7b63fb83-70ee-4124-8822-54e53e5d0773
361 (do not change this comment) */