]> code.delx.au - gnu-emacs/blob - lib-src/ntlib.c
Include ntlib.h.
[gnu-emacs] / lib-src / ntlib.c
1 /* Utility and Unix shadow routines for GNU Emacs support programs on NT.
2 Copyright (C) 1994 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 2, or (at your option)
9 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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20
21 Geoff Voelker (voelker@cs.washington.edu) 10-8-94
22 */
23
24 #include <windows.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <time.h>
28 #include <direct.h>
29
30 #include "ntlib.h"
31
32 #define MAXPATHLEN _MAX_PATH
33
34 /* Emulate sleep...we could have done this with a define, but that
35 would necessitate including windows.h in the files that used it.
36 This is much easier. */
37 void
38 sleep(int seconds)
39 {
40 Sleep (seconds * 1000);
41 }
42
43 /* Get the current working directory. */
44 int
45 getwd (char *dir)
46 {
47 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
48 return dir;
49 return NULL;
50 }
51
52 int
53 getpid ()
54 {
55 return _getpid ();
56 }
57
58 static HANDLE getppid_parent;
59 static int getppid_ppid;
60
61 int
62 getppid(void)
63 {
64 char *ppid;
65 DWORD result;
66
67 ppid = getenv ("__PARENT_PROCESS_ID");
68 if (!ppid)
69 {
70 printf("no pid.\n");
71 return 0;
72 }
73 else
74 {
75 getppid_ppid = atoi (ppid);
76 }
77
78 if (!getppid_parent)
79 {
80 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi(ppid));
81 if (!getppid_parent)
82 {
83 printf ("Failed to open handle to parent process: %d\n",
84 GetLastError());
85 exit (1);
86 }
87 }
88
89 result = WaitForSingleObject (getppid_parent, 0);
90 switch (result)
91 {
92 case WAIT_TIMEOUT:
93 /* The parent is still alive. */
94 return getppid_ppid;
95 case WAIT_OBJECT_0:
96 /* The parent is gone. Return the pid of Unix init (1). */
97 return 1;
98 case WAIT_FAILED:
99 default:
100 printf ("Checking parent status failed: %d\n", GetLastError());
101 exit (1);
102 }
103 }
104
105 char *
106 getlogin ()
107 {
108 static char user_name[256];
109 DWORD length = sizeof (user_name);
110
111 if (GetUserName (user_name, &length))
112 return user_name;
113 return NULL;
114 }
115
116 char *
117 cuserid (char * s)
118 {
119 char * name = getlogin ();
120 if (s)
121 return strcpy (s, name ? name : "");
122 return name;
123 }
124
125 int
126 getuid ()
127 {
128 return 0;
129 }
130
131 int
132 setuid (int uid)
133 {
134 return 0;
135 }
136
137 struct passwd *
138 getpwuid (int uid)
139 {
140 return NULL;
141 }
142
143 char *
144 getpass (const char * prompt)
145 {
146 static char input[256];
147 HANDLE in;
148 HANDLE err;
149 DWORD count;
150
151 in = GetStdHandle (STD_INPUT_HANDLE);
152 err = GetStdHandle (STD_ERROR_HANDLE);
153
154 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
155 return NULL;
156
157 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
158 {
159 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
160 DWORD old_flags;
161 int rc;
162
163 if (istty)
164 {
165 if (GetConsoleMode (in, &old_flags))
166 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
167 else
168 istty = 0;
169 }
170 rc = ReadFile (in, input, sizeof (input), &count, NULL);
171 if (count >= 2 && input[count - 2] == '\r')
172 input[count - 2] = '\0';
173 else
174 {
175 char buf[256];
176 while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0)
177 if (count >= 2 && buf[count - 2] == '\r')
178 break;
179 }
180 WriteFile (err, "\r\n", 2, &count, NULL);
181 if (istty)
182 SetConsoleMode (in, old_flags);
183 if (rc)
184 return input;
185 }
186
187 return NULL;
188 }
189
190 int
191 fchown (int fd, int uid, int gid)
192 {
193 return 0;
194 }
195
196 /* Place a wrapper around the MSVC version of ctime. It returns NULL
197 on network directories, so we handle that case here.
198 (Ulrich Leodolter, 1/11/95). */
199 char *
200 sys_ctime (const time_t *t)
201 {
202 char *str = (char *) ctime (t);
203 return (str ? str : "Sun Jan 01 00:00:00 1970");
204 }
205
206 FILE *
207 sys_fopen(const char * path, const char * mode)
208 {
209 return fopen (path, mode);
210 }
211
212 int
213 sys_chdir (const char * path)
214 {
215 return _chdir (path);
216 }